Skip to content

Commit

Permalink
CheckboxControl: Add custom label example to Storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Jan 30, 2024
1 parent 6a84d8f commit 16e9d1e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/components/src/checkbox-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useState } from '@wordpress/element';
*/
import CheckboxControl from '..';
import { VStack } from '../../v-stack';
import { HStack } from '../../h-stack';

const meta: Meta< typeof CheckboxControl > = {
component: CheckboxControl,
Expand Down Expand Up @@ -115,3 +116,46 @@ Indeterminate.args = {
label: 'Select all',
__nextHasNoMarginBottom: true,
};

/**
* For more complex designs, a custom `<label>` element can be associated with the checkbox
* by leaving the `label` prop undefined and using the `id` and `htmlFor` props instead.
* Because the label element also functions as a click target for the checkbox, [do not
* place interactive elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label#interactive_content)
* such as links or buttons inside the `<label>` node.
*
* Similarly, a custom description can be added by omitting the `help` prop
* and using the `aria-describedby` prop instead.
*/
export const WithCustomLabel: StoryFn< typeof CheckboxControl > = ( {
onChange,
...args
} ) => {
const [ isChecked, setChecked ] = useState( true );

return (
<HStack justify="flex-start" alignment="top" spacing={ 0 }>
<CheckboxControl
{ ...args }
checked={ isChecked }
onChange={ ( v ) => {
setChecked( v );
onChange( v );
} }
id="my-checkbox-with-custom-label"
aria-describedby="my-custom-description"
/>
<VStack>
<label htmlFor="my-checkbox-with-custom-label">
My custom label
</label>
<div id="my-custom-description" style={ { fontSize: 13 } }>
A custom description.
</div>
</VStack>
</HStack>
);
};
WithCustomLabel.args = {
__nextHasNoMarginBottom: true,
};

0 comments on commit 16e9d1e

Please sign in to comment.