Skip to content

Add support for prefixes#14501

Merged
thecrypticace merged 7 commits into
nextfrom
feat/v4-prefix-compat
Sep 25, 2024
Merged

Add support for prefixes#14501
thecrypticace merged 7 commits into
nextfrom
feat/v4-prefix-compat

Conversation

@thecrypticace

@thecrypticace thecrypticace commented Sep 23, 2024

Copy link
Copy Markdown
Contributor

This PR adds support for requiring a custom prefix on utility classes.

Prefixes work a bit differently in v4 than they did in v3:

  • They look like a custom variant: tw:bg-white
  • It is always first in a utility — even before other variants: tw:hover:bg-white
  • It is required on all utility classes — even arbitrary properties: tw:[color:red]
  • Prefixes also apply to generated CSS variables which will be separated by a dash: --tw-color-white: #fff;
  • Only alpha (a-z) characters are allowed in a prefix — so no #tw# or __ or similar prefixes are allowed

To configure a prefix you can use add prefix(tw) to your theme or when importing Tailwind CSS like so:

/* when importing `tailwindcss` */
@import 'tailwindcss' prefix(tw);

/* when importing the theme separately */
@import 'tailwindcss/theme' prefix(tw);

/* or when using an entirely custom theme */
@theme prefix(tw) {
  --color-white: #fff;
  --breakpoint-sm: 640px;
  /* … */
}

This will configure Tailwind CSS to require a prefix on all utility classes when used in HTML:

<div class="tw:bg-white tw:p-4">
  This will have a white background and 4 units of padding.
</div>

<div class="bg-white p-4">
  This will not because the prefix is missing.
</div>

and when used in CSS via @apply:

.my-class {
  @apply tw:bg-white tw:p-4;
}

Additionally, the prefix will be added to the generated CSS variables. You do not need to prefix the variables in the @theme block yourself — Tailwind CSS handles this automatically.

:root {
  --tw-color-white: #fff;
  --tw-breakpoint-sm: 640px;
}

A prefix is not necessary when using the theme(…) function in your CSS or JS given that plugins will not know what the current prefix is and must work with or without a prefix:

.my-class {
  color: theme(--color-white);
}

However, because the variables themselves are prefixed when outputting the CSS, you do need to prefix the variables when using var(…) in your CSS:

.my-class {
  color: var(--tw-color-white);
}

If you want to customize the prefix itself change tw to something else:

/* my:underline, my:hover:bg-red-500, etc… */
@import 'tailwindcss' prefix(my);

@thecrypticace thecrypticace marked this pull request as ready for review September 24, 2024 13:08
@thecrypticace

thecrypticace commented Sep 24, 2024

Copy link
Copy Markdown
Contributor Author

@RobinMalfait Should we switch to the new prefix syntax (where it is actually a prefix lol) in this PR and do the codemod later — or land the old prefix version and then switch to the new one when we land the codemod?

@RobinMalfait

Copy link
Copy Markdown
Member

@thecrypticace since we didn't have prefix support before this in v4, I would say use the new syntax only and introduce a codemod later (@philipp-spiess is already making good progress on the infra for that).

Don't think it's worth / necessary to ship support for the old prefix. Because, if we have a codemod, then we would remove support for the old prefix again which makes it a breaking change (it's still alpha so it's fine, but still.)

@thecrypticace thecrypticace force-pushed the feat/v4-prefix-compat branch 2 times, most recently from 6ffec7e to ed8a056 Compare September 24, 2024 14:11
@thecrypticace

thecrypticace commented Sep 24, 2024

Copy link
Copy Markdown
Contributor Author
  • Add one test to candidate.test for prefixes
  • Make sure var(--…) is properly prefixed in generated utilities
  • If config.prefix ends in dash we remove it and warn
  • Make sure prefixes are validated — lowercase alpha characters only (a-z)

@thecrypticace thecrypticace force-pushed the feat/v4-prefix-compat branch 3 times, most recently from a33fd23 to f06bb16 Compare September 24, 2024 18:52

@philipp-spiess philipp-spiess left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me overall! One question that could need some clarification is what should the theme() function do when it does get a prefix fed into it, see the inline comment on the new test.

Comment thread CHANGELOG.md Outdated
Comment thread packages/tailwindcss/src/compat/prefix.test.ts Outdated

@RobinMalfait RobinMalfait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A bit annoying to configure, but I do like the new syntax for it. Can't wait for writing a codemod for this 😅

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