-
Notifications
You must be signed in to change notification settings - Fork 114
Add read-only Switch #5902
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
base: main
Are you sure you want to change the base?
Add read-only Switch #5902
Changes from 9 commits
5a76029
5e28883
d7c47f9
640867c
4a56201
191e0cf
1698c9b
ca41212
8c267b7
7c32ca5
f458d85
636df55
eb8cde0
e3ec559
4ac75a9
567fb33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "@salt-ds/core": patch | ||
| --- | ||
|
|
||
| Added support for read-only `Switch` via `readOnly` prop. | ||
|
|
||
| Example usage: | ||
|
|
||
| ```jsx | ||
| <Switch readOnly label="Read only" /> | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,32 @@ | |
| width: calc((var(--salt-size-selectable) + var(--salt-spacing-25) + var(--salt-size-fixed-100)) * 2); | ||
| } | ||
|
|
||
| .saltSwitch-readOnly { | ||
| cursor: var(--salt-cursor-text); | ||
| } | ||
|
|
||
| .saltSwitch-readOnly .saltSwitch-track, | ||
| .saltSwitch-readOnly:hover .saltSwitch-track { | ||
| border-color: var(--salt-selectable-borderColor-readonly); | ||
| color: var(--salt-content-primary-foreground); | ||
| } | ||
|
|
||
| .saltSwitch-readOnly:hover .saltSwitch-thumb { | ||
| border-color: var(--salt-selectable-borderColor-readonly); | ||
| } | ||
|
|
||
| .saltSwitch-readOnly .saltSwitch-thumb, | ||
| .saltSwitch-readOnly.saltSwitch-checked .saltSwitch-thumb { | ||
| background: var(--salt-container-primary-background); | ||
| border: var(--salt-size-fixed-100) var(--salt-borderStyle-dashed) var(--salt-selectable-borderColor-readonly); | ||
| } | ||
|
|
||
| .saltSwitch-readOnly .saltSwitch-input:focus-visible + .saltSwitch-track, | ||
| .saltSwitch-readOnly .saltSwitch-input:focus-visible + .saltSwitch-track .saltSwitch-thumb { | ||
| border-color: var(--salt-selectable-borderColor-readonly); | ||
| color: var(--salt-content-primary-foreground); | ||
| } | ||
|
|
||
| @media (prefers-reduced-motion) { | ||
| .saltSwitch-input:focus-visible + .saltSwitch-track .saltSwitch-thumb, | ||
| .saltSwitch:hover .saltSwitch-thumb { | ||
|
|
@@ -121,6 +147,10 @@ | |
| border: 0; | ||
| } | ||
|
|
||
| .salt-density-high .saltSwitch-readOnly .saltSwitch-thumb { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right above sets border to zero for some reason
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume it's to do with sizing, could you check with the designers if what is there meets the expected outcome please
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Figma doesn't have a stroke on the switch thumb in all the densities. Read-only states look correct though. |
||
| border: var(--salt-size-fixed-100) var(--salt-borderStyle-dashed) var(--salt-selectable-borderColor-readonly); | ||
| } | ||
|
|
||
| /* Styles applied when switch is inside a form field with label left or right, ensuring alignment with base / input height */ | ||
| .saltFormField-labelRight .saltSwitch, | ||
| .saltFormField-labelLeft .saltSwitch { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,10 @@ data: | |
| $ref: ./#/data | ||
| --- | ||
|
|
||
| ## Best Practice | ||
|
|
||
| Ideally we shouln't use readOnly | ||
|
||
|
|
||
| ## Keyboard interactions | ||
|
|
||
| <KeyboardControls> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,16 @@ Use a disabled checked state for switches that have permissions, dependencies or | |
|
|
||
| <LivePreview componentName="switch" exampleName="DisabledChecked" /> | ||
|
|
||
| ## Read-only | ||
|
|
||
| A read-only switch allows the user to select and copy its text, but does not allow them to change its state. You can set a switch as read-only via the prop `readOnly`. | ||
|
|
||
| ### Best practices | ||
|
|
||
| Unlike disabled switches, read-only switches are focusable and shown in the tab order when using a keyboard, allowing users to interact with its text. Use a read-only Switch when its text contains information that is valuable to the user. | ||
|
|
||
| <LivePreview componentName="switch" exampleName="Readonly" /> | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No guidance currently on the accessibility tab.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From @jake-costa's comment earlier @karlgoldstraw might be adding more here but we thought the note in the examples section might be beneficial. We can move it to the accessibility tab if that's better
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wemmyo @mark-tate In addition, we have this kind of language on other read only examples (checkbox, radio, etc.) |
||
| ## Left aligned label | ||
|
|
||
| Use a form field if you need a left aligned label, avoid also having a switch label. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { StackLayout, Switch } from "@salt-ds/core"; | ||
| import type { ReactElement } from "react"; | ||
|
|
||
| export const Readonly = (): ReactElement => ( | ||
| <StackLayout> | ||
| <Switch readOnly checked label="Read only + checked" /> | ||
wemmyo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <Switch readOnly label="Read only" /> | ||
wemmyo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </StackLayout> | ||
| ); | ||
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.
no test of focus but it's something we call out as a differentiator between disabled?
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.
read-only attribute as well ?
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.
Good point thanks, I've included them in the tests