Skip to content

CSS scroll state container queries #4311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/less/src/less/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import string from './string';
import svg from './svg';
import types from './types';
import style from './style';
import scrollState from './scroll-state';

export default environment => {
const functions = { functionRegistry, functionCaller };
Expand All @@ -30,6 +31,7 @@ export default environment => {
functionRegistry.addMultiple(svg(environment));
functionRegistry.addMultiple(types);
functionRegistry.addMultiple(style);
functionRegistry.addMultiple(scrollState);

return functions;
};
23 changes: 23 additions & 0 deletions packages/less/src/less/functions/scroll-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Variable from '../tree/variable';
import Anonymous from '../tree/variable';

const scrollStateExpression = function (args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There have been a few PRs like this (I forget from whom) that to me, seem far too specific and as a result, are probably a bit bloated. The syntax of container queries should be mostly generic and aligned with media queries. That is, they should accept any property or custom property declaration. Scroll state is not special and should not be treated specially. Otherwise there would end up being n files like this related to every CSS property. Less is not a linter, and does not check to see if properties are valid, only that syntax is valid.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further to that, there should, ideally, be only one AtRule type for @container, @media, and @supports (and maybe more). Something that acts as a generic with roughly the same structure that has the same bubbling behavior. They wouldn't necessarily be parsed the same, but they would be stored the same (with the individual options specifying which actual at-rule it represents).

args = Array.prototype.slice.call(args);
switch (args.length) {
case 0: throw { type: 'Argument', message: 'one or more arguments required' };
}

const entityList = [new Variable(args[0].value, this.index, this.currentFileInfo).eval(this.context)];

args = entityList.map(a => { return a.toCSS(this.context); }).join(this.context.compress ? ',' : ', ');

return new Anonymous(`scroll-state(${args})`);
};

export default {
'scroll-state': function(...args) {
try {
return scrollStateExpression.call(this, args);
} catch (e) {}
},
};
2 changes: 1 addition & 1 deletion packages/less/src/less/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {

parserInput.save();

validCall = parserInput.$re(/^[\w]+\(/);
validCall = parserInput.$re(/^[\w-]+\(/);
if (!validCall) {
parserInput.forget();
return;
Expand Down
19 changes: 19 additions & 0 deletions packages/test-data/css/_main/container.css
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,22 @@
color: purple;
}
}
#sticky {
position: sticky;
container-type: scroll-state;
}
@container scroll-state(stuck: top) {
#sticky-child {
font-size: 75%;
}
}
@container scroll-state(snapped: x) {
#sticky-child {
font-size: 75%;
}
}
@container scroll-state(scrollable: top) {
#sticky-child {
font-size: 75%;
}
}
23 changes: 23 additions & 0 deletions packages/test-data/less/_main/container.less
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,26 @@
color: purple;
}
}

#sticky {
position: sticky;
container-type: scroll-state;
}

@container scroll-state(stuck: top) {
#sticky-child {
font-size: 75%;
}
}

@container scroll-state(snapped: x) {
#sticky-child {
font-size: 75%;
}
}

@container scroll-state(scrollable: top) {
#sticky-child {
font-size: 75%;
}
}