-
Notifications
You must be signed in to change notification settings - Fork 422
Toggle switch component guidelines #249
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
12 commits
Select commit
Hold shift + click to select a range
d56306e
adds interface guidelines for the toggle switch component
mperrotti e8f791a
updates saving/submitting guidelines with toggle switches
mperrotti 47427ac
Update content/ui-patterns/saving.mdx
mperrotti abd2986
Update content/components/toggle-switch.mdx
mperrotti b5c66ce
Update content/components/toggle-switch.mdx
mperrotti ee046ea
Update content/components/toggle-switch.mdx
mperrotti 981fad5
Update content/components/toggle-switch.mdx
mperrotti 55c76db
Update content/components/toggle-switch.mdx
mperrotti ed32686
Update content/components/toggle-switch.mdx
mperrotti d10454a
addresses remaining content feedback
mperrotti 77a4fa2
adds toggle switch to components index
mperrotti add514e
addresses additional PR feedback
mperrotti 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
--- | ||
title: Toggle switch | ||
description: A toggle switch is used to immediately toggle a setting on or off. | ||
--- | ||
|
||
import {Box, Text} from '@primer/components' | ||
|
||
## Anatomy | ||
|
||
 | ||
|
||
- **Track**: The clickable area where the toggle switch moves back and forth | ||
- **Knob**: Changes color and position depending on whether the switch is on or off | ||
- **Status**: A text label showing whether the switch is on or off | ||
|
||
## Options | ||
|
||
### Label and description | ||
|
||
<Box mb={5} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}> | ||
<div> | ||
<Box as="p" mt="0">Every toggle switch should have a label that says what is being turned on or off. Optionally, you may include a description about the setting that is being turned on or off.</Box> | ||
<Box as="p" mt="0">Labels should be succinct, and can describe an object or an action (for example: "Discussions" or "Automatically watch repositories"). Don't use phrases that describe the switch's state in your label.</Box> | ||
</div> | ||
<img | ||
width="456" | ||
role="presentation" | ||
src="https://user-images.githubusercontent.com/2313998/159587115-5ffcdefb-f5e9-4180-8dc3-5328b0440f7f.png" | ||
/> | ||
</Box> | ||
|
||
<DoDontContainer> | ||
<Do> | ||
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/159587088-99e8fe4c-59a3-4452-836d-bd6f2297045a.png" /> | ||
<Caption>Use succinct phrases with verbs or nouns</Caption> | ||
</Do> | ||
<Dont> | ||
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/159587096-5f8a6913-df95-4ea0-9838-a366faf95e52.png" /> | ||
<Caption>Don't use phrases that describe the switch's state</Caption> | ||
</Dont> | ||
</DoDontContainer> | ||
|
||
### Layout | ||
|
||
<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}> | ||
<div> | ||
<Box as="p" mt="0">By default, lay out a toggle switch horizontally justified with its label and optional description.</Box> | ||
<Box as="p" mt="0">You may use a vertical layout when a toggle switch is layed out inline with other content, or if a horizontal layout would put the switch too far from its label. If the vertical layout is left-aligned, the "on"/"off" text may be moved to the right of the switch.</Box> | ||
</div> | ||
<Box display="flex" flexDirection="column" sx={{gap: 2, flexGrow: 1, flexShrink: 0}}> | ||
<img | ||
width="456" | ||
role="presentation" | ||
src="https://user-images.githubusercontent.com/2313998/159587115-5ffcdefb-f5e9-4180-8dc3-5328b0440f7f.png" | ||
/> | ||
<img | ||
width="456" | ||
role="presentation" | ||
src="https://user-images.githubusercontent.com/2313998/159587126-8d30ff26-33a8-4b84-87db-13dcfdff9dac.png" | ||
/> | ||
</Box> | ||
</Box> | ||
|
||
### Size | ||
|
||
<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}> | ||
<Box as="p" mt="0">A small variant is provided to maintain visual hierarchy, or for dense areas where the default size will not fit.</Box> | ||
<img | ||
width="456" | ||
role="presentation" | ||
src="https://user-images.githubusercontent.com/2313998/159587124-e7ef0a16-fd42-4614-be3e-80c985332ef7.png" | ||
/> | ||
</Box> | ||
|
||
### Loading | ||
|
||
<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}> | ||
<Box as="p" mt="0">When a toggle switch is waiting for something to load before toggling, a loading indicator should be displayed. The switch should not prematurely toggle from "on" to "off". Wait until loading is complete before showing a change.</Box> | ||
<img | ||
width="456" | ||
role="presentation" | ||
src="https://user-images.githubusercontent.com/2313998/159587116-aa07a847-a633-4515-82b3-20ebe2349914.png" | ||
/> | ||
</Box> | ||
|
||
### Progressive disclosure | ||
|
||
<Box mb={3} display="flex" alignItems="flex-start" flexDirection={["column", "column", "column", "column", "row"]} sx={{gap: 3}}> | ||
<Box as="p" mt="0">The <a href="/ui-patterns/progressive-disclosure">progressive disclosure pattern</a> may be used to hide or show content based on whether a toggle switch is on. Content revealed on toggle switch activation should always come <em>after</em> the toggle switch.</Box> | ||
<img | ||
width="456" | ||
role="presentation" | ||
src="https://user-images.githubusercontent.com/2313998/159591632-f604ec1d-aca9-42f7-ad65-16f8811f192f.png" | ||
/> | ||
</Box> | ||
|
||
## Accessibility | ||
|
||
### Keyboard navigation | ||
|
||
Toggle switches should have the same keyboard navigation features as a button. A toggle switch may be focused using the `Tab` key, and pressing the `Enter` or `Space` key will toggle the switch. | ||
|
||
### Touch target | ||
|
||
When the UI is rendered on a touch-screen device, ensure that the toggle switch has a large enough touch target size. The toggle switch should respond to hovers, clicks, and taps anywhere in the touch target area, even if it isn't directly on the control. | ||
|
||
 | ||
|
||
## Mixed values | ||
|
||
Since a toggle switch can only be "on" or "off", it could be confusing to use a switch to represent mixed values. | ||
|
||
If you have a group of configuration options that can be disabled, use a button instead of a toggle switch. Disabling the group turns off all configuration options, but remembers their previous state when the user wants to turn that section back on. While the section is disabled, the configuration options are hidden. | ||
|
||
 | ||
|
||
|
||
<DoDontContainer> | ||
<Do> | ||
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/159587100-1a1af5b2-dd19-456a-a0ed-c2f9fa7f7c2e.png" /> | ||
<Caption>Use a button to affect multiple related toggles</Caption> | ||
</Do> | ||
<Dont> | ||
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/159587101-446548c2-41c3-45c1-b710-db3149e2ebe4.png" /> | ||
<Caption>Don't use a toggle switch to represent mixed values</Caption> | ||
</Dont> | ||
</DoDontContainer> | ||
|
||
## Toggle switch vs checkbox | ||
|
||
Toggle switches are buttons that change and save a boolean configuration option when they are toggled. | ||
Checkboxes are form inputs that are used to select one or more items from a list, and are not saved without explicit confirmation such as pressing a **Save** button. | ||
|
||
A toggle switch should never be used as a replacement for a checkbox. See [saving patterns](/ui-patterns/saving) for more details. | ||
|
||
<DoDontContainer> | ||
<Do> | ||
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/159587105-45d50974-eb76-46cd-a3fd-c1e0f605f3ef.png" /> | ||
<Caption>Use a toggle switch like a button</Caption> | ||
</Do> | ||
<Dont> | ||
<img role="presentation" src="https://user-images.githubusercontent.com/2313998/159587113-6c8461f7-3d4d-4e6a-b3a5-c585c5143fba.png" /> | ||
<Caption>Don't use a toggle switch as a replacement for a checkbox</Caption> | ||
</Dont> | ||
</DoDontContainer> |
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
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.
Uh oh!
There was an error while loading. Please reload this page.