|
1 | | -import type { PluginAPI } from '../plugin-api' |
| 1 | +import { rule } from '../ast' |
| 2 | +import type { DesignSystem } from '../design-system' |
| 3 | +import type { ResolvedConfig } from './config/types' |
2 | 4 | import DefaultTheme from './default-theme' |
3 | 5 |
|
4 | | -export function themeVariantsPlugin({ addVariant, config }: PluginAPI) { |
5 | | - let ariaVariants = config('theme.aria', {}) |
6 | | - let supportsVariants = config('theme.supports', {}) |
7 | | - let dataVariants = config('theme.data', {}) |
| 6 | +export function registerThemeVariantOverrides(config: ResolvedConfig, designSystem: DesignSystem) { |
| 7 | + let ariaVariants = config.theme.aria || {} |
| 8 | + let supportsVariants = config.theme.supports || {} |
| 9 | + let dataVariants = config.theme.data || {} |
8 | 10 |
|
9 | 11 | for (let [name, rule] of Object.entries(DefaultTheme.aria)) { |
10 | 12 | // `theme.aria` contains values from the default theme. We don't |
11 | 13 | // want to create variants for these values as these are already |
12 | 14 | // handled by the core utility. |
13 | | - if (Object.hasOwn(DefaultTheme.aria, name) && ariaVariants[name] === rule) { |
14 | | - continue |
| 15 | + if (ariaVariants[name] === rule) { |
| 16 | + delete ariaVariants[name] |
15 | 17 | } |
| 18 | + } |
16 | 19 |
|
| 20 | + for (let [name, rule] of Object.entries(DefaultTheme.aria)) { |
17 | 21 | if (ariaVariants[name] === rule) { |
18 | 22 | delete ariaVariants[name] |
19 | 23 | } |
20 | 24 | } |
21 | 25 |
|
22 | | - for (let [name, rule] of Object.entries(ariaVariants)) { |
23 | | - addVariant(`aria-${name}`, `&[aria-${rule}]`) |
| 26 | + if (Object.keys(ariaVariants).length > 0) { |
| 27 | + let coreAria = designSystem.variants.get('aria') |
| 28 | + let coreApplyFn = coreAria?.applyFn |
| 29 | + let coreCompounds = coreAria?.compounds |
| 30 | + designSystem.variants.functional( |
| 31 | + 'aria', |
| 32 | + (ruleNode, variant) => { |
| 33 | + let value = variant.value |
| 34 | + if (value && value.kind === 'named' && value.value in ariaVariants) { |
| 35 | + ruleNode.nodes = [rule(`&[aria-${ariaVariants[value.value]}]`, ruleNode.nodes)] |
| 36 | + return |
| 37 | + } |
| 38 | + return coreApplyFn?.(ruleNode, variant) |
| 39 | + }, |
| 40 | + { compounds: coreCompounds }, |
| 41 | + ) |
24 | 42 | } |
25 | 43 |
|
26 | | - for (let [name, rule] of Object.entries(supportsVariants)) { |
27 | | - addVariant(`supports-${name}`, `@supports (${rule})`) |
| 44 | + if (Object.keys(supportsVariants).length > 0) { |
| 45 | + let coreSupports = designSystem.variants.get('supports') |
| 46 | + let coreApplyFn = coreSupports?.applyFn |
| 47 | + let coreCompounds = coreSupports?.compounds |
| 48 | + designSystem.variants.functional( |
| 49 | + 'supports', |
| 50 | + (ruleNode, variant) => { |
| 51 | + let value = variant.value |
| 52 | + if (value && value.kind === 'named' && value.value in supportsVariants) { |
| 53 | + ruleNode.nodes = [rule(`@supports (${supportsVariants[value.value]})`, ruleNode.nodes)] |
| 54 | + return |
| 55 | + } |
| 56 | + return coreApplyFn?.(ruleNode, variant) |
| 57 | + }, |
| 58 | + { compounds: coreCompounds }, |
| 59 | + ) |
28 | 60 | } |
29 | 61 |
|
30 | | - for (let [name, rule] of Object.entries(dataVariants)) { |
31 | | - addVariant(`data-${name}`, `&[data-${rule}]`) |
| 62 | + if (Object.keys(dataVariants).length > 0) { |
| 63 | + let coreData = designSystem.variants.get('data') |
| 64 | + let coreApplyFn = coreData?.applyFn |
| 65 | + let coreCompounds = coreData?.compounds |
| 66 | + designSystem.variants.functional( |
| 67 | + 'data', |
| 68 | + (ruleNode, variant) => { |
| 69 | + let value = variant.value |
| 70 | + if (value && value.kind === 'named' && value.value in dataVariants) { |
| 71 | + ruleNode.nodes = [rule(`&[data-${dataVariants[value.value]}]`, ruleNode.nodes)] |
| 72 | + return |
| 73 | + } |
| 74 | + return coreApplyFn?.(ruleNode, variant) |
| 75 | + }, |
| 76 | + { compounds: coreCompounds }, |
| 77 | + ) |
32 | 78 | } |
33 | 79 | } |
0 commit comments