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

CustomSelectControl V2 legacy adapter: stabilize experimental props #63248

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function NonDefaultControls( { format, onChange } ) {
name: __( 'Custom' ),
className:
'block-editor-date-format-picker__custom-format-select-control__custom-option',
__experimentalHint: __( 'Enter your own date format' ),
hint: __( 'Enter your own date format' ),
};

const [ isCustom, setIsCustom ] = useState(
Expand Down
6 changes: 2 additions & 4 deletions packages/block-editor/src/hooks/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const STICKY_OPTION = {
key: 'sticky',
value: 'sticky',
name: _x( 'Sticky', 'Name for the value of the CSS position property' ),
__experimentalHint: __(
hint: __(
'The block will stick to the top of the window instead of scrolling.'
),
};
Expand All @@ -51,9 +51,7 @@ const FIXED_OPTION = {
key: 'fixed',
value: 'fixed',
name: _x( 'Fixed', 'Name for the value of the CSS position property' ),
__experimentalHint: __(
'The block will not move when the page is scrolled.'
),
hint: __( 'The block will not move when the page is scrolled.' ),
};

const POSITION_SIDES = [ 'top', 'right', 'bottom', 'left' ];
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `CustomSelectControlV2`: animate select popover appearance. ([#63343](https://github.com/WordPress/gutenberg/pull/63343))

### Enhancements

- `CustomSelectControl`: Stabilize `__experimentalShowSelectedHint` and `options[]. __experimentalHint` props ([#63248](https://github.com/WordPress/gutenberg/pull/63248)).

## 28.3.0 (2024-07-10)

### Enhancements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,41 @@ import CustomSelectItem from '../item';
import type { LegacyCustomSelectProps } from '../types';
import * as Styled from '../styles';

function useDeprecatedProps( {
__experimentalShowSelectedHint,
...otherProps
}: LegacyCustomSelectProps ) {
return {
showSelectedHint: __experimentalShowSelectedHint,
...otherProps,
};
}

// The removal of `__experimentalHint` in favour of `hint` doesn't happen in
// the `useDeprecatedProps` hook in order not to break consumers that rely
// on object identity (see https://github.com/WordPress/gutenberg/pull/63248#discussion_r1672213131)
function applyOptionDeprecations( {
__experimentalHint,
...rest
}: LegacyCustomSelectProps[ 'options' ][ number ] ) {
return {
hint: __experimentalHint,
...rest,
};
}
Comment on lines +28 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mirka Relatively to #63248 (comment), I applied your suggestion of adapting the options array at runtime where necessary


function CustomSelectControl( props: LegacyCustomSelectProps ) {
const {
__experimentalShowSelectedHint = false,
__next40pxDefaultSize = false,
describedBy,
options,
onChange,
size = 'default',
value,
className: classNameProp,
showSelectedHint = false,
...restProps
} = props;
} = useDeprecatedProps( props );

// Forward props + store from v2 implementation
const store = Ariakit.useSelectStore( {
Expand Down Expand Up @@ -60,16 +83,17 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) {
defaultValue: options[ 0 ]?.name,
} );

const children = options.map(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The following block of code is just about changing from mostly __experimentalHint to hint, the rest is whitespace-related changes (indentation)

( { name, key, __experimentalHint, style, className } ) => {
const children = options
.map( applyOptionDeprecations )
.map( ( { name, key, hint, style, className } ) => {
const withHint = (
<Styled.WithHintItemWrapper>
<span>{ name }</span>
<Styled.WithHintItemHint
// TODO: Legacy classname. Add V1 styles are removed from the codebase
// className="components-custom-select-control__item-hint"
>
{ __experimentalHint }
{ hint }
</Styled.WithHintItemHint>
</Styled.WithHintItemWrapper>
);
Expand All @@ -78,38 +102,39 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) {
<CustomSelectItem
key={ key }
value={ name }
children={ __experimentalHint ? withHint : name }
children={ hint ? withHint : name }
style={ style }
className={ clsx(
// TODO: Legacy classname. Add V1 styles are removed from the codebase
// 'components-custom-select-control__item',
className
// TODO: Legacy classname. Add V1 styles are removed from the codebase
// {
// 'has-hint': __experimentalHint,
// 'has-hint': hint,
// }
) }
/>
);
}
);
} );

const renderSelectedValueHint = () => {
const { value: currentValue } = store.getState();

const currentHint = options?.find(
( { name } ) => currentValue === name
);
const selectedOptionHint = options
?.map( applyOptionDeprecations )
?.find( ( { name } ) => currentValue === name )?.hint;

return (
<Styled.SelectedExperimentalHintWrapper>
{ currentValue }
<Styled.SelectedExperimentalHintItem
// TODO: Legacy classname. Add V1 styles are removed from the codebase
// className="components-custom-select-control__hint"
>
{ currentHint?.__experimentalHint }
</Styled.SelectedExperimentalHintItem>
{ selectedOptionHint && (
Copy link
Contributor Author

@ciampo ciampo Jul 8, 2024

Choose a reason for hiding this comment

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

Previously, we were rendering this component even where there wasn't a hint to show

Copy link
Member

Choose a reason for hiding this comment

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

Good catch!

<Styled.SelectedExperimentalHintItem
// TODO: Legacy classname. Add V1 styles are removed from the codebase
// className="components-custom-select-control__hint"
>
{ selectedOptionHint }
</Styled.SelectedExperimentalHintItem>
) }
</Styled.SelectedExperimentalHintWrapper>
);
};
Expand All @@ -131,9 +156,7 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) {
<_CustomSelect
aria-describedby={ describedBy }
renderSelectedValue={
__experimentalShowSelectedHint
? renderSelectedValueHint
: undefined
showSelectedHint ? renderSelectedValueHint : undefined
}
size={ translatedSize }
store={ store }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe.each( [
{
key: 'one',
name: 'One',
__experimentalHint: 'Hint',
hint: 'Hint',
},
] }
/>
Expand All @@ -309,7 +309,7 @@ describe.each( [
);
} );

it( 'shows selected hint when __experimentalShowSelectedHint is set', async () => {
it( 'shows selected hint when showSelectedHint is set', async () => {
render(
<Component
{ ...legacyProps }
Expand All @@ -318,10 +318,10 @@ describe.each( [
{
key: 'one',
name: 'One',
__experimentalHint: 'Hint',
hint: 'Hint',
},
] }
__experimentalShowSelectedHint
showSelectedHint
/>
);

Expand All @@ -334,7 +334,7 @@ describe.each( [
);
} );

it( 'shows selected hint in list of options when added, regardless of __experimentalShowSelectedHint prop', async () => {
it( 'shows selected hint in list of options when added, regardless of showSelectedHint prop', async () => {
render(
<Component
{ ...legacyProps }
Expand All @@ -343,7 +343,7 @@ describe.each( [
{
key: 'one',
name: 'One',
__experimentalHint: 'Hint',
hint: 'Hint',
},
] }
/>
Expand Down
16 changes: 14 additions & 2 deletions packages/components/src/custom-select-control-v2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ type LegacyOption = {
name: string;
style?: React.CSSProperties;
className?: string;
hint?: string;
/**
* Use the `hint` property instead
* @deprecated
* @ignore
*/
__experimentalHint?: string;
[ key: string ]: any;
};
Expand Down Expand Up @@ -169,11 +175,17 @@ export type LegacyCustomSelectProps = {
*/
value?: LegacyOption;
/**
* Legacy way to add additional text to the right of each option.
* Use the `showSelectedHint` property instead.
* @deprecated
* @ignore
*/
__experimentalShowSelectedHint?: boolean;
/**
* Show the hint of the selected item in the trigger button.
*
* @default false
*/
__experimentalShowSelectedHint?: boolean;
showSelectedHint?: boolean;
/**
* Opt-in prop for an unconstrained width style which became the default in
* WordPress 6.5. The prop is no longer needed and can be safely removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const FontSizePickerSelect = ( props: FontSizePickerSelectProps ) => {
key: fontSize.slug,
name: fontSize.name || fontSize.slug,
value: fontSize.size,
__experimentalHint: hint,
hint,
};
} ),
...( disableCustomFontSizes ? [] : [ CUSTOM_OPTION ] ),
Expand All @@ -79,7 +79,7 @@ const FontSizePickerSelect = ( props: FontSizePickerSelectProps ) => {
) }
options={ options }
value={ selectedOption }
__experimentalShowSelectedHint
showSelectedHint
onChange={ ( {
selectedItem,
}: {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/font-size-picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export type FontSizePickerSelectOption = {
key: string;
name: string;
value?: FontSize[ 'size' ];
__experimentalHint?: string;
hint?: string;
};

export type FontSizePickerToggleGroupProps = Pick<
Expand Down
Loading