Skip to content

Commit 0b36079

Browse files
committed
feat: imageBackground web
1 parent 415cb5d commit 0b36079

File tree

3 files changed

+35
-87
lines changed

3 files changed

+35
-87
lines changed

src/components/native/ImageBackground.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Props = {
1313

1414
export const ImageBackground = forwardRef<unknown, Props>((props, forwardedRef) => {
1515
let storedRef: NativeImageBackground | null = null
16+
let storedImageRef: NativeImageBackground | null = null
1617
const styleClassNames = getClassName(props.style)
1718
const imageClassNames = getClassName(props.imageStyle)
1819

@@ -26,15 +27,11 @@ export const ImageBackground = forwardRef<unknown, Props>((props, forwardedRef)
2627
if (!ref) {
2728
// @ts-expect-error hidden from TS
2829
UnistylesShadowRegistry.remove(storedRef, styleClassNames?.hash)
29-
// @ts-expect-error hidden from TS
30-
UnistylesShadowRegistry.remove(storedRef, imageClassNames?.hash)
3130
}
3231

3332
storedRef = ref
34-
// @ts-expect-error hidden from TS
35-
UnistylesShadowRegistry.add(ref, styleClassNames?.hash)
36-
// @ts-expect-error hidden from TS
37-
UnistylesShadowRegistry.add(ref, imageClassNames?.hash)
33+
// @ts-expect-error hidden from TS
34+
UnistylesShadowRegistry.add(ref, styleClassNames?.hash)
3835

3936
if (typeof forwardedRef === 'function') {
4037
return forwardedRef(ref)
@@ -44,6 +41,16 @@ export const ImageBackground = forwardRef<unknown, Props>((props, forwardedRef)
4441
forwardedRef.current = ref
4542
}
4643
}}
44+
imageRef={isServer() ? undefined : ref => {
45+
if (!ref) {
46+
// @ts-expect-error hidden from TS
47+
UnistylesShadowRegistry.remove(storedImageRef, imageClassNames?.hash)
48+
}
49+
50+
storedImageRef = ref
51+
// @ts-expect-error hidden from TS
52+
UnistylesShadowRegistry.add(ref, imageClassNames?.hash)
53+
}}
4754
/>
4855
)
4956
})

src/core/createUnistylesImageBackground.native.tsx

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,24 @@
11
import React from 'react'
2-
import { getClassName } from './getClassname'
3-
import type { UnistylesValues } from '../types'
4-
import { isServer } from '../web/utils'
5-
6-
type ComponentProps = {
7-
style?: UnistylesValues | Array<UnistylesValues>
8-
}
9-
10-
export const createUnistylesImageBackground = (Component: any) => React.forwardRef<HTMLElement, ComponentProps>((props, forwardedRef) => {
11-
let storedRef: HTMLElement | null = null
12-
let storedImageRef: HTMLElement | null = null
13-
const classNames = getClassName(props.style)
14-
const imageClassNames = getClassName(props.style)
15-
16-
return (
17-
<Component
18-
{...props}
19-
style={classNames}
20-
imageStyle={imageClassNames}
21-
imageRef={isServer() ? undefined : (ref: HTMLElement | null) => {
22-
if (!ref) {
23-
// @ts-expect-error hidden from TS
24-
UnistylesShadowRegistry.remove(storedImageRef, imageClassNames?.hash)
25-
}
26-
27-
storedImageRef = ref
28-
// @ts-expect-error hidden from TS
29-
UnistylesShadowRegistry.add(ref, imageClassNames?.hash)
30-
31-
if (typeof forwardedRef === 'function') {
32-
return forwardedRef(ref)
33-
}
34-
35-
if (forwardedRef) {
36-
forwardedRef.current = ref
37-
}
38-
}}
39-
ref={isServer() ? undefined : (ref: HTMLElement | null) => {
40-
if (!ref) {
41-
// @ts-expect-error hidden from TS
42-
UnistylesShadowRegistry.remove(storedRef, classNames?.hash)
43-
}
44-
45-
storedRef = ref
2+
import type { ImageBackground, ImageBackgroundProps } from 'react-native'
3+
import { UnistylesShadowRegistry } from '../specs'
4+
import { passForwardedRef } from './passForwardRef'
5+
6+
export const createUnistylesImageBackground = (Component: typeof ImageBackground) => React.forwardRef<ImageBackground, ImageBackgroundProps>((props, forwardedRef) => (
7+
<Component
8+
{...props}
9+
ref={ref => passForwardedRef(props, ref, forwardedRef)}
10+
imageRef={ref => {
11+
const style = Array.isArray(props.imageStyle)
12+
? props.imageStyle
13+
: [props.imageStyle]
14+
15+
// @ts-expect-error web types are not compatible with RN styles
16+
UnistylesShadowRegistry.add(ref, style)
17+
18+
return () => {
4619
// @ts-expect-error hidden from TS
47-
UnistylesShadowRegistry.add(ref, classNames?.hash)
48-
49-
if (typeof forwardedRef === 'function') {
50-
return forwardedRef(ref)
51-
}
52-
53-
if (forwardedRef) {
54-
forwardedRef.current = ref
55-
}
56-
}}
57-
/>
58-
)
59-
})
20+
UnistylesShadowRegistry.remove(ref)
21+
}
22+
}}
23+
/>
24+
))

0 commit comments

Comments
 (0)