[css-conditional] Custom `@if` conditions via JavaScript
Nobody has claimed this yet.
- Dominant language
- Bikeshed
- Stars
- 4.9k
- Forks
- 816
- PR merge metrics
- PR metrics pending
Description
In #3559 we were searching for a way to check for contextual support of features like gap in Flexbox, align-content in Block layout and similar things.
At some point in the discussion I then came up with the idea to solve this via of script-based custom @supports rules. I also provided an example for how those could be used to check whether gap is working in the context of a Flex layout.
@fantasai then pointed out in a call that those conditional checks can be thought broader and might be added to the @when (she said @if back then) instead.
The concept behind that is to allow to register a named condition worklet, which can then be used as condition in @when rules. Its way of working would be similar to other worklets like Paint Worklets.
This makes conditionally applying styles very versatile and covers a lot of use cases like the mentioned contextual feature check, time- and date-based styling, allow to apply styles based on scroll direction, or might even be a way to solve layout-dependent styling.
The concrete proposal is to add a worklet that looks like this:
class Condition {
static get inputProperties() { return []; }
static get inputArguments() { return []; }
static get contextOptions() { return {once: true, currentDocument: false}; }
check(document, styleMap) {
// Do check
}
}
registerCondition('--condition', Condition);
Where the context option once: true means the check is only executed a single time right after the rule is parsed. So it works like an @supports check. If it is set to false, the check is executed continuously like in different @media conditions.
Maybe instead of running continuously, some way to execute only once specific DOM properties change could be introduced. E.g. only execute once document.location changes.
The context option currentDocument indicates whether the document parameter of the check() method refers to the current page's document or is independent from it.
For some use cases like feature support checks you only need access to an independent DOM. Though in many use cases access to the current DOM is necessary.
Both of those options definitely require some deeper thought on performance, privacy, and security.
The worklet can then registered like that:
CSS.conditionWorklet.addModule('condition.js');
And finally, in CSS you'll use it like this:
@when --condition {}
Sebastian
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the CSS Conditional Level 5 @when rule and the linked CSSWG issue on script-based custom @supports rules. Evaluate the proposed condition worklet, its context options, and CSS.conditionWorklet.addModule() against the stated performance, privacy, and security concerns; done means a settled, implementable specification proposal.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- css, javascript
- Domain
- frontend, web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100