-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add stylelint rule to prevent theme CSS vars outside of wp-components #59020
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: -248 B (0%) Total Size: 1.71 MB
ℹ️ View Unchanged
|
Flaky tests detected in 5492949. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7903610741
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Question: How concerned are we with these variables being consumed in JS? Either directly in a component definition (currently hypothetical)...
<MyComponent style={{
color: "var(--wp-components-color-foreground, $gray-900)"
}} />
...or via some sort of embedded style (currently live)?
gutenberg/packages/edit-site/src/components/style-book/index.js
Lines 80 to 87 in 5492949
.edit-site-style-book__example.is-selected { | |
box-shadow: 0 0 0 1px var(--wp-components-color-accent, var(--wp-admin-theme-color, #007cba)); | |
} | |
.edit-site-style-book__example:focus:not(:disabled) { | |
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-components-color-accent, var(--wp-admin-theme-color, #007cba)); | |
outline: 3px solid transparent; | |
} |
Is it worth adding something to .eslintrc.js
to catch these as well?
diff --git a/.eslintrc.js b/.eslintrc.js
index 67671070aa..9ef58d3362 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -407,5 +407,20 @@ module.exports = {
'react/react-in-jsx-scope': 'error',
},
},
+ {
+ files: [ 'packages/*/src/**' ],
+ excludedFiles: [ 'packages/components/src/**' ],
+ rules: {
+ 'no-restricted-syntax': [
+ 'error',
+ {
+ selector:
+ ':matches(Literal[value=/--wp-components-color-/], TemplateLiteral:has(TemplateElement[value.cooked=/--wp-components-color-/]))',
+ message:
+ '--wp-components-color-* variables are not ready to be used outside of the components package.',
+ },
+ ],
+ },
+ },
],
};
...at the moment I would still like to allow overriding definitions...
This would be caught by that ESLint rule, which may or may not be an issue.
Ah I missed that one, thanks! Since this overlaps with #59022, let me resolve this separately as a follow-up once these two PRs are merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Adding to the ESLint config to support CSS-in-JS usages in a separate PR makes sense to me.
🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this overlaps with #59022, let me resolve this separately as a follow-up once these two PRs are merged.
Sounds good 🚢
* trunk: (78 commits) Components: Use `Element.scrollIntoView()` instead of `dom-scroll-into-view` (#59085) DataViews: Correctly display featured image that don't have image sizes (#59111) Elements: Fix block instance element styles for links applying to buttons (#59114) Allow editing of image block alt and title attributes in content only mode (#58998) Add toggle for grid types and stabilise Grid block variation. (#59051) Global Styles: fix console error in block preview (#59112) Navigation: Avoid using embedded records from fallback API (#59076) Refactor responsive logic for grid column spans. (#59057) Editor: Unify Mode Switcher component between post and site editor (#59100) Move StopEditingAsBlocksOnOutsideSelect to Root (#58412) Fix logic error in #58951 (#59101) Block-editor: Auto-register block commands (#59079) Fix small typo in rich text reference guide (#59089) Components: Add lint rules for theme color CSS var usage (#59022) Enter editing mode via Enter or Spacebar (#58795) Updating Storybook to v7.6.15 (latest) (#59074) CustomSelectControl (v1 & v2): Fix errors in unit test setup (#59038) Add stylelint rule to prevent theme CSS vars outside of wp-components (#59020) ColorPicker: Style without accessing InputControl internals (#59069) Pattern block: batch replacing actions (#59075) ...
Part of #44116
Follow-up to #58098
What?
Adds a stylelint rule to catch premature usages of the components theming CSS variables (
wp-components-color-*
) outside of the components package.Why?
These CSS variables are still experimental, and there is still no reason to use them outside of the components package unless it's part of a coordinated effort to use a particular package component with the
<Theme>
component.None of the existing usages (lint rule violations) are part of an intentional theming effort, and seem to be copy & pasting the variable (complete with fallbacks) from wp-component CSS, assuming that is the new way to do things.
This lint rule is meant to inform developers that this is not necessary.
How?
Although we are restricting variable access through
var()
, at the moment I would still like to allow overriding definitions (--wp-components-color-foo: #000
). Nobody has tried this yet, but it would be useful to monitor these to better understand the use cases for theming.Testing Instructions
npm run lint:css
. The violations should be caught.