@melcanz85/chaincss 1.12.4 → 1.12.6
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/browser/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export {$,run,compile,chain,createTokens,responsive,tokens } from './rtt.js';
|
|
1
|
+
// ChainCSS Browser Entry Point
|
|
2
|
+
export { $, run, compile, chain, createTokens, responsive, tokens } from './rtt.js';
|
|
3
|
+
export { useChainStyles, useDynamicChainStyles, useThemeChainStyles, ChainCSSGlobal, withChainStyles, cx } from './react-hooks.js';
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { useMemo, useEffect, useRef, useState } from 'react';
|
|
1
|
+
import React, { useMemo, useEffect, useRef, useState } from 'react';
|
|
2
2
|
import { $, compile, chain } from './rtt';
|
|
3
|
+
|
|
3
4
|
const styleCache = new Map();
|
|
4
5
|
let styleSheet = null;
|
|
6
|
+
|
|
5
7
|
const initStyleSheet = () => {
|
|
6
8
|
if (typeof document === 'undefined') return null;
|
|
7
9
|
if (!styleSheet) {
|
|
@@ -18,31 +20,38 @@ const initStyleSheet = () => {
|
|
|
18
20
|
}
|
|
19
21
|
return styleSheet;
|
|
20
22
|
};
|
|
23
|
+
|
|
21
24
|
const updateStyles = (css) => {
|
|
22
25
|
const sheet = initStyleSheet();
|
|
23
26
|
if (sheet) {
|
|
24
27
|
sheet.textContent = css;
|
|
25
28
|
}
|
|
26
29
|
};
|
|
30
|
+
|
|
27
31
|
export function useChainStyles(styles, deps = [], options = {}) {
|
|
28
32
|
const {
|
|
29
33
|
cache = true,
|
|
30
34
|
namespace = 'chain',
|
|
31
35
|
watch = false
|
|
32
36
|
} = options;
|
|
37
|
+
|
|
33
38
|
const id = useRef(`chain-${Math.random().toString(36).substr(2, 9)}`);
|
|
34
39
|
const [classNames, setClassNames] = useState({});
|
|
40
|
+
|
|
35
41
|
const processed = useMemo(() => {
|
|
36
42
|
const resolvedStyles = typeof styles === 'function' ? styles() : styles;
|
|
37
43
|
if (!resolvedStyles || Object.keys(resolvedStyles).length === 0) {
|
|
38
44
|
return { classNames: {}, css: '' };
|
|
39
45
|
}
|
|
46
|
+
|
|
40
47
|
const cacheKey = JSON.stringify(resolvedStyles);
|
|
41
48
|
if (cache && styleCache.has(cacheKey)) {
|
|
42
49
|
return styleCache.get(cacheKey);
|
|
43
50
|
}
|
|
51
|
+
|
|
44
52
|
const newClassNames = {};
|
|
45
53
|
const compiledStyles = {};
|
|
54
|
+
|
|
46
55
|
Object.entries(resolvedStyles).forEach(([key, styleDef]) => {
|
|
47
56
|
const className = `${namespace}-${key}-${id.current}`;
|
|
48
57
|
const styleObj = typeof styleDef === 'function'
|
|
@@ -54,14 +63,17 @@ export function useChainStyles(styles, deps = [], options = {}) {
|
|
|
54
63
|
...styleObj
|
|
55
64
|
};
|
|
56
65
|
});
|
|
66
|
+
|
|
57
67
|
compile(compiledStyles);
|
|
58
68
|
const css = chain.cssOutput;
|
|
59
69
|
const result = { classNames: newClassNames, css };
|
|
70
|
+
|
|
60
71
|
if (cache) {
|
|
61
72
|
styleCache.set(cacheKey, result);
|
|
62
73
|
}
|
|
63
74
|
return result;
|
|
64
75
|
}, [styles, namespace, id.current, ...deps]);
|
|
76
|
+
|
|
65
77
|
useEffect(() => {
|
|
66
78
|
if (processed.css) {
|
|
67
79
|
if (!watch) {
|
|
@@ -76,6 +88,7 @@ export function useChainStyles(styles, deps = [], options = {}) {
|
|
|
76
88
|
updateStyles(processed.css);
|
|
77
89
|
}
|
|
78
90
|
}
|
|
91
|
+
|
|
79
92
|
return () => {
|
|
80
93
|
if (!watch && styleSheet) {
|
|
81
94
|
const existingStyles = styleSheet.textContent || '';
|
|
@@ -84,14 +97,17 @@ export function useChainStyles(styles, deps = [], options = {}) {
|
|
|
84
97
|
}
|
|
85
98
|
};
|
|
86
99
|
}, [processed.css, watch]);
|
|
100
|
+
|
|
87
101
|
return processed.classNames;
|
|
88
102
|
}
|
|
103
|
+
|
|
89
104
|
export function useDynamicChainStyles(styleFactory, deps = [], options = {}) {
|
|
90
105
|
const styles = useMemo(() => {
|
|
91
106
|
return styleFactory();
|
|
92
107
|
}, deps);
|
|
93
108
|
return useChainStyles(styles, options);
|
|
94
109
|
}
|
|
110
|
+
|
|
95
111
|
export function useThemeChainStyles(styles, theme, options = {}) {
|
|
96
112
|
const themedStyles = useMemo(() => {
|
|
97
113
|
if (typeof styles === 'function') {
|
|
@@ -101,19 +117,22 @@ export function useThemeChainStyles(styles, theme, options = {}) {
|
|
|
101
117
|
}, [styles, theme]);
|
|
102
118
|
return useChainStyles(themedStyles, options);
|
|
103
119
|
}
|
|
120
|
+
|
|
104
121
|
export function ChainCSSGlobal({ styles }) {
|
|
105
122
|
useChainStyles(styles, { watch: true });
|
|
106
123
|
return null;
|
|
107
124
|
}
|
|
125
|
+
|
|
108
126
|
export function withChainStyles(styles, options = {}) {
|
|
109
127
|
return function WrappedComponent(props) {
|
|
110
128
|
const classNames = useChainStyles(
|
|
111
129
|
typeof styles === 'function' ? styles(props) : styles,
|
|
112
130
|
options
|
|
113
131
|
);
|
|
114
|
-
return
|
|
132
|
+
return React.createElement(WrappedComponent, { ...props, chainStyles: classNames });
|
|
115
133
|
};
|
|
116
134
|
}
|
|
135
|
+
|
|
117
136
|
export function cx(...classes) {
|
|
118
137
|
return classes.filter(Boolean).join(' ');
|
|
119
138
|
}
|