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

UnitControl : Deprecate 36px default size #66791

Merged
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 @@ -54,6 +54,7 @@
### Deprecations

- `Radio`: Deprecate 36px default size ([#66572](https://github.com/WordPress/gutenberg/pull/66572)).
- `UnitControl`: Deprecate 36px default size ([#66791](https://github.com/WordPress/gutenberg/pull/66791)).
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function AllInputControl( {
<>
<StyledUnitControl
{ ...props }
__shouldNotWarnDeprecated36pxSize
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved
__next40pxDefaultSize={ __next40pxDefaultSize }
className="component-box-control__unit-control"
disableUnits={ isMixed }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default function AxialInputControls( {
<Tooltip placement="top-end" text={ LABELS[ side ] }>
<StyledUnitControl
{ ...props }
__shouldNotWarnDeprecated36pxSize
__next40pxDefaultSize={ __next40pxDefaultSize }
className="component-box-control__unit-control"
id={ inputId }
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/box-control/input-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export default function BoxInputControls( {
<Tooltip placement="top-end" text={ LABELS[ side ] }>
<StyledUnitControl
{ ...props }
__shouldNotWarnDeprecated36pxSize
__next40pxDefaultSize={ __next40pxDefaultSize }
className="component-box-control__unit-control"
id={ inputId }
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/unit-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { __experimentalUnitControl as UnitControl } from '@wordpress/components'
const Example = () => {
const [ value, setValue ] = useState( '10px' );

return <UnitControl onChange={ setValue } value={ value } />;
return <UnitControl __next40pxDefaultSize onChange={ setValue } value={ value } />;
};
```

Expand Down
9 changes: 9 additions & 0 deletions packages/components/src/unit-control/index.tsx
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { useControlledState } from '../utils/hooks';
import { escapeRegExp } from '../utils/strings';
import type { UnitControlProps, UnitControlOnChangeCallback } from './types';
import { useDeprecated36pxDefaultSizeProp } from '../utils/use-deprecated-props';
import { maybeWarnDeprecated36pxSize } from '../utils/deprecated-36px-size';

function UnforwardedUnitControl(
unitControlProps: WordPressComponentProps<
Expand Down Expand Up @@ -55,9 +56,17 @@ function UnforwardedUnitControl(
units: unitsProp = CSS_UNITS,
value: valueProp,
onFocus: onFocusProp,
__shouldNotWarnDeprecated36pxSize,
...props
} = useDeprecated36pxDefaultSizeProp( unitControlProps );

maybeWarnDeprecated36pxSize( {
componentName: 'UnitControl',
__next40pxDefaultSize: props.__next40pxDefaultSize,
size,
__shouldNotWarnDeprecated36pxSize,
} );

if ( 'unit' in unitControlProps ) {
deprecated( 'UnitControl unit prop', {
since: '5.6',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const Default: StoryFn< typeof UnitControl > = DefaultTemplate.bind(
);
Default.args = {
label: 'Label',
__next40pxDefaultSize: true,
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
77 changes: 51 additions & 26 deletions packages/components/src/unit-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useState } from '@wordpress/element';
*/
import UnitControl from '..';
import { CSS_UNITS, parseQuantityAndUnitFromRawValue } from '../utils';
import type { UnitControlProps } from '../types';

const getInput = ( {
isInputTypeText = false,
Expand Down Expand Up @@ -70,11 +71,13 @@ const ControlledSyncUnits = () => {
return (
<>
<UnitControl
__next40pxDefaultSize
label="Field A"
value={ state.valueA }
onChange={ ( v ) => onUnitControlChange( 'valueA', v ) }
/>
<UnitControl
__next40pxDefaultSize
label="Field B"
value={ state.valueB }
onChange={ ( v ) => onUnitControlChange( 'valueB', v ) }
Expand All @@ -83,10 +86,14 @@ const ControlledSyncUnits = () => {
);
};

const ControlledUnitControl = ( { ...props }: UnitControlProps ) => {
return <UnitControl __next40pxDefaultSize { ...props } />;
};
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved

describe( 'UnitControl', () => {
describe( 'Basic rendering', () => {
it( 'should render', () => {
render( <UnitControl /> );
render( <ControlledUnitControl /> );
const input = getInput();
const select = getSelect();

Expand All @@ -95,10 +102,12 @@ describe( 'UnitControl', () => {
} );

it( 'should render custom className', () => {
const { container: withoutClassName } = render( <UnitControl /> );
const { container: withoutClassName } = render(
<ControlledUnitControl />
);

const { container: withClassName } = render(
<UnitControl className="hello" />
<ControlledUnitControl className="hello" />
);

expect(
Expand All @@ -112,7 +121,7 @@ describe( 'UnitControl', () => {
} );

it( 'should not render select, if units are disabled', () => {
render( <UnitControl value="3em" units={ [] } /> );
render( <ControlledUnitControl value="3em" units={ [] } /> );
const input = getInput();
// Using `queryByRole` instead of `getSelect` because we need to test
// for this element NOT to be in the document.
Expand All @@ -123,7 +132,11 @@ describe( 'UnitControl', () => {
} );

it( 'should render label if single units', () => {
render( <UnitControl units={ [ { value: '%', label: '%' } ] } /> );
render(
<ControlledUnitControl
units={ [ { value: '%', label: '%' } ] }
/>
);

const select = screen.queryByRole( 'combobox' );
const label = screen.getByText( '%' );
Expand All @@ -138,7 +151,9 @@ describe( 'UnitControl', () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();

render( <UnitControl value="50px" onChange={ onChangeSpy } /> );
render(
<ControlledUnitControl value="50px" onChange={ onChangeSpy } />
);

const input = getInput();
await user.clear( input );
Expand All @@ -159,7 +174,9 @@ describe( 'UnitControl', () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();

render( <UnitControl value="50px" onChange={ onChangeSpy } /> );
render(
<ControlledUnitControl value="50px" onChange={ onChangeSpy } />
);

const input = getInput();
await user.type( input, '{ArrowUp}' );
Expand All @@ -175,7 +192,9 @@ describe( 'UnitControl', () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();

render( <UnitControl value="50px" onChange={ onChangeSpy } /> );
render(
<ControlledUnitControl value="50px" onChange={ onChangeSpy } />
);

const input = getInput();
await user.type( input, '{Shift>}{ArrowUp}{/Shift}' );
Expand All @@ -191,7 +210,9 @@ describe( 'UnitControl', () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();

render( <UnitControl value={ 50 } onChange={ onChangeSpy } /> );
render(
<ControlledUnitControl value={ 50 } onChange={ onChangeSpy } />
);

const input = getInput();
await user.type( input, '{ArrowDown}' );
Expand All @@ -207,7 +228,9 @@ describe( 'UnitControl', () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();

render( <UnitControl value={ 50 } onChange={ onChangeSpy } /> );
render(
<ControlledUnitControl value={ 50 } onChange={ onChangeSpy } />
);

const input = getInput();
await user.type( input, '{Shift>}{ArrowDown}{/Shift}' );
Expand All @@ -224,7 +247,7 @@ describe( 'UnitControl', () => {
const onChangeSpy = jest.fn();

render(
<UnitControl
<ControlledUnitControl
value={ 50 }
onChange={ onChangeSpy }
isPressEnterToChange
Expand Down Expand Up @@ -252,7 +275,7 @@ describe( 'UnitControl', () => {
const onBlurSpy = jest.fn();

render(
<UnitControl
<ControlledUnitControl
value="33%"
onChange={ onChangeSpy }
onBlur={ onBlurSpy }
Expand Down Expand Up @@ -281,7 +304,7 @@ describe( 'UnitControl', () => {
const onChangeSpy = jest.fn();

render(
<UnitControl
<ControlledUnitControl
value="15px"
onChange={ onChangeSpy }
isPressEnterToChange
Expand Down Expand Up @@ -314,7 +337,7 @@ describe( 'UnitControl', () => {
render(
<>
<button>Click me</button>
<UnitControl
<ControlledUnitControl
units={ [ { value: '%', label: '%' } ] }
onChange={ onChangeSpy }
/>
Expand Down Expand Up @@ -347,7 +370,7 @@ describe( 'UnitControl', () => {
const onUnitChangeSpy = jest.fn();

render(
<UnitControl
<ControlledUnitControl
value="14rem"
onChange={ onChangeSpy }
onUnitChange={ onUnitChangeSpy }
Expand Down Expand Up @@ -377,7 +400,7 @@ describe( 'UnitControl', () => {
{ value: '+', label: '+', default: 10 },
];

render( <UnitControl units={ units } /> );
render( <ControlledUnitControl units={ units } /> );

const options = getSelectOptions();

Expand All @@ -400,7 +423,7 @@ describe( 'UnitControl', () => {
];

render(
<UnitControl
<ControlledUnitControl
isResetValueOnUnitChange
units={ units }
onChange={ onChangeSpy }
Expand Down Expand Up @@ -436,7 +459,7 @@ describe( 'UnitControl', () => {
];

render(
<UnitControl
<ControlledUnitControl
isResetValueOnUnitChange={ false }
value={ 50 }
units={ units }
Expand Down Expand Up @@ -467,7 +490,7 @@ describe( 'UnitControl', () => {
const onChangeSpy = jest.fn();

render(
<UnitControl
<ControlledUnitControl
value="50%"
units={ [ { value: '%', label: '%' } ] }
onChange={ onChangeSpy }
Expand Down Expand Up @@ -527,7 +550,7 @@ describe( 'UnitControl', () => {
{ value: 'vmax', label: 'vmax' },
];

render( <UnitControl units={ units } value="5" /> );
render( <ControlledUnitControl units={ units } value="5" /> );

const select = getSelect();
await user.selectOptions( select, [ 'vmax' ] );
Expand All @@ -545,7 +568,7 @@ describe( 'UnitControl', () => {
const onBlurSpy = jest.fn();

render(
<UnitControl
<ControlledUnitControl
value="15px"
onUnitChange={ onUnitChangeSpy }
onBlur={ onBlurSpy }
Expand All @@ -570,26 +593,28 @@ describe( 'UnitControl', () => {

describe( 'Unit Parser', () => {
it( 'should update unit after initial render and with new unit prop', async () => {
const { rerender } = render( <UnitControl value="10%" /> );
const { rerender } = render(
<ControlledUnitControl value="10%" />
);

const select = getSelect();

expect( select.value ).toBe( '%' );

rerender( <UnitControl value="20vh" /> );
rerender( <ControlledUnitControl value="20vh" /> );

expect( select.value ).toBe( 'vh' );
} );

it( 'should fallback to default unit if parsed unit is invalid', () => {
render( <UnitControl value="10null" /> );
render( <ControlledUnitControl value="10null" /> );

expect( getSelect().value ).toBe( 'px' );
} );

it( 'should display valid CSS unit when not explicitly included in units list', () => {
render(
<UnitControl
<ControlledUnitControl
value="10%"
units={ [
{ value: 'px', label: 'px' },
Expand All @@ -615,7 +640,7 @@ describe( 'UnitControl', () => {
const onUnitChangeSpy = jest.fn();

render(
<UnitControl
<ControlledUnitControl
value="10%"
onChange={ onChangeSpy }
onUnitChange={ onUnitChangeSpy }
Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/unit-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,11 @@ export type UnitControlProps = Pick< InputControlProps, 'size' > &
* Callback when either the quantity or the unit inputs gains focus.
*/
onFocus?: FocusEventHandler< HTMLInputElement | HTMLSelectElement >;
/**
* Do not throw a warning for the deprecated 36px default size.
* For internal components of other components that already throw the warning.
*
* @ignore
*/
__shouldNotWarnDeprecated36pxSize?: boolean;
};
Loading