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

Add support for defining simple custom utilities in CSS via @utility #14044

Merged
merged 30 commits into from
Jul 24, 2024

Conversation

thecrypticace
Copy link
Contributor

This PR allows you to add custom utilities to your project via the new @utility rule.

For example, given the following:

@utility text-trim {
  text-box-trim: both;
  text-box-edge: cap alphabetic;
}

A new text-trim utility is available and can be used directly or with variants:

<div class="text-trim">...</div>
<div class="hover:text-trim">...</div>
<div class="lg:dark:text-trim">...</div>

If a utility is defined more than one time the latest definition will be used:

@utility text-trim {
  text-box-trim: both;
  text-box-edge: cap alphabetic;
}

@utility text-trim {
  text-box-trim: both;
  text-box-edge: cap ideographic;
}

Then using text-trim will produce the following CSS:

.text-trim {
  text-box-trim: both;
  text-box-edge: cap ideographic;
}

You may also override specific existing utilities with this — for example to add a text-rendering property to the text-sm utility:

@utility text-sm {
  font-size: var(--font-size-sm, 0.875rem);
  line-height: var(--font-size-sm--line-height, 1.25rem);
  text-rendering: optimizeLegibility;
}

Though it's preferred, should you not need to add properties, to override the theme instead.

Lastly, utilities with special characters do not need to be escaped like you would for a class name in a selector:

@utility push-1/2 {
  right: 50%;
}

We do however explicitly error on certain patterns that we want to reserve for future use, for example push-* and push-[15px].

packages/tailwindcss/src/index.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/index.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/compile.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/index.ts Show resolved Hide resolved
packages/tailwindcss/src/utilities.test.ts Show resolved Hide resolved
packages/tailwindcss/src/utilities.ts Outdated Show resolved Hide resolved
@thecrypticace thecrypticace merged commit f0e9343 into next Jul 24, 2024
1 check passed
@thecrypticace thecrypticace deleted the custom-css-utilities branch July 24, 2024 15:07
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.

4 participants