@stylexjs/shared 0.2.0-beta.8 → 0.2.0-beta.9
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +81 -0
- package/lib/expand-shorthands.js +246 -303
- package/lib/generate-css-rule.js +6 -34
- package/lib/index.d.ts +1 -1
- package/lib/index.js.flow +5 -0
- package/lib/messages.js +3 -5
- package/lib/namespace-transforms/__tests__/preflatten.test.js +120 -0
- package/lib/namespace-transforms/preflatten.js +89 -0
- package/lib/preprocess-rules/application-order.js +259 -0
- package/lib/preprocess-rules/expand-shorthands.js +156 -0
- package/lib/preprocess-rules/index.js +39 -0
- package/lib/preprocess-rules/legacy-expand-shorthands.js +169 -0
- package/lib/preprocess-rules/null-out-longhand.js +310 -0
- package/lib/preprocess-rules/property-specificity.js +135 -0
- package/lib/preprocess-rules/react-native-web.js +142 -0
- package/lib/stylex-create.js +17 -13
- package/lib/stylex-keyframes.js +22 -10
- package/lib/utils/Rule.js +71 -0
- package/lib/utils/normalize-value.js +3 -0
- package/lib/utils/property-priorities.js +116 -0
- package/lib/utils/split-css-value.js +47 -0
- package/package.json +1 -1
package/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# @stylexjs/shared
|
2
|
+
|
3
|
+
This package contains most of the core JavaScript logic for stylex.
|
4
|
+
|
5
|
+
It exports two primary functions `create` and `keyframes`.
|
6
|
+
|
7
|
+
1. `create` - takes a map of style rules. The return value include: a) the map with each style value replaced by a unique, reproducible, hashed className string, and b) a list of the CSS styles to be inserted into the document.
|
8
|
+
2. `keyframes` - takes a `@keyframes` animation as JS object. Returns a hashed string and the style ot be injected.
|
9
|
+
|
10
|
+
#### ⭐️ `create`
|
11
|
+
|
12
|
+
The `stylex.create` function is implemented here and can be found within `stylex-create.js` and is the default export of a function named `styleXCreateSet(...)`.
|
13
|
+
|
14
|
+
##### `styleXCreateSet(...)`
|
15
|
+
|
16
|
+
> The function is called `styleXCreateSet` because `stylex.create` transforms a "set" or collection of multiple style [namespaces](#namespace)
|
17
|
+
|
18
|
+
This function itself mostly just traverses over the objects to run each [namespaces](#namespace) through the `styleXCreateNamespace(...)` function. Other than that, it takes the styles to be injected from each namespace in a [Namespace Set](#namespace-set) and deduplicates them so the style isn't injected multiple times if it's used within multiple Namespaces in the same set.
|
19
|
+
|
20
|
+
##### `styleXCreateNamespace(...)`
|
21
|
+
|
22
|
+
> This function has been kept separate in case we want to add a new function to the StyleX API in the future called `stylex.createOne` which transforms a single [namespace](#namespace) instead of a [Namespace Set](#namespace-set)
|
23
|
+
|
24
|
+
This function is responsible to transforming a [namespace](#namespace) to a [Compiled Namespace](#compiled-namespace) by hashing each key value pair and returning an object where the values have been replaced by classNames.
|
25
|
+
|
26
|
+
**Step 1**
|
27
|
+
|
28
|
+
The first step here is expanding all [shorthands](#shorthands) into their individual properties. To do this we `.flatMap` over the object entries of the Namespace and use the `expandShorthands(...)` function defined within `expand-shorthands.js`
|
29
|
+
|
30
|
+
**Step 2**
|
31
|
+
|
32
|
+
We hash each style `[key, value]` pair and generate a className and an associated CSS rule. Thie is done in the `convertToClassName(...)` function defined within [`convert-to-className.js`](#convert-to-classname-shared-package). (Explained below)
|
33
|
+
|
34
|
+
**Step 3**
|
35
|
+
|
36
|
+
Using the classNames generated in _step 2_ above, we collect all the individual style keys along with their associated classNames in the `resolvedNamespace` object.
|
37
|
+
|
38
|
+
All the generated CSS rules from _step 2_ are collected in the `injectedStyles` object.
|
39
|
+
|
40
|
+
The `[resolvedNamespace, injectedStyles]` is returned.
|
41
|
+
|
42
|
+
##### Back to `styleXCreateSet(...)`
|
43
|
+
|
44
|
+
`styleXCreateSet(...)` takes all the `[resolvedNamespace, finalInjectedStyles]` tuples and returns a tuple of `[compiledNamespaceSet, allInjectedStyles]`
|
45
|
+
|
46
|
+
### Back to `create` with the `@stylexjs/babel-plugin` package
|
47
|
+
|
48
|
+
The `create` function within the babel plugin package takes the `stylex.create(...)` function call and replaces it with the `compiledNamespaceSet`.
|
49
|
+
|
50
|
+
It also takes each of the `injectedStyles` and:
|
51
|
+
|
52
|
+
1. Either injects it as a `stylex.inject` call (if in `dev` mode)
|
53
|
+
2. Or, adds it to the array of injected styles on [`babel.state.metadata`](#babel-metadata)
|
54
|
+
|
55
|
+
#### ⭐️ `keyframes`
|
56
|
+
|
57
|
+
This is the function backing `stylex.keyframes`. It works similarly to `create` but it's more simplified since it only defines a single CSS `@keyframes` rule and returns a single string.
|
58
|
+
|
59
|
+
Here again, the source AST is converted to a JS object and passed to `stylex-keyframes.js` within the `shared` package.
|
60
|
+
|
61
|
+
There, first the shorthands are expanded and then the whole objects is hashed. The resulting hash is used as the generated `animation name` for a `@keyframes` rule.
|
62
|
+
|
63
|
+
The "name" and the CSS `@keyframes` rules are returned as a tuple.
|
64
|
+
|
65
|
+
The `stylex.keyframes` call is replaced with the final string.
|
66
|
+
|
67
|
+
The CSS `@keyframes` rule is either injected using `stylex.inject` in dev mode or set onto the `stylex` array on [`babel.state.metadata`](#babel-metadata).
|
68
|
+
|
69
|
+
#### `convert-to-className` (`shared` package)
|
70
|
+
|
71
|
+
This function is responsible for converting a single style key-value pair into a tuple of `[key, className, CSSRules]`
|
72
|
+
|
73
|
+
It does so in the following steps:
|
74
|
+
|
75
|
+
1. Convert the camelCased keys that are used by end-users to define [Namespaces](#namespace) and convert them to the dash-separated keys used within CSS.
|
76
|
+
2. Hash `key` + (any `pseudo` or `at-rule`) + `value` to generate a className
|
77
|
+
3. Generate the CSS rule using the `generateCSSRule` function defined in [`generate-css-rule.js`](#generate-css-rulejs) in the `shared` package.
|
78
|
+
|
79
|
+
#### `generate-css-rule.js`
|
80
|
+
|
81
|
+
This function takes a CSS key value pair, checks if has an RTL counterpart and returns them along side a pre-configured priority based on the type of CSS rule it is.
|