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 .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ module.exports = {
],
'@wordpress/no-unsafe-wp-apis': 'off',
'@wordpress/data-no-store-string-literals': 'error',
'@wordpress/use-recommended-components': 'error',
'eslint-comments/no-unused-disable': 'error',
'import/default': 'error',
'import/named': 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalHStack as HStack,
// TODO: Replace this ZStack with ad hoc CSS.
// eslint-disable-next-line @wordpress/use-recommended-components
__experimentalZStack as ZStack,
__experimentalDropdownContentWrapper as DropdownContentWrapper,
ColorIndicator,
Expand All @@ -22,6 +24,7 @@ import {
import { useCallback, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { getValueFromVariable } from '@wordpress/global-styles-engine';
import { reset as resetIcon } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -31,7 +34,6 @@ import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { useToolsPanelDropdownMenuProps } from './utils';
import { setImmutably } from '../../utils/object';
import { unlock } from '../../lock-unlock';
import { reset as resetIcon } from '@wordpress/icons';

export function useHasColorPanel( settings ) {
const hasTextPanel = useHasTextPanel( settings );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalHStack as HStack,
// TODO: Replace this ZStack with ad hoc CSS.
// eslint-disable-next-line @wordpress/use-recommended-components
__experimentalZStack as ZStack,
__experimentalDropdownContentWrapper as DropdownContentWrapper,
MenuGroup,
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Features

- Added [`use-recommended-components`](https://github.com/WordPress/gutenberg/blob/HEAD/packages/eslint-plugin/docs/rules/use-recommended-components.md) rule to encourage the use of recommended UI components in a WordPress environment ([#76222](https://github.com/WordPress/gutenberg/pull/76222)).

## 24.3.0 (2026-03-04)

### New Features
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The granular rulesets will not define any environment globals. As such, if they
| [no-i18n-in-save](https://github.com/WordPress/gutenberg/tree/HEAD/packages/eslint-plugin/docs/rules/no-i18n-in-save.md) | Disallow translation functions in block save methods. | |
| [no-unguarded-get-range-at](https://github.com/WordPress/gutenberg/tree/HEAD/packages/eslint-plugin/docs/rules/no-unguarded-get-range-at.md) | Disallow the usage of unguarded `getRangeAt` calls. | ✓ |
| [no-unsafe-wp-apis](https://github.com/WordPress/gutenberg/tree/HEAD/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md) | Disallow the usage of unsafe APIs from `@wordpress/*` packages | ✓ |
| [use-recommended-components](https://github.com/WordPress/gutenberg/tree/HEAD/packages/eslint-plugin/docs/rules/use-recommended-components.md) | Encourage the use of recommended UI components in a WordPress environment. | |
| [no-unused-vars-before-return](https://github.com/WordPress/gutenberg/tree/HEAD/packages/eslint-plugin/docs/rules/no-unused-vars-before-return.md) | Disallow assigning variable values if unused before a return. | ✓ |
| [no-wp-process-env](https://github.com/WordPress/gutenberg/tree/HEAD/packages/eslint-plugin/docs/rules/no-wp-process-env.md) | Disallow legacy usage of WordPress variables via `process.env` like `process.env.SCRIPT_DEBUG`. | ✓ |
| [react-no-unsafe-timeout](https://github.com/WordPress/gutenberg/tree/HEAD/packages/eslint-plugin/docs/rules/react-no-unsafe-timeout.md) | Disallow unsafe `setTimeout` in component. | |
Expand Down
25 changes: 25 additions & 0 deletions packages/eslint-plugin/docs/rules/use-recommended-components.md

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's also update the table in packages/eslint-plugin/README.md

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated in 7568ac1

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Encourage the use of recommended components (use-recommended-components)

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.

It might be worth a future refactor to generalize a rule for "don't use an named import from this package, use it from another" and not specifically to components. I see the value in pre-packaging the configuration for components usage, but there's some underlying usefulness to the general approach of redirecting package imports.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, possibly! Though, in the general case, these things have been motivated by deprecations, which can already be signaled by TS and deprecation messages. So in those cases a lint rule is convenient but also slightly redundant.


Encourages the use of recommended UI components in a WordPress environment by flagging imports that have a better alternative. The component lists are maintained directly in the rule and represent an evolving, up-to-date set of guidance.

## Rule details

Examples of **incorrect** code for this rule:

```js
// @wordpress/ui — this component isn't recommended yet.
import { SomeComponent } from '@wordpress/ui';

// @wordpress/components — a newer alternative is available.
import { TextControl } from '@wordpress/components';
```

Examples of **correct** code for this rule:

```js
// Packages not covered by the rule are unaffected.
import { Button } from '@wordpress/components';

// Default and namespace imports are not checked.
import UI from '@wordpress/ui';
```

@ciampo ciampo Mar 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we somehow test the denylist too? Either by adding a "test" entry, or by mocking it for the purpose of the tests?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added deny list entry in 47b5de8, added tests in 90fe6d0

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* eslint-env jest */

/**
* External dependencies
*/
import { RuleTester } from 'eslint';

/**
* Internal dependencies
*/
import rule, { ALLOWLIST, DENYLIST } from '../use-recommended-components';

const ruleTester = new RuleTester( {
parserOptions: {
sourceType: 'module',
ecmaVersion: 6,
},
} );

ruleTester.run( 'use-recommended-components', rule, {
valid: [
// Unrelated packages are not affected.
"import { Anything } from 'other-package';",
"import { Button } from '@wordpress/components';",

// Default and namespace imports are not affected.
"import UI from '@wordpress/ui';",
"import * as UI from '@wordpress/ui';",

// Allowed @wordpress/ui components.
"import { Badge } from '@wordpress/ui';",
"import { Stack } from '@wordpress/ui';",
"import { Badge, Stack } from '@wordpress/ui';",
],

invalid: [
// Allowlist: non-allowed @wordpress/ui imports are flagged.
{
code: "import { SomeComponent } from '@wordpress/ui';",
errors: [
{
message:
'`SomeComponent` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.',
type: 'ImportSpecifier',
},
],
},
{
code: "import { Foo, Bar } from '@wordpress/ui';",
errors: [
{
message:
'`Foo` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.',
type: 'ImportSpecifier',
},
{
message:
'`Bar` from `@wordpress/ui` is not yet recommended for use in a WordPress environment.',
type: 'ImportSpecifier',
},
],
},
// Denylist: denied components are flagged with their message.
{
code: "import { __experimentalZStack } from '@wordpress/components';",
errors: [
{
message:
'__experimentalZStack is planned for deprecation. Write your own CSS instead.',
type: 'ImportSpecifier',
},
],
},
{
code: "import { __experimentalZStack as ZStack } from '@wordpress/components';",
errors: [
{
message:
'__experimentalZStack is planned for deprecation. Write your own CSS instead.',
type: 'ImportSpecifier',
},
],
},
],
} );

describe( 'ALLOWLIST and DENYLIST', () => {
it( 'should not have overlapping package keys', () => {
const allowlistPackages = Object.keys( ALLOWLIST );
const denylistPackages = Object.keys( DENYLIST );
const overlap = allowlistPackages.filter( ( pkg ) =>
denylistPackages.includes( pkg )
);
expect( overlap ).toEqual( [] );
} );
} );
114 changes: 114 additions & 0 deletions packages/eslint-plugin/rules/use-recommended-components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Allowlist: only the listed components are permitted from these packages.
* Any other named import will be flagged with the package's message.
*
* `message` supports `{{ name }}` and `{{ source }}` placeholders.
*
* @type {Record<string, { allowed: string[], message?: string }>}
*/
const ALLOWLIST = {
'@wordpress/ui': {
allowed: [ 'Badge', 'Stack' ],
message:
'`{{ name }}` from `{{ source }}` is not yet recommended for use in a WordPress environment.',
},
};

/**
* Denylist: the listed components are flagged with a message pointing
* to a recommended alternative.
*
* Messages support `{{ name }}` and `{{ source }}` placeholders.
*
* @type {Record<string, Record<string, string>>}
*/
const DENYLIST = {
'@wordpress/components': {
__experimentalZStack:
'{{ name }} is planned for deprecation. Write your own CSS instead.',
},
};
Comment on lines +9 to +30

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know it's an unlikely edge case, but should we handle the situation in which a component is both in the allow and deny list?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added tests in 90fe6d0


/** @type {import('eslint').Rule.RuleModule} */
const rule = {
meta: {
type: 'suggestion',
docs: {
description:
'Encourage the use of recommended UI components in a WordPress environment.',
url: 'https://github.com/WordPress/gutenberg/blob/HEAD/packages/eslint-plugin/docs/rules/use-recommended-components.md',
},
schema: [],
},
create( context ) {
return {
/** @param {import('estree').ImportDeclaration} node */
ImportDeclaration( node ) {
if ( typeof node.source.value !== 'string' ) {
return;
}

const source = node.source.value;

const allowlistEntry = ALLOWLIST[ source ];
const denylistEntry = DENYLIST[ source ];

if ( ! allowlistEntry && ! denylistEntry ) {
return;
}

node.specifiers.forEach( ( specifier ) => {
if ( specifier.type !== 'ImportSpecifier' ) {
return;
}

const name = specifier.imported.name;

if (
allowlistEntry &&
! allowlistEntry.allowed.includes( name )
) {
context.report( {
node: specifier,
message: resolveMessage(
allowlistEntry.message,
name,
source
),
} );
}

if ( denylistEntry?.hasOwnProperty( name ) ) {
context.report( {
node: specifier,
message: resolveMessage(
denylistEntry[ name ],
name,
source
),
} );
}
} );
},
};
},
};

/**
* @param {string|undefined} template
* @param {string} name
* @param {string} source
* @return {string} Resolved message string.
*/
function resolveMessage( template, name, source ) {
if ( ! template ) {
return `\`${ name }\` from \`${ source }\` is not recommended.`;
}
return template
.replace( /\{\{\s*name\s*\}\}/g, name )
.replace( /\{\{\s*source\s*\}\}/g, source );
}

module.exports = rule;
module.exports.ALLOWLIST = ALLOWLIST;
module.exports.DENYLIST = DENYLIST;
2 changes: 2 additions & 0 deletions packages/global-styles-ui/src/palette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
__experimentalItemGroup as ItemGroup,
FlexItem,
__experimentalHStack as HStack,
// TODO: Replace this ZStack with ad hoc CSS.
// eslint-disable-next-line @wordpress/use-recommended-components
__experimentalZStack as ZStack,
__experimentalVStack as VStack,
ColorIndicator,
Expand Down
9 changes: 0 additions & 9 deletions packages/ui/AGENTS.md

This file was deleted.

1 change: 0 additions & 1 deletion packages/ui/CLAUDE.md

This file was deleted.

2 changes: 2 additions & 0 deletions routes/content-guidelines/components/actions-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useRef, useState } from '@wordpress/element';
// TODO: Revert to the `Notice` in `@wordpress/components` for now.
// eslint-disable-next-line @wordpress/use-recommended-components
import { Notice } from '@wordpress/ui';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
// TODO: Revert to the `Notice` in `@wordpress/components` for now.
// eslint-disable-next-line @wordpress/use-recommended-components
Comment on lines +16 to +17

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(Discussed with the team in charge)

import { Notice } from '@wordpress/ui';
import { createElement, useMemo, useState } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
Expand Down
2 changes: 2 additions & 0 deletions routes/content-guidelines/components/block-guidelines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
__experimentalHStack as HStack,
__experimentalText as Text,
} from '@wordpress/components';
// TODO: Revert to the `Notice` in `@wordpress/components` for now.
// eslint-disable-next-line @wordpress/use-recommended-components
import { Notice } from '@wordpress/ui';
import {
DataViews,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import { Button, __experimentalVStack as VStack } from '@wordpress/components';
import { DataForm } from '@wordpress/dataviews';
import type { Field, Form } from '@wordpress/dataviews';
// TODO: Revert to the `Notice` in `@wordpress/components` for now.
// eslint-disable-next-line @wordpress/use-recommended-components
import { Notice } from '@wordpress/ui';
import { __, sprintf } from '@wordpress/i18n';
import {
Expand Down
2 changes: 2 additions & 0 deletions routes/content-guidelines/stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
Navigator,
__experimentalVStack as VStack,
} from '@wordpress/components';
// TODO: Revert to the `Notice` in `@wordpress/components` for now.
// eslint-disable-next-line @wordpress/use-recommended-components
import { Notice } from '@wordpress/ui';

/**
Expand Down
Loading