Skip to content

Commit 8bcc9fe

Browse files
committed
feat: inline variables
1 parent c5baaad commit 8bcc9fe

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ These are the features that are "done", in that they pass basic testing. More co
3030
- [x] Container named queries
3131
- [x] Container media queries
3232
- [x] Dynamic Variables
33+
- [x] Inline Variables
3334
- [x] Global variables w/ media queries
34-
- [x] Transform
35-
- [ ] Filter
3635
- [ ] Animations
3736
- [x] Transitions
37+
- [x] Transform
38+
- [ ] Filter
3839
- [x] Important styles
3940
- [x] Important props
4041
- [ ] Safe area units
41-
- [ ] Em
42-
- [ ] `currentColor`
42+
- [ ] Em & `currentColor`
4343
- [ ] CSS functions (min, max, platform functions, etc)
4444
- [ ] Metro
4545
- [ ] Update compiler to new syntax (switch tuples to objects)

cpp/StyledComputedFactory.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "StyleFunction.hpp"
77
#include "Specificity.hpp"
88
#include "StyleResolver.hpp"
9+
#include "VariableContext.hpp"
910

1011
#include <regex>
1112
#include <variant>
@@ -65,9 +66,13 @@ namespace margelo::nitro::cssnitro {
6566

6667
const std::vector<HybridStyleRule> &styleRules = get(*styleIt->second);
6768

68-
// Add all style rules to the collection
69-
allStyleRules.insert(allStyleRules.end(), styleRules.begin(),
70-
styleRules.end());
69+
// Add only style rules that pass the test
70+
for (const HybridStyleRule &styleRule: styleRules) {
71+
if (Rules::testRule(styleRule, get, componentId, containerScope,
72+
validAttributeQueries)) {
73+
allStyleRules.push_back(styleRule);
74+
}
75+
}
7176
}
7277

7378
// Sort all style rules by specificity (highest specificity first)
@@ -76,14 +81,19 @@ namespace margelo::nitro::cssnitro {
7681
return Specificity::sort(a.s, b.s);
7782
});
7883

79-
// Now process the sorted style rules
84+
// Process the inline variables
8085
for (const HybridStyleRule &styleRule: allStyleRules) {
81-
// Skip rule if its conditions are not valid
82-
if (!Rules::testRule(styleRule, get, componentId, containerScope,
83-
validAttributeQueries)) {
84-
continue;
86+
if (styleRule.v.has_value()) {
87+
const auto &inlineVariables = styleRule.v.value();
88+
for (const auto &kv: inlineVariables->getMap()) {
89+
VariableContext::setVariable(variableScope, kv.first, kv.second);
90+
}
8591
}
8692

93+
}
94+
95+
// Process the declarations
96+
for (const HybridStyleRule &styleRule: allStyleRules) {
8797
if (styleRule.d.has_value()) {
8898
const auto &declarations = styleRule.d.value();
8999

example/src/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ StyleRegistry.addStyleSheet({
1010
[
1111
{
1212
s: specificity({ className: 1 }),
13+
v: {
14+
"my-custom-color": "yellow",
15+
},
1316
d: [
1417
{
1518
color: ["fn", "var", "my-custom-color", "blue"],

0 commit comments

Comments
 (0)