-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overwrite core utilities to ensure we have a consistent order
- Loading branch information
1 parent
669fd15
commit 5dcb301
Showing
3 changed files
with
95 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,79 @@ | ||
import type { PluginAPI } from '../plugin-api' | ||
import { rule } from '../ast' | ||
import type { DesignSystem } from '../design-system' | ||
import type { ResolvedConfig } from './config/types' | ||
import DefaultTheme from './default-theme' | ||
|
||
export function themeVariantsPlugin({ addVariant, config }: PluginAPI) { | ||
let ariaVariants = config('theme.aria', {}) | ||
let supportsVariants = config('theme.supports', {}) | ||
let dataVariants = config('theme.data', {}) | ||
export function registerThemeVariantOverrides(config: ResolvedConfig, designSystem: DesignSystem) { | ||
let ariaVariants = config.theme.aria || {} | ||
let supportsVariants = config.theme.supports || {} | ||
let dataVariants = config.theme.data || {} | ||
|
||
for (let [name, rule] of Object.entries(DefaultTheme.aria)) { | ||
// `theme.aria` contains values from the default theme. We don't | ||
// want to create variants for these values as these are already | ||
// handled by the core utility. | ||
if (Object.hasOwn(DefaultTheme.aria, name) && ariaVariants[name] === rule) { | ||
continue | ||
if (ariaVariants[name] === rule) { | ||
delete ariaVariants[name] | ||
} | ||
} | ||
|
||
for (let [name, rule] of Object.entries(DefaultTheme.aria)) { | ||
if (ariaVariants[name] === rule) { | ||
delete ariaVariants[name] | ||
} | ||
} | ||
|
||
for (let [name, rule] of Object.entries(ariaVariants)) { | ||
addVariant(`aria-${name}`, `&[aria-${rule}]`) | ||
if (Object.keys(ariaVariants).length > 0) { | ||
let coreAria = designSystem.variants.get('aria') | ||
let coreApplyFn = coreAria?.applyFn | ||
let coreCompounds = coreAria?.compounds | ||
designSystem.variants.functional( | ||
'aria', | ||
(ruleNode, variant) => { | ||
let value = variant.value | ||
if (value && value.kind === 'named' && value.value in ariaVariants) { | ||
ruleNode.nodes = [rule(`&[aria-${ariaVariants[value.value]}]`, ruleNode.nodes)] | ||
return | ||
} | ||
return coreApplyFn?.(ruleNode, variant) | ||
}, | ||
{ compounds: coreCompounds }, | ||
) | ||
} | ||
|
||
for (let [name, rule] of Object.entries(supportsVariants)) { | ||
addVariant(`supports-${name}`, `@supports (${rule})`) | ||
if (Object.keys(supportsVariants).length > 0) { | ||
let coreSupports = designSystem.variants.get('supports') | ||
let coreApplyFn = coreSupports?.applyFn | ||
let coreCompounds = coreSupports?.compounds | ||
designSystem.variants.functional( | ||
'supports', | ||
(ruleNode, variant) => { | ||
let value = variant.value | ||
if (value && value.kind === 'named' && value.value in supportsVariants) { | ||
ruleNode.nodes = [rule(`@supports (${supportsVariants[value.value]})`, ruleNode.nodes)] | ||
return | ||
} | ||
return coreApplyFn?.(ruleNode, variant) | ||
}, | ||
{ compounds: coreCompounds }, | ||
) | ||
} | ||
|
||
for (let [name, rule] of Object.entries(dataVariants)) { | ||
addVariant(`data-${name}`, `&[data-${rule}]`) | ||
if (Object.keys(dataVariants).length > 0) { | ||
let coreData = designSystem.variants.get('data') | ||
let coreApplyFn = coreData?.applyFn | ||
let coreCompounds = coreData?.compounds | ||
designSystem.variants.functional( | ||
'data', | ||
(ruleNode, variant) => { | ||
let value = variant.value | ||
if (value && value.kind === 'named' && value.value in dataVariants) { | ||
ruleNode.nodes = [rule(`&[data-${dataVariants[value.value]}]`, ruleNode.nodes)] | ||
return | ||
} | ||
return coreApplyFn?.(ruleNode, variant) | ||
}, | ||
{ compounds: coreCompounds }, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters