Skip to content

Commit

Permalink
FormTokenField, ComboboxControl: Add opt-in prop for next 40px defaul…
Browse files Browse the repository at this point in the history
…t size (#50261)

* FormTokenField: Add opt-in prop for next 40px default size

* ComboboxControl: Add opt-in prop for next 40px default size

* Update changelog
  • Loading branch information
mirka authored May 4, 2023
1 parent 2afdedf commit 33fbd4a
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

### Enhancements

- `FormTokenField`, `ComboboxControl`: Add `__next40pxDefaultSize` prop to opt into the new 40px default size, superseding the `__next36pxDefaultSize` prop ([#50261](https://github.com/WordPress/gutenberg/pull/50261)).
- `Modal`: Add css class to children container ([#50099](https://github.com/WordPress/gutenberg/pull/50099)).
- `PaletteEdit`: Allow custom popover configuration ([#49975](https://github.com/WordPress/gutenberg/pull/49975)).

Expand Down
42 changes: 24 additions & 18 deletions packages/components/src/combobox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useControlledValue } from '../utils/hooks';
import { normalizeTextString } from '../utils/strings';
import type { ComboboxControlOption, ComboboxControlProps } from './types';
import type { TokenInputProps } from '../form-token-field/types';
import { useDeprecated36pxDefaultSizeProp } from '../utils/use-deprecated-props';

const noop = () => {};

Expand Down Expand Up @@ -104,23 +105,28 @@ const getIndexOfMatchingSuggestion = (
* }
* ```
*/
function ComboboxControl( {
__nextHasNoMarginBottom = false,
__next36pxDefaultSize = false,
value: valueProp,
label,
options,
onChange: onChangeProp,
onFilterValueChange = noop,
hideLabelFromVision,
help,
allowReset = true,
className,
messages = {
selected: __( 'Item selected.' ),
},
__experimentalRenderItem,
}: ComboboxControlProps ) {
function ComboboxControl( props: ComboboxControlProps ) {
const {
__nextHasNoMarginBottom = false,
__next40pxDefaultSize = false,
value: valueProp,
label,
options,
onChange: onChangeProp,
onFilterValueChange = noop,
hideLabelFromVision,
help,
allowReset = true,
className,
messages = {
selected: __( 'Item selected.' ),
},
__experimentalRenderItem,
} = useDeprecated36pxDefaultSizeProp< ComboboxControlProps >(
props,
'wp.components.ComboboxControl'
);

const [ value, setValue ] = useControlledValue( {
value: valueProp,
onChange: onChangeProp,
Expand Down Expand Up @@ -314,7 +320,7 @@ function ComboboxControl( {
onKeyDown={ onKeyDown }
>
<InputWrapperFlex
__next36pxDefaultSize={ __next36pxDefaultSize }
__next40pxDefaultSize={ __next40pxDefaultSize }
>
<FlexBlock>
<TokenInput
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/combobox-control/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const Template: ComponentStory< typeof ComboboxControl > = ( {
};
export const Default = Template.bind( {} );
Default.args = {
__next36pxDefaultSize: false,
allowReset: false,
label: 'Select a country',
options: countryOptions,
Expand Down
8 changes: 4 additions & 4 deletions packages/components/src/combobox-control/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import { space } from '../ui/utils/space';
import type { ComboboxControlProps } from './types';

const deprecatedDefaultSize = ( {
__next36pxDefaultSize,
}: Pick< ComboboxControlProps, '__next36pxDefaultSize' > ) =>
! __next36pxDefaultSize &&
__next40pxDefaultSize,
}: Pick< ComboboxControlProps, '__next40pxDefaultSize' > ) =>
! __next40pxDefaultSize &&
css`
height: 28px; // 30px - 2px vertical borders on parent container
padding-left: ${ space( 1 ) };
padding-right: ${ space( 1 ) };
`;

export const InputWrapperFlex = styled( Flex )`
height: 34px; // 36px - 2px vertical borders on parent container
height: 38px; // 40px - 2px vertical borders on parent container
padding-left: ${ space( 2 ) };
padding-right: ${ space( 2 ) };
Expand Down
9 changes: 8 additions & 1 deletion packages/components/src/combobox-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ export type ComboboxControlProps = Pick<
item: ComboboxControlOption;
} ) => React.ReactNode;
/**
* Start opting into the larger default height that will become the default size in a future version.
* Deprecated. Use `__next40pxDefaultSize` instead.
*
* @default false
* @deprecated
*/
__next36pxDefaultSize?: boolean;
/**
* Start opting into the larger default height that will become the default size in a future version.
*
* @default false
*/
__next40pxDefaultSize?: boolean;
/**
* Show a reset button to clear the input.
*
Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/form-token-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
StyledLabel,
} from '../base-control/styles/base-control-styles';
import { Spacer } from '../spacer';
import { useDeprecated36pxDefaultSizeProp } from '../utils/use-deprecated-props';

const identity = ( value: string ) => value;

Expand Down Expand Up @@ -69,10 +70,13 @@ export function FormTokenField( props: FormTokenFieldProps ) {
__experimentalExpandOnFocus = false,
__experimentalValidateInput = () => true,
__experimentalShowHowTo = true,
__next36pxDefaultSize = false,
__next40pxDefaultSize = false,
__experimentalAutoSelectFirstMatch = false,
__nextHasNoMarginBottom = false,
} = props;
} = useDeprecated36pxDefaultSizeProp< FormTokenFieldProps >(
props,
'wp.components.FormTokenField'
);

const instanceId = useInstanceId( FormTokenField );

Expand Down Expand Up @@ -702,7 +706,7 @@ export function FormTokenField( props: FormTokenFieldProps ) {
align="center"
gap={ 1 }
wrap={ true }
__next36pxDefaultSize={ __next36pxDefaultSize }
__next40pxDefaultSize={ __next40pxDefaultSize }
hasTokens={ !! value.length }
>
{ renderTokensAndInput() }
Expand Down
8 changes: 4 additions & 4 deletions packages/components/src/form-token-field/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import { Flex } from '../flex';
import { space } from '../ui/utils/space';

type TokensAndInputWrapperProps = {
__next36pxDefaultSize: boolean;
__next40pxDefaultSize: boolean;
hasTokens: boolean;
};

const deprecatedPaddings = ( {
__next36pxDefaultSize,
__next40pxDefaultSize,
hasTokens,
}: TokensAndInputWrapperProps ) =>
! __next36pxDefaultSize &&
! __next40pxDefaultSize &&
css`
padding-top: ${ space( hasTokens ? 1 : 0.5 ) };
padding-bottom: ${ space( hasTokens ? 1 : 0.5 ) };
`;

export const TokensAndInputWrapperFlex = styled( Flex )`
padding: 5px ${ space( 1 ) };
padding: 7px;
${ deprecatedPaddings }
`;
9 changes: 8 additions & 1 deletion packages/components/src/form-token-field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,20 @@ export interface FormTokenFieldProps
* @default true
*/
__experimentalShowHowTo?: boolean;
/**
* Deprecated. Use `__next40pxDefaultSize` instead.
*
* @default false
* @deprecated
*/
__next36pxDefaultSize?: boolean;
/**
* Start opting into the larger default height that will become the
* default size in a future version.
*
* @default false
*/
__next36pxDefaultSize?: boolean;
__next40pxDefaultSize?: boolean;
/**
* If true, the select the first matching suggestion when the user presses
* the Enter key (or space when tokenizeOnSpace is true).
Expand Down
29 changes: 29 additions & 0 deletions packages/components/src/utils/use-deprecated-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';

export function useDeprecated36pxDefaultSizeProp<
P extends Record< string, any > & {
__next36pxDefaultSize?: boolean;
__next40pxDefaultSize?: boolean;
}
>(
props: P,
/** The component identifier in dot notation, e.g. `wp.components.ComponentName`. */
componentIdentifier: string
) {
const { __next36pxDefaultSize, __next40pxDefaultSize, ...otherProps } =
props;
if ( typeof __next36pxDefaultSize !== 'undefined' ) {
deprecated( '`__next36pxDefaultSize` prop in ' + componentIdentifier, {
alternative: '`__next40pxDefaultSize`',
since: '6.3',
} );
}

return {
...otherProps,
__next40pxDefaultSize: __next40pxDefaultSize ?? __next36pxDefaultSize,
};
}

0 comments on commit 33fbd4a

Please sign in to comment.