Skip to content

Commit 6e0095d

Browse files
authored
CSS scroll state container queries and improved function handling (#4342)
* feat: add support for CSS scroll state container * Add support for CSS scroll state container queries. * Improve at-rule function handling. * chore: clean up scroll-state solution * Clean up scroll-state solution code for merge.
1 parent 01a187e commit 6e0095d

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

packages/less/src/less/parser/parser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,8 +1884,15 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
18841884
let e;
18851885
let p;
18861886
let rangeP;
1887+
let spacing = false;
18871888
parserInput.save();
18881889
do {
1890+
parserInput.save();
1891+
if (parserInput.$re(/^[0-9a-z-]*\s+\(/)) {
1892+
spacing = true;
1893+
}
1894+
parserInput.restore();
1895+
18891896
e = entities.declarationCall.bind(this)() || entities.keyword() || entities.variable() || entities.mixinLookup()
18901897
if (e) {
18911898
nodes.push(e);
@@ -1911,8 +1918,13 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
19111918
e = p;
19121919
} else if (p && e) {
19131920
nodes.push(new (tree.Paren)(new (tree.Declaration)(p, e, null, null, parserInput.i + currentIndex, fileInfo, true)));
1921+
if (!spacing) {
1922+
nodes[nodes.length - 1].noSpacing = true;
1923+
}
1924+
spacing = false;
19141925
} else if (e) {
19151926
nodes.push(new(tree.Paren)(e));
1927+
spacing = false;
19161928
} else {
19171929
error('badly formed media feature definition');
19181930
}

packages/less/src/less/tree/nested-at-rule.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,32 @@ const NestableAtRulePrototype = {
2020
}
2121
},
2222

23+
evalFunction: function () {
24+
if (!this.features || !Array.isArray(this.features.value) || this.features.value.length < 1) {
25+
return;
26+
}
27+
28+
const exprValues = this.features.value;
29+
let expr, paren;
30+
31+
for (let index = 0; index < exprValues.length; ++index) {
32+
expr = exprValues[index];
33+
34+
if (expr.type === 'Keyword' && index + 1 < exprValues.length) {
35+
paren = exprValues[index + 1];
36+
37+
if (paren.type === 'Paren' && paren.noSpacing) {
38+
exprValues[index]= new Expression([expr, paren]);
39+
exprValues.splice(index + 1, 1);
40+
exprValues[index].noSpacing = true;
41+
}
42+
}
43+
}
44+
},
45+
2346
evalTop(context) {
47+
this.evalFunction();
48+
2449
let result = this;
2550

2651
// Render all dependent Media blocks.
@@ -39,6 +64,8 @@ const NestableAtRulePrototype = {
3964
},
4065

4166
evalNested(context) {
67+
this.evalFunction();
68+
4269
let i;
4370
let value;
4471
const path = context.mediaPath.concat([this]);

packages/less/src/less/tree/paren.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ Paren.prototype = Object.assign(new Node(), {
1414
},
1515

1616
eval(context) {
17-
return new Paren(this.value.eval(context));
17+
const paren = new Paren(this.value.eval(context));
18+
19+
if (this.noSpacing) {
20+
paren.noSpacing = true;
21+
}
22+
23+
return paren;
1824
}
1925
});
2026

packages/test-data/css/_main/container.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,22 @@
241241
color: purple;
242242
}
243243
}
244+
#sticky {
245+
position: sticky;
246+
container-type: scroll-state;
247+
}
248+
@container scroll-state(stuck: top) {
249+
#sticky-child {
250+
font-size: 75%;
251+
}
252+
}
253+
@container scroll-state(snapped: x) {
254+
#sticky-child {
255+
font-size: 75%;
256+
}
257+
}
258+
@container scroll-state(scrollable: top) {
259+
#sticky-child {
260+
font-size: 75%;
261+
}
262+
}

packages/test-data/less/_main/container.less

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,26 @@
289289
color: purple;
290290
}
291291
}
292+
293+
#sticky {
294+
position: sticky;
295+
container-type: scroll-state;
296+
}
297+
298+
@container scroll-state(stuck: top) {
299+
#sticky-child {
300+
font-size: 75%;
301+
}
302+
}
303+
304+
@container scroll-state(snapped: x) {
305+
#sticky-child {
306+
font-size: 75%;
307+
}
308+
}
309+
310+
@container scroll-state(scrollable: top) {
311+
#sticky-child {
312+
font-size: 75%;
313+
}
314+
}

0 commit comments

Comments
 (0)