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

CheckboxControl: Add custom label example to Storybook #58438

Merged
merged 1 commit into from
Jan 30, 2024
Merged
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
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,
};
Loading