Repository navigation
UI: Add SwitchControl component #83146
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { SwitchControl } from './switch-control'; |
77 changes: 77 additions & 0 deletions
77
packages/ui/src/form/switch-control/stories/index.story.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
| import { SwitchControl } from '../'; | ||
| import { | ||
| WITH_DETAILS_DESCRIPTION, | ||
| DETAILS_EXAMPLE, | ||
| } from '../../stories/shared'; | ||
|
|
||
| const meta: Meta< typeof SwitchControl > = { | ||
| title: 'Design System/Components/Form/SwitchControl', | ||
| component: SwitchControl, | ||
| argTypes: { | ||
| checked: { control: false }, | ||
| onCheckedChange: { action: 'onCheckedChange' }, | ||
| }, | ||
| parameters: { | ||
| componentStatus: { | ||
| status: 'use-with-caution', | ||
| whereUsed: 'global', | ||
| notes: 'Not yet recommended for use alongside components from `@wordpress/components`, pending review of style consistency with `@wordpress/components` and component set completeness. See [WordPress/gutenberg#76135](https://github.com/WordPress/gutenberg/issues/76135).', | ||
| }, | ||
| }, | ||
| }; | ||
| export default meta; | ||
|
|
||
| type Story = StoryObj< typeof SwitchControl >; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| label: 'Label', | ||
| }, | ||
| }; | ||
|
|
||
| export const WithDescription: Story = { | ||
| args: { | ||
| ...Default.args, | ||
| description: 'This is the description.', | ||
| }, | ||
| }; | ||
|
|
||
| export const WithLongLabel: Story = { | ||
| args: { | ||
| ...Default.args, | ||
| label: 'This is a long label that should wrap to the next line. This is a long label that should wrap to the next line. This is a long label that should wrap to the next line.', | ||
| }, | ||
| }; | ||
|
|
||
| export const VisuallyHiddenLabel: Story = { | ||
| args: { | ||
| ...Default.args, | ||
| hideLabelFromVision: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const WithDetails: Story = { | ||
| parameters: { | ||
| docs: { description: { story: WITH_DETAILS_DESCRIPTION } }, | ||
| }, | ||
| args: { | ||
| ...Default.args, | ||
| details: DETAILS_EXAMPLE, | ||
| }, | ||
| }; | ||
|
|
||
| export const Disabled: Story = { | ||
| args: { | ||
| ...Default.args, | ||
| disabled: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const DisabledChecked: Story = { | ||
| args: { | ||
| ...Default.args, | ||
| disabled: true, | ||
| defaultChecked: true, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| @layer wp-ui { | ||
| @layer utilities, components, compositions, overrides; | ||
|
|
||
| @layer components { | ||
| .root { | ||
| --_wp-ui-switch-control-switch-height: var(--wpds-dimension-size-2xs); | ||
| --_wp-ui-switch-control-label-line-height: max(var(--_wp-ui-switch-control-switch-height), var(--wpds-typography-line-height-xs)); | ||
| } | ||
|
|
||
| .switch-wrapper { | ||
| display: flex; | ||
| align-items: center; | ||
| /* Size the wrapper to one label line so the switch centers on the first line. */ | ||
| height: 1lh; | ||
| line-height: var(--_wp-ui-switch-control-label-line-height); | ||
| overflow: visible; | ||
| } | ||
|
|
||
| .label { | ||
| --wp-ui-field-label-line-height: var(--_wp-ui-switch-control-label-line-height); | ||
| } | ||
|
|
||
| .root:not(:has(.switch[data-disabled])) .label { | ||
| cursor: var(--wpds-cursor-control); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import clsx from 'clsx'; | ||
| import { forwardRef, Fragment } from '@wordpress/element'; | ||
| import { Field, Switch } from '../primitives'; | ||
| import { Stack } from '../../stack'; | ||
| import type { SwitchControlProps } from './types'; | ||
| import styles from './style.module.css'; | ||
|
|
||
| const ROOT_RENDER = <Stack gap="sm" align="start" />; | ||
|
|
||
| const TextContainer = ( { | ||
| isStack, | ||
| ...restProps | ||
| }: { | ||
| isStack?: boolean; | ||
| } & React.PropsWithChildren ) => | ||
| isStack ? ( | ||
| <Stack direction="column" gap="xs" { ...restProps } /> | ||
| ) : ( | ||
| <Fragment { ...restProps } /> | ||
| ); | ||
|
|
||
| /** | ||
| * A complete switch field with integrated label and description. | ||
| */ | ||
| export const SwitchControl = forwardRef< HTMLSpanElement, SwitchControlProps >( | ||
| function SwitchControl( | ||
| { | ||
| label, | ||
| description, | ||
| details, | ||
| hideLabelFromVision, | ||
| className, | ||
| name, | ||
| ...switchProps | ||
| }, | ||
| ref | ||
| ) { | ||
| const hasSupportingText = !! description || !! details; | ||
|
|
||
| return ( | ||
| <Field.Root | ||
| name={ name } | ||
| className={ clsx( styles.root, className ) } | ||
| render={ ROOT_RENDER } | ||
| > | ||
| <div className={ styles[ 'switch-wrapper' ] }> | ||
| <Switch | ||
| ref={ ref } | ||
| className={ styles.switch } | ||
| { ...switchProps } | ||
| /> | ||
| </div> | ||
| <TextContainer isStack={ hasSupportingText }> | ||
| <Field.Label | ||
| variant="plain" | ||
| hideFromVision={ hideLabelFromVision } | ||
| className={ styles.label } | ||
| > | ||
| { label } | ||
| </Field.Label> | ||
| { description && ( | ||
| <Field.Description>{ description }</Field.Description> | ||
| ) } | ||
| { details && <Field.Details>{ details }</Field.Details> } | ||
| </TextContainer> | ||
| </Field.Root> | ||
| ); | ||
| } | ||
| ); |
174 changes: 174 additions & 0 deletions
174
packages/ui/src/form/switch-control/test/index.jsdom.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { createRef } from '@wordpress/element'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { SwitchControl } from '../index'; | ||
|
|
||
| globalThis.wpVitest.mockPointerEvent(); | ||
|
|
||
| describe( 'SwitchControl', () => { | ||
| it( 'forwards ref', () => { | ||
| const ref = createRef< HTMLSpanElement >(); | ||
|
|
||
| render( <SwitchControl ref={ ref } label="Enable updates" /> ); | ||
|
|
||
| expect( ref.current ).toBeInstanceOf( HTMLSpanElement ); | ||
| } ); | ||
|
|
||
| it( 'renders with a visible label', () => { | ||
| render( <SwitchControl label="Enable updates" /> ); | ||
|
|
||
| expect( | ||
| screen.getByRole( 'switch', { name: 'Enable updates' } ) | ||
| ).toBeVisible(); | ||
| expect( screen.getByText( 'Enable updates' ) ).toBeVisible(); | ||
| } ); | ||
|
|
||
| it( 'renders with a visually hidden label', () => { | ||
| render( <SwitchControl label="Enable updates" hideLabelFromVision /> ); | ||
|
|
||
| expect( | ||
| screen.getByRole( 'switch', { name: 'Enable updates' } ) | ||
| ).toBeVisible(); | ||
| } ); | ||
|
|
||
| it( 'renders accessible label and description', () => { | ||
| render( | ||
| <SwitchControl | ||
| label="Enable updates" | ||
| description="Receive email updates" | ||
| /> | ||
| ); | ||
|
|
||
| expect( | ||
| screen.getByRole( 'switch', { | ||
| name: 'Enable updates', | ||
| description: 'Receive email updates', | ||
| } ) | ||
| ).toBeVisible(); | ||
| } ); | ||
|
|
||
| it( 'renders with details', () => { | ||
| render( | ||
| <SwitchControl | ||
| label="Terms" | ||
| details={ | ||
| <span> | ||
| Read the <a href="#terms">terms</a> | ||
| </span> | ||
| } | ||
| /> | ||
| ); | ||
|
|
||
| expect( screen.getByText( /Read the/ ) ).toBeVisible(); | ||
| } ); | ||
|
|
||
| it( 'toggles when the label is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render( <SwitchControl label="Enable updates" /> ); | ||
|
|
||
| const control = screen.getByRole( 'switch', { | ||
| name: 'Enable updates', | ||
| } ); | ||
| expect( control ).not.toBeChecked(); | ||
|
|
||
| await user.click( screen.getByText( 'Enable updates' ) ); | ||
|
|
||
| expect( control ).toBeChecked(); | ||
| } ); | ||
|
|
||
| describe( 'Form data behavior', () => { | ||
| it( 'submits correct form data when checked with custom name and value', async () => { | ||
| const user = userEvent.setup(); | ||
| const handleSubmit = vi.fn( ( event ) => { | ||
| event.preventDefault(); | ||
| return new FormData( event.currentTarget ); | ||
| } ); | ||
|
|
||
| render( | ||
| <form onSubmit={ handleSubmit }> | ||
| <SwitchControl | ||
| label="Enable notifications" | ||
| name="notifications" | ||
| value="yes" | ||
| /> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| ); | ||
|
|
||
| const control = screen.getByRole( 'switch', { | ||
| name: 'Enable notifications', | ||
| } ); | ||
| const submitButton = screen.getByRole( 'button', { | ||
| name: 'Submit', | ||
| } ); | ||
|
|
||
| await user.click( control ); | ||
| expect( control ).toBeChecked(); | ||
|
|
||
| await user.click( submitButton ); | ||
|
|
||
| const formData = handleSubmit.mock.results[ 0 ].value; | ||
| expect( formData.get( 'notifications' ) ).toBe( 'yes' ); | ||
| } ); | ||
|
|
||
| it( 'does not include an unchecked switch in form data', async () => { | ||
| const user = userEvent.setup(); | ||
| const handleSubmit = vi.fn( ( event ) => { | ||
| event.preventDefault(); | ||
| return new FormData( event.currentTarget ); | ||
| } ); | ||
|
|
||
| render( | ||
| <form onSubmit={ handleSubmit }> | ||
| <SwitchControl | ||
| label="Enable notifications" | ||
| name="notifications" | ||
| value="yes" | ||
| /> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| ); | ||
|
|
||
| const submitButton = screen.getByRole( 'button', { | ||
| name: 'Submit', | ||
| } ); | ||
|
|
||
| await user.click( submitButton ); | ||
|
|
||
| const formData = handleSubmit.mock.results[ 0 ].value; | ||
| expect( formData.get( 'notifications' ) ).toBeNull(); | ||
| } ); | ||
|
|
||
| it( 'uses "on" as default value when no value prop is provided', async () => { | ||
| const user = userEvent.setup(); | ||
| const handleSubmit = vi.fn( ( event ) => { | ||
| event.preventDefault(); | ||
| return new FormData( event.currentTarget ); | ||
| } ); | ||
|
|
||
| render( | ||
| <form onSubmit={ handleSubmit }> | ||
| <SwitchControl label="Enable feature" name="feature" /> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| ); | ||
|
|
||
| const control = screen.getByRole( 'switch', { | ||
| name: 'Enable feature', | ||
| } ); | ||
| const submitButton = screen.getByRole( 'button', { | ||
| name: 'Submit', | ||
| } ); | ||
|
|
||
| await user.click( control ); | ||
| expect( control ).toBeChecked(); | ||
|
|
||
| await user.click( submitButton ); | ||
|
|
||
| const formData = handleSubmit.mock.results[ 0 ].value; | ||
| expect( formData.get( 'feature' ) ).toBe( 'on' ); | ||
| } ); | ||
| } ); | ||
| } ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import type { Switch } from '../primitives'; | ||
| import type { ControlProps } from '../types'; | ||
|
|
||
| export type SwitchControlProps = React.ComponentProps< typeof Switch > & | ||
| ControlProps; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed no tests for the disabled state here, but I checked other form components don't have them either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I'm trying not to overtest things where there should be sufficient test coverage upstream in Base UI, and where we have no integration risks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, seems fine