react-native-unistyles 3.0.0-rc.3 → 3.0.0-rc.5
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 +10 -8
- package/android/src/main/cxx/NativeUnistylesModule.cpp +3 -7
- package/android/src/main/cxx/NativeUnistylesModule.h +0 -4
- package/android/src/main/java/com/unistyles/UnistylesModule.kt +1 -7
- package/cxx/core/UnistyleWrapper.h +4 -1
- package/cxx/core/UnistylesCommitShadowNode.h +0 -21
- package/cxx/core/UnistylesRegistry.cpp +45 -49
- package/cxx/core/UnistylesState.cpp +10 -0
- package/cxx/core/UnistylesState.h +3 -0
- package/cxx/hybridObjects/HybridShadowRegistry.cpp +6 -1
- package/cxx/hybridObjects/HybridShadowRegistry.h +6 -0
- package/cxx/hybridObjects/HybridStyleSheet.cpp +13 -16
- package/cxx/hybridObjects/HybridStyleSheet.h +2 -7
- package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +8 -0
- package/cxx/hybridObjects/HybridUnistylesRuntime.h +2 -0
- package/cxx/parser/Parser.cpp +175 -110
- package/cxx/parser/Parser.h +1 -0
- package/cxx/shadowTree/ShadowTreeManager.cpp +36 -33
- package/cxx/shadowTree/ShadowTreeManager.h +0 -1
- package/ios/UnistylesModuleOnLoad.h +1 -5
- package/ios/UnistylesModuleOnLoad.mm +5 -13
- package/lib/commonjs/components/AdaptiveTheme.js +35 -0
- package/lib/commonjs/components/AdaptiveTheme.js.map +1 -0
- package/lib/commonjs/components/ApplyScopedTheme.js +19 -0
- package/lib/commonjs/components/ApplyScopedTheme.js.map +1 -0
- package/lib/commonjs/components/NamedTheme.js +31 -0
- package/lib/commonjs/components/NamedTheme.js.map +1 -0
- package/lib/commonjs/components/ScopedTheme.js +24 -20
- package/lib/commonjs/components/ScopedTheme.js.map +1 -1
- package/lib/commonjs/components/native/Image.js +1 -2
- package/lib/commonjs/components/native/Image.js.map +1 -1
- package/lib/commonjs/components/native/ImageBackground.js +1 -2
- package/lib/commonjs/components/native/ImageBackground.js.map +1 -1
- package/lib/commonjs/components/native/Pressable.js +1 -2
- package/lib/commonjs/components/native/Pressable.js.map +1 -1
- package/lib/commonjs/components/native/Pressable.native.js +1 -2
- package/lib/commonjs/components/native/Pressable.native.js.map +1 -1
- package/lib/commonjs/core/createUnistylesElement.js +3 -3
- package/lib/commonjs/core/createUnistylesElement.js.map +1 -1
- package/lib/commonjs/core/createUnistylesElement.native.js +18 -31
- package/lib/commonjs/core/createUnistylesElement.native.js.map +1 -1
- package/lib/commonjs/core/createUnistylesImageBackground.js +6 -21
- package/lib/commonjs/core/createUnistylesImageBackground.js.map +1 -1
- package/lib/commonjs/core/getClassname.js +1 -2
- package/lib/commonjs/core/getClassname.js.map +1 -1
- package/lib/commonjs/core/parseBoxShadow.js +57 -0
- package/lib/commonjs/core/parseBoxShadow.js.map +1 -0
- package/lib/commonjs/core/useProxifiedUnistyles/listener.js +1 -2
- package/lib/commonjs/core/useProxifiedUnistyles/listener.js.map +1 -1
- package/lib/commonjs/core/withUnistyles/withUnistyles.js +1 -2
- package/lib/commonjs/core/withUnistyles/withUnistyles.js.map +1 -1
- package/lib/commonjs/core/withUnistyles/withUnistyles.native.js +1 -2
- package/lib/commonjs/core/withUnistyles/withUnistyles.native.js.map +1 -1
- package/lib/commonjs/index.js +6 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/mocks.js +5 -2
- package/lib/commonjs/mocks.js.map +1 -1
- package/lib/commonjs/reanimated/index.js +20 -0
- package/lib/commonjs/reanimated/index.js.map +1 -0
- package/lib/commonjs/reanimated/useAnimatedTheme.js +22 -0
- package/lib/commonjs/reanimated/useAnimatedTheme.js.map +1 -0
- package/lib/commonjs/reanimated/useAnimatedTheme.native.js +24 -0
- package/lib/commonjs/reanimated/useAnimatedTheme.native.js.map +1 -0
- package/lib/commonjs/reanimated/variant/index.js +17 -0
- package/lib/commonjs/reanimated/variant/index.js.map +1 -0
- package/lib/commonjs/reanimated/variant/types.js +2 -0
- package/lib/commonjs/reanimated/variant/types.js.map +1 -0
- package/lib/commonjs/reanimated/variant/useAnimatedVariantColor.js +38 -0
- package/lib/commonjs/reanimated/variant/useAnimatedVariantColor.js.map +1 -0
- package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js +66 -0
- package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js.map +1 -0
- package/lib/commonjs/reanimated/variant/useUpdateVariantColor.native.js +39 -0
- package/lib/commonjs/reanimated/variant/useUpdateVariantColor.native.js.map +1 -0
- package/lib/commonjs/server/getServerUnistyles.js +1 -2
- package/lib/commonjs/server/getServerUnistyles.js.map +1 -1
- package/lib/commonjs/server/hydrateServerUnistyles.js +1 -2
- package/lib/commonjs/server/hydrateServerUnistyles.js.map +1 -1
- package/lib/commonjs/server/resetServerUnistyles.js +1 -2
- package/lib/commonjs/server/resetServerUnistyles.js.map +1 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js +6 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/commonjs/specs/StyleSheet/index.js +3 -1
- package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
- package/lib/commonjs/utils.js +0 -3
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/commonjs/web/create.js +2 -3
- package/lib/commonjs/web/create.js.map +1 -1
- package/lib/commonjs/web/css/core.js +6 -1
- package/lib/commonjs/web/css/core.js.map +1 -1
- package/lib/commonjs/web/css/state.js +10 -7
- package/lib/commonjs/web/css/state.js.map +1 -1
- package/lib/commonjs/web/index.js +1 -2
- package/lib/commonjs/web/index.js.map +1 -1
- package/lib/commonjs/web/listener.js +2 -0
- package/lib/commonjs/web/listener.js.map +1 -1
- package/lib/commonjs/web/runtime.js +15 -1
- package/lib/commonjs/web/runtime.js.map +1 -1
- package/lib/commonjs/web/shadowRegistry.js +12 -4
- package/lib/commonjs/web/shadowRegistry.js.map +1 -1
- package/lib/commonjs/web/state.js +1 -10
- package/lib/commonjs/web/state.js.map +1 -1
- package/lib/commonjs/web/types.js +2 -0
- package/lib/commonjs/web/types.js.map +1 -1
- package/lib/commonjs/web/utils/createUnistylesRef.js +1 -2
- package/lib/commonjs/web/utils/createUnistylesRef.js.map +1 -1
- package/lib/commonjs/web/utils/unistyle.js +30 -3
- package/lib/commonjs/web/utils/unistyle.js.map +1 -1
- package/lib/commonjs/web-only/getWebProps.js +19 -0
- package/lib/commonjs/web-only/getWebProps.js.map +1 -0
- package/lib/commonjs/web-only/index.js +13 -0
- package/lib/commonjs/web-only/index.js.map +1 -0
- package/lib/module/components/AdaptiveTheme.js +29 -0
- package/lib/module/components/AdaptiveTheme.js.map +1 -0
- package/lib/module/components/ApplyScopedTheme.js +14 -0
- package/lib/module/components/ApplyScopedTheme.js.map +1 -0
- package/lib/module/components/NamedTheme.js +25 -0
- package/lib/module/components/NamedTheme.js.map +1 -0
- package/lib/module/components/ScopedTheme.js +24 -19
- package/lib/module/components/ScopedTheme.js.map +1 -1
- package/lib/module/core/createUnistylesElement.js +3 -3
- package/lib/module/core/createUnistylesElement.js.map +1 -1
- package/lib/module/core/createUnistylesElement.native.js +18 -30
- package/lib/module/core/createUnistylesElement.native.js.map +1 -1
- package/lib/module/core/createUnistylesImageBackground.js +5 -19
- package/lib/module/core/createUnistylesImageBackground.js.map +1 -1
- package/lib/module/core/parseBoxShadow.js +52 -0
- package/lib/module/core/parseBoxShadow.js.map +1 -0
- package/lib/module/core/withUnistyles/withUnistyles.native.js.map +1 -1
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/mocks.js +5 -2
- package/lib/module/mocks.js.map +1 -1
- package/lib/module/reanimated/index.js +5 -0
- package/lib/module/reanimated/index.js.map +1 -0
- package/lib/module/reanimated/useAnimatedTheme.js +17 -0
- package/lib/module/reanimated/useAnimatedTheme.js.map +1 -0
- package/lib/module/reanimated/useAnimatedTheme.native.js +19 -0
- package/lib/module/reanimated/useAnimatedTheme.native.js.map +1 -0
- package/lib/module/reanimated/variant/index.js +4 -0
- package/lib/module/reanimated/variant/index.js.map +1 -0
- package/lib/module/reanimated/variant/types.js +2 -0
- package/lib/module/reanimated/variant/types.js.map +1 -0
- package/lib/module/reanimated/variant/useAnimatedVariantColor.js +33 -0
- package/lib/module/reanimated/variant/useAnimatedVariantColor.js.map +1 -0
- package/lib/module/reanimated/variant/useUpdateVariantColor.js +61 -0
- package/lib/module/reanimated/variant/useUpdateVariantColor.js.map +1 -0
- package/lib/module/reanimated/variant/useUpdateVariantColor.native.js +34 -0
- package/lib/module/reanimated/variant/useUpdateVariantColor.native.js.map +1 -0
- package/lib/module/specs/ShadowRegistry/index.js +6 -1
- package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/module/specs/StyleSheet/index.js +3 -1
- package/lib/module/specs/StyleSheet/index.js.map +1 -1
- package/lib/module/utils.js +0 -3
- package/lib/module/utils.js.map +1 -1
- package/lib/module/web/create.js +1 -1
- package/lib/module/web/create.js.map +1 -1
- package/lib/module/web/css/core.js +6 -1
- package/lib/module/web/css/core.js.map +1 -1
- package/lib/module/web/css/state.js +10 -7
- package/lib/module/web/css/state.js.map +1 -1
- package/lib/module/web/listener.js +2 -0
- package/lib/module/web/listener.js.map +1 -1
- package/lib/module/web/runtime.js +16 -2
- package/lib/module/web/runtime.js.map +1 -1
- package/lib/module/web/shadowRegistry.js +13 -5
- package/lib/module/web/shadowRegistry.js.map +1 -1
- package/lib/module/web/state.js +1 -10
- package/lib/module/web/state.js.map +1 -1
- package/lib/module/web/types.js +1 -1
- package/lib/module/web/types.js.map +1 -1
- package/lib/module/web/utils/unistyle.js +27 -2
- package/lib/module/web/utils/unistyle.js.map +1 -1
- package/lib/module/web-only/getWebProps.js +14 -0
- package/lib/module/web-only/getWebProps.js.map +1 -0
- package/lib/module/web-only/index.js +4 -0
- package/lib/module/web-only/index.js.map +1 -0
- package/lib/typescript/plugin/src/consts.d.ts +17 -0
- package/lib/typescript/plugin/src/consts.d.ts.map +1 -0
- package/lib/typescript/repack-plugin/src/index.d.ts +18 -0
- package/lib/typescript/repack-plugin/src/index.d.ts.map +1 -0
- package/lib/typescript/repack-plugin/src/loader.d.ts +9 -0
- package/lib/typescript/repack-plugin/src/loader.d.ts.map +1 -0
- package/lib/typescript/src/components/AdaptiveTheme.d.ts +8 -0
- package/lib/typescript/src/components/AdaptiveTheme.d.ts.map +1 -0
- package/lib/typescript/src/components/ApplyScopedTheme.d.ts +7 -0
- package/lib/typescript/src/components/ApplyScopedTheme.d.ts.map +1 -0
- package/lib/typescript/src/components/NamedTheme.d.ts +10 -0
- package/lib/typescript/src/components/NamedTheme.d.ts.map +1 -0
- package/lib/typescript/src/components/ScopedTheme.d.ts +4 -0
- package/lib/typescript/src/components/ScopedTheme.d.ts.map +1 -1
- package/lib/typescript/src/components/native/Animated.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/getClassname.d.ts.map +1 -1
- package/lib/typescript/src/core/parseBoxShadow.d.ts +11 -0
- package/lib/typescript/src/core/parseBoxShadow.d.ts.map +1 -0
- package/lib/typescript/src/core/passForwardRef.d.ts.map +1 -1
- package/lib/typescript/src/core/useProxifiedUnistyles/listener.d.ts.map +1 -1
- package/lib/typescript/src/core/useProxifiedUnistyles/listener.native.d.ts.map +1 -1
- package/lib/typescript/src/core/useProxifiedUnistyles/useProxifiedUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/core/warn.d.ts.map +1 -1
- package/lib/typescript/src/core/withUnistyles/withUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useMedia.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useMedia.native.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/reanimated/index.d.ts +3 -0
- package/lib/typescript/src/reanimated/index.d.ts.map +1 -0
- package/lib/typescript/src/reanimated/useAnimatedTheme.d.ts +4 -0
- package/lib/typescript/src/reanimated/useAnimatedTheme.d.ts.map +1 -0
- package/lib/typescript/src/reanimated/useAnimatedTheme.native.d.ts +4 -0
- package/lib/typescript/src/reanimated/useAnimatedTheme.native.d.ts.map +1 -0
- package/lib/typescript/src/reanimated/variant/index.d.ts +2 -0
- package/lib/typescript/src/reanimated/variant/index.d.ts.map +1 -0
- package/lib/typescript/src/reanimated/variant/types.d.ts +10 -0
- package/lib/typescript/src/reanimated/variant/types.d.ts.map +1 -0
- package/lib/typescript/src/reanimated/variant/useAnimatedVariantColor.d.ts +3 -0
- package/lib/typescript/src/reanimated/variant/useAnimatedVariantColor.d.ts.map +1 -0
- package/lib/typescript/src/reanimated/variant/useUpdateVariantColor.d.ts +6 -0
- package/lib/typescript/src/reanimated/variant/useUpdateVariantColor.d.ts.map +1 -0
- package/lib/typescript/src/reanimated/variant/useUpdateVariantColor.native.d.ts +6 -0
- package/lib/typescript/src/reanimated/variant/useUpdateVariantColor.native.d.ts.map +1 -0
- package/lib/typescript/src/server/getServerUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/server/serialize.d.ts.map +1 -1
- package/lib/typescript/src/server/useServerUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +1 -0
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/StatusBar/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/StyleSheet/index.d.ts +2 -0
- package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/UnistylesRuntime/UnistylesRuntime.nitro.d.ts +2 -0
- package/lib/typescript/src/specs/UnistylesRuntime/UnistylesRuntime.nitro.d.ts.map +1 -1
- package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
- package/lib/typescript/src/utils.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/index.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/object/boxShadow.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/object/filter.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/object/objectStyle.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/object/transform.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/pseudo.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/shadow/boxShadow.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/shadow/getShadowBreakpoints.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/shadow/textShadow.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/style.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/utils.d.ts.map +1 -1
- package/lib/typescript/src/web/create.d.ts.map +1 -1
- package/lib/typescript/src/web/css/core.d.ts.map +1 -1
- package/lib/typescript/src/web/css/state.d.ts +2 -1
- package/lib/typescript/src/web/css/state.d.ts.map +1 -1
- package/lib/typescript/src/web/css/utils.d.ts.map +1 -1
- package/lib/typescript/src/web/listener.d.ts.map +1 -1
- package/lib/typescript/src/web/registry.d.ts.map +1 -1
- package/lib/typescript/src/web/runtime.d.ts +2 -0
- package/lib/typescript/src/web/runtime.d.ts.map +1 -1
- package/lib/typescript/src/web/shadowRegistry.d.ts +4 -2
- package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -1
- package/lib/typescript/src/web/state.d.ts.map +1 -1
- package/lib/typescript/src/web/types.d.ts +6 -0
- package/lib/typescript/src/web/types.d.ts.map +1 -1
- package/lib/typescript/src/web/utils/common.d.ts.map +1 -1
- package/lib/typescript/src/web/utils/createUnistylesRef.d.ts.map +1 -1
- package/lib/typescript/src/web/utils/unistyle.d.ts +7 -1
- package/lib/typescript/src/web/utils/unistyle.d.ts.map +1 -1
- package/lib/typescript/src/web/variants.d.ts.map +1 -1
- package/lib/typescript/src/web-only/getWebProps.d.ts +7 -0
- package/lib/typescript/src/web-only/getWebProps.d.ts.map +1 -0
- package/lib/typescript/src/web-only/index.d.ts +2 -0
- package/lib/typescript/src/web-only/index.d.ts.map +1 -0
- package/nitrogen/generated/ios/Unistyles-Swift-Cxx-Umbrella.hpp +1 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.cpp +2 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.hpp +2 -0
- package/package.json +57 -25
- package/plugin/index.d.ts +6 -5
- package/plugin/index.js +103 -99
- package/reanimated/package.json +6 -0
- package/repack-plugin/index.d.ts +19 -0
- package/repack-plugin/index.js +149 -0
- package/src/components/AdaptiveTheme.tsx +34 -0
- package/src/components/ApplyScopedTheme.tsx +17 -0
- package/src/components/NamedTheme.tsx +33 -0
- package/src/components/ScopedTheme.tsx +42 -22
- package/src/core/createUnistylesElement.native.tsx +21 -31
- package/src/core/createUnistylesElement.tsx +3 -3
- package/src/core/createUnistylesImageBackground.tsx +7 -22
- package/src/core/parseBoxShadow.ts +87 -0
- package/src/core/withUnistyles/withUnistyles.native.tsx +1 -0
- package/src/index.ts +8 -0
- package/src/mocks.ts +5 -2
- package/src/reanimated/index.ts +2 -0
- package/src/reanimated/useAnimatedTheme.native.ts +21 -0
- package/src/reanimated/useAnimatedTheme.ts +19 -0
- package/src/reanimated/variant/index.ts +1 -0
- package/src/reanimated/variant/types.ts +14 -0
- package/src/reanimated/variant/useAnimatedVariantColor.ts +38 -0
- package/src/reanimated/variant/useUpdateVariantColor.native.ts +38 -0
- package/src/reanimated/variant/useUpdateVariantColor.ts +81 -0
- package/src/specs/ShadowRegistry/index.ts +7 -1
- package/src/specs/StyleSheet/index.ts +5 -2
- package/src/specs/UnistylesRuntime/UnistylesRuntime.nitro.ts +2 -0
- package/src/utils.ts +1 -3
- package/src/web/create.ts +1 -1
- package/src/web/css/core.ts +9 -1
- package/src/web/css/state.ts +10 -8
- package/src/web/listener.ts +2 -0
- package/src/web/runtime.ts +23 -2
- package/src/web/shadowRegistry.ts +15 -6
- package/src/web/state.ts +7 -17
- package/src/web/types.ts +12 -4
- package/src/web/utils/unistyle.ts +38 -3
- package/src/web-only/getWebProps.ts +18 -0
- package/src/web-only/index.ts +2 -0
- package/web/package.json +6 -0
- package/cxx/core/UnistylesCommitHook.cpp +0 -52
- package/cxx/core/UnistylesCommitHook.h +0 -27
- package/cxx/core/UnistylesMountHook.cpp +0 -28
- package/cxx/core/UnistylesMountHook.h +0 -24
- package/lib/module/package.json +0 -1
@@ -0,0 +1,38 @@
|
|
1
|
+
import { interpolateColor, useDerivedValue, useSharedValue } from 'react-native-reanimated'
|
2
|
+
import type { ColorKeys } from './types'
|
3
|
+
import { useUpdateVariantColor } from './useUpdateVariantColor'
|
4
|
+
|
5
|
+
export const useAnimatedVariantColor = <T extends Record<string, any>>(style: T, colorKey: ColorKeys<T>) => {
|
6
|
+
const secretKey = Object.keys(style).find(key => key.startsWith('unistyles_'))
|
7
|
+
// @ts-ignore this is hidden from TS
|
8
|
+
const hasVariants = style[secretKey]?.__stylesheetVariants
|
9
|
+
|
10
|
+
if (!hasVariants || !colorKey.toLowerCase().includes('color')) {
|
11
|
+
throw new Error('useAnimatedVariantColor: Style was not created by Unistyles, does not have variants or has no color property')
|
12
|
+
}
|
13
|
+
|
14
|
+
const { fromValue, toValue } = useUpdateVariantColor({
|
15
|
+
animateCallback: (from, to) => animate(from, to),
|
16
|
+
colorKey,
|
17
|
+
secretKey,
|
18
|
+
style,
|
19
|
+
})
|
20
|
+
|
21
|
+
const progress = useSharedValue(1)
|
22
|
+
const animate = (from: string, to: string) => {
|
23
|
+
'worklet'
|
24
|
+
|
25
|
+
fromValue.set(from)
|
26
|
+
toValue.set(to)
|
27
|
+
}
|
28
|
+
|
29
|
+
const derivedColor = useDerivedValue(() => {
|
30
|
+
return interpolateColor(
|
31
|
+
progress.value,
|
32
|
+
[0, 1],
|
33
|
+
[fromValue.get(), toValue.get()]
|
34
|
+
)
|
35
|
+
})
|
36
|
+
|
37
|
+
return derivedColor
|
38
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
import { useEffect, useLayoutEffect } from 'react'
|
2
|
+
import { useSharedValue } from 'react-native-reanimated'
|
3
|
+
import { StyleSheet, UnistyleDependency } from '../../specs'
|
4
|
+
import type { UseUpdateVariantColorConfig } from './types'
|
5
|
+
|
6
|
+
export const useUpdateVariantColor = <T extends Record<string, any>>({
|
7
|
+
colorKey,
|
8
|
+
style,
|
9
|
+
secretKey
|
10
|
+
}: UseUpdateVariantColorConfig<T>) => {
|
11
|
+
const fromValue = useSharedValue<string>(style[colorKey])
|
12
|
+
const toValue = useSharedValue<string>(style[colorKey])
|
13
|
+
|
14
|
+
useEffect(() => {
|
15
|
+
// @ts-ignore this is hidden from TS
|
16
|
+
const dispose = StyleSheet.addChangeListener(changedDependencies => {
|
17
|
+
if (changedDependencies.includes(UnistyleDependency.Theme) || changedDependencies.includes(UnistyleDependency.Breakpoints)) {
|
18
|
+
// @ts-ignore
|
19
|
+
const newStyles = style[secretKey]?.uni__getStyles()
|
20
|
+
|
21
|
+
fromValue.set(toValue.value)
|
22
|
+
toValue.set(newStyles[colorKey])
|
23
|
+
}
|
24
|
+
})
|
25
|
+
|
26
|
+
return () => dispose()
|
27
|
+
}, [style, colorKey])
|
28
|
+
|
29
|
+
useLayoutEffect(() => {
|
30
|
+
fromValue.set(toValue.value)
|
31
|
+
toValue.set(style[colorKey])
|
32
|
+
}, [style, colorKey])
|
33
|
+
|
34
|
+
return {
|
35
|
+
fromValue,
|
36
|
+
toValue
|
37
|
+
}
|
38
|
+
}
|
@@ -0,0 +1,81 @@
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useState } from 'react'
|
2
|
+
import { runOnUI, useSharedValue } from 'react-native-reanimated'
|
3
|
+
import { UnistyleDependency } from '../../specs'
|
4
|
+
import type { UnistylesValues } from '../../types'
|
5
|
+
import { services } from '../../web/services'
|
6
|
+
import { getClosestBreakpointValue } from '../../web/utils'
|
7
|
+
import type { UseUpdateVariantColorConfig } from './types'
|
8
|
+
|
9
|
+
export const useUpdateVariantColor = <T extends Record<string, any>>({
|
10
|
+
animateCallback,
|
11
|
+
colorKey,
|
12
|
+
style
|
13
|
+
}: UseUpdateVariantColorConfig<T>) => {
|
14
|
+
const [dummyDiv] = useState(() => {
|
15
|
+
const div = document.createElement('div')
|
16
|
+
|
17
|
+
div.style.display = 'none'
|
18
|
+
document.body.appendChild(div)
|
19
|
+
|
20
|
+
return div
|
21
|
+
})
|
22
|
+
const parsedStyles = useMemo(() => {
|
23
|
+
return services.shadowRegistry.addStyles([style]).parsedStyles
|
24
|
+
}, [style])
|
25
|
+
const getCurrentColor = useCallback(
|
26
|
+
() => {
|
27
|
+
if (!parsedStyles) {
|
28
|
+
return 'rgb(0, 0, 0)'
|
29
|
+
}
|
30
|
+
|
31
|
+
const currentColor = parsedStyles[colorKey as keyof UnistylesValues] as string | Record<string, string>
|
32
|
+
const currentColorVar = typeof currentColor === 'string'
|
33
|
+
? currentColor
|
34
|
+
: getClosestBreakpointValue<string>(services.runtime, currentColor) ?? 'rgb(0, 0, 0)'
|
35
|
+
|
36
|
+
if (currentColorVar.startsWith('var(--')) {
|
37
|
+
dummyDiv.style.color = currentColorVar
|
38
|
+
|
39
|
+
return getComputedStyle(dummyDiv).color
|
40
|
+
}
|
41
|
+
|
42
|
+
return currentColorVar
|
43
|
+
},
|
44
|
+
[style, colorKey]
|
45
|
+
)
|
46
|
+
const fromValue = useSharedValue<string>(getCurrentColor())
|
47
|
+
const toValue = useSharedValue<string>(getCurrentColor())
|
48
|
+
|
49
|
+
useEffect(() => {
|
50
|
+
const dispose = services.listener.addListeners([UnistyleDependency.Theme], () => {
|
51
|
+
runOnUI(() => {
|
52
|
+
animateCallback?.(toValue.get(), getCurrentColor())
|
53
|
+
})()
|
54
|
+
})
|
55
|
+
|
56
|
+
return () => dispose()
|
57
|
+
}, [style, colorKey])
|
58
|
+
|
59
|
+
useLayoutEffect(() => {
|
60
|
+
animateCallback?.(toValue.get(), getCurrentColor())
|
61
|
+
|
62
|
+
const colorStyle = parsedStyles?.[colorKey as keyof UnistylesValues]
|
63
|
+
|
64
|
+
if (typeof colorStyle !== 'object' || colorStyle === null) {
|
65
|
+
return
|
66
|
+
}
|
67
|
+
|
68
|
+
const dispose = services.listener.addListeners([UnistyleDependency.Breakpoints], () => {
|
69
|
+
animateCallback?.(toValue.get(), getCurrentColor())
|
70
|
+
})
|
71
|
+
|
72
|
+
return () => dispose()
|
73
|
+
}, [style, colorKey])
|
74
|
+
|
75
|
+
useEffect(() => () => dummyDiv.remove(), [])
|
76
|
+
|
77
|
+
return {
|
78
|
+
fromValue,
|
79
|
+
toValue
|
80
|
+
}
|
81
|
+
}
|
@@ -9,6 +9,7 @@ interface ShadowRegistry extends UnistylesShadowRegistrySpec {
|
|
9
9
|
// JSI
|
10
10
|
link(node: ShadowNode, styles?: Array<Unistyle>): void,
|
11
11
|
unlink(node: ShadowNode): void,
|
12
|
+
flush(): void,
|
12
13
|
setScopedTheme(themeName?: string): void,
|
13
14
|
getScopedTheme(): string | undefined
|
14
15
|
}
|
@@ -23,8 +24,13 @@ const findShadowNodeForHandle = (handle: ViewHandle) => {
|
|
23
24
|
?? handle?.viewRef?.current?.__internalInstanceHandle?.stateNode?.node
|
24
25
|
?? handle?._nativeRef?.__internalInstanceHandle?.stateNode?.node
|
25
26
|
|
27
|
+
// @ts-ignore we don't know the type of handle
|
28
|
+
if (!node && handle?.props?.horizontal && handle?.constructor?.name === 'FlatList') {
|
29
|
+
throw new Error('Unistyles: detected an unsupported FlatList with the horizontal prop. This will cause crashes on Android due to a bug in React Native core. Read more: https://github.com/facebook/react-native/issues/51601')
|
30
|
+
}
|
31
|
+
|
26
32
|
if (!node) {
|
27
|
-
throw new Error(`Unistyles: Could not find shadow node for one of your components of type ${handle?.
|
33
|
+
throw new Error(`Unistyles: Could not find shadow node for one of your components of type ${handle?.constructor?.name ?? 'unknown'}`)
|
28
34
|
}
|
29
35
|
|
30
36
|
return node
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { StyleSheet as NativeStyleSheet, processColor } from 'react-native'
|
2
2
|
import type { StyleSheet as NativeStyleSheetType } from 'react-native'
|
3
3
|
import { NitroModules } from 'react-native-nitro-modules'
|
4
|
+
import { parseBoxShadowString } from '../../core/parseBoxShadow'
|
4
5
|
import type { UnistylesBreakpoints, UnistylesThemes } from '../../global'
|
5
6
|
import type { CreateUnistylesStyleSheet } from '../../types'
|
6
7
|
import type { UnistylesStyleSheet as UnistylesStyleSheetSpec } from './UnistylesStyleSheet.nitro'
|
@@ -33,7 +34,8 @@ export interface UnistylesStyleSheet extends UnistylesStyleSheetSpec {
|
|
33
34
|
create: CreateUnistylesStyleSheet,
|
34
35
|
configure(config: UnistylesConfig): void,
|
35
36
|
jsMethods: {
|
36
|
-
processColor: typeof processColor
|
37
|
+
processColor: typeof processColor,
|
38
|
+
parseBoxShadowString: typeof parseBoxShadowString
|
37
39
|
}
|
38
40
|
}
|
39
41
|
|
@@ -45,7 +47,8 @@ HybridUnistylesStyleSheet.absoluteFill = NativeStyleSheet.absoluteFill
|
|
45
47
|
HybridUnistylesStyleSheet.flatten = NativeStyleSheet.flatten
|
46
48
|
HybridUnistylesStyleSheet.compose = NativeStyleSheet.compose
|
47
49
|
HybridUnistylesStyleSheet.jsMethods = {
|
48
|
-
processColor
|
50
|
+
processColor,
|
51
|
+
parseBoxShadowString
|
49
52
|
}
|
50
53
|
|
51
54
|
HybridUnistylesStyleSheet.init()
|
@@ -36,6 +36,8 @@ export interface UnistylesRuntime extends HybridObject<{ ios: 'c++', android: 'c
|
|
36
36
|
readonly pixelRatio: number,
|
37
37
|
readonly fontScale: number,
|
38
38
|
readonly rtl: boolean,
|
39
|
+
readonly isLandscape: boolean,
|
40
|
+
readonly isPortrait: boolean,
|
39
41
|
|
40
42
|
setTheme(themeName: string): void,
|
41
43
|
setAdaptiveThemes(isEnabled: boolean): void,
|
package/src/utils.ts
CHANGED
@@ -35,10 +35,8 @@ export const copyComponentProperties = (Component: any, UnistylesComponent: any)
|
|
35
35
|
UnistylesComponent[key] = value
|
36
36
|
})
|
37
37
|
|
38
|
-
// Those are not enumerable, so we need to copy them manually
|
39
38
|
UnistylesComponent.displayName = Component.displayName
|
40
|
-
|
41
|
-
|
39
|
+
|
42
40
|
return UnistylesComponent
|
43
41
|
}
|
44
42
|
|
package/src/web/create.ts
CHANGED
@@ -21,7 +21,7 @@ export const create = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>, id?: s
|
|
21
21
|
__uni__key: key,
|
22
22
|
__uni__stylesheet: stylesheet,
|
23
23
|
__uni__args: args,
|
24
|
-
|
24
|
+
__stylesheetVariants: variants
|
25
25
|
})
|
26
26
|
|
27
27
|
const createStyleSheetStyles = (variants?: Variants) => {
|
package/src/web/css/core.ts
CHANGED
@@ -34,8 +34,16 @@ export const convertToCSS = (hash: string, value: Record<string, any>, state: CS
|
|
34
34
|
}
|
35
35
|
|
36
36
|
if (typeof styleValue === 'object') {
|
37
|
-
const allBreakpoints = Object.keys(value)
|
38
37
|
Object.entries(styleValue).forEach(([breakpointStyleKey, breakpointStyleValue]) => {
|
38
|
+
const allBreakpoints = Object.entries(value)
|
39
|
+
.filter(([_, value]) => {
|
40
|
+
if (typeof value !== 'object' || value === null) {
|
41
|
+
return false
|
42
|
+
}
|
43
|
+
|
44
|
+
return breakpointStyleKey in value
|
45
|
+
})
|
46
|
+
.map(([key]) => key)
|
39
47
|
const mediaQuery = getMediaQuery(styleKey, allBreakpoints)
|
40
48
|
|
41
49
|
state.set({
|
package/src/web/css/state.ts
CHANGED
@@ -32,7 +32,7 @@ export class CSSState {
|
|
32
32
|
mainMap: MapType = new Map()
|
33
33
|
mqMap: MapType = new Map()
|
34
34
|
private styleTag: HTMLStyleElement | null = null
|
35
|
-
private
|
35
|
+
private themesCSS = new Map<string, string>()
|
36
36
|
|
37
37
|
constructor(private services: UnistylesServices) {
|
38
38
|
if (isServer()) {
|
@@ -62,7 +62,10 @@ export class CSSState {
|
|
62
62
|
|
63
63
|
add = (hash: string, values: UnistylesValues) => {
|
64
64
|
convertToCSS(hash, convertUnistyles(values, this.services.runtime), this)
|
65
|
+
this.recreate()
|
66
|
+
}
|
65
67
|
|
68
|
+
recreate = () => {
|
66
69
|
if (this.styleTag) {
|
67
70
|
this.styleTag.innerText = this.getStyles()
|
68
71
|
}
|
@@ -84,10 +87,10 @@ export class CSSState {
|
|
84
87
|
Object.entries(values).forEach(([key, value]) => convertToCSS(key, value))
|
85
88
|
|
86
89
|
if (theme === 'light' || theme === 'dark') {
|
87
|
-
this.
|
90
|
+
this.themesCSS.set(`media ${theme}`, `@media (prefers-color-scheme: ${theme}){:root{${themeVars}}}`)
|
88
91
|
}
|
89
92
|
|
90
|
-
this.
|
93
|
+
this.themesCSS.set(theme, `:root.${theme}{${themeVars}}`)
|
91
94
|
}
|
92
95
|
|
93
96
|
remove = (hash: string) => {
|
@@ -97,14 +100,13 @@ export class CSSState {
|
|
97
100
|
this.mqMap.forEach(styles => {
|
98
101
|
styles.delete(hash)
|
99
102
|
})
|
100
|
-
|
101
|
-
if (this.styleTag) {
|
102
|
-
this.styleTag.innerText = this.getStyles()
|
103
|
-
}
|
103
|
+
this.recreate()
|
104
104
|
}
|
105
105
|
|
106
106
|
getStyles = () => {
|
107
|
-
let styles = this.
|
107
|
+
let styles = Array.from(this.themesCSS.entries()).reduce((acc, [, themeCss]) => {
|
108
|
+
return acc + themeCss
|
109
|
+
}, '')
|
108
110
|
|
109
111
|
const generate = (mediaQuery: string, secondLevelMap: Map<string, Map<string, string>>) => {
|
110
112
|
if (mediaQuery) {
|
package/src/web/listener.ts
CHANGED
@@ -29,6 +29,7 @@ export class UnistylesListener {
|
|
29
29
|
|
30
30
|
if (this.services.runtime.hasAdaptiveThemes) {
|
31
31
|
this.emitChange(UnistyleDependency.Theme)
|
32
|
+
this.emitChange(UnistyleDependency.ThemeName)
|
32
33
|
}
|
33
34
|
})
|
34
35
|
this.services.runtime.lightMedia?.addEventListener('change', event => {
|
@@ -40,6 +41,7 @@ export class UnistylesListener {
|
|
40
41
|
|
41
42
|
if (this.services.runtime.hasAdaptiveThemes) {
|
42
43
|
this.emitChange(UnistyleDependency.Theme)
|
44
|
+
this.emitChange(UnistyleDependency.ThemeName)
|
43
45
|
}
|
44
46
|
})
|
45
47
|
|
package/src/web/runtime.ts
CHANGED
@@ -4,7 +4,7 @@ import { type AppTheme, type AppThemeName, ColorScheme, Orientation } from '../s
|
|
4
4
|
import { type UnistylesTheme, WebContentSizeCategory } from '../types'
|
5
5
|
import { NavigationBar, StatusBar } from './mock'
|
6
6
|
import type { UnistylesServices } from './types'
|
7
|
-
import { error, isServer, schemeToTheme } from './utils'
|
7
|
+
import { convertTheme, error, isServer, schemeToTheme } from './utils'
|
8
8
|
|
9
9
|
export class UnistylesRuntime {
|
10
10
|
lightMedia = this.getLightMedia()
|
@@ -76,6 +76,14 @@ export class UnistylesRuntime {
|
|
76
76
|
return screen.orientation.type.includes('portrait') ? Orientation.Portrait : Orientation.Landscape
|
77
77
|
}
|
78
78
|
|
79
|
+
get isLandscape() {
|
80
|
+
return this.orientation === Orientation.Landscape
|
81
|
+
}
|
82
|
+
|
83
|
+
get isPortrait() {
|
84
|
+
return this.orientation === Orientation.Portrait
|
85
|
+
}
|
86
|
+
|
79
87
|
get theme() {
|
80
88
|
return this.getTheme(this.themeName)
|
81
89
|
}
|
@@ -204,7 +212,20 @@ export class UnistylesRuntime {
|
|
204
212
|
throw error(`Unistyles: You're trying to update theme "${themeName}" but it wasn't registered.`)
|
205
213
|
}
|
206
214
|
|
207
|
-
|
215
|
+
const newTheme = updater(oldTheme)
|
216
|
+
|
217
|
+
this.services.state.themes.set(themeName, newTheme)
|
218
|
+
|
219
|
+
if (this.services.state.CSSVars) {
|
220
|
+
this.services.state.cssThemes.set(
|
221
|
+
themeName,
|
222
|
+
Object.fromEntries(Object.entries(newTheme).map(([key, value]) => {
|
223
|
+
return convertTheme(key, value)
|
224
|
+
})) as UnistylesTheme
|
225
|
+
)
|
226
|
+
this.services.registry.css.addTheme(themeName, newTheme)
|
227
|
+
this.services.registry.css.recreate()
|
228
|
+
}
|
208
229
|
}
|
209
230
|
|
210
231
|
getTheme = (themeName = this.themeName, CSSVars = false) => {
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { UnistyleDependency } from '../specs/NativePlatform/NativePlatform.nitro'
|
2
2
|
import type { UnistylesTheme, UnistylesValues } from '../types'
|
3
3
|
import { deepMergeObjects } from '../utils'
|
4
|
-
import type { UnistylesServices } from './types'
|
5
|
-
import { extractSecrets, extractUnistyleDependencies } from './utils'
|
4
|
+
import type { UniGeneratedStyle, UnistylesServices } from './types'
|
5
|
+
import { extractSecrets, extractUnistyleDependencies, isGeneratedUnistyle, isServer } from './utils'
|
6
6
|
import { getVariants } from './variants'
|
7
7
|
|
8
8
|
export class UnistylesShadowRegistry {
|
@@ -20,7 +20,7 @@ export class UnistylesShadowRegistry {
|
|
20
20
|
constructor(private services: UnistylesServices) {}
|
21
21
|
|
22
22
|
add = (ref: any, hash?: string) => {
|
23
|
-
if (!(ref instanceof HTMLElement) || !hash) {
|
23
|
+
if (isServer() || !(ref instanceof HTMLElement) || !hash) {
|
24
24
|
return
|
25
25
|
}
|
26
26
|
|
@@ -28,6 +28,13 @@ export class UnistylesShadowRegistry {
|
|
28
28
|
}
|
29
29
|
|
30
30
|
addStyles = (unistyles: Array<UnistylesValues>, forChild?: boolean) => {
|
31
|
+
const [firstStyle] = unistyles
|
32
|
+
|
33
|
+
// If it is already a generated style, return it
|
34
|
+
if (firstStyle && isGeneratedUnistyle(firstStyle)) {
|
35
|
+
return firstStyle as UniGeneratedStyle
|
36
|
+
}
|
37
|
+
|
31
38
|
const getParsedStyles = () => {
|
32
39
|
const allStyles = unistyles.map(unistyle => {
|
33
40
|
const secrets = extractSecrets(unistyle)
|
@@ -42,7 +49,7 @@ export class UnistylesShadowRegistry {
|
|
42
49
|
return {}
|
43
50
|
}
|
44
51
|
|
45
|
-
const { __uni__key, __uni__stylesheet, __uni__args = [],
|
52
|
+
const { __uni__key, __uni__stylesheet, __uni__args = [], __stylesheetVariants: variants } = secrets
|
46
53
|
const newComputedStylesheet = this.services.registry.getComputedStylesheet(__uni__stylesheet, scopedTheme)
|
47
54
|
const style = newComputedStylesheet[__uni__key] as (UnistylesValues | ((...args: any) => UnistylesValues))
|
48
55
|
const result = typeof style === 'function'
|
@@ -87,7 +94,7 @@ export class UnistylesShadowRegistry {
|
|
87
94
|
? hash.replace(' > *', '')
|
88
95
|
: hash
|
89
96
|
|
90
|
-
return { injectedClassName, hash: hashClassname }
|
97
|
+
return { injectedClassName, hash: hashClassname, parsedStyles }
|
91
98
|
}
|
92
99
|
|
93
100
|
setScopedTheme = (theme?: UnistylesTheme) => {
|
@@ -97,7 +104,7 @@ export class UnistylesShadowRegistry {
|
|
97
104
|
getScopedTheme = () => this.scopedTheme
|
98
105
|
|
99
106
|
remove = (ref: any, hash?: string) => {
|
100
|
-
if (!(ref instanceof HTMLElement) || !hash) {
|
107
|
+
if (isServer() || !(ref instanceof HTMLElement) || !hash) {
|
101
108
|
return
|
102
109
|
}
|
103
110
|
|
@@ -111,4 +118,6 @@ export class UnistylesShadowRegistry {
|
|
111
118
|
this.disposeMap.delete(hash)
|
112
119
|
})
|
113
120
|
}
|
121
|
+
|
122
|
+
flush = () => {}
|
114
123
|
}
|
package/src/web/state.ts
CHANGED
@@ -5,7 +5,7 @@ import type { AppBreakpoint, AppTheme, AppThemeName } from '../specs/types'
|
|
5
5
|
import type { UnistylesTheme } from '../types'
|
6
6
|
import type { UnionToIntersection } from '../types'
|
7
7
|
import type { UnistylesServices } from './types'
|
8
|
-
import {
|
8
|
+
import { convertTheme, error, isServer, schemeToTheme } from './utils'
|
9
9
|
|
10
10
|
type UnistylesSettings = Partial<UnionToIntersection<Required<UnistylesConfig>['settings']>>
|
11
11
|
|
@@ -72,22 +72,12 @@ export class UnistylesState {
|
|
72
72
|
|
73
73
|
if (CSSVars) {
|
74
74
|
this.services.registry.css.addTheme(themeName, theme)
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
return
|
79
|
-
}
|
80
|
-
|
81
|
-
if (typeof value === 'string') {
|
82
|
-
return [key, `var(${prev}-${hyphenate(key)})`]
|
83
|
-
}
|
84
|
-
|
85
|
-
return [key, value]
|
86
|
-
}
|
87
|
-
|
88
|
-
this.cssThemes.set(themeName, Object.fromEntries(Object.entries(theme).map(([key, value]) => {
|
89
|
-
return convertTheme(key, value)
|
90
|
-
})) as UnistylesTheme)
|
75
|
+
this.cssThemes.set(
|
76
|
+
themeName,
|
77
|
+
Object.fromEntries(Object.entries(theme).map(([key, value]) => {
|
78
|
+
return convertTheme(key, value)
|
79
|
+
})) as UnistylesTheme
|
80
|
+
)
|
91
81
|
}
|
92
82
|
})
|
93
83
|
}
|
package/src/web/types.ts
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
import type { UnistylesValues } from '../types'
|
1
2
|
import type { UnistylesListener } from './listener'
|
2
|
-
import type{ UnistylesRegistry } from './registry'
|
3
|
-
import type{ UnistylesRuntime } from './runtime'
|
4
|
-
import type{ UnistylesShadowRegistry } from './shadowRegistry'
|
5
|
-
import type{ UnistylesState } from './state'
|
3
|
+
import type { UnistylesRegistry } from './registry'
|
4
|
+
import type { UnistylesRuntime } from './runtime'
|
5
|
+
import type { UnistylesShadowRegistry } from './shadowRegistry'
|
6
|
+
import type { UnistylesState } from './state'
|
6
7
|
|
7
8
|
export type UnistylesServices = {
|
8
9
|
runtime: UnistylesRuntime,
|
@@ -11,3 +12,10 @@ export type UnistylesServices = {
|
|
11
12
|
state: UnistylesState,
|
12
13
|
listener: UnistylesListener
|
13
14
|
}
|
15
|
+
|
16
|
+
export const UNI_GENERATED_KEYS = ['$$css', 'hash', 'injectedClassName'] as const
|
17
|
+
export type UniGeneratedKey = typeof UNI_GENERATED_KEYS[number]
|
18
|
+
|
19
|
+
export type UniGeneratedStyle = Record<UniGeneratedKey, string> & {
|
20
|
+
parsedStyles?: UnistylesValues
|
21
|
+
}
|
@@ -3,9 +3,11 @@ import type { UnistylesBreakpoints } from '../../global'
|
|
3
3
|
import type { UnistyleDependency } from '../../specs/NativePlatform/NativePlatform.nitro'
|
4
4
|
import { ColorScheme, Orientation } from '../../specs/types'
|
5
5
|
import type { StyleSheet, StyleSheetWithSuperPowers, UnistylesValues } from '../../types/stylesheet'
|
6
|
-
import { isUnistylesMq, parseMq } from '../../utils'
|
6
|
+
import { isDefined, isUnistylesMq, parseMq } from '../../utils'
|
7
|
+
import type { UnistylesRuntime } from '../runtime'
|
7
8
|
import * as unistyles from '../services'
|
8
|
-
import {
|
9
|
+
import { UNI_GENERATED_KEYS, type UniGeneratedKey, type UniGeneratedStyle } from '../types'
|
10
|
+
import { hyphenate, keyInObject, reduceObject } from './common'
|
9
11
|
|
10
12
|
export const schemeToTheme = (scheme: ColorScheme) => {
|
11
13
|
switch (scheme) {
|
@@ -21,7 +23,7 @@ export type UnistyleSecrets = {
|
|
21
23
|
__uni__stylesheet: StyleSheetWithSuperPowers<StyleSheet>,
|
22
24
|
__uni__key: string,
|
23
25
|
__uni__args?: Array<any>,
|
24
|
-
|
26
|
+
__stylesheetVariants: Record<string, string | boolean | undefined>
|
25
27
|
}
|
26
28
|
|
27
29
|
export const assignSecrets = <T>(object: T, secrets: UnistyleSecrets) => {
|
@@ -139,3 +141,36 @@ export const checkForAnimated = (value: any): boolean => {
|
|
139
141
|
|
140
142
|
return false
|
141
143
|
}
|
144
|
+
|
145
|
+
export const isGeneratedUnistyle = (value: Record<string, any>): value is UniGeneratedStyle => {
|
146
|
+
const keys = Object.keys(value)
|
147
|
+
|
148
|
+
return keys.length > 0 && keys.every(key => UNI_GENERATED_KEYS.includes(key as UniGeneratedKey))
|
149
|
+
}
|
150
|
+
|
151
|
+
export const convertTheme = (key: string, value: any, prev = '-'): [string, any] => {
|
152
|
+
if (typeof value === 'object' && value !== null) {
|
153
|
+
return [key, Object.fromEntries(Object.entries(value).map(([nestedKey, nestedValue]) => convertTheme(nestedKey, nestedValue, `${prev}-${key}`)))]
|
154
|
+
}
|
155
|
+
|
156
|
+
if (typeof value === 'string') {
|
157
|
+
return [key, `var(${prev}-${hyphenate(key)})`]
|
158
|
+
}
|
159
|
+
|
160
|
+
return [key, value]
|
161
|
+
}
|
162
|
+
|
163
|
+
export const getClosestBreakpointValue = <T>(runtime: UnistylesRuntime, values: Partial<Record<keyof UnistylesBreakpoints, T>>) => {
|
164
|
+
const breakpoints = runtime.breakpoints
|
165
|
+
const breakpointValues = Object.entries(values)
|
166
|
+
// Filter out non-breakpoint values
|
167
|
+
.filter((pair): pair is [keyof UnistylesBreakpoints, T] => pair[0] in breakpoints)
|
168
|
+
// Sort in descending order
|
169
|
+
.sort(([a], [b]) => (breakpoints[b] ?? 0) - (breakpoints[a] ?? 0))
|
170
|
+
// Get breakpoint value with highest priority
|
171
|
+
const [_, currentBreakpointValue] = breakpointValues.find(
|
172
|
+
([key]) => isDefined(runtime.breakpoint) && (breakpoints[key] ?? 0) <= (breakpoints[runtime.breakpoint] ?? 0)
|
173
|
+
) ?? []
|
174
|
+
|
175
|
+
return currentBreakpointValue
|
176
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import type { StyleProp } from 'react-native'
|
2
|
+
import { getClassName } from '../core'
|
3
|
+
import type { RNStyle, UnistylesValues } from '../types'
|
4
|
+
import { createUnistylesRef } from '../web/utils/createUnistylesRef'
|
5
|
+
|
6
|
+
export const getWebProps = <T>(style: StyleProp<RNStyle>) => {
|
7
|
+
const styles = getClassName(style as UnistylesValues)
|
8
|
+
const ref = createUnistylesRef<T>(styles)
|
9
|
+
const [generatedStyles] = styles ?? []
|
10
|
+
|
11
|
+
return {
|
12
|
+
className: [
|
13
|
+
generatedStyles?.hash,
|
14
|
+
generatedStyles?.injectedClassName,
|
15
|
+
].filter(Boolean).join(' '),
|
16
|
+
ref
|
17
|
+
}
|
18
|
+
}
|
package/web/package.json
ADDED
@@ -1,52 +0,0 @@
|
|
1
|
-
#include "UnistylesCommitHook.h"
|
2
|
-
|
3
|
-
using namespace margelo::nitro::unistyles;
|
4
|
-
using namespace facebook::react;
|
5
|
-
|
6
|
-
core::UnistylesCommitHook::~UnistylesCommitHook() noexcept {
|
7
|
-
_uiManager->unregisterCommitHook(*this);
|
8
|
-
}
|
9
|
-
|
10
|
-
void core::UnistylesCommitHook::commitHookWasRegistered(const UIManager &uiManager) noexcept {}
|
11
|
-
void core::UnistylesCommitHook::commitHookWasUnregistered(const UIManager &uiManager) noexcept {}
|
12
|
-
|
13
|
-
RootShadowNode::Unshared core::UnistylesCommitHook::shadowTreeWillCommit(
|
14
|
-
const ShadowTree &shadowTree,
|
15
|
-
const RootShadowNode::Shared &oldRootShadowNode,
|
16
|
-
const RootShadowNode::Unshared &newRootShadowNode
|
17
|
-
) noexcept {
|
18
|
-
RootShadowNode::Unshared rootNode = newRootShadowNode;
|
19
|
-
auto unistylesRootNode = std::reinterpret_pointer_cast<core::UnistylesCommitShadowNode>(newRootShadowNode);
|
20
|
-
|
21
|
-
// this is Unistyles commit, we don't need to override it
|
22
|
-
if (unistylesRootNode->hasUnistylesCommitTrait()) {
|
23
|
-
unistylesRootNode->removeUnistylesCommitTrait();
|
24
|
-
unistylesRootNode->addUnistylesMountTrait();
|
25
|
-
|
26
|
-
return newRootShadowNode;
|
27
|
-
}
|
28
|
-
|
29
|
-
// this is React Native / Reanimated commit
|
30
|
-
// merge Unistyles updates before it completes
|
31
|
-
auto& registry = core::UnistylesRegistry::get();
|
32
|
-
|
33
|
-
return registry.trafficController.withLock([&](){
|
34
|
-
auto& shadowLeafUpdates = registry.trafficController.getUpdates();
|
35
|
-
|
36
|
-
// oops, no updates from Unistyles yet, skip it!
|
37
|
-
if (shadowLeafUpdates.size() == 0) {
|
38
|
-
return newRootShadowNode;
|
39
|
-
}
|
40
|
-
|
41
|
-
auto affectedNodes = shadow::ShadowTreeManager::findAffectedNodes(*rootNode, shadowLeafUpdates);
|
42
|
-
|
43
|
-
registry.trafficController.stopUnistylesTraffic();
|
44
|
-
|
45
|
-
// we have few updates, so merge it
|
46
|
-
return std::static_pointer_cast<RootShadowNode>(shadow::ShadowTreeManager::cloneShadowTree(
|
47
|
-
*rootNode,
|
48
|
-
shadowLeafUpdates,
|
49
|
-
affectedNodes
|
50
|
-
));
|
51
|
-
});
|
52
|
-
}
|