Repository navigation
Components: add Emotion migration guardrails #79442
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
Changes from all commits
cc49680
52b6654
a714e39
bf99b2a
be94453
4bb58ff
779ba24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,10 +191,10 @@ function useExampleComponent( | |
| ); | ||
|
|
||
| // Any other reusable rendering logic (e.g. computing className, state, event listeners...) | ||
| const cx = useCx(); | ||
| const classes = useMemo( | ||
| () => cx( styles.example, isVisible && styles.visible, className ), | ||
| [ className, isVisible ] | ||
| const classes = clsx( | ||
| styles.example, | ||
| isVisible && styles.visible, | ||
| className | ||
| ); | ||
|
|
||
| return { | ||
|
|
@@ -416,48 +416,55 @@ On the component's main named export, add a JSDoc comment that includes the main | |
|
|
||
| ## Styling | ||
|
|
||
| All new component should be styled using [Emotion](https://emotion.sh/docs/introduction). | ||
| All new components should be styled using SCSS Modules. | ||
|
|
||
| Note: Instead of using Emotion's standard `cx` function, the custom [`useCx` hook](https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/utils/hooks/use-cx.ts) should be used instead. | ||
| Place component-local styles in a `style.module.scss` file next to the component, import the module from JavaScript or TypeScript, and compose class names with `clsx`. Preserve existing public `components-*` class names where consumers may rely on them. For dynamic values, prefer inline CSS custom properties consumed by the SCSS module. For variants and state, prefer conditional module classes composed with `clsx`. | ||
|
|
||
| Legacy components may still use Emotion while they are being migrated, but new Emotion usage should not be added. | ||
|
|
||
| ### Deprecating styles | ||
|
|
||
| Changing the styles of a non-experimental component must be done with care. To prevent serious breakage in third-party usage, in some cases we may want a grace period before fully removing the old styles. This can be done by temporarily placing the new styles behind a feature flag prop prefixed by `__next`, accompanied by a `deprecate()` warning in the console. The feature flag should be opt-in (false by default), and have a reasonably descriptive name (**not** `__nextHasNewStyles`). A descriptive name allows for multiple deprecations to proceed in parallel, separated by concerns or by deprecation version. | ||
|
|
||
| ```jsx | ||
| // component.tsx | ||
| import clsx from 'clsx'; | ||
| import deprecated from '@wordpress/deprecated'; | ||
| import { Wrapper } from './styles.ts'; | ||
| import styles from './style.module.scss'; | ||
|
|
||
| function MyComponent( { __nextHasNoOuterMargins = false } ) { | ||
| function MyComponent( { __nextHasNoOuterMargins = false, className } ) { | ||
| if ( ! __nextHasNoOuterMargins ) { | ||
| deprecated( 'Outer margin styles for wp.components.MyComponent', { | ||
| since: '6.0', | ||
| version: '6.2', // Set a reasonable grace period depending on impact | ||
| hint: 'Set the `__nextHasNoOuterMargins` prop to true to start opting into the new styles, which will become the default in a future version.', | ||
| } ); | ||
| } | ||
| return <Wrapper __nextHasNoOuterMargins={ __nextHasNoOuterMargins } />; | ||
| return ( | ||
| <div | ||
| className={ clsx( | ||
| 'components-my-component', | ||
| styles.root, | ||
| ! __nextHasNoOuterMargins && | ||
| styles.deprecatedOuterMargins, | ||
| className | ||
| ) } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this is a suboptimal usage of clsx( 'components-my-component', styles.root, className, {
[ styles.deprecatedOuterMargins ]: ! __nextHasNoOuterMargins,
} );
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've noticed that this object key style can emit a literal
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That can happen when
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, adding a small tweak in #79490 |
||
| /> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| Styles should be structured so the deprecated styles are cleanly encapsulated, and can be easily removed when the deprecation version arrives. | ||
|
|
||
| ```js | ||
| // styles.ts | ||
| const deprecatedMargins = ( { __nextHasNoOuterMargins } ) => { | ||
| if ( ! __nextHasNoOuterMargins ) { | ||
| return css` | ||
| margin: 8px; | ||
| `; | ||
| } | ||
| }; | ||
|
|
||
| export const Wrapper = styled.div` | ||
| ```scss | ||
| // style.module.scss | ||
| .root { | ||
| margin: 0; | ||
| } | ||
|
|
||
| ${ deprecatedMargins } | ||
| `; | ||
| .deprecatedOuterMargins { | ||
| margin: 8px; | ||
| } | ||
| ``` | ||
|
|
||
| Once deprecated, code examples in docs/stories should include the opt-in prop set to `true` so that new consumers are encouraged to adopt it from the start. | ||
|
|
@@ -683,7 +690,7 @@ component-name/ | |
| ├── hook.ts | ||
| ├── index.ts | ||
| ├── README.md | ||
| ├── styles.ts | ||
| ├── style.module.scss | ||
| └── types.ts | ||
| ``` | ||
|
|
||
|
|
@@ -696,13 +703,13 @@ component-family-name/ | |
| │ ├── component.tsx | ||
| │ ├── hook.ts | ||
| │ ├── README.md | ||
| │ └── styles.ts | ||
| │ └── style.module.scss | ||
| ├── sub-component-name/ | ||
| │ ├── index.ts | ||
| │ ├── component.tsx | ||
| │ ├── hook.ts | ||
| │ ├── README.md | ||
| │ └── styles.ts | ||
| │ └── style.module.scss | ||
| ├── stories | ||
| │ └── index.js | ||
| ├── test | ||
|
|
@@ -742,7 +749,7 @@ This second approach involves creating a new, separate version (ie. export) of t | |
|
|
||
| If possible, the legacy version of the component should be rewritten so that it uses the same underlying implementation of the new version, with an extra API "translation" layer to adapt the legacy API surface to the new API surface, e.g: | ||
|
|
||
| ``` | ||
| ```tsx | ||
| // legacy-component/index.tsx | ||
|
|
||
| function LegacyComponent( props ) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -655,7 +655,8 @@ export default dedupePlugins( [ | |
| }, | ||
| }, | ||
|
|
||
| // Override: Components src — allow ariakit and framer-motion imports. | ||
| // Override: Components src — allow ariakit/framer-motion imports and | ||
| // prevent new Emotion usage while existing styles are migrated. | ||
| { | ||
| files: [ 'packages/components/src/**' ], | ||
| rules: { | ||
|
|
@@ -666,8 +667,9 @@ export default dedupePlugins( [ | |
| ( { name } ) => | ||
| ! [ '@ariakit/react', 'framer-motion' ].includes( | ||
| name | ||
| ) | ||
| ) && ! name.startsWith( '@emotion/' ) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is to prevent emotion imports from being flagged twice
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also considered just restricting all
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we could start with the components package, and then apply the refactor to the whole repo after |
||
| ), | ||
| patterns: [ '@emotion/**' ], | ||
| }, | ||
| ], | ||
| }, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.