Skip to content
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

Implement --spacing(…), --alpha(…) and --theme(…) CSS functions #15572

Merged
merged 10 commits into from
Jan 8, 2025

Conversation

RobinMalfait
Copy link
Member

This PR implements new CSS functions that you can use in your CSS (or even in arbitrary value position).

For starters, we renamed the theme(…) function to --theme(…). The legacy theme(…) function is still available for backwards compatibility reasons, but this allows us to be future proof since --foo(…) is the syntax the CSS spec recommends. See: https://drafts.csswg.org/css-mixins/

In addition, this PR implements a new --spacing(…) function, this allows you to write:

@import "tailwindcss";

@theme {
  --spacing: 0.25rem;
}

.foo {
  margin: --spacing(4):
}

This is syntax sugar over:

@import "tailwindcss";

@theme {
  --spacing: 0.25rem;
}

.foo {
  margin: calc(var(--spacing) * 4);
}

If your @theme uses the inline keyword, we will also make sure to inline the value:

@import "tailwindcss";

@theme inline {
  --spacing: 0.25rem;
}

.foo {
  margin: --spacing(4):
}

Boils down to:

@import "tailwindcss";

@theme {
  --spacing: 0.25rem;
}

.foo {
  margin: calc(0.25rem * 4); /* And will be optimised to just 1rem */
}

Another new function function we added is the --alpha(…) function that requires a value, and a number / percentage value. This allows you to apply an alpha value to any color, but with a much shorter syntax:

@import "tailwindcss";

.foo {
  color: --alpha(var(--color-red-500), 0.5);
}

This is syntax sugar over:

@import "tailwindcss";

.foo {
  color: color-mix(in oklab, var(--color-red-500) 50%, transparent);
}

RobinMalfait and others added 5 commits January 8, 2025 16:48
Co-authored-by: Philipp Spiess <[email protected]>
Co-authored-by: Jordan Pittman <[email protected]>
Co-authored-by: Philipp Spiess <[email protected]>
Co-authored-by: Jordan Pittman <[email protected]>
Co-authored-by: Philipp Spiess <[email protected]>
Co-authored-by: Jordan Pittman <[email protected]>
Co-authored-by: Philipp Spiess <[email protected]>
Co-authored-by: Jordan Pittman <[email protected]>
@RobinMalfait RobinMalfait requested a review from a team as a code owner January 8, 2025 16:21
CHANGELOG.md Outdated
@@ -790,3 +791,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [4.0.0-alpha.1] - 2024-03-06

- First 4.0.0-alpha.1 release

Copy link
Contributor

Choose a reason for hiding this comment

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

lol this came back

Copy link
Member Author

Choose a reason for hiding this comment

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

🙃

CHANGELOG.md Outdated Show resolved Hide resolved

let multiplier = designSystem.theme.resolve(null, ['--spacing'])
if (!multiplier) {
throw new Error('--spacing(…) depends on the `--spacing` theme value, but it was not found.')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new Error('--spacing(…) depends on the `--spacing` theme value, but it was not found.')
throw new Error('The --spacing(…) function requires that the `--spacing` theme variable be set, but it was not found.')

Co-authored-by: Jonathan Reinink <[email protected]>
@RobinMalfait RobinMalfait force-pushed the feat/add-new-css-functions branch from 79e11c5 to b995a5b Compare January 8, 2025 19:47
@RobinMalfait RobinMalfait enabled auto-merge (squash) January 8, 2025 20:14
@RobinMalfait RobinMalfait merged commit 8d03db8 into next Jan 8, 2025
5 checks passed
@RobinMalfait RobinMalfait deleted the feat/add-new-css-functions branch January 8, 2025 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants