-
Notifications
You must be signed in to change notification settings - Fork 8
improvement: enrich selection controls guidelines (Checkbox, Toggle, Radio, Select) #1060
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
Closed
Cuervino
wants to merge
10
commits into
development/1.0
from
improvement/selection-controls-guidelines
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb0d1ea
Add GitHub Actions workflow to deploy Storybook to GitHub Pages
Cuervino cd54965
Trigger GitHub Pages deployment
Cuervino f9d8bcf
Add confidential Templates stories
Cuervino 80378ff
Add Radio component
Cuervino ac7c74a
Fix TypeScript error in Radio component
Cuervino 84ae8f8
feat(templates): add layout redline and background levels to artesca-…
Cuervino c335553
Add browser identity guideline
Cuervino 7c01de8
Convert browser identity guideline to MDX for Storybook
Cuervino dafc3b3
Add FormatValidation story for deployment name edit
Cuervino 5bbac65
improvement: enrich selection controls guidelines (Checkbox, Toggle, …
Cuervino 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| name: Deploy Storybook to GitHub Pages | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pages: write | ||
| id-token: write | ||
|
|
||
| concurrency: | ||
| group: pages | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| deploy: | ||
| runs-on: ubuntu-latest | ||
| environment: | ||
| name: github-pages | ||
| url: ${{ steps.deployment.outputs.page_url }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Node | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 20 | ||
| cache: npm | ||
|
|
||
| - name: Install dependencies | ||
| run: npm ci | ||
|
|
||
| - name: Build Storybook | ||
| run: npm run build-storybook | ||
|
|
||
| - name: Upload artifact | ||
| uses: actions/upload-pages-artifact@v3 | ||
| with: | ||
| path: storybook-static | ||
|
|
||
| - name: Deploy to GitHub Pages | ||
| id: deployment | ||
| uses: actions/deploy-pages@v4 |
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,122 @@ | ||
| import { ChangeEvent, InputHTMLAttributes, forwardRef } from 'react'; | ||
| import styled from 'styled-components'; | ||
| import { spacing, Stack } from '../../spacing'; | ||
| import { Text } from '../text/Text.component'; | ||
| import { FocusVisibleStyle } from '../buttonv2/Buttonv2.component'; | ||
|
|
||
| const getDotSvgUrl = (color: string) => { | ||
| const encodedColor = color.replace('#', '%23'); | ||
| return `url('data:image/svg+xml,%3Csvg width="13" height="13" viewBox="0 0 13 13" fill="none" xmlns="http://www.w3.org/2000/svg"%3E%3Ccircle cx="6.5" cy="6.5" r="3" fill="${encodedColor}"/%3E%3C/svg%3E')`; | ||
| }; | ||
|
|
||
| export type Props = { | ||
| label?: string; | ||
| name: string; | ||
| value: string; | ||
| checked?: boolean; | ||
| disabled?: boolean; | ||
| onChange?: (e: ChangeEvent<HTMLInputElement>) => void; | ||
| } & Omit<InputHTMLAttributes<HTMLInputElement>, 'type'>; | ||
|
|
||
| const Radio = forwardRef<HTMLInputElement, Props>( | ||
| ({ disabled, checked, label, name, value, onChange, ...rest }, ref) => { | ||
| return ( | ||
| <StyledRadio checked={checked} disabled={disabled}> | ||
| <Stack gap="r12" style={{ alignItems: 'baseline' }}> | ||
| <RadioInput | ||
| type="radio" | ||
| name={name} | ||
| value={value} | ||
| checked={checked} | ||
| disabled={disabled} | ||
| onChange={onChange} | ||
| ref={ref} | ||
| {...rest} | ||
| /> | ||
| {label && <Text>{label}</Text>} | ||
| </Stack> | ||
| </StyledRadio> | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| export { Radio }; | ||
|
|
||
| const RadioInput = styled.input` | ||
| align-self: center; | ||
| `; | ||
|
|
||
| const StyledRadio = styled.label<{ | ||
| disabled?: boolean; | ||
| checked?: boolean; | ||
| }>` | ||
| display: inline-flex; | ||
| ${(props) => (props.disabled ? 'opacity: 0.5;' : '')} | ||
|
|
||
| [type='radio'] { | ||
| width: 1.125rem; | ||
| height: 1.125rem; | ||
| color: ${(props) => props.theme.textPrimary}; | ||
| vertical-align: middle; | ||
| -webkit-appearance: none; | ||
| -moz-appearance: none; | ||
| appearance: none; | ||
| background: none; | ||
| border: 0; | ||
| outline: 0; | ||
| flex-grow: 0; | ||
| border-radius: 50%; | ||
| background-color: ${(props) => props.theme.backgroundLevel1}; | ||
| transition: background 300ms; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| [type='radio']::before { | ||
| content: ''; | ||
| color: transparent; | ||
| display: block; | ||
| width: inherit; | ||
| height: inherit; | ||
| border-radius: 50%; | ||
| border: 0; | ||
| background-color: transparent; | ||
| background-size: contain; | ||
| box-shadow: inset 0 0 0 ${spacing.r1} ${(props) => props.theme.textSecondary}; | ||
| } | ||
|
|
||
| [type='radio']:checked { | ||
| background-color: ${(props) => props.theme.selectedActive}; | ||
| } | ||
|
|
||
| [type='radio']:checked::before { | ||
| box-shadow: none; | ||
| background-image: ${(props) => getDotSvgUrl(props.theme.textPrimary)}; | ||
| background-repeat: no-repeat; | ||
| background-position: center; | ||
| } | ||
|
|
||
| [type='radio']:hover { | ||
| ${(props) => | ||
| !props.disabled && `background-color: ${props.theme.highlight};`} | ||
| } | ||
|
|
||
| [type='radio']:hover::before { | ||
| ${(props) => | ||
| !props.disabled && | ||
| `box-shadow: inset 0 0 0 ${spacing.r1} ${props.theme.selectedActive};`} | ||
| } | ||
|
|
||
| [type='radio']:focus-visible:enabled { | ||
| ${FocusVisibleStyle} | ||
| } | ||
|
|
||
| [type='radio']:checked:disabled { | ||
| cursor: not-allowed; | ||
| background-color: ${(props) => props.theme.selectedActive}; | ||
| } | ||
|
|
||
| [type='radio']:not(:checked):disabled { | ||
| cursor: not-allowed; | ||
| background-color: ${(props) => props.theme.textSecondary}; | ||
| } | ||
| `; | ||
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 |
|---|---|---|
| @@ -1,55 +1,66 @@ | ||
| import { | ||
| Meta, | ||
| Story, | ||
| Canvas, | ||
| Primary, | ||
| Controls, | ||
| Unstyled, | ||
| Source, | ||
| } from '@storybook/blocks'; | ||
| import { Checkbox } from '../../src/lib/components/checkbox/Checkbox.component'; | ||
|
|
||
| import * as CheckboxStories from './checkbox.stories'; | ||
|
|
||
| <Meta of={CheckboxStories} name="Guideline" /> | ||
|
|
||
| <style>{` | ||
| .sbdocs-content h2 { margin-top: 5rem; } | ||
| .sbdocs-content h3 { margin-top: 3rem; } | ||
| .sbdocs-content .sb-story { margin-bottom: 0.5rem; } | ||
| .sbdocs-content .sbdocs-preview { margin-bottom: 1.5rem; } | ||
| `}</style> | ||
|
|
||
| # Checkbox | ||
|
|
||
| Checkboxes are used to select one or more options in a list. | ||
| Checkboxes let users enable an option or select one or more items from a list. | ||
|
|
||
| For guidance on when to use Checkbox vs Toggle vs Radio vs Select, see [Guidelines/Selection Controls](?path=/docs/guidelines-selection-controls--stories). | ||
|
|
||
| ## Patterns | ||
|
|
||
| ### Enable / disable an option | ||
|
|
||
| The primary use case. The label sits on the left and describes what enabling the option does. The checkbox sits on the right. | ||
|
|
||
| <Canvas of={CheckboxStories.OptionCheckbox} layout="fullscreen" sourceState="none" /> | ||
|
|
||
| Label rules: | ||
| - Use a verb phrase: "Enable compression", "Allow public access", "Send notifications" | ||
| - The label describes the active (checked) state | ||
| - Avoid nouns alone: "Compression" or "Notifications" are ambiguous | ||
|
|
||
| ### Multi-select in tables | ||
|
|
||
| ## Usage | ||
| The primary multi-select use case is row selection in a table. This is handled by `Table.MultiSelectableContent`, which manages the checkboxes internally — the Checkbox component is not used directly here. | ||
|
|
||
| Unlike the radio element it is possible to select more than one option. \ | ||
| A tick/check indicates that the element is selected. \ | ||
| <Canvas of={CheckboxStories.IndeterminateUseCase} layout="fullscreen" sourceState="none" /> | ||
|
|
||
| <Canvas of={CheckboxStories.ChoiceCheckbox} layout="fullscreen" /> | ||
| The header checkbox controls the full selection: checking it selects all rows, unchecking it deselects all. When some rows are selected, the header checkbox enters the indeterminate state. | ||
|
|
||
| It is possible to use the checkbox to enable or disable an option: | ||
| ## States | ||
|
|
||
| <Canvas of={CheckboxStories.OptionCheckbox} layout="fullscreen" /> | ||
| <Canvas of={CheckboxStories.AllStates} layout="fullscreen" sourceState="none" /> | ||
|
|
||
| ## State Variations | ||
| All five visual states from left to right: unchecked, checked, indeterminate, disabled, disabled checked. | ||
|
|
||
| ### Indeterminate State | ||
| ### Indeterminate | ||
|
|
||
| Apart from checked and unchecked, checkboxes can be in a third state : indeterminate. \ | ||
| When a checkbox has sub-options checkboxes, this state indicates that some of the sub-options are checked. \ | ||
| Clicking on the main checkbox select or unselect all the sub-options boxes. | ||
| The indeterminate state indicates a partial selection: some children are checked, but not all. It appears on the header checkbox in a table when some rows are selected. It is never used as an initial or standalone state. | ||
|
|
||
| <Canvas of={CheckboxStories.IndeterminateUseCase} layout="fullscreen" /> | ||
| <Canvas of={CheckboxStories.IndeterminateCheckbox} layout="fullscreen" sourceState="none" /> | ||
|
|
||
| ### Disabled state | ||
| ### Disabled | ||
|
|
||
| Checkboxes can be disabled, making it impossible to change the box state. \ | ||
| A not-allowed cursor inform the user about the unavaibility of the action. | ||
| A disabled checkbox prevents the user from changing its state. Use when the option is unavailable due to permissions, dependencies, or system state. A not-allowed cursor informs the user that the action is unavailable. | ||
|
|
||
| <Canvas | ||
| of={CheckboxStories.DisabledCheckboxes} | ||
| <Canvas of={CheckboxStories.DisabledCheckboxes} layout="fullscreen" sourceState="none" /> | ||
|
|
||
| layout="fullscreen" | ||
| /> | ||
| When the reason for the disabled state is not obvious from context, add a tooltip to explain it. | ||
|
|
||
| ### Playground | ||
| <Canvas of={CheckboxStories.DisabledWithReason} layout="fullscreen" sourceState="none" /> | ||
|
|
||
| <Canvas of={CheckboxStories.Playground} layout="fullscreen" /> | ||
| <Controls of={CheckboxStories.Playground} /> |
Oops, something went wrong.
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.
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Copilot Autofix
AI 11 days ago
In general, to fix this kind of problem you should avoid ad‑hoc string replacement for escaping and instead either (1) use a standard encoder such as
encodeURIComponenton the interpolated value, or (2) if you must usereplace, use a global regular expression so that all occurrences are handled (/pattern/g), and ensure you also escape backslashes and any other relevant meta‑characters for the target context.For this specific case, the safest and simplest fix without changing visible functionality is to ensure that every
#incoloris percent‑encoded before inserting it into the SVG data URL. The minimal change is to replacecolor.replace('#', '%23')withcolor.replace(/#/g, '%23'), which will encode all#characters while preserving the overall behavior for standard hex colors. An even more robust alternative would beconst encodedColor = encodeURIComponent(color);, but that would also encode characters like(,), and,, which might alter behavior if the function is (now or in the future) passed CSS color formats that include those characters; so the least‑intrusive, targeted fix is to just make the existing replacement global.All changes are confined to
src/lib/components/radio/Radio.component.tsx, within thegetDotSvgUrlhelper. No new imports or additional helper methods are required.