|
| 1 | +/* |
| 2 | + * Tailwind 4 uses @property but these don't work inside shadow DOM |
| 3 | + * |
| 4 | + * See: https://github.com/tailwindlabs/tailwindcss/issues/15005 |
| 5 | + * |
| 6 | + * This script converts @property rules to custom properties on :root, :host |
| 7 | + * |
| 8 | + * Courtesy of https://github.com/tailwindlabs/tailwindcss/discussions/16772#discussioncomment-12309978 |
| 9 | + */ |
| 10 | +export function propertyToCustomProp() { |
| 11 | + return { |
| 12 | + postcssPlugin: 'postcss-property-to-custom-prop', |
| 13 | + prepare() { |
| 14 | + // Store all the properties we find |
| 15 | + const properties = []; |
| 16 | + |
| 17 | + return { |
| 18 | + AtRule: { |
| 19 | + property: (rule) => { |
| 20 | + // Extract the property name and initial value |
| 21 | + const propertyName = rule.params.match(/--[\w-]+/)?.[0]; |
| 22 | + let initialValue = ''; |
| 23 | + |
| 24 | + rule.walkDecls('initial-value', (decl) => { |
| 25 | + initialValue = decl.value; |
| 26 | + }); |
| 27 | + |
| 28 | + if (propertyName && initialValue) { |
| 29 | + // Store the property |
| 30 | + properties.push({ name: propertyName, value: initialValue }); |
| 31 | + |
| 32 | + // Remove the original @property rule |
| 33 | + rule.remove(); |
| 34 | + } |
| 35 | + }, |
| 36 | + }, |
| 37 | + |
| 38 | + OnceExit(root, { Rule, Declaration }) { |
| 39 | + // If we found properties, add them to :root, :host |
| 40 | + if (properties.length > 0) { |
| 41 | + // Create the :root, :host rule using the Rule constructor from helpers |
| 42 | + const rootRule = new Rule({ selector: ':root, :host' }); |
| 43 | + |
| 44 | + // Add all properties as declarations |
| 45 | + properties.forEach((prop) => { |
| 46 | + // Create a new declaration for each property |
| 47 | + const decl = new Declaration({ |
| 48 | + prop: prop.name, |
| 49 | + value: prop.value, |
| 50 | + }); |
| 51 | + rootRule.append(decl); |
| 52 | + }); |
| 53 | + |
| 54 | + // Add the rule to the beginning of the CSS |
| 55 | + root.prepend(rootRule); |
| 56 | + } |
| 57 | + }, |
| 58 | + }; |
| 59 | + }, |
| 60 | + }; |
| 61 | +} |
0 commit comments