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

Fix(CSS): override doesn't work #304

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions packages/css/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class MasterCSS {
if (!customConfig?.override) {
this.config = this.getExtendedConfig(defaultConfig, customConfig)
} else {
this.config = this.getExtendedConfig(this.config)
this.config = this.getExtendedConfig(customConfig)
}
this.resolve()
globalThis.masterCSSs.push(this)
Expand Down Expand Up @@ -370,17 +370,16 @@ export class MasterCSS {
})
}

const rulesEntries = Object.entries(rules)
.sort((a: any, b: any) => {
if (rules) {
const rulesEntries = Object.entries(rules).sort((a: any, b: any) => {
if (a[1].layer !== b[1].layer) {
return (b[1].layer || 0) - (a[1].layer || 0)
}
return b[0].localeCompare(a[0])
})
const rulesEntriesLength = rulesEntries.length
const colorNames = Object.keys(colorVariableNames)
rulesEntries
.forEach(([id, eachRuleOptions]: [string, RuleOptions], index: number) => {
const rulesEntriesLength = rulesEntries.length
const colorNames = Object.keys(colorVariableNames)
rulesEntries.forEach(([id, eachRuleOptions]: [string, RuleOptions], index: number) => {
this.ruleOptions.push(eachRuleOptions)
eachRuleOptions.order = this.semanticRuleOptions.length + rulesEntriesLength - 1 - index
const match = eachRuleOptions.match
Expand All @@ -391,21 +390,30 @@ export class MasterCSS {
eachRuleOptions.layer === CoreLayer.Native ||
eachRuleOptions.layer === CoreLayer.NativeShorthand
) {
eachRuleOptions.resolvedPropName = id.replace(/(?!^)[A-Z]/g, m => '-' + m).toLowerCase()
eachRuleOptions.resolvedPropName = id
.replace(/(?!^)[A-Z]/g, (m) => '-' + m)
.toLowerCase()
}
eachRuleOptions.resolvedVariables = {}
const addResolvedVariables = (prefix: string) => {
Object.assign(
eachRuleOptions.resolvedVariables,
Object.keys(this.variables)
.filter(eachVariableName => eachVariableName.startsWith(prefix + '-') || eachVariableName.startsWith('-' + prefix + '-'))
.filter((eachVariableName) =>
eachVariableName.startsWith(prefix + '-') ||
eachVariableName.startsWith('-' + prefix + '-'),
)
.reduce((newResolvedVariables, eachVariableName) => {
newResolvedVariables[eachVariableName.slice(prefix.length + (prefix.startsWith('-') ? 0 : 1))] = {
newResolvedVariables[
eachVariableName.slice(
prefix.length + (prefix.startsWith('-') ? 0 : 1),
)
] = {
...this.variables[eachVariableName],
name: eachVariableName
name: eachVariableName,
}
return newResolvedVariables
}, {})
}, {}),
)
}
// 1. custom `config.rules[id].variableGroups`
Expand All @@ -425,24 +433,30 @@ export class MasterCSS {
valueMatches.push(`(?:${values.join('|')})(?![a-zA-Z0-9-])`)
}
if (Object.keys(eachRuleOptions.resolvedVariables).length) {
valueMatches.push(`(?:${Object.keys(eachRuleOptions.resolvedVariables).join('|')})(?![a-zA-Z0-9-])`)
valueMatches.push(
`(?:${Object.keys(eachRuleOptions.resolvedVariables).join('|')})(?![a-zA-Z0-9-])`,
)
}
if (eachRuleOptions.colored) {
valueMatches.push(
'#',
'(?:color|color-contrast|color-mix|hwb|lab|lch|oklab|oklch|rgb|rgba|hsl|hsla)\\(.*\\)',
`(?:${colorNames.join('|')})(?![a-zA-Z0-9-])`
`(?:${colorNames.join('|')})(?![a-zA-Z0-9-])`,
)
}
if (eachRuleOptions.numeric) {
valueMatches.push('[\\d\\.]', '(?:max|min|calc|clamp)\\(.*\\)')
}
eachRuleOptions.resolvedMatch = new RegExp(`^${key}:(?:${valueMatches.join('|')})[^|]*?(?:@|$)`)
eachRuleOptions.resolvedMatch = new RegExp(
`^${key}:(?:${valueMatches.join('|')})[^|]*?(?:@|$)`,
)
} else {
eachRuleOptions.resolvedMatch = match as RegExp
}
}
})
},
)
}
}

/**
Expand Down Expand Up @@ -1446,23 +1460,23 @@ export class MasterCSS {
for (let i = 0; i < sheet.cssRules.length; i++) {
const eachCssRule = sheet.cssRules[i]
if (
eachCssRule.constructor.name === 'CSSStyleRule'
&& (eachCssRule as CSSStyleRule).style.length === 1
&& (eachCssRule as CSSStyleRule).style[0].startsWith('--')
eachCssRule.constructor.name === 'CSSStyleRule'
&& (eachCssRule as CSSStyleRule).style.length === 1
&& (eachCssRule as CSSStyleRule).style[0].startsWith('--')
&& !(eachCssRule as CSSStyleRule).selectorText.startsWith('.\\$')
)
continue

if (eachCssRule.constructor.name !== 'CSSKeyframesRule')
break

if ((eachCssRule as CSSKeyframesRule).name === eachKeyframeName) {
cssRule = eachCssRule
break
}
}
}

if (cssRule) {
nativeRule.cssRule = cssRule
} else {
Expand Down Expand Up @@ -1533,8 +1547,8 @@ export class MasterCSS {
const eachCssRule = sheet.cssRules[i]
if (
eachCssRule.constructor.name !== 'CSSStyleRule'
|| (eachCssRule as CSSStyleRule).style.length !== 1
|| !(eachCssRule as CSSStyleRule).style[0].startsWith('--')
|| (eachCssRule as CSSStyleRule).style.length !== 1
|| !(eachCssRule as CSSStyleRule).style[0].startsWith('--')
|| (eachCssRule as CSSStyleRule).selectorText.startsWith('.\\$')
)
break
Expand Down
20 changes: 20 additions & 0 deletions packages/css/tests/config/override/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Config, CoreLayer } from '../../../src'
import { testCSS } from '../../css'

const customConfig: Config = {
override: true,
rootSize: 10,
rules: {
fontSize: {
match: ['custom'],
numeric: true,
unit: 'rem',
layer: CoreLayer.Native,
},
},
}

test('override', () => {
testCSS('font:16', '', customConfig)
testCSS('custom:16', '.custom\\:16{font-size:1.6rem}', customConfig)
})