react-native-unistyles 3.0.0-alpha.42 → 3.0.0-alpha.44

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.
Files changed (55) hide show
  1. package/lib/commonjs/components/Pressable.js +15 -5
  2. package/lib/commonjs/components/Pressable.js.map +1 -1
  3. package/lib/commonjs/components/Pressable.web.js +35 -17
  4. package/lib/commonjs/components/Pressable.web.js.map +1 -1
  5. package/lib/commonjs/web/create.js +6 -1
  6. package/lib/commonjs/web/create.js.map +1 -1
  7. package/lib/commonjs/web/shadowRegistry.js +49 -48
  8. package/lib/commonjs/web/shadowRegistry.js.map +1 -1
  9. package/lib/commonjs/web/{variants/getVariants.js → variants.js} +2 -2
  10. package/lib/commonjs/web/variants.js.map +1 -0
  11. package/lib/module/components/Pressable.js +15 -5
  12. package/lib/module/components/Pressable.js.map +1 -1
  13. package/lib/module/components/Pressable.web.js +35 -17
  14. package/lib/module/components/Pressable.web.js.map +1 -1
  15. package/lib/module/web/create.js +7 -2
  16. package/lib/module/web/create.js.map +1 -1
  17. package/lib/module/web/shadowRegistry.js +49 -48
  18. package/lib/module/web/shadowRegistry.js.map +1 -1
  19. package/lib/module/web/{variants/getVariants.js → variants.js} +2 -2
  20. package/lib/module/web/variants.js.map +1 -0
  21. package/lib/typescript/src/components/Pressable.d.ts +2 -1
  22. package/lib/typescript/src/components/Pressable.d.ts.map +1 -1
  23. package/lib/typescript/src/components/Pressable.web.d.ts +2 -1
  24. package/lib/typescript/src/components/Pressable.web.d.ts.map +1 -1
  25. package/lib/typescript/src/web/create.d.ts.map +1 -1
  26. package/lib/typescript/src/web/shadowRegistry.d.ts +1 -1
  27. package/lib/typescript/src/web/{variants/getVariants.d.ts → variants.d.ts} +2 -2
  28. package/lib/typescript/src/web/variants.d.ts.map +1 -0
  29. package/package.json +1 -1
  30. package/plugin/index.js +1 -1
  31. package/plugin/ref.js +1 -1
  32. package/plugin/style.js +282 -99
  33. package/plugin/stylesheet.js +1 -1
  34. package/src/components/Pressable.tsx +24 -7
  35. package/src/components/Pressable.web.tsx +54 -28
  36. package/src/web/create.ts +7 -2
  37. package/src/web/shadowRegistry.ts +47 -47
  38. package/src/web/{variants/getVariants.ts → variants.ts} +2 -2
  39. package/lib/commonjs/web/variants/getVariants.js.map +0 -1
  40. package/lib/commonjs/web/variants/index.js +0 -28
  41. package/lib/commonjs/web/variants/index.js.map +0 -1
  42. package/lib/commonjs/web/variants/useVariants.js +0 -59
  43. package/lib/commonjs/web/variants/useVariants.js.map +0 -1
  44. package/lib/module/web/variants/getVariants.js.map +0 -1
  45. package/lib/module/web/variants/index.js +0 -5
  46. package/lib/module/web/variants/index.js.map +0 -1
  47. package/lib/module/web/variants/useVariants.js +0 -54
  48. package/lib/module/web/variants/useVariants.js.map +0 -1
  49. package/lib/typescript/src/web/variants/getVariants.d.ts.map +0 -1
  50. package/lib/typescript/src/web/variants/index.d.ts +0 -3
  51. package/lib/typescript/src/web/variants/index.d.ts.map +0 -1
  52. package/lib/typescript/src/web/variants/useVariants.d.ts +0 -3
  53. package/lib/typescript/src/web/variants/useVariants.d.ts.map +0 -1
  54. package/src/web/variants/index.ts +0 -2
  55. package/src/web/variants/useVariants.ts +0 -65
@@ -9,9 +9,11 @@ type WebPressableState = {
9
9
  focused: boolean
10
10
  }
11
11
 
12
+ type WebPressableStyle = ((state: WebPressableState) => ViewStyle) | ViewStyle
13
+
12
14
  type PressableProps = Props & {
13
15
  variants?: Record<string, string | boolean>
14
- style?: ((state: WebPressableState) => ViewStyle) | ViewStyle,
16
+ style?: WebPressableStyle,
15
17
  }
16
18
 
17
19
  const initialState: WebPressableState = {
@@ -29,8 +31,46 @@ const events = {
29
31
  'blur': { focused: false }
30
32
  } satisfies Partial<Record<keyof HTMLElementEventMap, Partial<WebPressableState>>>
31
33
 
34
+ type UpdateStylesProps = {
35
+ ref: View | null,
36
+ style: WebPressableStyle,
37
+ variants?: Record<string, string | boolean>,
38
+ state: WebPressableState
39
+ }
40
+
41
+ const isFunctionWithBoundArgs = (fn: any): fn is Function & { getBoundArgs: Function } => {
42
+ return typeof fn === 'function' && 'getBoundArgs' in fn
43
+ }
44
+
45
+ const extractFunctionArgs = (styleResult: any) => {
46
+ return isFunctionWithBoundArgs(styleResult)
47
+ ? [styleResult.getBoundArgs()]
48
+ : Array.isArray(styleResult)
49
+ ? styleResult.map(style => isFunctionWithBoundArgs(style) ? style.getBoundArgs() : [])
50
+ : []
51
+ }
52
+
53
+ const extractStyleResult = (style: any) => {
54
+ return typeof style === 'function'
55
+ ? [style()]
56
+ : Array.isArray(style)
57
+ ? style.map(style => typeof style === 'function' ? style() : style)
58
+ : [style]
59
+ }
60
+
61
+ const updateStyles = ({ ref, style, variants, state }: UpdateStylesProps) => {
62
+ const styleResult = typeof style === 'function'
63
+ ? style(state)
64
+ : style
65
+ const fnArgs = extractFunctionArgs(styleResult)
66
+ const extractedResult = extractStyleResult(styleResult)
67
+
68
+ // @ts-expect-error - this is hidden from TS
69
+ UnistylesShadowRegistry.add(ref, extractedResult, variants, fnArgs)
70
+ }
71
+
32
72
  export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ...props }, passedRef) => {
33
- const storedRef = useRef<View | null>()
73
+ const storedRef = useRef<View | null>(null)
34
74
  const state = useRef<WebPressableState>(initialState)
35
75
  const styleRef = useRef(style)
36
76
 
@@ -42,19 +82,12 @@ export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ..
42
82
  const handler = (newState: Partial<WebPressableState>) => () => {
43
83
  state.current = { ...state.current, ...newState }
44
84
 
45
- const styleResult = typeof styleRef.current === 'function'
46
- ? styleRef.current(state.current)
47
- : styleRef.current
48
- const fnArgs = typeof styleResult === 'function'
49
- // @ts-expect-error - this is hidden from TS
50
- ? styleResult.getBoundArgs()
51
- : []
52
- const extractedResult = typeof styleResult === 'function'
53
- ? (styleResult as Function)()
54
- : styleResult
55
-
56
- // @ts-expect-error - this is hidden from TS
57
- UnistylesShadowRegistry.add(storedRef.current, [extractedResult], variants, [fnArgs])
85
+ updateStyles({
86
+ ref: storedRef.current,
87
+ style: styleRef.current as WebPressableStyle,
88
+ variants,
89
+ state: state.current
90
+ })
58
91
  }
59
92
 
60
93
  if (!storedRef.current) {
@@ -80,23 +113,16 @@ export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ..
80
113
  {...props}
81
114
  ref={ref => {
82
115
  storedRef.current = ref
83
- const styleResult = typeof style === 'function'
84
- ? style(initialState)
85
- : style
86
- const fnArgs = typeof styleResult === 'function'
87
- // @ts-expect-error - this is hidden from TS
88
- ? styleResult.getBoundArgs()
89
- : []
90
- const extractedResult = typeof styleResult === 'function'
91
- ? (styleResult as Function)()
92
- : styleResult
116
+ updateStyles({
117
+ ref,
118
+ style: style as WebPressableStyle,
119
+ variants,
120
+ state: initialState
121
+ })
93
122
 
94
123
  if (typeof passedRef === 'object' && passedRef !== null) {
95
124
  passedRef.current = ref
96
125
  }
97
-
98
- // @ts-expect-error - this is hidden from TS
99
- UnistylesShadowRegistry.add(ref, [extractedResult], variants, [fnArgs])
100
126
  }}
101
127
  />
102
128
  )
package/src/web/create.ts CHANGED
@@ -3,7 +3,7 @@ import type { StyleSheetWithSuperPowers, StyleSheet } from '../types/stylesheet'
3
3
  import { assignSecrets, reduceObject, getStyles, error } from './utils'
4
4
  import { deepMergeObjects } from '../utils'
5
5
  import { UnistylesRuntime } from './runtime'
6
- import { createUseVariants, getVariants } from './variants'
6
+ import { getVariants } from './variants'
7
7
 
8
8
  export const create = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>, id?: string) => {
9
9
  if (!id) {
@@ -46,7 +46,12 @@ export const create = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>, id?: s
46
46
  }) as ReactNativeStyleSheet<StyleSheet>
47
47
 
48
48
  // Inject useVariants hook to styles
49
- createUseVariants(styles, newVariants => Object.entries(newVariants).forEach(([key, value]) => selectedVariants.set(key, value)))
49
+ Object.defineProperty(styles, 'useVariants', {
50
+ value: (variants: Record<string, string | boolean>) => {
51
+ Object.entries(variants).forEach(([key, value]) => selectedVariants.set(key, value))
52
+ },
53
+ configurable: false,
54
+ })
50
55
 
51
56
  return styles
52
57
  }
@@ -21,17 +21,15 @@ class UnistylesShadowRegistryBuilder {
21
21
  private hashMap = new Map<HTMLElement, string>()
22
22
  private classNamesMap = new Map<HTMLElement, Array<string>>()
23
23
 
24
- add = (ref: any, _style: Array<Style>, _variants: Record<string, any> | undefined, _args: Array<Array<any>>) => {
25
- // Style is not provided
26
- if (!_style) {
24
+ add = (ref: any, styles: Array<Style>, _variants: Record<string, any> | undefined, _args: Array<Array<any>>) => {
25
+ // Styles are not provided
26
+ if (!styles) {
27
27
  return
28
28
  }
29
29
 
30
30
  // Ref is unmounted, remove style tags from the document
31
31
  if (ref === null) {
32
- const secrets = extractSecrets(_style)
33
-
34
- secrets.forEach(({ __uni__refs }) => {
32
+ extractSecrets(styles).forEach(({ __uni__refs }) => {
35
33
  __uni__refs.forEach(ref => {
36
34
  if (isInDocument(ref)) {
37
35
  return
@@ -55,56 +53,58 @@ class UnistylesShadowRegistryBuilder {
55
53
  return
56
54
  }
57
55
 
58
- const styles = _style.filter(style => Object.keys(style ?? {}).some(key => key.startsWith('__uni__')))
59
-
60
- // No unistyles
61
- if (styles.length === 0) {
62
- return
63
- }
56
+ const parsedStyles = styles.flatMap((style, styleIndex) => {
57
+ const secrets = extractSecrets(style)
64
58
 
65
- const parsedStyles = styles.flatMap((style, styleIndex) => extractSecrets(style).map(secret => {
66
- const { __uni__key, __uni__stylesheet, __uni__variants, __uni__args = [], __uni__refs } = secret
67
- const newComputedStylesheet = UnistylesRegistry.getComputedStylesheet(__uni__stylesheet)
68
- const style = newComputedStylesheet[__uni__key] as (UnistylesValues | ((...args: any) => UnistylesValues))
69
- const variants = _variants && Object.keys(_variants).length > 0 ? _variants : __uni__variants
70
- const args = _args[styleIndex] ? _args[styleIndex] : __uni__args
71
- const result = typeof style === 'function'
72
- ? style(...args)
73
- : style
74
- const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants))
75
- const resultWithVariants = deepMergeObjects(result, variantsResult ?? {})
76
- const dependencies = extractUnistyleDependencies(resultWithVariants)
77
-
78
- if (typeof __uni__stylesheet === 'function') {
79
- // Add dependencies from dynamic styles to stylesheet
80
- UnistylesRegistry.addDependenciesToStylesheet(__uni__stylesheet, dependencies)
59
+ // Regular style
60
+ if (secrets.length === 0) {
61
+ return style as UnistylesValues
81
62
  }
82
63
 
83
- __uni__refs.add(ref)
64
+ return secrets.map(secret => {
65
+ const { __uni__key, __uni__stylesheet, __uni__variants, __uni__args = [], __uni__refs } = secret
66
+ const newComputedStylesheet = UnistylesRegistry.getComputedStylesheet(__uni__stylesheet)
67
+ const style = newComputedStylesheet[__uni__key] as (UnistylesValues | ((...args: any) => UnistylesValues))
68
+ const variants = _variants && Object.keys(_variants).length > 0 ? _variants : __uni__variants
69
+ const args = _args[styleIndex] && _args[styleIndex].length > 0 ? _args[styleIndex] : __uni__args
70
+ const result = typeof style === 'function'
71
+ ? style(...args)
72
+ : style
73
+ const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants))
74
+ const resultWithVariants = deepMergeObjects(result, variantsResult ?? {})
75
+ const dependencies = extractUnistyleDependencies(resultWithVariants)
76
+
77
+ if (typeof __uni__stylesheet === 'function') {
78
+ // Add dependencies from dynamic styles to stylesheet
79
+ UnistylesRegistry.addDependenciesToStylesheet(__uni__stylesheet, dependencies)
80
+ }
84
81
 
85
- const dispose = UnistylesListener.addListeners(extractUnistyleDependencies(resultWithVariants), () => {
86
- const hash = this.hashMap.get(ref)
82
+ __uni__refs.add(ref)
87
83
 
88
- // Dispose listener if there is no hash
89
- if (!hash) {
90
- dispose()
84
+ const dispose = UnistylesListener.addListeners(extractUnistyleDependencies(resultWithVariants), () => {
85
+ const hash = this.hashMap.get(ref)
91
86
 
92
- return
93
- }
87
+ // Dispose listener if there is no hash
88
+ if (!hash) {
89
+ dispose()
94
90
 
95
- const newComputedStyleSheet = UnistylesRegistry.getComputedStylesheet(__uni__stylesheet)
96
- const newValue = newComputedStyleSheet[__uni__key] as (UnistylesValues | ((...args: any) => UnistylesValues))
97
- const result = typeof newValue === 'function'
98
- ? newValue(...args)
99
- : newValue
100
- const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants))
101
- const resultWithVariants = deepMergeObjects(result, variantsResult ?? {})
91
+ return
92
+ }
102
93
 
103
- UnistylesRegistry.applyStyles(hash, convertUnistyles(resultWithVariants))
104
- })
94
+ const newComputedStyleSheet = UnistylesRegistry.getComputedStylesheet(__uni__stylesheet)
95
+ const newValue = newComputedStyleSheet[__uni__key] as (UnistylesValues | ((...args: any) => UnistylesValues))
96
+ const result = typeof newValue === 'function'
97
+ ? newValue(...args)
98
+ : newValue
99
+ const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants))
100
+ const resultWithVariants = deepMergeObjects(result, variantsResult ?? {})
101
+
102
+ UnistylesRegistry.applyStyles(hash, convertUnistyles(resultWithVariants))
103
+ })
105
104
 
106
- return resultWithVariants as UnistylesValues
107
- }))
105
+ return resultWithVariants as UnistylesValues
106
+ })
107
+ })
108
108
  const combinedStyles = deepMergeObjects(...parsedStyles)
109
109
  const oldStyles = this.resultsMap.get(ref)
110
110
 
@@ -1,5 +1,5 @@
1
- import type { ReactNativeStyleSheet, StyleSheet } from '../../types'
2
- import { deepMergeObjects } from '../../utils'
1
+ import type { ReactNativeStyleSheet, StyleSheet } from '../types'
2
+ import { deepMergeObjects } from '../utils'
3
3
 
4
4
  type StylesWithVariants = {
5
5
  variants: Record<string, any>,
@@ -1 +0,0 @@
1
- {"version":3,"names":["_utils","require","hasVariants","value","getVariants","styles","selectedVariants","Object","entries","filter","_key","variants","keys","some","variant","map","key","compoundVariants","variantStyles","flatMap","selectedVariant","selectedVariantStyles","default","compoundVariantStyles","compoundVariant","conditions","String","mergedVariantStyles","deepMergeObjects","exports"],"sourceRoot":"../../../../src","sources":["web/variants/getVariants.ts"],"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AC,CAAC,EAAE;QAC5G,OAAO,EAAE;MACb;MAEA,OAAOE,MAAM;IACjB,CAAC,CAAC;IAEF,MAAMsB,mBAAmB,GAAG,IAAAC,uBAAgB,EAAC,GAAGV,aAAa,EAAE,GAAGK,qBAAqB,CAAC;IAExF,OAAO,CAACP,GAAG,EAAEW,mBAAmB,CAAC;EACrC,CAAC,CAAC;AACV,CAAC;AAAAE,OAAA,CAAAzB,WAAA,GAAAA,WAAA","ignoreList":[]}
@@ -1,28 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _useVariants = require("./useVariants");
7
- Object.keys(_useVariants).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _useVariants[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function () {
13
- return _useVariants[key];
14
- }
15
- });
16
- });
17
- var _getVariants = require("./getVariants");
18
- Object.keys(_getVariants).forEach(function (key) {
19
- if (key === "default" || key === "__esModule") return;
20
- if (key in exports && exports[key] === _getVariants[key]) return;
21
- Object.defineProperty(exports, key, {
22
- enumerable: true,
23
- get: function () {
24
- return _getVariants[key];
25
- }
26
- });
27
- });
28
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_useVariants","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_getVariants"],"sourceRoot":"../../../../src","sources":["web/variants/index.ts"],"mappings":";;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,YAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,YAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,YAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,YAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,YAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,YAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,YAAA,CAAAL,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
@@ -1,59 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createUseVariants = void 0;
7
- var _react = require("react");
8
- var _utils = require("../utils");
9
- var _getVariants = require("./getVariants");
10
- const createUseVariants = (styles, setSelectedVariants) => {
11
- const useVariants = selectedVariants => {
12
- const [selectedVariantStylesMap] = (0, _react.useState)(() => new Map());
13
- const lastSelectedVariantsRef = (0, _react.useRef)();
14
- // Variable that determines if variants have changed, and we need to recalculate styles
15
- const variantsChanged = !(0, _utils.equal)(lastSelectedVariantsRef.current, selectedVariants);
16
- if (variantsChanged) {
17
- lastSelectedVariantsRef.current = selectedVariants;
18
- setSelectedVariants(selectedVariants);
19
- }
20
- const combinedVariantStyles = (0, _react.useMemo)(() => {
21
- const result = (0, _getVariants.getVariants)(styles, selectedVariants);
22
- result.forEach(([key, value]) => {
23
- selectedVariantStylesMap.set(key, value);
24
- });
25
- return result;
26
- }, [lastSelectedVariantsRef.current]);
27
- combinedVariantStyles.forEach(([key]) => {
28
- const styleEntry = styles[key];
29
- if (!styleEntry) {
30
- return;
31
- }
32
- const selectedVariantStyles = selectedVariantStylesMap.get(key);
33
- Object.defineProperties(styleEntry, (0, _utils.reduceObject)(selectedVariantStyles ?? {}, value => ({
34
- value,
35
- enumerable: false,
36
- configurable: true
37
- })));
38
-
39
- // Add __uni__variants to static styles
40
- Object.keys(styleEntry).forEach(key => {
41
- if (!key.startsWith('__uni__secrets__')) {
42
- return;
43
- }
44
- const secret = (0, _utils.keyInObject)(styleEntry, key) ? styleEntry[key] : undefined;
45
- if (!secret) {
46
- return;
47
- }
48
- Object.defineProperty(secret, '__uni__variants', {
49
- value: selectedVariants
50
- });
51
- });
52
- });
53
- };
54
- Object.defineProperty(styles, 'useVariants', {
55
- value: useVariants
56
- });
57
- };
58
- exports.createUseVariants = createUseVariants;
59
- //# sourceMappingURL=useVariants.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_react","require","_utils","_getVariants","createUseVariants","styles","setSelectedVariants","useVariants","selectedVariants","selectedVariantStylesMap","useState","Map","lastSelectedVariantsRef","useRef","variantsChanged","equal","current","combinedVariantStyles","useMemo","result","getVariants","forEach","key","value","set","styleEntry","selectedVariantStyles","get","Object","defineProperties","reduceObject","enumerable","configurable","keys","startsWith","secret","keyInObject","undefined","defineProperty","exports"],"sourceRoot":"../../../../src","sources":["web/variants/useVariants.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAEO,MAAMG,iBAAiB,GAAGA,CAACC,MAAyC,EAAEC,mBAA4D,KAAK;EAC1I,MAAMC,WAAW,GAAIC,gBAAqC,IAAK;IAC3D,MAAM,CAACC,wBAAwB,CAAC,GAAG,IAAAC,eAAQ,EAAC,MAAM,IAAIC,GAAG,CAA8B,CAAC,CAAC;IACzF,MAAMC,uBAAuB,GAAG,IAAAC,aAAM,EAAsB,CAAC;IAC7D;IACA,MAAMC,eAAe,GAAG,CAAC,IAAAC,YAAK,EAACH,uBAAuB,CAACI,OAAO,EAAER,gBAAgB,CAAC;IAEjF,IAAIM,eAAe,EAAE;MACjBF,uBAAuB,CAACI,OAAO,GAAGR,gBAAgB;MAClDF,mBAAmB,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ACN,CAAC,CAAC;IACN,CAAC,CAAC;EACN,CAAC;EAEDoB,MAAM,CAACU,cAAc,CAACjC,MAAM,EAAE,aAAa,EAAE;IACzCkB,KAAK,EAAEhB;EACX,CAAC,CAAC;AACN,CAAC;AAAAgC,OAAA,CAAAnC,iBAAA,GAAAA,iBAAA","ignoreList":[]}
@@ -1 +0,0 @@
1
- {"version":3,"names":["deepMergeObjects","hasVariants","value","getVariants","styles","selectedVariants","Object","entries","filter","_key","variants","keys","some","variant","map","key","compoundVariants","variantStyles","flatMap","selectedVariant","selectedVariantStyles","default","compoundVariantStyles","compoundVariant","conditions","String","mergedVariantStyles"],"sourceRoot":"../../../../src","sources":["web/variants/getVariants.ts"],"mappings":";;AACA,SAASA,gBAAgB,QAAQ,aAAa;AAQ9C,MAAMC,WAAW,GAAsBC,KAAkB,IAAgD,UAAU,IAAIA,KAAK,CAAC,CAAC,CAAC;AAE/H,OAAO,MAAMC,WAAW,GAAGA,CAACC,MAAyC,EAAEC,gBAAqC,KAAK;EAC7G,OAAOC,MAAM,CAACC,OAAO,CAACH,MAAM,CAAC,CACxBI,MAAM,CAACP,WAAW,CAAC,CACnBO,MAAM,CAAC,CAAC,CAACC,IAAI,EAAE;IAAEC;EAAS,CAAC,CAAC,KAAKJ,MAAM,CAACK,IAAI,CAACD,QAAQ,CAAC,CAACE,IAAI,CAACC,OAAO,IAAIA,OAAO,IAAIH,QAAQ,CAAC,CAAC,CAC5FI,GAAG,CAAC,CAAC,CAACC,GAAG,EAAE;IAAEL,QAAQ;IAAEM,gBAAgB,GAAG;EAAG,CAAC,CAAC,KAAK;IACjD,MAAMC,aAAa,GAAGX,MAAM,CAACC,OAAO,CAACG,QAAQ,CAAC,CAACQ,OAAO,CAAC,CAAC,CAACL,OAAO,EAAET,MAAM,CAAC,KAAK;MAC1E,MAAMe,eAAe,GAAGd,gBAAgB,CAACQ,OAAO,CAAC;MACjD,MAAMO,qBAAqB,GAAGhB,MAAM,CAACe,eAAe,CAAC,IAAIf,MAAM,CAACiB,OAAO;MAEvE,IAAI,CAACD,qBAAqB,EAAE;QACxB,OAAO,EAAE;MACb;MAEA,OAAOA,qBAAqB;IAChC,CAAC,CAAC;IAEF,MAAME,qBAAqB,GAAGN,gBAAgB,CAACE,OAAO,CAACK,eAAe,IAAI;MACtE,MAAM;QAAEnB,MAAM;QAAE,GAAGoB;MAAW,CAAC,GAAGD,eAAe;MAEjD,IAAIjB,MAAM,CAACC,OAAO,CAACiB,UAAU,CAAC,CAACZ,IAAI,CAAC,CAAC,CAACC,OAAO,EAAEX,KAAK,CAAC,KAAKuB,MAAM,CAACpB,gBAAgB,CAACQ,OAAO,CAAC,CAAC,KAAKY,MAAM,CAACvB,KAAK,CAAC,CAAC,EAAE;QAC5G,OAAO,EAAE;MACb;MAEA,OAAOE,MAAM;IACjB,CAAC,CAAC;IAEF,MAAMsB,mBAAmB,GAAG1B,gBAAgB,CAAC,GAAGiB,aAAa,EAAE,GAAGK,qBAAqB,CAAC;IAExF,OAAO,CAACP,GAAG,EAAEW,mBAAmB,CAAC;EACrC,CAAC,CAAC;AACV,CAAC","ignoreList":[]}
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- export * from './useVariants';
4
- export * from './getVariants';
5
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":[],"sourceRoot":"../../../../src","sources":["web/variants/index.ts"],"mappings":";;AAAA,cAAc,eAAe;AAC7B,cAAc,eAAe","ignoreList":[]}
@@ -1,54 +0,0 @@
1
- "use strict";
2
-
3
- import { useMemo, useRef, useState } from 'react';
4
- import { equal, keyInObject, reduceObject } from '../utils';
5
- import { getVariants } from './getVariants';
6
- export const createUseVariants = (styles, setSelectedVariants) => {
7
- const useVariants = selectedVariants => {
8
- const [selectedVariantStylesMap] = useState(() => new Map());
9
- const lastSelectedVariantsRef = useRef();
10
- // Variable that determines if variants have changed, and we need to recalculate styles
11
- const variantsChanged = !equal(lastSelectedVariantsRef.current, selectedVariants);
12
- if (variantsChanged) {
13
- lastSelectedVariantsRef.current = selectedVariants;
14
- setSelectedVariants(selectedVariants);
15
- }
16
- const combinedVariantStyles = useMemo(() => {
17
- const result = getVariants(styles, selectedVariants);
18
- result.forEach(([key, value]) => {
19
- selectedVariantStylesMap.set(key, value);
20
- });
21
- return result;
22
- }, [lastSelectedVariantsRef.current]);
23
- combinedVariantStyles.forEach(([key]) => {
24
- const styleEntry = styles[key];
25
- if (!styleEntry) {
26
- return;
27
- }
28
- const selectedVariantStyles = selectedVariantStylesMap.get(key);
29
- Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
30
- value,
31
- enumerable: false,
32
- configurable: true
33
- })));
34
-
35
- // Add __uni__variants to static styles
36
- Object.keys(styleEntry).forEach(key => {
37
- if (!key.startsWith('__uni__secrets__')) {
38
- return;
39
- }
40
- const secret = keyInObject(styleEntry, key) ? styleEntry[key] : undefined;
41
- if (!secret) {
42
- return;
43
- }
44
- Object.defineProperty(secret, '__uni__variants', {
45
- value: selectedVariants
46
- });
47
- });
48
- });
49
- };
50
- Object.defineProperty(styles, 'useVariants', {
51
- value: useVariants
52
- });
53
- };
54
- //# sourceMappingURL=useVariants.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["useMemo","useRef","useState","equal","keyInObject","reduceObject","getVariants","createUseVariants","styles","setSelectedVariants","useVariants","selectedVariants","selectedVariantStylesMap","Map","lastSelectedVariantsRef","variantsChanged","current","combinedVariantStyles","result","forEach","key","value","set","styleEntry","selectedVariantStyles","get","Object","defineProperties","enumerable","configurable","keys","startsWith","secret","undefined","defineProperty"],"sourceRoot":"../../../../src","sources":["web/variants/useVariants.ts"],"mappings":";;AAAA,SAASA,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAEjD,SAASC,KAAK,EAAEC,WAAW,EAAEC,YAAY,QAAQ,UAAU;AAC3D,SAASC,WAAW,QAAQ,eAAe;AAE3C,OAAO,MAAMC,iBAAiB,GAAGA,CAACC,MAA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AACJ,OAAO,CAACC,GAAG,IAAI;QACnC,IAAI,CAACA,GAAG,CAACW,UAAU,CAAC,kBAAkB,CAAC,EAAE;UACrC;QACJ;QAEA,MAAMC,MAAM,GAAG5B,WAAW,CAACmB,UAAU,EAAEH,GAAG,CAAC,GAAGG,UAAU,CAACH,GAAG,CAAC,GAAGa,SAAS;QAEzE,IAAI,CAACD,MAAM,EAAE;UACT;QACJ;QAEAN,MAAM,CAACQ,cAAc,CAACF,MAAM,EAAE,iBAAiB,EAAE;UAC7CX,KAAK,EAAEV;QACX,CAAC,CAAC;MACN,CAAC,CAAC;IACN,CAAC,CAAC;EACN,CAAC;EAEDe,MAAM,CAACQ,cAAc,CAAC1B,MAAM,EAAE,aAAa,EAAE;IACzCa,KAAK,EAAEX;EACX,CAAC,CAAC;AACN,CAAC","ignoreList":[]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"getVariants.d.ts","sourceRoot":"","sources":["../../../../../src/web/variants/getVariants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAWpE,eAAO,MAAM,WAAW,WAAY,qBAAqB,CAAC,UAAU,CAAC,oBAAoB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,+BA8B3G,CAAA"}
@@ -1,3 +0,0 @@
1
- export * from './useVariants';
2
- export * from './getVariants';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/web/variants/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA"}
@@ -1,3 +0,0 @@
1
- import type { ReactNativeStyleSheet, StyleSheet } from '../../types';
2
- export declare const createUseVariants: (styles: ReactNativeStyleSheet<StyleSheet>, setSelectedVariants: (variants: Record<string, any>) => void) => void;
3
- //# sourceMappingURL=useVariants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useVariants.d.ts","sourceRoot":"","sources":["../../../../../src/web/variants/useVariants.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAIpE,eAAO,MAAM,iBAAiB,WAAY,qBAAqB,CAAC,UAAU,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,SA2DxI,CAAA"}
@@ -1,2 +0,0 @@
1
- export * from './useVariants'
2
- export * from './getVariants'
@@ -1,65 +0,0 @@
1
- import { useMemo, useRef, useState } from 'react'
2
- import type { ReactNativeStyleSheet, StyleSheet } from '../../types'
3
- import { equal, keyInObject, reduceObject } from '../utils'
4
- import { getVariants } from './getVariants'
5
-
6
- export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, setSelectedVariants: (variants: Record<string, any>) => void) => {
7
- const useVariants = (selectedVariants: Record<string, any>) => {
8
- const [selectedVariantStylesMap] = useState(() => new Map<string, Record<string, any>>())
9
- const lastSelectedVariantsRef = useRef<Record<string, any>>()
10
- // Variable that determines if variants have changed, and we need to recalculate styles
11
- const variantsChanged = !equal(lastSelectedVariantsRef.current, selectedVariants)
12
-
13
- if (variantsChanged) {
14
- lastSelectedVariantsRef.current = selectedVariants
15
- setSelectedVariants(selectedVariants)
16
- }
17
-
18
- const combinedVariantStyles = useMemo(() => {
19
- const result = getVariants(styles, selectedVariants)
20
-
21
- result.forEach(([key, value]) => {
22
- selectedVariantStylesMap.set(key, value)
23
- })
24
-
25
- return result
26
- }, [lastSelectedVariantsRef.current])
27
-
28
- combinedVariantStyles.forEach(([key]) => {
29
- const styleEntry = styles[key]
30
-
31
- if (!styleEntry) {
32
- return
33
- }
34
-
35
- const selectedVariantStyles = selectedVariantStylesMap.get(key)
36
-
37
- Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
38
- value,
39
- enumerable: false,
40
- configurable: true
41
- })))
42
-
43
- // Add __uni__variants to static styles
44
- Object.keys(styleEntry).forEach(key => {
45
- if (!key.startsWith('__uni__secrets__')) {
46
- return
47
- }
48
-
49
- const secret = keyInObject(styleEntry, key) ? styleEntry[key] : undefined
50
-
51
- if (!secret) {
52
- return
53
- }
54
-
55
- Object.defineProperty(secret, '__uni__variants', {
56
- value: selectedVariants
57
- })
58
- })
59
- })
60
- }
61
-
62
- Object.defineProperty(styles, 'useVariants', {
63
- value: useVariants
64
- })
65
- }