|
1 | 1 | 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 () => { |
46 | 19 | // @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