@stylexjs/stylex 0.12.0 → 0.13.0

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 (66) hide show
  1. package/lib/{stylex-inject.d.ts → cjs/inject.d.ts} +1 -5
  2. package/lib/cjs/inject.js +210 -0
  3. package/lib/{stylex-inject.js.flow → cjs/inject.js.flow} +2 -3
  4. package/lib/cjs/stylesheet/createCSSStyleSheet.d.ts +13 -0
  5. package/lib/cjs/stylesheet/createCSSStyleSheet.js.flow +13 -0
  6. package/lib/cjs/stylesheet/createOrderedCSSStyleSheet.d.ts +31 -0
  7. package/lib/cjs/stylesheet/createOrderedCSSStyleSheet.js.flow +32 -0
  8. package/lib/cjs/stylesheet/createSheet.d.ts +12 -0
  9. package/lib/cjs/stylesheet/createSheet.js.flow +16 -0
  10. package/lib/cjs/stylesheet/utils.d.ts +18 -0
  11. package/lib/cjs/stylesheet/utils.js.flow +19 -0
  12. package/lib/{stylex.d.ts → cjs/stylex.d.ts} +8 -21
  13. package/lib/cjs/stylex.js +242 -0
  14. package/lib/{stylex.js.flow → cjs/stylex.js.flow} +10 -26
  15. package/lib/{StyleXCSSTypes.d.ts → cjs/types/StyleXCSSTypes.d.ts} +22 -0
  16. package/lib/{StyleXCSSTypes.js.flow → cjs/types/StyleXCSSTypes.js.flow} +25 -0
  17. package/lib/{StyleXTypes.d.ts → cjs/types/StyleXTypes.d.ts} +60 -5
  18. package/lib/{StyleXTypes.js.flow → cjs/types/StyleXTypes.js.flow} +54 -3
  19. package/lib/{VarTypes.d.ts → cjs/types/VarTypes.d.ts} +19 -18
  20. package/lib/{VarTypes.js.flow → cjs/types/VarTypes.js.flow} +17 -19
  21. package/lib/es/inject.d.ts +11 -0
  22. package/lib/es/inject.js.flow +13 -0
  23. package/lib/es/inject.mjs +208 -0
  24. package/lib/es/stylesheet/createCSSStyleSheet.d.ts +13 -0
  25. package/lib/es/stylesheet/createCSSStyleSheet.js.flow +13 -0
  26. package/lib/es/stylesheet/createOrderedCSSStyleSheet.d.ts +31 -0
  27. package/lib/es/stylesheet/createOrderedCSSStyleSheet.js.flow +32 -0
  28. package/lib/es/stylesheet/createSheet.d.ts +12 -0
  29. package/lib/es/stylesheet/createSheet.js.flow +16 -0
  30. package/lib/es/stylesheet/utils.d.ts +18 -0
  31. package/lib/es/stylesheet/utils.js.flow +19 -0
  32. package/lib/es/stylex.d.ts +135 -0
  33. package/lib/es/stylex.js.flow +134 -0
  34. package/lib/es/stylex.mjs +8 -20
  35. package/lib/es/types/StyleXCSSTypes.d.ts +1487 -0
  36. package/lib/es/types/StyleXCSSTypes.js.flow +1580 -0
  37. package/lib/es/types/StyleXOpaqueTypes.d.ts +11 -0
  38. package/lib/es/types/StyleXOpaqueTypes.js.flow +16 -0
  39. package/lib/es/types/StyleXTypes.d.ts +292 -0
  40. package/lib/es/types/StyleXTypes.js.flow +240 -0
  41. package/lib/es/types/StyleXUtils.d.ts +15 -0
  42. package/lib/es/types/StyleXUtils.js.flow +15 -0
  43. package/lib/es/types/VarTypes.d.ts +104 -0
  44. package/lib/es/types/VarTypes.js.flow +102 -0
  45. package/package.json +15 -26
  46. package/lib/StyleXCSSTypes.js +0 -1
  47. package/lib/StyleXOpaqueTypes.js +0 -1
  48. package/lib/StyleXSheet.d.ts +0 -49
  49. package/lib/StyleXSheet.js +0 -188
  50. package/lib/StyleXSheet.js.flow +0 -49
  51. package/lib/StyleXTypes.js +0 -1
  52. package/lib/VarTypes.js +0 -1
  53. package/lib/es/StyleXCSSTypes.mjs +0 -1
  54. package/lib/es/StyleXOpaqueTypes.mjs +0 -0
  55. package/lib/es/StyleXSheet.mjs +0 -247
  56. package/lib/es/StyleXTypes.mjs +0 -1
  57. package/lib/es/VarTypes.mjs +0 -0
  58. package/lib/es/stylex-inject.mjs +0 -5
  59. package/lib/es/util-types.mjs +0 -0
  60. package/lib/stylex-inject.js +0 -11
  61. package/lib/stylex.js +0 -125
  62. package/lib/util-types.js +0 -1
  63. /package/lib/{StyleXOpaqueTypes.d.ts → cjs/types/StyleXOpaqueTypes.d.ts} +0 -0
  64. /package/lib/{StyleXOpaqueTypes.js.flow → cjs/types/StyleXOpaqueTypes.js.flow} +0 -0
  65. /package/lib/{util-types.d.ts → cjs/types/StyleXUtils.d.ts} +0 -0
  66. /package/lib/{util-types.js.flow → cjs/types/StyleXUtils.js.flow} +0 -0
@@ -7,9 +7,5 @@
7
7
  *
8
8
  */
9
9
 
10
- declare function inject(
11
- ltrRule: string,
12
- priority: number,
13
- rtlRule: null | undefined | string,
14
- ): void;
10
+ declare function inject(cssText: string, priority: number): string;
15
11
  export default inject;
@@ -0,0 +1,210 @@
1
+ 'use strict';
2
+
3
+ const canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
4
+ function addSpecificityLevel(cssText, index) {
5
+ if (cssText.startsWith('@keyframes')) {
6
+ return cssText;
7
+ }
8
+ const pseudoSelector = Array.from({
9
+ length: index
10
+ }).map(() => ':not(#\\#)').join('');
11
+ const lastOpenCurly = cssText.includes('::') ? cssText.indexOf('::') : cssText.lastIndexOf('{');
12
+ const beforeCurly = cssText.slice(0, lastOpenCurly);
13
+ const afterCurly = cssText.slice(lastOpenCurly);
14
+ return `${beforeCurly}${pseudoSelector}${afterCurly}`;
15
+ }
16
+
17
+ function createCSSStyleSheet(rootNode, textContent) {
18
+ if (canUseDOM) {
19
+ const root = rootNode != null ? rootNode : document;
20
+ let element = root?.querySelector('[data-stylex]');
21
+ if (element == null) {
22
+ element = document.createElement('style');
23
+ element.setAttribute('data-stylex', 'true');
24
+ if (typeof textContent === 'string') {
25
+ element.appendChild(document.createTextNode(textContent));
26
+ }
27
+ const container = root.nodeType === Node.DOCUMENT_NODE ? root.head : root;
28
+ if (container) {
29
+ const firstChild = container.firstChild;
30
+ if (firstChild != null) {
31
+ container.insertBefore(element, firstChild);
32
+ } else {
33
+ container.appendChild(element);
34
+ }
35
+ }
36
+ }
37
+ return element.sheet;
38
+ } else {
39
+ return null;
40
+ }
41
+ }
42
+
43
+ function createOrderedCSSStyleSheet(sheet) {
44
+ const groups = {};
45
+ const seenRules = {};
46
+ if (sheet != null) {
47
+ let group = 0;
48
+ for (let i = 0; i < sheet.cssRules.length; i++) {
49
+ const cssRule = sheet.cssRules[i];
50
+ const cssText = cssRule.cssText;
51
+ if (cssText.indexOf('stylesheet-group') > -1) {
52
+ group = decodeGroupRule(cssRule);
53
+ groups[group] = {
54
+ start: i,
55
+ rules: [cssText]
56
+ };
57
+ } else {
58
+ const key = getSeenRuleKey(cssText);
59
+ if (key != null) {
60
+ seenRules[key] = true;
61
+ groups[group].rules.push(cssText);
62
+ }
63
+ }
64
+ }
65
+ }
66
+ function sheetInsert(sheet, group, text) {
67
+ const orderedGroups = getOrderedGroups(groups);
68
+ const groupIndex = orderedGroups.indexOf(group);
69
+ const nextGroupIndex = groupIndex + 1;
70
+ const nextGroup = orderedGroups[nextGroupIndex];
71
+ const position = nextGroup != null && groups[nextGroup].start != null ? groups[nextGroup].start : sheet.cssRules.length;
72
+ const isInserted = insertRuleAt(sheet, text, position);
73
+ if (isInserted) {
74
+ if (groups[group].start == null) {
75
+ groups[group].start = position;
76
+ }
77
+ for (let i = nextGroupIndex; i < orderedGroups.length; i += 1) {
78
+ const groupNumber = orderedGroups[i];
79
+ const previousStart = groups[groupNumber].start || 0;
80
+ groups[groupNumber].start = previousStart + 1;
81
+ }
82
+ }
83
+ return isInserted;
84
+ }
85
+ const OrderedCSSStyleSheet = {
86
+ getTextContent() {
87
+ return getOrderedGroups(groups).map(group => {
88
+ const rules = groups[group].rules;
89
+ const marker = rules.shift();
90
+ rules.sort();
91
+ if (marker !== undefined) {
92
+ rules.unshift(marker);
93
+ }
94
+ return rules.join('\n');
95
+ }).join('\n');
96
+ },
97
+ insert(cssText, groupValue) {
98
+ const group = Number(groupValue);
99
+ if (groups[group] == null) {
100
+ const markerRule = encodeGroupRule(group);
101
+ groups[group] = {
102
+ start: null,
103
+ rules: [markerRule]
104
+ };
105
+ if (sheet != null) {
106
+ sheetInsert(sheet, group, markerRule);
107
+ }
108
+ }
109
+ const key = getSeenRuleKey(cssText);
110
+ if (key != null && seenRules[key] == null) {
111
+ seenRules[key] = true;
112
+ let shouldUpdate = true;
113
+ if (sheet != null) {
114
+ const isInserted = sheetInsert(sheet, group, cssText);
115
+ if (!isInserted) {
116
+ shouldUpdate = false;
117
+ }
118
+ }
119
+ if (shouldUpdate) {
120
+ groups[group].rules.push(cssText);
121
+ }
122
+ }
123
+ }
124
+ };
125
+ return OrderedCSSStyleSheet;
126
+ }
127
+ function encodeGroupRule(group) {
128
+ return `[stylesheet-group="${group}"]{}`;
129
+ }
130
+ const groupPattern = /["']/g;
131
+ function decodeGroupRule(cssRule) {
132
+ return Number(cssRule.selectorText.split(groupPattern)[1]);
133
+ }
134
+ function getOrderedGroups(obj) {
135
+ return Object.keys(obj).map(Number).sort((a, b) => a > b ? 1 : -1);
136
+ }
137
+ const selectorPattern = /\s*([,])\s*/g;
138
+ const conditionalRulePattern = /^@(media|supports|container)\s*\([^)]+\)\s*{/;
139
+ function getSeenRuleKey(cssText) {
140
+ if (conditionalRulePattern.test(cssText)) {
141
+ const index = cssText.indexOf('{');
142
+ const query = cssText.substring(0, index).trim();
143
+ const rest = cssText.substring(index + 1).trim();
144
+ const next = getSeenRuleKey(rest);
145
+ const normalizedNext = next !== null && next !== '' ? next.replace(selectorPattern, '$1') : '';
146
+ return `${query} { ${normalizedNext}`;
147
+ } else {
148
+ const selector = cssText.split('{')[0].trim();
149
+ return selector !== '' ? selector.replace(selectorPattern, '$1') : null;
150
+ }
151
+ }
152
+ function insertRuleAt(root, cssText, position) {
153
+ try {
154
+ root.insertRule(cssText, position);
155
+ return true;
156
+ } catch (e) {
157
+ return false;
158
+ }
159
+ }
160
+
161
+ const roots = new WeakMap();
162
+ const sheets = [];
163
+ function createSheet(root) {
164
+ let sheet;
165
+ if (canUseDOM) {
166
+ const rootNode = document;
167
+ if (sheets.length === 0) {
168
+ sheet = createOrderedCSSStyleSheet(createCSSStyleSheet(rootNode));
169
+ roots.set(rootNode, sheets.length);
170
+ sheets.push(sheet);
171
+ } else {
172
+ const index = roots.get(rootNode);
173
+ if (index == null) {
174
+ const initialSheet = sheets[0];
175
+ const textContent = initialSheet != null ? initialSheet.getTextContent() : '';
176
+ sheet = createOrderedCSSStyleSheet(createCSSStyleSheet(rootNode, textContent));
177
+ roots.set(rootNode, sheets.length);
178
+ sheets.push(sheet);
179
+ } else {
180
+ sheet = sheets[index];
181
+ }
182
+ }
183
+ } else {
184
+ if (sheets.length === 0) {
185
+ sheet = createOrderedCSSStyleSheet(createCSSStyleSheet());
186
+ sheets.push(sheet);
187
+ } else {
188
+ sheet = sheets[0];
189
+ }
190
+ }
191
+ return {
192
+ getTextContent() {
193
+ return sheet.getTextContent();
194
+ },
195
+ insert(cssText, groupValue) {
196
+ sheets.forEach(s => {
197
+ s.insert(cssText, groupValue);
198
+ });
199
+ }
200
+ };
201
+ }
202
+
203
+ const sheet = createSheet();
204
+ function inject(cssText, priority) {
205
+ const text = addSpecificityLevel(cssText, Math.floor(priority / 1000));
206
+ sheet.insert(text, priority);
207
+ return text;
208
+ }
209
+
210
+ module.exports = inject;
@@ -8,7 +8,6 @@
8
8
  */
9
9
 
10
10
  declare export default function inject(
11
- ltrRule: string,
11
+ cssText: string,
12
12
  priority: number,
13
- rtlRule: ?string,
14
- ): void;
13
+ ): string;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ export declare function createCSSStyleSheet(
11
+ rootNode?: Node,
12
+ textContent?: string,
13
+ ): null | undefined | CSSStyleSheet;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ declare export function createCSSStyleSheet(
11
+ rootNode?: Node,
12
+ textContent?: string,
13
+ ): ?CSSStyleSheet;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ export type OrderedCSSStyleSheet = {
11
+ getTextContent: () => string;
12
+ insert: (cssText: string, groupValue: number) => void;
13
+ };
14
+ /**
15
+ * Order-based insertion of CSS.
16
+ *
17
+ * Each rule is associated with a numerically defined group.
18
+ * Groups are ordered within the style sheet according to their number, with the
19
+ * lowest first.
20
+ *
21
+ * Groups are implemented using marker rules. The selector of the first rule of
22
+ * each group is used only to encode the group number for hydration. An
23
+ * alternative implementation could rely on CSSMediaRule, allowing groups to be
24
+ * treated as a sub-sheet, but the Edge implementation of CSSMediaRule is
25
+ * broken.
26
+ * https://developer.mozilla.org/en-US/docs/Web/API/CSSMediaRule
27
+ * https://gist.github.com/necolas/aa0c37846ad6bd3b05b727b959e82674
28
+ */
29
+ export declare function createOrderedCSSStyleSheet(
30
+ sheet: null | undefined | CSSStyleSheet,
31
+ ): OrderedCSSStyleSheet;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ export type OrderedCSSStyleSheet = {|
11
+ getTextContent: () => string,
12
+ insert: (cssText: string, groupValue: number) => void,
13
+ |};
14
+
15
+ /**
16
+ * Order-based insertion of CSS.
17
+ *
18
+ * Each rule is associated with a numerically defined group.
19
+ * Groups are ordered within the style sheet according to their number, with the
20
+ * lowest first.
21
+ *
22
+ * Groups are implemented using marker rules. The selector of the first rule of
23
+ * each group is used only to encode the group number for hydration. An
24
+ * alternative implementation could rely on CSSMediaRule, allowing groups to be
25
+ * treated as a sub-sheet, but the Edge implementation of CSSMediaRule is
26
+ * broken.
27
+ * https://developer.mozilla.org/en-US/docs/Web/API/CSSMediaRule
28
+ * https://gist.github.com/necolas/aa0c37846ad6bd3b05b727b959e82674
29
+ */
30
+ declare export function createOrderedCSSStyleSheet(
31
+ sheet: ?CSSStyleSheet,
32
+ ): OrderedCSSStyleSheet;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ import type { OrderedCSSStyleSheet } from './createOrderedCSSStyleSheet';
11
+ type Sheet = Omit<OrderedCSSStyleSheet, keyof ({})> & {};
12
+ export declare function createSheet(root?: HTMLElement): Sheet;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ import type { OrderedCSSStyleSheet } from './createOrderedCSSStyleSheet';
11
+
12
+ type Sheet = {
13
+ ...OrderedCSSStyleSheet,
14
+ };
15
+
16
+ declare export function createSheet(root?: HTMLElement): Sheet;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ export declare const canUseDOM: boolean;
11
+ /**
12
+ * Adds :not(#\#) to selectors to increase their specificity.
13
+ * This is used to polyfill @layer
14
+ */
15
+ export declare function addSpecificityLevel(
16
+ cssText: string,
17
+ index: number,
18
+ ): string;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ declare export const canUseDOM: boolean;
11
+
12
+ /**
13
+ * Adds :not(#\#) to selectors to increase their specificity.
14
+ * This is used to polyfill @layer
15
+ */
16
+ declare export function addSpecificityLevel(
17
+ cssText: string,
18
+ index: number,
19
+ ): string;
@@ -16,14 +16,16 @@ import type {
16
16
  StyleX$Create,
17
17
  StyleX$CreateTheme,
18
18
  StyleX$DefineVars,
19
+ StyleX$DefineConsts,
19
20
  StyleXArray,
20
21
  StyleXStyles,
21
22
  StyleXStylesWithout,
22
23
  Theme,
23
24
  VarGroup,
24
- } from './StyleXTypes';
25
- import type { ValueWithDefault } from './util-types';
26
- import * as Types from './VarTypes';
25
+ PositionTry,
26
+ } from './types/StyleXTypes';
27
+ import type { ValueWithDefault } from './types/StyleXUtils';
28
+ import * as Types from './types/VarTypes';
27
29
  export type {
28
30
  StaticStyles,
29
31
  StaticStylesWithout,
@@ -33,22 +35,15 @@ export type {
33
35
  Types,
34
36
  VarGroup,
35
37
  };
36
- export declare function attrs(
37
- ...styles: ReadonlyArray<
38
- StyleXArray<
39
- | (null | undefined | CompiledStyles)
40
- | boolean
41
- | Readonly<[CompiledStyles, InlineStyles]>
42
- >
43
- >
44
- ): Readonly<{ class?: string; 'data-style-src'?: string; style?: string }>;
45
38
  export declare const create: StyleX$Create;
46
39
  export declare const createTheme: StyleX$CreateTheme;
40
+ export declare const defineConsts: StyleX$DefineConsts;
47
41
  export declare const defineVars: StyleX$DefineVars;
48
42
  export declare const firstThatWorks: <T extends string | number>(
49
43
  ..._styles: ReadonlyArray<T>
50
44
  ) => ReadonlyArray<T>;
51
45
  export declare const keyframes: (_keyframes: Keyframes) => string;
46
+ export declare const positionTry: (_positionTry: PositionTry) => string;
52
47
  export declare function props(
53
48
  this: null | undefined | unknown,
54
49
  ...styles: ReadonlyArray<
@@ -112,15 +107,6 @@ type IStyleX = {
112
107
  StyleXArray<(null | undefined | CompiledStyles) | boolean>
113
108
  >
114
109
  ): string;
115
- attrs: (
116
- ...styles: ReadonlyArray<
117
- StyleXArray<
118
- | (null | undefined | CompiledStyles)
119
- | boolean
120
- | Readonly<[CompiledStyles, InlineStyles]>
121
- >
122
- >
123
- ) => Readonly<{ class?: string; 'data-style-src'?: string; style?: string }>;
124
110
  create: StyleX$Create;
125
111
  createTheme: StyleX$CreateTheme;
126
112
  defineVars: StyleX$DefineVars;
@@ -128,6 +114,7 @@ type IStyleX = {
128
114
  ...v: ReadonlyArray<T>
129
115
  ) => ReadonlyArray<T>;
130
116
  keyframes: (keyframes: Keyframes) => string;
117
+ positionTry: (positionTry: PositionTry) => string;
131
118
  props: (
132
119
  this: null | undefined | unknown,
133
120
  ...styles: ReadonlyArray<