@niibase/uniwind 1.1.11 → 1.4.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/dist/common/components/ScopedTheme/ScopedTheme.js +28 -0
- package/dist/common/components/ScopedTheme/ScopedTheme.native.js +22 -0
- package/dist/common/components/ScopedTheme/index.js +16 -0
- package/dist/common/components/native/TextInput.android.js +52 -0
- package/dist/common/components/native/useStyle.js +4 -7
- package/dist/common/core/config/config.native.js +4 -14
- package/dist/common/core/context.js +13 -0
- package/dist/common/core/native/runtime.js +11 -1
- package/dist/common/core/native/store.js +36 -36
- package/dist/common/core/web/getWebStyles.js +26 -6
- package/dist/common/{css/processFunctions.js → css-visitor/function-visitor.js} +20 -8
- package/dist/common/css-visitor/index.js +16 -0
- package/dist/common/css-visitor/rule-visitor.js +122 -0
- package/dist/common/css-visitor/visitor.js +23 -0
- package/dist/common/hoc/withUniwind.js +5 -2
- package/dist/common/hoc/withUniwind.native.js +7 -4
- package/dist/common/hooks/useCSSVariable/getVariableValue.js +1 -12
- package/dist/common/hooks/useCSSVariable/getVariableValue.native.js +1 -1
- package/dist/common/hooks/useCSSVariable/useCSSVariable.js +8 -6
- package/dist/common/hooks/useResolveClassNames.js +4 -2
- package/dist/common/hooks/useResolveClassNames.native.js +4 -2
- package/dist/common/hooks/useUniwind.js +8 -3
- package/dist/common/index.js +19 -0
- package/dist/common/vite/vite.js +2 -4
- package/dist/metro/index.cjs +1 -1
- package/dist/metro/index.mjs +1 -1
- package/dist/metro/metro-transformer.cjs +33 -17
- package/dist/metro/metro-transformer.mjs +33 -17
- package/dist/module/components/ScopedTheme/ScopedTheme.d.ts +7 -0
- package/dist/module/components/ScopedTheme/ScopedTheme.js +7 -0
- package/dist/module/components/ScopedTheme/ScopedTheme.native.d.ts +7 -0
- package/dist/module/components/ScopedTheme/ScopedTheme.native.js +7 -0
- package/dist/module/components/ScopedTheme/index.d.ts +1 -0
- package/dist/module/components/ScopedTheme/index.js +1 -0
- package/dist/module/components/native/TextInput.android.d.ts +3 -0
- package/dist/module/components/native/TextInput.android.js +49 -0
- package/dist/module/components/native/useStyle.js +4 -7
- package/dist/module/core/config/config.native.js +4 -14
- package/dist/module/core/context.d.ts +7 -0
- package/dist/module/core/context.js +6 -0
- package/dist/module/core/native/runtime.js +5 -2
- package/dist/module/core/native/store.d.ts +4 -6
- package/dist/module/core/native/store.js +36 -36
- package/dist/module/core/types.d.ts +4 -0
- package/dist/module/core/web/getWebStyles.d.ts +3 -2
- package/dist/module/core/web/getWebStyles.js +23 -4
- package/dist/module/css-visitor/function-visitor.d.ts +8 -0
- package/dist/module/{css/processFunctions.js → css-visitor/function-visitor.js} +12 -7
- package/dist/module/css-visitor/index.d.ts +1 -0
- package/dist/module/css-visitor/index.js +1 -0
- package/dist/module/css-visitor/rule-visitor.d.ts +26 -0
- package/dist/module/css-visitor/rule-visitor.js +111 -0
- package/dist/module/css-visitor/visitor.d.ts +8 -0
- package/dist/module/css-visitor/visitor.js +16 -0
- package/dist/module/hoc/withUniwind.js +5 -2
- package/dist/module/hoc/withUniwind.native.js +7 -4
- package/dist/module/hooks/useCSSVariable/getVariableValue.d.ts +1 -1
- package/dist/module/hooks/useCSSVariable/getVariableValue.js +2 -12
- package/dist/module/hooks/useCSSVariable/getVariableValue.native.d.ts +2 -1
- package/dist/module/hooks/useCSSVariable/getVariableValue.native.js +2 -2
- package/dist/module/hooks/useCSSVariable/useCSSVariable.js +8 -6
- package/dist/module/hooks/useResolveClassNames.js +6 -3
- package/dist/module/hooks/useResolveClassNames.native.js +6 -3
- package/dist/module/hooks/useUniwind.d.ts +1 -2
- package/dist/module/hooks/useUniwind.js +8 -3
- package/dist/module/index.d.ts +1 -0
- package/dist/module/index.js +1 -0
- package/dist/module/vite/vite.js +2 -4
- package/dist/shared/{uniwind.xtsPxiWD.mjs → uniwind.B_j3NcHy.mjs} +141 -9
- package/dist/shared/{uniwind.BJE4mTIG.cjs → uniwind.D7C2Zt-r.cjs} +141 -9
- package/dist/shared/{uniwind.BxeutNHQ.mjs → uniwind.F-0-Rr--.mjs} +1 -0
- package/dist/shared/{uniwind.thpLtIq6.cjs → uniwind.nl8746mK.cjs} +1 -0
- package/dist/vite/index.cjs +3 -5
- package/dist/vite/index.mjs +3 -5
- package/package.json +4 -3
- package/readme.md +4 -12
- package/src/components/ScopedTheme/ScopedTheme.native.tsx +17 -0
- package/src/components/ScopedTheme/ScopedTheme.tsx +19 -0
- package/src/components/ScopedTheme/index.ts +1 -0
- package/src/components/native/TextInput.android.tsx +51 -0
- package/src/components/native/useStyle.ts +4 -12
- package/src/core/config/config.native.ts +4 -16
- package/src/core/context.ts +10 -0
- package/src/core/native/runtime.ts +5 -1
- package/src/core/native/store.ts +56 -43
- package/src/core/types.ts +5 -0
- package/src/core/web/getWebStyles.ts +31 -5
- package/src/{css/processFunctions.ts → css-visitor/function-visitor.ts} +19 -9
- package/src/css-visitor/index.ts +1 -0
- package/src/css-visitor/rule-visitor.ts +155 -0
- package/src/css-visitor/visitor.ts +20 -0
- package/src/hoc/withUniwind.native.tsx +7 -4
- package/src/hoc/withUniwind.tsx +7 -2
- package/src/hooks/useCSSVariable/getVariableValue.native.ts +4 -2
- package/src/hooks/useCSSVariable/getVariableValue.ts +2 -18
- package/src/hooks/useCSSVariable/useCSSVariable.ts +11 -8
- package/src/hooks/useResolveClassNames.native.ts +6 -3
- package/src/hooks/useResolveClassNames.ts +6 -3
- package/src/hooks/useUniwind.ts +9 -3
- package/src/index.ts +1 -0
- package/src/metro/compileVirtual.ts +7 -2
- package/src/metro/processor/functions.ts +12 -0
- package/src/metro/processor/rn.ts +15 -0
- package/src/metro/utils/common.ts +1 -0
- package/src/metro/utils/serialize.ts +7 -1
- package/src/vite/vite.ts +2 -4
- package/dist/module/css/processFunctions.d.ts +0 -2
- package/src/metro/polyfillWeb.ts +0 -14
|
@@ -24,21 +24,12 @@ class UniwindConfigBuilder extends UniwindConfigBuilderBase {
|
|
|
24
24
|
}
|
|
25
25
|
return varValue;
|
|
26
26
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (theme === this.currentTheme) {
|
|
30
|
-
Object.defineProperty(UniwindStore.vars, varName, {
|
|
31
|
-
configurable: true,
|
|
32
|
-
enumerable: true,
|
|
33
|
-
get: () => value
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
Object.defineProperty(runtimeThemeVariables, varName, {
|
|
27
|
+
UniwindStore.vars[theme] ??= {};
|
|
28
|
+
Object.defineProperty(UniwindStore.vars[theme], varName, {
|
|
37
29
|
configurable: true,
|
|
38
30
|
enumerable: true,
|
|
39
|
-
get:
|
|
31
|
+
get: getValue
|
|
40
32
|
});
|
|
41
|
-
UniwindStore.runtimeThemeVariables.set(theme, runtimeThemeVariables);
|
|
42
33
|
});
|
|
43
34
|
if (theme === this.currentTheme) {
|
|
44
35
|
UniwindListener.notify([StyleDependency.Variables]);
|
|
@@ -53,11 +44,10 @@ class UniwindConfigBuilder extends UniwindConfigBuilderBase {
|
|
|
53
44
|
}
|
|
54
45
|
__reinit(generateStyleSheetCallback, themes) {
|
|
55
46
|
super.__reinit(generateStyleSheetCallback, themes);
|
|
56
|
-
UniwindStore.reinit(generateStyleSheetCallback);
|
|
47
|
+
UniwindStore.reinit(generateStyleSheetCallback, themes);
|
|
57
48
|
}
|
|
58
49
|
onThemeChange() {
|
|
59
50
|
UniwindStore.runtime.currentThemeName = this.currentTheme;
|
|
60
|
-
UniwindStore.reinit();
|
|
61
51
|
}
|
|
62
52
|
}
|
|
63
53
|
export const Uniwind = new UniwindConfigBuilder();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Appearance, Dimensions, I18nManager, PixelRatio, StyleSheet } from "react-native";
|
|
1
|
+
import { Appearance, Dimensions, I18nManager, PixelRatio, Platform, StyleSheet } from "react-native";
|
|
2
2
|
import { cubicBezier, linear, steps } from "react-native-reanimated";
|
|
3
3
|
import { initialWindowMetrics } from "react-native-safe-area-context";
|
|
4
4
|
import { ColorScheme, Orientation } from "../../types.js";
|
|
@@ -23,6 +23,9 @@ export const UniwindRuntime = {
|
|
|
23
23
|
steps,
|
|
24
24
|
linear,
|
|
25
25
|
lightDark: () => "",
|
|
26
|
-
parseColor
|
|
26
|
+
parseColor,
|
|
27
|
+
platformSelect: (android, ios, other) => {
|
|
28
|
+
return Platform.select(Boolean(other) ? { android, ios, default: other } : { android, default: ios });
|
|
29
|
+
}
|
|
27
30
|
};
|
|
28
31
|
UniwindRuntime.lightDark = lightDark.bind(UniwindRuntime);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StyleDependency } from '../../types';
|
|
2
|
-
import { ComponentState,
|
|
2
|
+
import { ComponentState, GenerateStyleSheetsCallback, RNStyle, ThemeName, UniwindContextType } from '../types';
|
|
3
3
|
type StylesResult = {
|
|
4
4
|
styles: RNStyle;
|
|
5
5
|
dependencies: Array<StyleDependency>;
|
|
@@ -7,14 +7,12 @@ type StylesResult = {
|
|
|
7
7
|
};
|
|
8
8
|
declare class UniwindStoreBuilder {
|
|
9
9
|
runtime: import("../types").UniwindRuntime;
|
|
10
|
-
vars: Record<string, unknown
|
|
11
|
-
runtimeThemeVariables: Map<string, CSSVariables>;
|
|
10
|
+
vars: Record<ThemeName, Record<string, unknown>>;
|
|
12
11
|
private stylesheet;
|
|
13
12
|
private keyframes;
|
|
14
13
|
private cache;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
reinit: (generateStyleSheetCallback?: GenerateStyleSheetsCallback) => void;
|
|
14
|
+
getStyles(className: string | undefined, componentProps: Record<string, any> | undefined, state: ComponentState | undefined, uniwindContext: UniwindContextType): StylesResult;
|
|
15
|
+
reinit: (generateStyleSheetCallback: GenerateStyleSheetsCallback, themes: Array<string>) => void;
|
|
18
16
|
private resolveStyles;
|
|
19
17
|
private validateDataAttributes;
|
|
20
18
|
}
|
|
@@ -15,63 +15,60 @@ const emptyState = { styles: {}, dependencies: [], dependencySum: 0 };
|
|
|
15
15
|
class UniwindStoreBuilder {
|
|
16
16
|
runtime = UniwindRuntime;
|
|
17
17
|
vars = {};
|
|
18
|
-
runtimeThemeVariables = /* @__PURE__ */ new Map();
|
|
19
18
|
stylesheet = {};
|
|
20
19
|
keyframes = {};
|
|
21
|
-
cache =
|
|
22
|
-
|
|
23
|
-
getStyles(className, componentProps, state) {
|
|
20
|
+
cache = {};
|
|
21
|
+
getStyles(className, componentProps, state, uniwindContext) {
|
|
24
22
|
if (className === void 0 || className === "") {
|
|
25
23
|
return emptyState;
|
|
26
24
|
}
|
|
27
|
-
const
|
|
28
|
-
const cacheKey = `${className}
|
|
29
|
-
const
|
|
30
|
-
if (
|
|
31
|
-
|
|
25
|
+
const isScopedTheme = uniwindContext.scopedTheme !== null;
|
|
26
|
+
const cacheKey = `${className}${state?.isDisabled ?? false}${state?.isFocused ?? false}${state?.isPressed ?? false}${isScopedTheme}`;
|
|
27
|
+
const cache = this.cache[uniwindContext.scopedTheme ?? this.runtime.currentThemeName];
|
|
28
|
+
if (!cache) return emptyState;
|
|
29
|
+
if (cache.has(cacheKey)) return cache.get(cacheKey);
|
|
30
|
+
const result = this.resolveStyles(className, componentProps, state, uniwindContext);
|
|
32
31
|
if (!result.hasDataAttributes) {
|
|
33
|
-
|
|
32
|
+
cache.set(cacheKey, result);
|
|
34
33
|
UniwindListener.subscribe(
|
|
35
|
-
() =>
|
|
34
|
+
() => cache.delete(cacheKey),
|
|
36
35
|
result.dependencies,
|
|
37
36
|
{ once: true }
|
|
38
37
|
);
|
|
39
38
|
}
|
|
40
39
|
return result;
|
|
41
40
|
}
|
|
42
|
-
reinit = (generateStyleSheetCallback) => {
|
|
43
|
-
const
|
|
44
|
-
if (!config) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
const { scopedVars, stylesheet, vars, keyframes } = config;
|
|
48
|
-
this.generateStyleSheetCallbackResult = config;
|
|
49
|
-
this.stylesheet = stylesheet;
|
|
50
|
-
this.keyframes = keyframes;
|
|
51
|
-
this.vars = vars;
|
|
52
|
-
const themeVars = scopedVars[`__uniwind-theme-${this.runtime.currentThemeName}`];
|
|
41
|
+
reinit = (generateStyleSheetCallback, themes) => {
|
|
42
|
+
const { scopedVars, stylesheet, vars, keyframes } = generateStyleSheetCallback(this.runtime);
|
|
53
43
|
const platformVars = scopedVars[`__uniwind-platform-${Platform.OS}`];
|
|
54
|
-
const runtimeThemeVars = this.runtimeThemeVariables.get(this.runtime.currentThemeName);
|
|
55
|
-
if (themeVars) {
|
|
56
|
-
Object.defineProperties(this.vars, Object.getOwnPropertyDescriptors(themeVars));
|
|
57
|
-
}
|
|
58
44
|
if (platformVars) {
|
|
59
|
-
Object.defineProperties(
|
|
60
|
-
}
|
|
61
|
-
if (runtimeThemeVars) {
|
|
62
|
-
Object.defineProperties(this.vars, Object.getOwnPropertyDescriptors(runtimeThemeVars));
|
|
45
|
+
Object.defineProperties(vars, Object.getOwnPropertyDescriptors(platformVars));
|
|
63
46
|
}
|
|
64
|
-
|
|
47
|
+
this.keyframes = keyframes;
|
|
48
|
+
this.stylesheet = stylesheet;
|
|
49
|
+
this.vars = Object.fromEntries(themes.map((theme) => {
|
|
50
|
+
const clonedVars = cloneWithAccessors(vars);
|
|
51
|
+
const themeVars = scopedVars[`__uniwind-theme-${theme}`];
|
|
52
|
+
if (themeVars) {
|
|
53
|
+
Object.defineProperties(clonedVars, Object.getOwnPropertyDescriptors(themeVars));
|
|
54
|
+
}
|
|
55
|
+
return [theme, clonedVars];
|
|
56
|
+
}));
|
|
57
|
+
this.cache = Object.fromEntries(themes.map((theme) => [theme, /* @__PURE__ */ new Map()]));
|
|
58
|
+
if (__DEV__) {
|
|
65
59
|
UniwindListener.notifyAll();
|
|
66
60
|
}
|
|
67
61
|
};
|
|
68
|
-
resolveStyles(classNames, componentProps, state) {
|
|
62
|
+
resolveStyles(classNames, componentProps, state, uniwindContext) {
|
|
69
63
|
const result = {};
|
|
70
|
-
|
|
64
|
+
const theme = uniwindContext.scopedTheme ?? this.runtime.currentThemeName;
|
|
65
|
+
let vars = this.vars[theme];
|
|
66
|
+
const originalVars = vars;
|
|
71
67
|
let hasDataAttributes = false;
|
|
72
68
|
const dependencies = /* @__PURE__ */ new Set();
|
|
73
69
|
let dependencySum = 0;
|
|
74
70
|
const bestBreakpoints = /* @__PURE__ */ new Map();
|
|
71
|
+
const isScopedTheme = uniwindContext.scopedTheme !== null;
|
|
75
72
|
for (const className of classNames.split(" ")) {
|
|
76
73
|
if (!(className in this.stylesheet)) {
|
|
77
74
|
continue;
|
|
@@ -79,6 +76,9 @@ class UniwindStoreBuilder {
|
|
|
79
76
|
for (const style of this.stylesheet[className]) {
|
|
80
77
|
if (style.dependencies) {
|
|
81
78
|
style.dependencies.forEach((dep) => {
|
|
79
|
+
if (dep === StyleDependency.Theme && isScopedTheme) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
82
|
dependencies.add(dep);
|
|
83
83
|
dependencySum |= 1 << dep;
|
|
84
84
|
});
|
|
@@ -86,7 +86,7 @@ class UniwindStoreBuilder {
|
|
|
86
86
|
if (style.dataAttributes !== null) {
|
|
87
87
|
hasDataAttributes = true;
|
|
88
88
|
}
|
|
89
|
-
if (style.minWidth > this.runtime.screen.width || style.maxWidth < this.runtime.screen.width || style.theme !== null &&
|
|
89
|
+
if (style.minWidth > this.runtime.screen.width || style.maxWidth < this.runtime.screen.width || style.theme !== null && theme !== style.theme || style.orientation !== null && this.runtime.orientation !== style.orientation || style.rtl !== null && this.runtime.rtl !== style.rtl || style.active !== null && state?.isPressed !== style.active || style.focus !== null && state?.isFocused !== style.focus || style.disabled !== null && state?.isDisabled !== style.disabled || style.dataAttributes !== null && !this.validateDataAttributes(style.dataAttributes, componentProps)) {
|
|
90
90
|
continue;
|
|
91
91
|
}
|
|
92
92
|
for (const [property, valueGetter] of style.entries) {
|
|
@@ -95,8 +95,8 @@ class UniwindStoreBuilder {
|
|
|
95
95
|
continue;
|
|
96
96
|
}
|
|
97
97
|
if (property[0] === "-") {
|
|
98
|
-
if (vars ===
|
|
99
|
-
vars = cloneWithAccessors(
|
|
98
|
+
if (vars === originalVars) {
|
|
99
|
+
vars = cloneWithAccessors(originalVars);
|
|
100
100
|
}
|
|
101
101
|
Object.defineProperty(vars, property, {
|
|
102
102
|
configurable: true,
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import React from 'react';
|
|
1
2
|
import type { ImageStyle, StyleProp, TextStyle, ViewStyle } from 'react-native';
|
|
2
3
|
import { CSSAnimationKeyframes } from 'react-native-reanimated';
|
|
3
4
|
import { ControlPoint, CubicBezierEasing, LinearEasing, StepsEasing, StepsModifier } from 'react-native-reanimated/lib/typescript/css/easing';
|
|
4
5
|
import { ColorScheme, Orientation, StyleDependency, UniwindConfig } from '../types';
|
|
6
|
+
import type { UniwindContext } from './context';
|
|
5
7
|
export type Style = {
|
|
6
8
|
entries: Array<[string, () => unknown]>;
|
|
7
9
|
minWidth: number;
|
|
@@ -55,6 +57,7 @@ export type UniwindRuntime = {
|
|
|
55
57
|
linear: (...points: ControlPoint[]) => LinearEasing;
|
|
56
58
|
lightDark: (light: string, dark: string) => string;
|
|
57
59
|
parseColor: (type: string, color: string) => string;
|
|
60
|
+
platformSelect: (android: string, ios: string, other?: string) => string;
|
|
58
61
|
};
|
|
59
62
|
export type RNStyle = ViewStyle & TextStyle & ImageStyle & {
|
|
60
63
|
accentColor?: string;
|
|
@@ -77,4 +80,5 @@ export type ComponentState = {
|
|
|
77
80
|
isFocused?: boolean;
|
|
78
81
|
};
|
|
79
82
|
export type CSSVariables = Record<string, string | number>;
|
|
83
|
+
export type UniwindContextType = React.ContextType<typeof UniwindContext>;
|
|
80
84
|
export {};
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { RNStyle } from '../types';
|
|
2
|
-
export declare const getWebStyles: (className
|
|
1
|
+
import { RNStyle, UniwindContextType } from '../types';
|
|
2
|
+
export declare const getWebStyles: (className: string | undefined, uniwindContext: UniwindContextType) => RNStyle;
|
|
3
|
+
export declare const getWebVariable: (name: string, uniwindContext: UniwindContextType) => string | undefined;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { parseCSSValue } from "./parseCSSValue.js";
|
|
2
|
-
const
|
|
2
|
+
const dummyParent = typeof document !== "undefined" ? Object.assign(document.createElement("div"), {
|
|
3
3
|
style: "display: none"
|
|
4
4
|
}) : null;
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const dummy = typeof document !== "undefined" ? document.createElement("div") : null;
|
|
6
|
+
if (dummyParent && dummy) {
|
|
7
|
+
document.body.appendChild(dummyParent);
|
|
8
|
+
dummyParent.appendChild(dummy);
|
|
7
9
|
}
|
|
8
10
|
const getComputedStyles = () => {
|
|
9
11
|
if (!dummy) {
|
|
@@ -28,13 +30,18 @@ const getObjectDifference = (obj1, obj2) => {
|
|
|
28
30
|
});
|
|
29
31
|
return diff;
|
|
30
32
|
};
|
|
31
|
-
export const getWebStyles = (className) => {
|
|
33
|
+
export const getWebStyles = (className, uniwindContext) => {
|
|
32
34
|
if (className === void 0) {
|
|
33
35
|
return {};
|
|
34
36
|
}
|
|
35
37
|
if (!dummy) {
|
|
36
38
|
return {};
|
|
37
39
|
}
|
|
40
|
+
if (uniwindContext.scopedTheme !== null) {
|
|
41
|
+
dummyParent?.setAttribute("class", uniwindContext.scopedTheme);
|
|
42
|
+
} else {
|
|
43
|
+
dummyParent?.removeAttribute("class");
|
|
44
|
+
}
|
|
38
45
|
dummy.className = className;
|
|
39
46
|
const computedStyles = getObjectDifference(initialStyles, getComputedStyles());
|
|
40
47
|
return Object.fromEntries(
|
|
@@ -47,3 +54,15 @@ export const getWebStyles = (className) => {
|
|
|
47
54
|
})
|
|
48
55
|
);
|
|
49
56
|
};
|
|
57
|
+
export const getWebVariable = (name, uniwindContext) => {
|
|
58
|
+
if (!dummyParent) {
|
|
59
|
+
return void 0;
|
|
60
|
+
}
|
|
61
|
+
if (uniwindContext.scopedTheme !== null) {
|
|
62
|
+
dummyParent.setAttribute("class", uniwindContext.scopedTheme);
|
|
63
|
+
} else {
|
|
64
|
+
dummyParent.removeAttribute("class");
|
|
65
|
+
}
|
|
66
|
+
const variable = window.getComputedStyle(dummyParent).getPropertyValue(name);
|
|
67
|
+
return parseCSSValue(variable);
|
|
68
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Function as LightningCSSFunction, TokenOrValue } from 'lightningcss';
|
|
2
|
+
export declare class FunctionVisitor {
|
|
3
|
+
[name: string]: (fn: LightningCSSFunction) => TokenOrValue;
|
|
4
|
+
platformSelect(fn: LightningCSSFunction): TokenOrValue;
|
|
5
|
+
pixelRatio(fn: LightningCSSFunction): TokenOrValue;
|
|
6
|
+
fontScale(fn: LightningCSSFunction): TokenOrValue;
|
|
7
|
+
hairlineWidth(): TokenOrValue;
|
|
8
|
+
}
|
|
@@ -2,8 +2,11 @@ const ONE_PX = {
|
|
|
2
2
|
type: "token",
|
|
3
3
|
value: { type: "dimension", unit: "px", value: 1 }
|
|
4
4
|
};
|
|
5
|
-
export
|
|
6
|
-
|
|
5
|
+
export class FunctionVisitor {
|
|
6
|
+
platformSelect(fn) {
|
|
7
|
+
return fn.arguments.at(0) ?? { type: "token", value: { type: "ident", value: "initial" } };
|
|
8
|
+
}
|
|
9
|
+
pixelRatio(fn) {
|
|
7
10
|
return {
|
|
8
11
|
type: "function",
|
|
9
12
|
value: {
|
|
@@ -15,8 +18,8 @@ export const processFunctions = {
|
|
|
15
18
|
]
|
|
16
19
|
}
|
|
17
20
|
};
|
|
18
|
-
}
|
|
19
|
-
fontScale
|
|
21
|
+
}
|
|
22
|
+
fontScale(fn) {
|
|
20
23
|
return {
|
|
21
24
|
type: "function",
|
|
22
25
|
value: {
|
|
@@ -31,6 +34,8 @@ export const processFunctions = {
|
|
|
31
34
|
]
|
|
32
35
|
}
|
|
33
36
|
};
|
|
34
|
-
}
|
|
35
|
-
hairlineWidth
|
|
36
|
-
|
|
37
|
+
}
|
|
38
|
+
hairlineWidth() {
|
|
39
|
+
return ONE_PX;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './visitor';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./visitor.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ReturnedDeclaration, ReturnedMediaQuery, ReturnedRule, Rule } from 'lightningcss';
|
|
2
|
+
type LightningRuleVisitor = Rule<ReturnedDeclaration, ReturnedMediaQuery>;
|
|
3
|
+
type LightningRuleVisitors = Partial<{
|
|
4
|
+
[K in LightningRuleVisitor['type']]: (rule: Extract<LightningRuleVisitor, {
|
|
5
|
+
type: K;
|
|
6
|
+
}>) => ReturnedRule | Array<ReturnedRule> | void;
|
|
7
|
+
}>;
|
|
8
|
+
export declare class RuleVisitor implements LightningRuleVisitors {
|
|
9
|
+
private readonly themes;
|
|
10
|
+
processedClassNames: Set<string>;
|
|
11
|
+
processedVariables: Set<string>;
|
|
12
|
+
currentLayerName: string;
|
|
13
|
+
constructor(themes: Array<string>);
|
|
14
|
+
'layer-block': (layer: Extract<LightningRuleVisitor, {
|
|
15
|
+
type: "layer-block";
|
|
16
|
+
}>) => void;
|
|
17
|
+
style: (styleRule: Extract<LightningRuleVisitor, {
|
|
18
|
+
type: "style";
|
|
19
|
+
}>) => void | ReturnedRule | ReturnedRule[];
|
|
20
|
+
cleanup(): void;
|
|
21
|
+
private processThemeRoot;
|
|
22
|
+
private processThemeStyle;
|
|
23
|
+
private processClassStyle;
|
|
24
|
+
private removeNulls;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export class RuleVisitor {
|
|
2
|
+
constructor(themes) {
|
|
3
|
+
this.themes = themes;
|
|
4
|
+
}
|
|
5
|
+
processedClassNames = /* @__PURE__ */ new Set();
|
|
6
|
+
processedVariables = /* @__PURE__ */ new Set();
|
|
7
|
+
currentLayerName = "";
|
|
8
|
+
"layer-block" = (layer) => {
|
|
9
|
+
this.currentLayerName = layer.value.name?.join("") ?? "";
|
|
10
|
+
};
|
|
11
|
+
style = (styleRule) => {
|
|
12
|
+
const firstSelector = styleRule.value.selectors.at(0)?.at(0);
|
|
13
|
+
if (this.currentLayerName === "theme" && firstSelector?.type === "pseudo-class" && firstSelector.kind === "root") {
|
|
14
|
+
return this.removeNulls(this.processThemeRoot(styleRule));
|
|
15
|
+
}
|
|
16
|
+
if (firstSelector?.type === "class") {
|
|
17
|
+
return this.processClassStyle(styleRule, firstSelector);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
cleanup() {
|
|
21
|
+
this.currentLayerName = "";
|
|
22
|
+
this.processedClassNames.clear();
|
|
23
|
+
this.processedVariables.clear();
|
|
24
|
+
}
|
|
25
|
+
processThemeRoot(styleRule) {
|
|
26
|
+
const themeScopedRules = styleRule.value.rules?.filter((rule) => {
|
|
27
|
+
if (rule.type !== "style") {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const firstSelector = rule.value.selectors.at(0)?.at(0);
|
|
31
|
+
const secondSelector = rule.value.selectors.at(0)?.at(1);
|
|
32
|
+
return firstSelector?.type === "nesting" && secondSelector?.type === "pseudo-class" && secondSelector.kind === "where";
|
|
33
|
+
}) ?? [];
|
|
34
|
+
const nonThemeRules = styleRule.value.rules?.filter((rule) => !themeScopedRules.includes(rule));
|
|
35
|
+
const processedThemeScopedRules = themeScopedRules.map((rule) => {
|
|
36
|
+
if (rule.type !== "style") {
|
|
37
|
+
return rule;
|
|
38
|
+
}
|
|
39
|
+
const secondSelector = rule.value.selectors.at(0)?.at(1);
|
|
40
|
+
if (secondSelector?.type === "pseudo-class" && secondSelector.kind === "where") {
|
|
41
|
+
return this.processThemeStyle(rule, secondSelector);
|
|
42
|
+
}
|
|
43
|
+
return rule;
|
|
44
|
+
});
|
|
45
|
+
return [
|
|
46
|
+
{
|
|
47
|
+
type: "style",
|
|
48
|
+
value: {
|
|
49
|
+
loc: styleRule.value.loc,
|
|
50
|
+
selectors: styleRule.value.selectors,
|
|
51
|
+
rules: nonThemeRules,
|
|
52
|
+
declarations: styleRule.value.declarations
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
...processedThemeScopedRules
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
processThemeStyle(styleRule, secondSelector) {
|
|
59
|
+
const whereSelector = secondSelector.selectors.at(0)?.at(0);
|
|
60
|
+
if (whereSelector?.type !== "class") {
|
|
61
|
+
return styleRule;
|
|
62
|
+
}
|
|
63
|
+
const selectedVariant = this.themes.find((theme) => whereSelector.name === theme);
|
|
64
|
+
if (selectedVariant === void 0 || this.processedVariables.has(selectedVariant)) {
|
|
65
|
+
return styleRule;
|
|
66
|
+
}
|
|
67
|
+
this.processedVariables.add(selectedVariant);
|
|
68
|
+
return {
|
|
69
|
+
type: "style",
|
|
70
|
+
value: {
|
|
71
|
+
loc: styleRule.value.loc,
|
|
72
|
+
selectors: [[{ type: "class", name: selectedVariant }]],
|
|
73
|
+
declarations: styleRule.value.declarations,
|
|
74
|
+
rules: styleRule.value.rules
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
processClassStyle(styleRule, firstSelector) {
|
|
79
|
+
const selectedVariant = this.themes.find((theme) => firstSelector.name.includes(`${theme}:`));
|
|
80
|
+
if (selectedVariant === void 0 || this.processedClassNames.has(firstSelector.name)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.processedClassNames.add(firstSelector.name);
|
|
84
|
+
return {
|
|
85
|
+
type: "scope",
|
|
86
|
+
value: {
|
|
87
|
+
loc: styleRule.value.loc,
|
|
88
|
+
rules: [styleRule],
|
|
89
|
+
scopeStart: [[{ type: "class", name: selectedVariant }]],
|
|
90
|
+
scopeEnd: this.themes.filter((theme) => theme !== selectedVariant).map((theme) => [{ type: "class", name: theme }])
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
// Fixes lightningcss serialization bug
|
|
95
|
+
removeNulls(value) {
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
return value.map((v) => this.removeNulls(v));
|
|
98
|
+
}
|
|
99
|
+
if (typeof value === "object" && value !== null) {
|
|
100
|
+
return Object.fromEntries(
|
|
101
|
+
Object.entries(value).filter(([_, value2]) => {
|
|
102
|
+
if (value2 === null) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
}).map(([key, value2]) => [key, this.removeNulls(value2)])
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CustomAtRules, Visitor } from 'lightningcss';
|
|
2
|
+
export declare class UniwindCSSVisitor implements Visitor<CustomAtRules> {
|
|
3
|
+
private readonly themes;
|
|
4
|
+
Function: Visitor<CustomAtRules>['Function'];
|
|
5
|
+
Rule: Visitor<CustomAtRules>['Rule'];
|
|
6
|
+
StyleSheet: Visitor<CustomAtRules>['StyleSheet'];
|
|
7
|
+
constructor(themes: Array<string>);
|
|
8
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FunctionVisitor } from "./function-visitor.js";
|
|
2
|
+
import { RuleVisitor } from "./rule-visitor.js";
|
|
3
|
+
export class UniwindCSSVisitor {
|
|
4
|
+
constructor(themes) {
|
|
5
|
+
this.themes = themes;
|
|
6
|
+
const ruleVisitor = new RuleVisitor(this.themes);
|
|
7
|
+
this.Function = new FunctionVisitor();
|
|
8
|
+
this.Rule = ruleVisitor;
|
|
9
|
+
this.StyleSheet = () => {
|
|
10
|
+
ruleVisitor.cleanup();
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
Function;
|
|
14
|
+
Rule;
|
|
15
|
+
StyleSheet;
|
|
16
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useLayoutEffect, useReducer } from "react";
|
|
3
|
+
import { useUniwindContext } from "../core/context.js";
|
|
3
4
|
import { CSSListener, formatColor, getWebStyles } from "../core/web/index.js";
|
|
4
5
|
import { classToColor, classToStyle, isClassProperty, isColorClassProperty, isStyleProperty } from "./withUniwindUtils.js";
|
|
5
6
|
export const withUniwind = (Component2, options) => options ? withManualUniwind(Component2, options) : withAutoUniwind(Component2);
|
|
6
7
|
const withAutoUniwind = (Component2) => (props) => {
|
|
8
|
+
const uniwindContext = useUniwindContext();
|
|
7
9
|
const { classNames, generatedProps } = Object.entries(props).reduce((acc, [propName, propValue]) => {
|
|
8
10
|
if (isColorClassProperty(propName)) {
|
|
9
11
|
const colorProp = classToColor(propName);
|
|
@@ -11,7 +13,7 @@ const withAutoUniwind = (Component2) => (props) => {
|
|
|
11
13
|
return acc;
|
|
12
14
|
}
|
|
13
15
|
const className = propValue;
|
|
14
|
-
const color = getWebStyles(className).accentColor;
|
|
16
|
+
const color = getWebStyles(className, uniwindContext).accentColor;
|
|
15
17
|
acc.generatedProps[colorProp] = color !== void 0 ? formatColor(color) : void 0;
|
|
16
18
|
acc.classNames += `${className} `;
|
|
17
19
|
return acc;
|
|
@@ -43,6 +45,7 @@ const withAutoUniwind = (Component2) => (props) => {
|
|
|
43
45
|
);
|
|
44
46
|
};
|
|
45
47
|
const withManualUniwind = (Component2, options) => (props) => {
|
|
48
|
+
const uniwindContext = useUniwindContext();
|
|
46
49
|
const { generatedProps, classNames } = Object.entries(options).reduce((acc, [propName, option]) => {
|
|
47
50
|
const className = props[option.fromClassName];
|
|
48
51
|
if (className === void 0) {
|
|
@@ -52,7 +55,7 @@ const withManualUniwind = (Component2, options) => (props) => {
|
|
|
52
55
|
if (props[propName] !== void 0) {
|
|
53
56
|
return acc;
|
|
54
57
|
}
|
|
55
|
-
const value = getWebStyles(className)[option.styleProperty];
|
|
58
|
+
const value = getWebStyles(className, uniwindContext)[option.styleProperty];
|
|
56
59
|
const transformedValue = value !== void 0 && option.styleProperty.toLowerCase().includes("color") ? formatColor(value) : value;
|
|
57
60
|
acc.classNames += `${className} `;
|
|
58
61
|
acc.generatedProps[propName] = transformedValue;
|
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useLayoutEffect, useReducer } from "react";
|
|
3
|
+
import { useUniwindContext } from "../core/context.js";
|
|
3
4
|
import { UniwindListener } from "../core/listener.js";
|
|
4
5
|
import { UniwindStore } from "../core/native/index.js";
|
|
5
6
|
import { classToColor, classToStyle, isClassProperty, isColorClassProperty, isStyleProperty } from "./withUniwindUtils.js";
|
|
6
7
|
export const withUniwind = (Component2, options) => options ? withManualUniwind(Component2, options) : withAutoUniwind(Component2);
|
|
7
8
|
const withAutoUniwind = (Component2) => (props) => {
|
|
9
|
+
const uniwindContext = useUniwindContext();
|
|
8
10
|
const { dependencies, generatedProps } = Object.entries(props).reduce((acc, [propName, propValue]) => {
|
|
9
11
|
if (isColorClassProperty(propName)) {
|
|
10
12
|
const colorProp = classToColor(propName);
|
|
11
13
|
if (props[colorProp] !== void 0) {
|
|
12
14
|
return acc;
|
|
13
15
|
}
|
|
14
|
-
const { styles, dependencies: dependencies2 } = UniwindStore.getStyles(propValue);
|
|
16
|
+
const { styles, dependencies: dependencies2 } = UniwindStore.getStyles(propValue, void 0, void 0, uniwindContext);
|
|
15
17
|
acc.dependencies.push(...dependencies2);
|
|
16
18
|
acc.generatedProps[colorProp] = styles.accentColor;
|
|
17
19
|
return acc;
|
|
18
20
|
}
|
|
19
21
|
if (isClassProperty(propName)) {
|
|
20
22
|
const styleProp = classToStyle(propName);
|
|
21
|
-
const { styles, dependencies: dependencies2 } = UniwindStore.getStyles(propValue);
|
|
23
|
+
const { styles, dependencies: dependencies2 } = UniwindStore.getStyles(propValue, void 0, void 0, uniwindContext);
|
|
22
24
|
acc.dependencies.push(...dependencies2);
|
|
23
25
|
acc.generatedProps[styleProp] ??= [];
|
|
24
26
|
acc.generatedProps[styleProp][0] = styles;
|
|
@@ -49,6 +51,7 @@ const withAutoUniwind = (Component2) => (props) => {
|
|
|
49
51
|
);
|
|
50
52
|
};
|
|
51
53
|
const withManualUniwind = (Component2, options) => (props) => {
|
|
54
|
+
const uniwindContext = useUniwindContext();
|
|
52
55
|
const { generatedProps, dependencies } = Object.entries(options).reduce((acc, [propName, option]) => {
|
|
53
56
|
const className = props[option.fromClassName];
|
|
54
57
|
if (className === void 0) {
|
|
@@ -58,12 +61,12 @@ const withManualUniwind = (Component2, options) => (props) => {
|
|
|
58
61
|
if (props[propName] !== void 0) {
|
|
59
62
|
return acc;
|
|
60
63
|
}
|
|
61
|
-
const { styles: styles2, dependencies: dependencies3 } = UniwindStore.getStyles(className);
|
|
64
|
+
const { styles: styles2, dependencies: dependencies3 } = UniwindStore.getStyles(className, void 0, void 0, uniwindContext);
|
|
62
65
|
acc.generatedProps[propName] = styles2[option.styleProperty];
|
|
63
66
|
acc.dependencies.push(...dependencies3);
|
|
64
67
|
return acc;
|
|
65
68
|
}
|
|
66
|
-
const { styles, dependencies: dependencies2 } = UniwindStore.getStyles(className);
|
|
69
|
+
const { styles, dependencies: dependencies2 } = UniwindStore.getStyles(className, void 0, void 0, uniwindContext);
|
|
67
70
|
acc.dependencies.push(...dependencies2);
|
|
68
71
|
if (!isStyleProperty(propName)) {
|
|
69
72
|
acc.generatedProps[propName] = styles;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const getVariableValue: (name: string) => string | undefined;
|
|
1
|
+
export declare const getVariableValue: (name: string, uniwindContext: import("../../core/types").UniwindContextType) => string | undefined;
|
|
@@ -1,12 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const
|
|
3
|
-
export const getVariableValue = (name) => {
|
|
4
|
-
if (!documentStyles) {
|
|
5
|
-
return void 0;
|
|
6
|
-
}
|
|
7
|
-
const value = documentStyles.getPropertyValue(name).trim();
|
|
8
|
-
if (value === "") {
|
|
9
|
-
return void 0;
|
|
10
|
-
}
|
|
11
|
-
return parseCSSValue(value);
|
|
12
|
-
};
|
|
1
|
+
import { getWebVariable } from "../../core/web/index.js";
|
|
2
|
+
export const getVariableValue = getWebVariable;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { UniwindContextType } from '../../core/types';
|
|
2
|
+
export declare const getVariableValue: (name: string, uniwindContext: UniwindContextType) => unknown;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { UniwindStore } from "../../core/native/index.js";
|
|
2
|
-
export const getVariableValue = (name) => UniwindStore.vars[name];
|
|
1
|
+
import { UniwindRuntime, UniwindStore } from "../../core/native/index.js";
|
|
2
|
+
export const getVariableValue = (name, uniwindContext) => UniwindStore.vars[uniwindContext.scopedTheme ?? UniwindRuntime.currentThemeName]?.[name];
|