Skip to content

Commit

Permalink
Merge pull request #501 from jpudysz/feat/update-native-pressable-ima…
Browse files Browse the repository at this point in the history
…geBackground

feat: update native pressable and imageBackground implementations
  • Loading branch information
Brentlok authored Jan 20, 2025
2 parents 817e994 + 262712e commit 59ca583
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
28 changes: 16 additions & 12 deletions src/components/native/Pressable.native.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useRef } from 'react'
import React, { forwardRef, useEffect, useRef } from 'react'
import { Pressable as NativePressableReactNative } from 'react-native'
import type { PressableProps as Props, View } from 'react-native'
import { UnistylesShadowRegistry } from '../../specs'
Expand All @@ -11,39 +11,43 @@ type PressableProps = Props & {
export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ...props }, forwardedRef) => {
const storedRef = useRef<View | null>()

useEffect(() => {
return () => {
if (storedRef.current) {
// @ts-expect-error - this is hidden from TS
UnistylesShadowRegistry.remove(storedRef.current)
}
}
}, [])

return (
<NativePressableReactNative
{...props}
ref={ref => {
const unistyles = typeof style === 'function'
? style({ pressed: false })
: style
const styles = Array.isArray(unistyles)
? unistyles
: [unistyles]

// @ts-expect-error web types are not compatible with RN styles
UnistylesShadowRegistry.add(ref, styles)
storedRef.current = ref
UnistylesShadowRegistry.add(ref, unistyles)

if (ref) {
storedRef.current = ref
}

return passForwardedRef(props, ref, forwardedRef)
}}
style={state => {
const unistyles = typeof style === 'function'
? style(state)
: style
const styles = Array.isArray(unistyles)
? unistyles
: [unistyles]

if (!storedRef.current) {
return unistyles
}

// @ts-expect-error hidden from TS
UnistylesShadowRegistry.remove(storedRef.current)
// @ts-expect-error - this is hidden from TS
UnistylesShadowRegistry.add(storedRef.current, styles)
UnistylesShadowRegistry.add(storedRef.current, unistyles)

return unistyles
}}
Expand Down
6 changes: 1 addition & 5 deletions src/core/createUnistylesImageBackground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ export const createUnistylesImageBackground = (Component: typeof ImageBackground
storedImageRef.current = ref
}

const style = Array.isArray(props.imageStyle)
? props.imageStyle
: [props.imageStyle]

// @ts-expect-error web types are not compatible with RN styles
UnistylesShadowRegistry.add(ref, style)
UnistylesShadowRegistry.add(ref, props.imageStyle)
}}
/>
)
Expand Down

0 comments on commit 59ca583

Please sign in to comment.