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: Fixed documentation for units attribute #31901

Merged
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
8 changes: 5 additions & 3 deletions packages/components/src/unit-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,23 @@ Collection of available units.
Example:

```jsx
import { __experimentalUnitControl as UnitControl } from '@wordpress/block-editor/';
import { __experimentalUnitControl as UnitControl } from '@wordpress/components';
import { useState } from '@wordpress/element';

const Example = () => {
const [ value, setValue ] = useState( '10px' );

const units = [
{ value: 'px', label: 'px', default: 0 },
{ value: '%', label: '%', default: 10 },
{ value: 'em', label: 'em', default: 0 },
];

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

A `default` value for (in the example above, `10` for `%`), if defined, is set as the new `value` when a unit changes. This is helpful in scenarios where changing a unit may cause drastic results, such as changing from `px` to `vh`.
A `default` value (in the example above, `10` for `%`), if defined, is set as the new `value` when a unit changes. This is helpful in scenarios where changing a unit may cause drastic results, such as changing from `px` to `vh`.

### value

Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/unit-control/stories/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { boolean, number, select, text } from '@storybook/addon-knobs';
import { boolean, number, select, text, object } from '@storybook/addon-knobs';
import styled from '@emotion/styled';

/**
Expand All @@ -13,6 +13,7 @@ import { useState } from '@wordpress/element';
* Internal dependencies
*/
import UnitControl from '../';
import { CSS_UNITS } from '../utils';

export default {
title: 'Components/UnitControl',
Expand Down Expand Up @@ -41,6 +42,7 @@ function Example() {
'default'
),
step: number( 'step', 1 ),
units: object( 'units', CSS_UNITS ),
};

return (
Expand Down