Skip to content
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
### Internal

- `ResizableBox`: Make the `children` prop optional ([#79370](https://github.com/WordPress/gutenberg/pull/79370)).
- Add documentation and lint guardrails for the ongoing Emotion migration ([#79442](https://github.com/WordPress/gutenberg/pull/79442)).
- Adopt `--wpds-dimension-size-*` design tokens [#79093](https://github.com/WordPress/gutenberg/pull/79093).
- Point the legacy `--wp-components-*` color fallbacks at the design system tokens (`--wpds-*` / `--wp-admin-theme-color*`), so component styles get sensible defaults from the prebuilt token stylesheet without a runtime `<ThemeProvider>` ([#78664](https://github.com/WordPress/gutenberg/pull/78664)).
- `withFallbackStyles`: Refactor from a class component to a function component with hooks ([#78837](https://github.com/WordPress/gutenberg/pull/78837)).
Expand Down
59 changes: 33 additions & 26 deletions packages/components/CONTRIBUTING.md
Comment thread
ciampo marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
) }

@jsnajdr jsnajdr Jun 24, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: this is a suboptimal usage of clsx, it should be:

clsx( 'components-my-component', styles.root, className, {
  [ styles.deprecatedOuterMargins ]: ! __nextHasNoOuterMargins,
} );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've noticed that this object key style can emit a literal undefined class in Jest tests. (Perhaps not super important, but a notable difference between the two styles.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That can happen when styles.deprecatedOuterMargins is undefined. The value used as a key. And yes, the non-object form avoids that, because the result of the && expression is undefined and therefore ignored.

@ciampo ciampo Jun 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -683,7 +690,7 @@ component-name/
├── hook.ts
├── index.ts
├── README.md
├── styles.ts
├── style.module.scss
└── types.ts
```

Expand All @@ -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
Expand Down Expand Up @@ -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 ) {
Expand Down
6 changes: 4 additions & 2 deletions tools/eslint/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -666,8 +667,9 @@ export default dedupePlugins( [
( { name } ) =>
! [ '@ariakit/react', 'framer-motion' ].includes(
name
)
) && ! name.startsWith( '@emotion/' )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This check is to prevent emotion imports from being flagged twice

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I also considered just restricting all @emotion imports everywhere, but… yeah I guess this is fine as a short term thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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/**' ],
},
],
},
Expand Down
Loading
Loading