[css-variables-2] Custom shorthands with `@property`

Open
#7,879 26 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
css
Domain
frontend, web-dev

Research direction

Start by reviewing the proposal in this issue and the related CSSWG issue #7273, then examine the surrounding discussion for constraints on custom shorthands, constituent properties, syntax, and keyword behavior. Done means reaching a resolved CSSWG design or specification decision, rather than making a localized code edit.

Written by the indexing model from the issue text.

Description

css-variables-2

Context :

Currently there is no way to reuse small groups of declarations (keys and values) in multiple styles without creating a style rule for them.

.want-to-reuse {
	color: hotpink;
	padding: 15px;
	display: flex;
}

To apply this to multiple components you need to alter your markup.
Or you need to copy/paste the declarations to each style rule.


Mixins are often used to work around this.
I know mixins will never be a thing in native CSS and maybe the same reasons behind that apply to this proposal.

@mixin some-mixin {
	color: hotpink;
	padding: 15px;
	display: flex;
}

selector {
	@include some-mixin;
}

Custom properties also help to some extend because they allow values to be re-used.
Naming conventions help to group sets of values.

:root {
	--some-color: purple;
	--some-padding: 20px;
	--some-display: flex;
	
	--fancy-color: hotpink;
	--fancy-padding: 15px;
	--fancy-display: flex;
}

Proposal :

Can custom shorthands be defined through @property?

  • syntax must match the declared constituent properties
  • keywords like initial work exactly like they work for native shorthands

I included padding in the example but maybe shorthand in shorthands have issues?

@property --some-shorthand {
	syntax: "<color>" "<length>" "<ident>";
	constituent-properties: color padding display;
	values:
		default / purple 20px flex,
		fancy / hotpink 15px flex;
}

.foo {
	--some-shorthand: fancy;
}

Is equivalent to :

.foo {
	color: hotpink;
	padding: 15px;
	display: flex;
}

Set values of constituent properties :

.foo {
	--some-shorthand: green 10px block;
}

This is somewhat related to https://github.com/w3c/csswg-drafts/issues/7273 as it allows similar patterns when styling elements.

Dominant language
Bikeshed
Stars
4.9k
Forks
816
Avg merge
2d 18h
Merged PRs (30d)
24

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from w3c/csswg-drafts

All issues in w3c/csswg-drafts

Similar issues

More Web Dev issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.