[bug] box-shadow doesn't work in shadow-dom due to reliance on @property #16772
Replies: 3 comments 4 replies
-
Hey @snaptopixel! Unfortunately The issue is that we rely on |
Beta Was this translation helpful? Give feedback.
-
Hi, Just started a new project and found this discussion. The plugin looks for I guess i'll see some errors when it comes to animations since the css rules will not have an initial value to animate from. Anyway, here is the Plugin code: /**
* PostCSS plugin to convert @property declarations to CSS custom properties
* This helps with using property values inside Shadow DOM
*/
export default (opts = {}) => {
return {
postcssPlugin: 'postcss-property-to-custom-prop',
prepare(result) {
// Store all the properties we find
const properties = [];
return {
AtRule: {
property: (rule) => {
// Extract the property name and initial value
const propertyName = rule.params.match(/--[\w-]+/)?.[0];
let initialValue = '';
rule.walkDecls('initial-value', (decl) => {
initialValue = decl.value;
});
if (propertyName && initialValue) {
// Store the property
properties.push({ name: propertyName, value: initialValue });
// Remove the original @property rule
rule.remove();
}
},
},
OnceExit(root, { Rule, Declaration }) {
// If we found properties, add them to :root, :host
if (properties.length > 0) {
// Create the :root, :host rule using the Rule constructor from helpers
const rootRule = new Rule({ selector: ':root, :host' });
// Add all properties as declarations
properties.forEach((prop) => {
// Create a new declaration for each property
const decl = new Declaration({
prop: prop.name,
value: prop.value,
});
rootRule.append(decl);
});
// Add the rule to the beginning of the CSS
root.prepend(rootRule);
}
},
};
},
};
};
export const postcss = true; Usage: import propertyToCustomProp from './plugins/postcss-property-to-custom-prop';
import tailwindcss from '@tailwindcss/postcss';
export default {
plugins: [
tailwindcss(),
propertyToCustomProp()
],
}; Inside my web component i'm using the import globalCss from '@/styles/global.css?inline';
const sheet = new CSSStyleSheet();
sheet.replaceSync(globalCss);
export class MyComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot!.adoptedStyleSheets = [sheet];
// ...
}
} |
Beta Was this translation helpful? Give feedback.
-
I stumbled upon this too. The decision to go for |
Beta Was this translation helpful? Give feedback.
-
What version of Tailwind CSS are you using?
v4.0.6
What build tool (or framework if it abstracts the build tool) are you using?
tailwind-cli
What version of Node.js are you using?
v20.0.0
What browser are you using?
Chrome
What operating system are you using?
macOS
Reproduction URL
https://codepen.io/snaptopixel/pen/GgRZebj
Describe your issue
Tailwind's box-shadow based utils (ring, shadow, etc) use multiple shadow syntax with custom properties:
Since these vars don't have default/fallback values the whole box-shadow breaks if any one of them is undefined. In non-shadow this is a non-issue since
@property
provides default values. However in shadow-dom@property
does nothing, so ironically, in shadow-dom you will have no shadows on your dom.Essentially it seems there should be suitable fallbacks for any utils that currently rely on
@property
since it doesn't work in shadow-dom land.Beta Was this translation helpful? Give feedback.
All reactions