Skip to content

Commit

Permalink
CheckboxControl: Add custom label example to Storybook (#58438)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Jan 30, 2024
1 parent 775eed2 commit 8c80c0e
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,
};

1 comment on commit 8c80c0e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8c80c0e.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7715123881
📝 Reported issues:

Please sign in to comment.