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

Box Control: Add Runtime Check & Conditional Types for presets and presetKey Props #68385

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -23,6 +23,7 @@
- `Text`: Fix text contrast for dark mode ([#68349](https://github.com/WordPress/gutenberg/pull/68349)).
- `Heading`: Revert text contrast fix for dark mode with optimizeReadabilityFor ([#68472](https://github.com/WordPress/gutenberg/pull/68472)).
- `Text`: Revert text contrast fix for dark mode with optimizeReadabilityFor ([#68472](https://github.com/WordPress/gutenberg/pull/68472)).
- `BoxControl`: Add runtime check for presets and presetKey ([#68385](https://github.com/WordPress/gutenberg/pull/68385)).

### Deprecations

Expand Down
9 changes: 9 additions & 0 deletions packages/components/src/box-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { useInstanceId } from '@wordpress/compose';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import warning from '@wordpress/warning';

/**
* Internal dependencies
Expand Down Expand Up @@ -166,6 +167,14 @@ function BoxControl( {
} );
const sidesToRender = getAllowedSides( sides );

if ( ( presets && ! presetKey ) || ( ! presets && presetKey ) ) {
const definedProp = presets ? 'presets' : 'presetKey';
const missingProp = presets ? 'presetKey' : 'presets';
warning(
`wp.components.BoxControl: the '${ missingProp }' prop is required when the '${ definedProp }' prop is defined.`
);
}

return (
<Grid
id={ id }
Expand Down
11 changes: 9 additions & 2 deletions packages/components/src/box-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ import { useState } from '@wordpress/element';
import BoxControl from '..';
import type { BoxControlProps, BoxControlValue } from '../types';

// Since `BoxControlProps` is a the result of type unions, we need to use
// a distributive version of the standard `Omit` utility.
// See https://stackoverflow.com/a/57103940
type DistributiveOmit< T, K extends keyof any > = T extends any
? Omit< T, K >
: never;

const ControlledBoxControl = (
extraProps: Omit< BoxControlProps, 'onChange' >
extraProps: DistributiveOmit< BoxControlProps, 'onChange' >
) => {
const [ state, setState ] = useState< BoxControlValue >();

Expand All @@ -33,7 +40,7 @@ const ControlledBoxControl = (
const UncontrolledBoxControl = ( {
onChange = () => {},
...props
}: Omit< BoxControlProps, 'onChange' > & {
}: DistributiveOmit< BoxControlProps, 'onChange' > & {
onChange?: BoxControlProps[ 'onChange' ];
} ) => <BoxControl __next40pxDefaultSize onChange={ onChange } { ...props } />;

Expand Down
37 changes: 26 additions & 11 deletions packages/components/src/box-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,32 @@ export type BoxControlProps = Pick< UnitControlProps, 'units' > &
* @default false
*/
__next40pxDefaultSize?: boolean;
/**
* Available presets to pick from.
*/
presets?: Preset[];
/**
* The key of the preset to apply.
* If you provide a list of presets, you must provide a preset key to use.
* The format of preset selected values is going to be `var:preset|${ presetKey }|${ presetSlug }`
*/
presetKey?: string;
};
} & (
| {
/**
* Available presets to pick from.
*/
presets?: never;
/**
* The key of the preset to apply.
* If you provide a list of presets, you must provide a preset key to use.
* The format of preset selected values is going to be `var:preset|${ presetKey }|${ presetSlug }`
*/
presetKey?: never;
}
| {
/**
* Available presets to pick from.
*/
presets: Preset[];
/**
* The key of the preset to apply.
* If you provide a list of presets, you must provide a preset key to use.
* The format of preset selected values is going to be `var:preset|${ presetKey }|${ presetSlug }`
*/
presetKey: string;
}
);

export type BoxControlInputControlProps = UnitControlPassthroughProps & {
onChange?: ( nextValues: BoxControlValue ) => void;
Expand Down
Loading