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

Components: Add lint rules for theme color CSS var usage #59022

Merged
merged 7 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 16 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ module.exports = {
},
{
files: [ 'packages/components/src/**' ],
excludedFiles: [ 'packages/components/src/utils/colors-values.js' ],
excludedFiles: [
'packages/components/src/utils/colors-values.js',
'packages/components/src/theme/**',
],
rules: {
'no-restricted-syntax': [
'error',
Expand All @@ -369,6 +372,18 @@ module.exports = {
message:
'--wp-admin-theme-* variables do not support component theming. Use variables from the COLORS object in packages/components/src/utils/colors-values.js instead.',
},
{
selector:
'Literal[value=/var.+--wp-components-color-/]', // allow overriding definitions, but not access with var()
message:
'To ensure proper fallbacks, --wp-components-color-* variables should not be used directly. Use variables from the COLORS object in packages/components/src/utils/colors-values.js instead.',
},
{
selector:
'TemplateElement[value.cooked=/var.+--wp-components-color-/]', // allow overriding definitions, but not access with var()
message:
'To ensure proper fallbacks, --wp-components-color-* variables should not be used directly. Use variables from the COLORS object in packages/components/src/utils/colors-values.js instead.',
},
Copy link
Contributor

@andrewhayward andrewhayward Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if it makes things more or less understandable, but you could squash these rules with a :matches...

Suggested change
{
selector:
'Literal[value=/var.+--wp-components-color-/]', // allow overriding definitions, but not access with var()
message:
'To ensure proper fallbacks, --wp-components-color-* variables should not be used directly. Use variables from the COLORS object in packages/components/src/utils/colors-values.js instead.',
},
{
selector:
'TemplateElement[value.cooked=/var.+--wp-components-color-/]', // allow overriding definitions, but not access with var()
message:
'To ensure proper fallbacks, --wp-components-color-* variables should not be used directly. Use variables from the COLORS object in packages/components/src/utils/colors-values.js instead.',
},
{
selector:
// allow overriding definitions, but not access with var()
':matches(Literal[value=/var.+--wp-components-color-/],TemplateElement[value.cooked=/var.+--wp-components-color-/])',
message:
'To ensure proper fallbacks, --wp-components-color-* variables should not be used directly. Use variables from the COLORS object in packages/components/src/utils/colors-values.js instead.',
},

Also, is it worth being a bit more specific with the RegExp? It currently matches anything that starts with var and ends with --wp-components-color-, so (while highly unlikely to happen, I'll grant you!) could hit some false positives:

// ⛔️ /var.+--wp-components-color-/
const myCss = 'color: var(--wp-components-color-foreground, #FFF);'
// Matches            ^------------------------^
const myCss = 'color: var(--anything, #000); --wp-components-color-background: #FFF;'
// Matches            ^-------------------------------------------^

// ✅ /var\s*\(\s*--wp-components-color-/
const myCss = 'color: var(--wp-components-color-foreground, #FFF);'
// Matches            ^------------------------^
const myCss = 'color: var(--anything, #000); --wp-components-color-background: #FFF;'
// No match

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could squash these rules with a :matches

Ooh, TIL! That looks cleaner to me too, thanks.

Also, is it worth being a bit more specific with the RegExp?

I actually wanted to add the parenthesis too, but I couldn't figure out how to do the escaping without Prettier complaining. Turns out I just needed a double backslash. Done 👍

],
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/components/.stylelintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module.exports = {
rules: {
'declaration-property-value-disallowed-list': [
{
'/.*/': '/--wp-admin-theme-/',
'/.*/': [ '/--wp-admin-theme-/', '/--wp-components-color-/' ],
},
{
message:
'--wp-admin-theme-* variables do not support component theming. Use Sass variables from packages/components/src/utils/theme-variables.scss instead.',
'To support component theming and ensure proper fallbacks, use Sass variables from packages/components/src/utils/theme-variables.scss instead.',
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/progress-bar/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const Track = styled.div`
/* Text color at 10% opacity */
background-color: color-mix(
in srgb,
var( --wp-components-color-foreground, ${ COLORS.gray[ 900 ] } ),
${ COLORS.theme.foreground },
transparent 90%
);
border-radius: ${ CONFIG.radiusBlockUi };
Expand All @@ -52,7 +52,7 @@ export const Indicator = styled.div< {
/* Text color at 90% opacity */
background-color: color-mix(
in srgb,
var( --wp-components-color-foreground, ${ COLORS.gray[ 900 ] } ),
${ COLORS.theme.foreground },
transparent 10%
);

Expand Down
Loading