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

RadioGroup: Log deprecation warning #68067

Merged
merged 4 commits into from
Dec 18, 2024
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 @@ -11,6 +11,7 @@
- `TreeSelect`: Deprecate 36px default size ([#67855](https://github.com/WordPress/gutenberg/pull/67855)).
- `SelectControl`: Deprecate 36px default size ([#66898](https://github.com/WordPress/gutenberg/pull/66898)).
- `InputControl`: Deprecate 36px default size ([#66897](https://github.com/WordPress/gutenberg/pull/66897)).
- `RadioGroup`: Log deprecation warning ([#68067](https://github.com/WordPress/gutenberg/pull/68067)).
- Soft deprecate `ButtonGroup` component. Use `ToggleGroupControl` instead ([#65429](https://github.com/WordPress/gutenberg/pull/65429)).

### Bug Fixes
Expand Down
12 changes: 7 additions & 5 deletions packages/components/src/button-group/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ function UnforwardedButtonGroup(
props: WordPressComponentProps< ButtonGroupProps, 'div', false >,
ref: ForwardedRef< HTMLDivElement >
) {
const { className, ...restProps } = props;
const { className, __shouldNotWarnDeprecated, ...restProps } = props;
const classes = clsx( 'components-button-group', className );

deprecated( 'wp.components.ButtonGroup', {
since: '6.8',
alternative: 'wp.components.ToggleGroupControl',
} );
if ( ! __shouldNotWarnDeprecated ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Adds a mechanism to suppress a confusing "double warning" coming from the ButtonGroup used internally in RadioGroup.

Copy link
Member

Choose a reason for hiding this comment

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

React uses global variables for this purpose:

let didWarnAboutSomething = false;
function something() {
  if ( ! didWarnAboutSomething ) {
    didWarnAboutSomething = true;
    console.error( /* ... */ );
  }
}

They also use Sets as didWarn* values, when they want to track a warning for each component name or something else separately.

We could use this, too, instead of the semi-official prop.

Copy link
Member

Choose a reason for hiding this comment

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

If we decide to go the didWarn way, I think it makes sense to implement it directly in the deprecated() helper function.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking about smarter ways to implement this mechanism when I started logging the size deprecation warnings, but came to the conclusion that an explicit prop was the only way. I am imagining consumer code like this, where the ButtonGroup warning inside the RadioGroup should be suppressed, but the standalone ButtonGroup instance should still warn:

const MyUI = () => (
  <RadioGroup />
  <ButtonGroup />
);

I'm all ears if you have any clever ideas!

Copy link
Contributor

Choose a reason for hiding this comment

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

Could component's internal context system help?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not in a way that's "more automatic" than the current manual prop-based system, I don't think. Also context could be problematic when render props or slots are involved (e.g. we don't want to suppress warnings on a component that the consumer passed in through a prefix prop).

deprecated( 'wp.components.ButtonGroup', {
since: '6.8',
alternative: 'wp.components.__experimentalToggleGroupControl',
Copy link
Member Author

Choose a reason for hiding this comment

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

Added the __experimental prefix for consistency.

} );
}

return (
<div ref={ ref } role="group" className={ classes } { ...restProps } />
Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/button-group/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ export type ButtonGroupProps = {
* The children elements.
*/
children: ReactNode;
/**
* Do not throw a warning for component deprecation.
* For internal components of other components that already throw the warning.
*
* @ignore
*/
__shouldNotWarnDeprecated?: boolean;
};
13 changes: 12 additions & 1 deletion packages/components/src/radio-group/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Ariakit from '@ariakit/react';
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import { useMemo, forwardRef } from '@wordpress/element';
import { isRTL } from '@wordpress/i18n';

Expand Down Expand Up @@ -46,11 +47,21 @@ function UnforwardedRadioGroup(
[ radioStore, disabled ]
);

deprecated( 'wp.components.__experimentalRadioGroup', {
alternative:
'wp.components.RadioControl or wp.components.__experimentalToggleGroupControl',
since: '6.8',
} );

return (
<RadioGroupContext.Provider value={ contextValue }>
<Ariakit.RadioGroup
store={ radioStore }
render={ <ButtonGroup>{ children }</ButtonGroup> }
render={
<ButtonGroup __shouldNotWarnDeprecated>
{ children }
</ButtonGroup>
}
aria-label={ label }
ref={ ref }
{ ...props }
Expand Down
Loading