react-native-unistyles 3.0.0-nightly-20250328 → 3.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/cxx/core/UnistylesCommitHook.cpp +2 -2
- package/cxx/core/UnistylesRegistry.cpp +24 -20
- package/cxx/hybridObjects/HybridStyleSheet.cpp +15 -11
- package/cxx/parser/Parser.cpp +11 -8
- package/cxx/shadowTree/ShadowTrafficController.h +7 -9
- package/lib/commonjs/components/native/Image.js +26 -1
- package/lib/commonjs/components/native/Image.js.map +1 -1
- package/lib/commonjs/components/native/Image.native.js +10 -0
- package/lib/commonjs/components/native/Image.native.js.map +1 -0
- package/lib/commonjs/components/native/ImageBackground.js +16 -28
- package/lib/commonjs/components/native/ImageBackground.js.map +1 -1
- package/lib/commonjs/components/native/Pressable.native.js +7 -4
- package/lib/commonjs/components/native/Pressable.native.js.map +1 -1
- package/lib/commonjs/core/createUnistylesElement.js +3 -19
- package/lib/commonjs/core/createUnistylesElement.js.map +1 -1
- package/lib/commonjs/core/createUnistylesElement.native.js +6 -4
- package/lib/commonjs/core/createUnistylesElement.native.js.map +1 -1
- package/lib/commonjs/core/createUnistylesImageBackground.js +7 -5
- package/lib/commonjs/core/createUnistylesImageBackground.js.map +1 -1
- package/lib/commonjs/core/passForwardRef.js +3 -5
- package/lib/commonjs/core/passForwardRef.js.map +1 -1
- package/lib/commonjs/server/getServerUnistyles.js +4 -1
- package/lib/commonjs/server/getServerUnistyles.js.map +1 -1
- package/lib/commonjs/server/serialize.js +24 -0
- package/lib/commonjs/server/serialize.js.map +1 -0
- package/lib/commonjs/specs/ShadowRegistry/index.js +2 -2
- package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/commonjs/web/create.js +6 -0
- package/lib/commonjs/web/create.js.map +1 -1
- package/lib/commonjs/web/css/state.js +5 -2
- package/lib/commonjs/web/css/state.js.map +1 -1
- package/lib/commonjs/web/registry.js +1 -1
- package/lib/commonjs/web/registry.js.map +1 -1
- package/lib/commonjs/web/state.js +3 -0
- package/lib/commonjs/web/state.js.map +1 -1
- package/lib/commonjs/web/utils/createUnistylesRef.js +28 -0
- package/lib/commonjs/web/utils/createUnistylesRef.js.map +1 -0
- package/lib/commonjs/web/utils/index.js +11 -0
- package/lib/commonjs/web/utils/index.js.map +1 -1
- package/lib/module/components/native/Image.js +25 -2
- package/lib/module/components/native/Image.js.map +1 -1
- package/lib/module/components/native/Image.native.js +6 -0
- package/lib/module/components/native/Image.native.js.map +1 -0
- package/lib/module/components/native/ImageBackground.js +17 -29
- package/lib/module/components/native/ImageBackground.js.map +1 -1
- package/lib/module/components/native/Pressable.native.js +7 -4
- package/lib/module/components/native/Pressable.native.js.map +1 -1
- package/lib/module/core/createUnistylesElement.js +4 -20
- package/lib/module/core/createUnistylesElement.js.map +1 -1
- package/lib/module/core/createUnistylesElement.native.js +6 -4
- package/lib/module/core/createUnistylesElement.native.js.map +1 -1
- package/lib/module/core/createUnistylesImageBackground.js +7 -5
- package/lib/module/core/createUnistylesImageBackground.js.map +1 -1
- package/lib/module/core/passForwardRef.js +3 -5
- package/lib/module/core/passForwardRef.js.map +1 -1
- package/lib/module/server/getServerUnistyles.js +4 -1
- package/lib/module/server/getServerUnistyles.js.map +1 -1
- package/lib/module/server/serialize.js +19 -0
- package/lib/module/server/serialize.js.map +1 -0
- package/lib/module/specs/ShadowRegistry/index.js +2 -2
- package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/module/web/create.js +7 -1
- package/lib/module/web/create.js.map +1 -1
- package/lib/module/web/css/state.js +5 -2
- package/lib/module/web/css/state.js.map +1 -1
- package/lib/module/web/registry.js +1 -1
- package/lib/module/web/registry.js.map +1 -1
- package/lib/module/web/state.js +3 -0
- package/lib/module/web/state.js.map +1 -1
- package/lib/module/web/utils/createUnistylesRef.js +23 -0
- package/lib/module/web/utils/createUnistylesRef.js.map +1 -0
- package/lib/module/web/utils/index.js +1 -0
- package/lib/module/web/utils/index.js.map +1 -1
- package/lib/typescript/src/components/native/Image.d.ts.map +1 -1
- package/lib/typescript/src/components/native/Image.native.d.ts +2 -0
- package/lib/typescript/src/components/native/Image.native.d.ts.map +1 -0
- package/lib/typescript/src/components/native/ImageBackground.d.ts.map +1 -1
- package/lib/typescript/src/components/native/Pressable.native.d.ts.map +1 -1
- package/lib/typescript/src/core/createUnistylesElement.d.ts.map +1 -1
- package/lib/typescript/src/core/createUnistylesElement.native.d.ts.map +1 -1
- package/lib/typescript/src/core/createUnistylesImageBackground.d.ts.map +1 -1
- package/lib/typescript/src/core/passForwardRef.d.ts +1 -1
- package/lib/typescript/src/core/passForwardRef.d.ts.map +1 -1
- package/lib/typescript/src/server/getServerUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/server/serialize.d.ts +2 -0
- package/lib/typescript/src/server/serialize.d.ts.map +1 -0
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
- package/lib/typescript/src/web/create.d.ts.map +1 -1
- package/lib/typescript/src/web/css/state.d.ts +4 -1
- package/lib/typescript/src/web/css/state.d.ts.map +1 -1
- package/lib/typescript/src/web/registry.d.ts.map +1 -1
- package/lib/typescript/src/web/state.d.ts +2 -0
- package/lib/typescript/src/web/state.d.ts.map +1 -1
- package/lib/typescript/src/web/utils/createUnistylesRef.d.ts +8 -0
- package/lib/typescript/src/web/utils/createUnistylesRef.d.ts.map +1 -0
- package/lib/typescript/src/web/utils/index.d.ts +1 -0
- package/lib/typescript/src/web/utils/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/native/Image.native.tsx +4 -0
- package/src/components/native/Image.tsx +35 -3
- package/src/components/native/ImageBackground.tsx +17 -33
- package/src/components/native/Pressable.native.tsx +12 -4
- package/src/core/createUnistylesElement.native.tsx +12 -6
- package/src/core/createUnistylesElement.tsx +6 -24
- package/src/core/createUnistylesImageBackground.tsx +16 -10
- package/src/core/passForwardRef.ts +5 -5
- package/src/server/getServerUnistyles.tsx +12 -7
- package/src/server/serialize.ts +20 -0
- package/src/specs/ShadowRegistry/index.ts +1 -2
- package/src/web/create.ts +8 -1
- package/src/web/css/state.ts +4 -2
- package/src/web/registry.ts +4 -2
- package/src/web/state.ts +4 -0
- package/src/web/utils/createUnistylesRef.ts +29 -0
- package/src/web/utils/index.ts +1 -0
@@ -1,4 +1,36 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import React, { type ComponentProps, forwardRef } from 'react'
|
2
|
+
import { type ImageStyle, Image as NativeImage, type StyleProp, type ViewStyle } from 'react-native'
|
3
|
+
import { getClassName } from '../../core'
|
4
|
+
import { maybeWarnAboutMultipleUnistyles } from '../../core/warn'
|
5
|
+
import type { UnistylesValues } from '../../types'
|
6
|
+
import { copyComponentProperties } from '../../utils'
|
7
|
+
import { createUnistylesRef, keyInObject } from '../../web/utils'
|
3
8
|
|
4
|
-
|
9
|
+
type Props = ComponentProps<typeof NativeImage> & {
|
10
|
+
style?: UnistylesValues
|
11
|
+
imageStyle?: UnistylesValues
|
12
|
+
}
|
13
|
+
|
14
|
+
const UnistylesImage = forwardRef<unknown, Props>((props, forwardedRef) => {
|
15
|
+
const classNames = getClassName(props.style)
|
16
|
+
const ref = createUnistylesRef(classNames, forwardedRef)
|
17
|
+
const hasWidthStyle = typeof props.style === 'object' && keyInObject(props.style, 'width')
|
18
|
+
const hasHeightStyle = typeof props.style === 'object' && keyInObject(props.style, 'height')
|
19
|
+
|
20
|
+
maybeWarnAboutMultipleUnistyles(props.style as ViewStyle, 'Image')
|
21
|
+
|
22
|
+
return (
|
23
|
+
<NativeImage
|
24
|
+
{...props}
|
25
|
+
style={[
|
26
|
+
classNames,
|
27
|
+
// Clear inline width and height extracted from source
|
28
|
+
hasWidthStyle && { width: '' },
|
29
|
+
hasHeightStyle && { height: '' }
|
30
|
+
] as StyleProp<ImageStyle>}
|
31
|
+
ref={ref}
|
32
|
+
/>
|
33
|
+
)
|
34
|
+
})
|
35
|
+
|
36
|
+
export const Image = copyComponentProperties(NativeImage, UnistylesImage)
|
@@ -2,10 +2,10 @@ import React from 'react'
|
|
2
2
|
import { forwardRef } from 'react'
|
3
3
|
import { type ImageStyle, ImageBackground as NativeImageBackground, type StyleProp, type ViewStyle } from 'react-native'
|
4
4
|
import { getClassName } from '../../core'
|
5
|
+
import { maybeWarnAboutMultipleUnistyles } from '../../core/warn'
|
5
6
|
import type { UnistylesValues } from '../../types'
|
6
7
|
import { copyComponentProperties } from '../../utils'
|
7
|
-
import {
|
8
|
-
import { isServer } from '../../web/utils'
|
8
|
+
import { createUnistylesRef, keyInObject } from '../../web/utils'
|
9
9
|
|
10
10
|
type Props = {
|
11
11
|
style?: UnistylesValues
|
@@ -13,44 +13,28 @@ type Props = {
|
|
13
13
|
}
|
14
14
|
|
15
15
|
const UnistylesImageBackground = forwardRef<unknown, Props>((props, forwardedRef) => {
|
16
|
-
let storedRef: NativeImageBackground | null = null
|
17
|
-
let storedImageRef: NativeImageBackground | null = null
|
18
16
|
const styleClassNames = getClassName(props.style)
|
19
17
|
const imageClassNames = getClassName(props.imageStyle)
|
18
|
+
const ref = createUnistylesRef(styleClassNames, forwardedRef)
|
19
|
+
const imageRef = createUnistylesRef(imageClassNames)
|
20
|
+
const hasWidthStyle = typeof props.imageStyle === 'object' && keyInObject(props.imageStyle, 'width')
|
21
|
+
const hasHeightStyle = typeof props.imageStyle === 'object' && keyInObject(props.imageStyle, 'height')
|
22
|
+
|
23
|
+
maybeWarnAboutMultipleUnistyles(props.style as ViewStyle, 'ImageBackground')
|
24
|
+
maybeWarnAboutMultipleUnistyles(props.imageStyle as ViewStyle, 'ImageBackground')
|
20
25
|
|
21
26
|
return (
|
22
27
|
<NativeImageBackground
|
23
28
|
{...props}
|
24
29
|
style={styleClassNames as StyleProp<ViewStyle>}
|
25
|
-
imageStyle={
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
// @ts-expect-error hidden from TS
|
34
|
-
UnistylesShadowRegistry.add(ref, styleClassNames?.hash)
|
35
|
-
|
36
|
-
if (typeof forwardedRef === 'function') {
|
37
|
-
return forwardedRef(ref)
|
38
|
-
}
|
39
|
-
|
40
|
-
if (forwardedRef) {
|
41
|
-
forwardedRef.current = ref
|
42
|
-
}
|
43
|
-
}}
|
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
|
-
}}
|
30
|
+
imageStyle={[
|
31
|
+
imageClassNames,
|
32
|
+
// Clear inline width and height extracted from source
|
33
|
+
hasWidthStyle && { width: '' },
|
34
|
+
hasHeightStyle && { height: '' }
|
35
|
+
] as StyleProp<ImageStyle>}
|
36
|
+
ref={ref}
|
37
|
+
imageRef={imageRef}
|
54
38
|
/>
|
55
39
|
)
|
56
40
|
})
|
@@ -50,10 +50,18 @@ export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ..
|
|
50
50
|
storedRef.current = ref
|
51
51
|
}
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
return passForwardedRef(
|
54
|
+
ref,
|
55
|
+
forwardedRef,
|
56
|
+
() => {
|
57
|
+
// @ts-expect-error - this is hidden from TS
|
58
|
+
UnistylesShadowRegistry.add(ref, unistyles)
|
59
|
+
},
|
60
|
+
() => {
|
61
|
+
// @ts-expect-error - this is hidden from TS
|
62
|
+
UnistylesShadowRegistry.remove(ref)
|
63
|
+
}
|
64
|
+
)
|
57
65
|
}}
|
58
66
|
style={state => {
|
59
67
|
const isPropStyleAFunction = typeof style === 'function'
|
@@ -38,15 +38,21 @@ export const createUnistylesElement = (Component: any) => {
|
|
38
38
|
storedRef.current = getNativeRef(Component, ref)
|
39
39
|
}
|
40
40
|
|
41
|
-
passForwardedRef(props, ref, forwardedRef)
|
42
|
-
|
43
41
|
// @ts-ignore we don't know the type of the component
|
44
42
|
maybeWarnAboutMultipleUnistyles(props.style, Component.displayName)
|
45
43
|
|
46
|
-
return (
|
47
|
-
|
48
|
-
|
49
|
-
|
44
|
+
return passForwardedRef(
|
45
|
+
ref,
|
46
|
+
forwardedRef,
|
47
|
+
() => {
|
48
|
+
// @ts-ignore this is hidden from TS
|
49
|
+
UnistylesShadowRegistry.add(ref, props.style)
|
50
|
+
},
|
51
|
+
() => {
|
52
|
+
// @ts-ignore this is hidden from TS
|
53
|
+
UnistylesShadowRegistry.remove(ref)
|
54
|
+
}
|
55
|
+
)
|
50
56
|
}}
|
51
57
|
/>
|
52
58
|
)
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import React from 'react'
|
2
|
+
import type { ViewStyle } from 'react-native'
|
2
3
|
import type { UnistylesValues } from '../types'
|
3
4
|
import { copyComponentProperties } from '../utils'
|
4
|
-
import {
|
5
|
-
import { isServer } from '../web/utils'
|
5
|
+
import { createUnistylesRef } from '../web/utils'
|
6
6
|
import { getClassName } from './getClassname'
|
7
7
|
import { maybeWarnAboutMultipleUnistyles } from './warn'
|
8
8
|
|
@@ -12,34 +12,16 @@ type ComponentProps = {
|
|
12
12
|
|
13
13
|
export const createUnistylesElement = (Component: any) => {
|
14
14
|
const UnistylesComponent = React.forwardRef<unknown, ComponentProps>((props, forwardedRef) => {
|
15
|
-
let storedRef: HTMLElement | null = null
|
16
15
|
const classNames = getClassName(props.style)
|
16
|
+
const ref = createUnistylesRef(classNames, forwardedRef)
|
17
|
+
|
18
|
+
maybeWarnAboutMultipleUnistyles(props.style as ViewStyle, Component.displayName)
|
17
19
|
|
18
20
|
return (
|
19
21
|
<Component
|
20
22
|
{...props}
|
21
23
|
style={classNames}
|
22
|
-
ref={
|
23
|
-
// @ts-ignore we don't know the type of the component
|
24
|
-
maybeWarnAboutMultipleUnistyles(props.style, Component.displayName)
|
25
|
-
|
26
|
-
if (!ref) {
|
27
|
-
// @ts-expect-error hidden from TS
|
28
|
-
UnistylesShadowRegistry.remove(storedRef, classNames?.hash)
|
29
|
-
}
|
30
|
-
|
31
|
-
storedRef = ref
|
32
|
-
// @ts-expect-error hidden from TS
|
33
|
-
UnistylesShadowRegistry.add(ref, classNames?.hash)
|
34
|
-
|
35
|
-
if (typeof forwardedRef === 'function') {
|
36
|
-
return forwardedRef(ref)
|
37
|
-
}
|
38
|
-
|
39
|
-
if (forwardedRef) {
|
40
|
-
forwardedRef.current = ref
|
41
|
-
}
|
42
|
-
}}
|
24
|
+
ref={ref}
|
43
25
|
/>
|
44
26
|
)
|
45
27
|
})
|
@@ -27,17 +27,23 @@ export const createUnistylesImageBackground = (Component: typeof ImageBackground
|
|
27
27
|
<Component
|
28
28
|
{...props}
|
29
29
|
ref={ref => {
|
30
|
-
passForwardedRef(
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
30
|
+
return passForwardedRef(
|
31
|
+
ref,
|
32
|
+
forwardedRef,
|
33
|
+
() => {
|
34
|
+
// @ts-expect-error - this is hidden from TS
|
35
|
+
UnistylesShadowRegistry.add(ref, props.style)
|
36
|
+
},
|
37
|
+
() => {
|
38
|
+
// @ts-expect-error - this is hidden from TS
|
39
|
+
UnistylesShadowRegistry.remove(ref)
|
40
|
+
|
41
|
+
if (storedImageRef.current) {
|
42
|
+
// @ts-expect-error - this is hidden from TS
|
43
|
+
UnistylesShadowRegistry.remove(storedImageRef.current)
|
44
|
+
}
|
39
45
|
}
|
40
|
-
|
46
|
+
)
|
41
47
|
}}
|
42
48
|
imageRef={ref => {
|
43
49
|
if (ref) {
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import React from 'react'
|
2
|
-
import { UnistylesShadowRegistry } from '../specs'
|
3
2
|
|
4
3
|
export const passForwardedRef = <T>(
|
5
|
-
props: any,
|
6
4
|
ref: T,
|
7
|
-
forwardedRef: React.ForwardedRef<T
|
5
|
+
forwardedRef: React.ForwardedRef<T>,
|
6
|
+
onMount?: () => void,
|
7
|
+
onUnmount?: () => void
|
8
8
|
) => {
|
9
9
|
const passForwardedRef = () => {
|
10
10
|
if (typeof forwardedRef === 'function') {
|
@@ -19,10 +19,10 @@ export const passForwardedRef = <T>(
|
|
19
19
|
}
|
20
20
|
const forwardedRefReturnFn = passForwardedRef()
|
21
21
|
|
22
|
-
|
23
|
-
UnistylesShadowRegistry.add(ref, props.style)
|
22
|
+
onMount?.()
|
24
23
|
|
25
24
|
return () => {
|
26
25
|
forwardedRefReturnFn?.()
|
26
|
+
onUnmount?.()
|
27
27
|
}
|
28
28
|
}
|
@@ -2,20 +2,25 @@ import React from 'react'
|
|
2
2
|
import { StyleSheet } from 'react-native'
|
3
3
|
import { UnistylesWeb } from '../web'
|
4
4
|
import { error, isServer } from '../web/utils'
|
5
|
+
import { serialize } from './serialize'
|
5
6
|
import { DefaultServerUnistylesSettings, type ServerUnistylesSettings } from './types'
|
6
7
|
|
7
8
|
export const getServerUnistyles = ({ includeRNWStyles = true }: ServerUnistylesSettings = DefaultServerUnistylesSettings) => {
|
8
9
|
if (!isServer()) {
|
9
10
|
throw error('Server styles should only be read on the server')
|
10
11
|
}
|
12
|
+
|
11
13
|
// @ts-ignore
|
12
14
|
const rnwStyle: string | null = includeRNWStyles ? (StyleSheet?.getSheet().textContent ?? '') : null
|
13
15
|
const css = UnistylesWeb.registry.css.getStyles()
|
14
16
|
const state = UnistylesWeb.registry.css.getState()
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
}
|
17
|
+
|
18
|
+
return (
|
19
|
+
<>
|
20
|
+
{rnwStyle && <style id='rnw-style'>{rnwStyle}</style>}
|
21
|
+
<style id='unistyles-web'>{css}</style>
|
22
|
+
{/* biome-ignore lint/security/noDangerouslySetInnerHtml: Needs the json quotes to be unescaped */}
|
23
|
+
<script id='unistyles-script' defer dangerouslySetInnerHTML={{ __html: `window.__UNISTYLES_STATE__ = ${serialize(state)}`}} />
|
24
|
+
</>
|
25
|
+
)
|
26
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
export const serialize = (value: any): string => {
|
2
|
+
switch (typeof value) {
|
3
|
+
case 'function':
|
4
|
+
return value.toString()
|
5
|
+
case 'object':
|
6
|
+
if (Array.isArray(value)) {
|
7
|
+
return `[${value.map(serialize).join(',')}]`
|
8
|
+
}
|
9
|
+
|
10
|
+
if (value === null) {
|
11
|
+
return 'null'
|
12
|
+
}
|
13
|
+
|
14
|
+
return `{${Object.entries(value)
|
15
|
+
.map(([key, value]) => `'${key}':${serialize(value)}`)
|
16
|
+
.join(',')}}`
|
17
|
+
default:
|
18
|
+
return JSON.stringify(value)
|
19
|
+
}
|
20
|
+
}
|
@@ -40,9 +40,8 @@ HybridShadowRegistry.add = (handle, styles) => {
|
|
40
40
|
? styles.flat()
|
41
41
|
: [styles]
|
42
42
|
|
43
|
-
// filter
|
43
|
+
// filter styles that are undefined or with no keys
|
44
44
|
const filteredStyles = stylesArray
|
45
|
-
.filter(style => !style?.initial?.updater)
|
46
45
|
.filter(style => style && Object.keys(style).length > 0)
|
47
46
|
.flat()
|
48
47
|
.filter(Boolean)
|
package/src/web/create.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { StyleSheet, StyleSheetWithSuperPowers } from '../types/stylesheet'
|
2
2
|
import { UnistylesWeb } from './index'
|
3
|
-
import { assignSecrets, error, removeInlineStyles } from './utils'
|
3
|
+
import { assignSecrets, error, isServer, removeInlineStyles } from './utils'
|
4
4
|
|
5
5
|
type Variants = Record<string, string | boolean | undefined>
|
6
6
|
|
@@ -9,6 +9,13 @@ export const create = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>, id?: s
|
|
9
9
|
throw error('Unistyles is not initialized correctly. Please add babel plugin to your babel config.')
|
10
10
|
}
|
11
11
|
|
12
|
+
// For SSR
|
13
|
+
if (!UnistylesWeb.state.isInitialized && !isServer()) {
|
14
|
+
const config = window?.__UNISTYLES_STATE__?.config
|
15
|
+
|
16
|
+
config && UnistylesWeb.state.init(config)
|
17
|
+
}
|
18
|
+
|
12
19
|
const computedStylesheet = UnistylesWeb.registry.getComputedStylesheet(stylesheet)
|
13
20
|
const addSecrets = (value: any, key: string, args = undefined as Array<any> | undefined, variants = {} as Variants) => assignSecrets(value, {
|
14
21
|
__uni__key: key,
|
package/src/web/css/state.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import type { UnistylesValues } from '../../types'
|
2
2
|
import { convertUnistyles } from '../convert'
|
3
|
+
import type { UnistylesServices } from '../types'
|
3
4
|
import { hyphenate, isServer } from '../utils'
|
4
5
|
import { convertToCSS } from './core'
|
5
6
|
|
@@ -33,7 +34,7 @@ export class CSSState {
|
|
33
34
|
private styleTag: HTMLStyleElement | null = null
|
34
35
|
private CSS = ''
|
35
36
|
|
36
|
-
constructor() {
|
37
|
+
constructor(private services: UnistylesServices) {
|
37
38
|
if (isServer()) {
|
38
39
|
return
|
39
40
|
}
|
@@ -159,8 +160,9 @@ export class CSSState {
|
|
159
160
|
|
160
161
|
const mainState = getState(this.mainMap)
|
161
162
|
const mqState = getState(this.mqMap)
|
163
|
+
const config = this.services.state.getConfig()
|
162
164
|
|
163
|
-
return { mainState, mqState }
|
165
|
+
return { mainState, mqState, config }
|
164
166
|
}
|
165
167
|
|
166
168
|
hydrate = ({ mainState, mqState }: ReturnType<typeof this.getState>) => {
|
package/src/web/registry.ts
CHANGED
@@ -11,9 +11,11 @@ export class UnistylesRegistry {
|
|
11
11
|
private readonly stylesCounter = new Map<string, Set<HTMLElement>>()
|
12
12
|
private readonly disposeListenersMap = new Map<object, VoidFunction>()
|
13
13
|
private readonly dependenciesMap = new Map<StyleSheetWithSuperPowers<StyleSheet>, Set<UnistyleDependency>>()
|
14
|
-
readonly css
|
14
|
+
readonly css: CSSState
|
15
15
|
|
16
|
-
constructor(private services: UnistylesServices) {
|
16
|
+
constructor(private services: UnistylesServices) {
|
17
|
+
this.css = new CSSState(services)
|
18
|
+
}
|
17
19
|
|
18
20
|
getComputedStylesheet = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>, scopedThemeName?: UnistylesTheme) => {
|
19
21
|
if (typeof stylesheet !== 'function') {
|
package/src/web/state.ts
CHANGED
@@ -17,6 +17,7 @@ export class UnistylesState {
|
|
17
17
|
CSSVars = true
|
18
18
|
|
19
19
|
private matchingBreakpoints = new Map<string, boolean>()
|
20
|
+
private _config: UnistylesConfig = {}
|
20
21
|
|
21
22
|
get breakpoint() {
|
22
23
|
const [currentBreakpoint] = Array.from(this.matchingBreakpoints)
|
@@ -37,6 +38,7 @@ export class UnistylesState {
|
|
37
38
|
return
|
38
39
|
}
|
39
40
|
|
41
|
+
this._config = config
|
40
42
|
this.isInitialized = true
|
41
43
|
this.initThemes(config.themes, config.settings?.CSSVars)
|
42
44
|
this.initBreakpoints(config.breakpoints)
|
@@ -153,4 +155,6 @@ export class UnistylesState {
|
|
153
155
|
})
|
154
156
|
})
|
155
157
|
}
|
158
|
+
|
159
|
+
getConfig = () => this._config
|
156
160
|
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import type React from 'react'
|
2
|
+
import type { Nullable } from '../../types'
|
3
|
+
import { UnistylesWeb } from '../index'
|
4
|
+
import { isServer } from './common'
|
5
|
+
|
6
|
+
type ClassNames = {
|
7
|
+
hash: string
|
8
|
+
}
|
9
|
+
|
10
|
+
export const createUnistylesRef = <T>(classNames?: ClassNames, forwardedRef?: React.ForwardedRef<T>) => {
|
11
|
+
const storedRef = { current: null as Nullable<T> }
|
12
|
+
|
13
|
+
return isServer() ? undefined : (ref: Nullable<T>) => {
|
14
|
+
if (!ref) {
|
15
|
+
UnistylesWeb.shadowRegistry.remove(storedRef, classNames?.hash)
|
16
|
+
}
|
17
|
+
|
18
|
+
storedRef.current = ref
|
19
|
+
UnistylesWeb.shadowRegistry.add(ref, classNames?.hash)
|
20
|
+
|
21
|
+
if (typeof forwardedRef === 'function') {
|
22
|
+
return forwardedRef(ref)
|
23
|
+
}
|
24
|
+
|
25
|
+
if (forwardedRef) {
|
26
|
+
forwardedRef.current = ref
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
package/src/web/utils/index.ts
CHANGED