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 :host rule to @theme layer #15975

Merged
merged 7 commits into from
Jan 29, 2025

Conversation

hugo-vrijswijk
Copy link
Contributor

@hugo-vrijswijk hugo-vrijswijk commented Jan 28, 2025

Resolves #15799
Resolves #14478
Part-of #15005

Adds a :host selector for the @theme layer. This is necessary for the @theme layer to work correctly in shadow DOM.

Also updates the snapshots for the tests that were affected by this change (in a separate commit).

Test plan

Tested via the Vite playground:

Screenshot 2025-01-29 at 15 06 49

Additionally made sure that @property defaults also work across Firefox, Chrome, and Safari (the @property definition from the root is pulled in) and added a UI spec.

Fixes tailwindlabs#15799

Adds a `:host` selector for the `@theme` layer. This is necessary for the `@theme` layer to work correctly in shadow DOM.

Also updates the snapshots for the tests that were affected by this change.
@lubomirblazekcz
Copy link
Contributor

This should also fix #14478

@philipp-spiess
Copy link
Member

@hugo-vrijswijk Thanks a ton for this PR! I went ahead and did a round of testing on the known repro cases and added a test plan to your description. I also added a UI spec to ensure we have some validation. Note that because of @property not being able to be scoped to a shadow root, you still have to have the stylesheet added to the browser root too.

CHANGELOG.md Outdated Show resolved Hide resolved
@philipp-spiess philipp-spiess merged commit 9fef2bd into tailwindlabs:main Jan 29, 2025
5 checks passed
@khopha
Copy link

khopha commented Feb 10, 2025

@hugo-vrijswijk Thanks a ton for this PR! I went ahead and did a round of testing on the known repro cases and added a test plan to your description. I also added a UI spec to ensure we have some validation. Note that because of @property not being able to be scoped to a shadow root, you still have to have the stylesheet added to the browser root too.

@philipp-spiess Thank you for the note. Does this mean that Tailwind CSS v4 is not fully compatible with Shadow DOM? (Because we still need to add it to the browser root too.) Is there any workaround that doesn't require adding it to the browser root, or is the only workaround to downgrade to v3?

@hugo-vrijswijk hugo-vrijswijk deleted the add-host-theme-rule branch February 10, 2025 14:05
@hugo-vrijswijk
Copy link
Contributor Author

@philipp-spiess Thank you for the note. Does this mean that Tailwind CSS v4 is not fully compatible with Shadow DOM? (Because we still need to add it to the browser root too.) Is there any workaround that doesn't require adding it to the browser root, or is the only workaround to downgrade to v3?

Below is the workaround I'm using at the moment. IMO, it is quite ugly and does go against the principles and encapsulation of shadow DOM. Since you are polluting styles outside your component. Which in turn could change other components. The other way around, your component is now susceptible to changes from properties set in the document. Ideally components are fully encapsulated and don't need to touch anything outside the host, but tailwind 4 breaks this principle.

I'm not exactly sure how CSS properties work and how they are different from variables. But if there were an option to have all the properties set as variables instead, that would solve the issue.

import tailwindCss from './tailwind.css?inline';
export const tailwind = unsafeCSS(tailwindCss);

// https://github.com/tailwindlabs/tailwindcss/issues/15005
// Set all @property values from tailwind on the document
// And only do this once (check if the there is already a stylesheet with the same content)
if (
  tailwind.styleSheet &&
  document?.adoptedStyleSheets &&
  !document.adoptedStyleSheets.some((sheet) => sheet.cssRules[0]?.cssText === tailwind.styleSheet!.cssRules[0].cssText)
) {
  const propertiesSheet = new CSSStyleSheet();
  let code = tailwind.cssText;

  code = code
    // Change custom properties to inherit
    .replaceAll('inherits: false', 'inherits: true')
    // Remove everything before the property declarations
    .substring(code.indexOf('@property'));

  propertiesSheet.replaceSync(code);

  document.adoptedStyleSheets.push(propertiesSheet);
}

@khopha
Copy link

khopha commented Feb 11, 2025

@hugo-vrijswijk
Thank you for your workaround. I followed your approach, and it’s working fine now. Yes, it would be great if Tailwind could work properly or have an option to make it work in Shadow DOM.

@lubomirblazekcz
Copy link
Contributor

lubomirblazekcz commented Feb 11, 2025

Sadly it's more of a spec problem, because @property at-rule doesn't work in shadow dom.

https://stackoverflow.com/questions/78717295/property-not-recognized-in-shadow-dom

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.

tailwindcss and shadow dom not working well (--spacing) [v4] Expose CSS properties also to :host
5 participants