-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add Toggle component #696
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
Open
rohanchkrabrty
wants to merge
1
commit into
main
Choose a base branch
from
feat-toggle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| 'use client'; | ||
|
|
||
| import { | ||
| FontBoldIcon, | ||
| FontItalicIcon, | ||
| TextAlignCenterIcon, | ||
| TextAlignLeftIcon, | ||
| TextAlignRightIcon, | ||
| UnderlineIcon | ||
| } from '@radix-ui/react-icons'; | ||
| import { Flex, Text, Toggle } from '@raystack/apsara'; | ||
| import PlaygroundLayout from './playground-layout'; | ||
|
|
||
| export function ToggleExamples() { | ||
| return ( | ||
| <PlaygroundLayout title='Toggle'> | ||
| <Flex direction='column' gap='large'> | ||
| <Text size={2} weight={500}> | ||
| Standalone | ||
| </Text> | ||
| <Flex gap='medium' align='center'> | ||
| <Toggle aria-label='Bold'> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle aria-label='Bold' defaultPressed> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle aria-label='Bold' disabled> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| </Flex> | ||
|
|
||
| <Text size={2} weight={500}> | ||
| Sizes | ||
| </Text> | ||
| <Flex gap='medium' align='center'> | ||
| <Toggle size={1} aria-label='Bold'> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle size={2} aria-label='Bold'> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle size={3} aria-label='Bold'> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle size={4} aria-label='Bold'> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| </Flex> | ||
|
|
||
| <Text size={2} weight={500}> | ||
| Toggle.Group (single selection) | ||
| </Text> | ||
| <Toggle.Group defaultValue={['center']} aria-label='Text alignment'> | ||
| <Toggle value='left' aria-label='Align left'> | ||
| <TextAlignLeftIcon /> | ||
| </Toggle> | ||
| <Toggle value='center' aria-label='Align center'> | ||
| <TextAlignCenterIcon /> | ||
| </Toggle> | ||
| <Toggle value='right' aria-label='Align right'> | ||
| <TextAlignRightIcon /> | ||
| </Toggle> | ||
| </Toggle.Group> | ||
|
|
||
| <Text size={2} weight={500}> | ||
| Toggle.Group (multiple selection) | ||
| </Text> | ||
| <Toggle.Group multiple aria-label='Text formatting'> | ||
| <Toggle value='bold' aria-label='Bold'> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle value='italic' aria-label='Italic'> | ||
| <FontItalicIcon /> | ||
| </Toggle> | ||
| <Toggle value='underline' aria-label='Underline'> | ||
| <UnderlineIcon /> | ||
| </Toggle> | ||
| </Toggle.Group> | ||
|
|
||
| <Text size={2} weight={500}> | ||
| Disabled Group | ||
| </Text> | ||
| <Toggle.Group disabled aria-label='Disabled alignment'> | ||
| <Toggle value='left' aria-label='Align left'> | ||
| <TextAlignLeftIcon /> | ||
| </Toggle> | ||
| <Toggle value='center' aria-label='Align center'> | ||
| <TextAlignCenterIcon /> | ||
| </Toggle> | ||
| </Toggle.Group> | ||
| </Flex> | ||
| </PlaygroundLayout> | ||
| ); | ||
| } | ||
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,123 @@ | ||
| 'use client'; | ||
|
|
||
| import { getPropsString } from '@/lib/utils'; | ||
|
|
||
| const getCode = (props: any) => { | ||
| return `<Toggle aria-label="Bold"${getPropsString(props)}> | ||
| <FontBoldIcon /> | ||
| </Toggle>`; | ||
| }; | ||
|
|
||
| export const playground = { | ||
| type: 'playground', | ||
| controls: { | ||
| defaultPressed: { | ||
| type: 'checkbox', | ||
| defaultValue: false | ||
| }, | ||
| disabled: { | ||
| type: 'checkbox', | ||
| defaultValue: false | ||
| }, | ||
| size: { | ||
| type: 'select', | ||
| options: ['1', '2', '3', '4'], | ||
| defaultValue: '3' | ||
| } | ||
| }, | ||
| getCode | ||
| }; | ||
rohanchkrabrty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export const preview = { | ||
| type: 'code', | ||
| code: `<Toggle aria-label="Bold" defaultPressed> | ||
| <b>B</b> | ||
| </Toggle>` | ||
| }; | ||
|
|
||
| export const groupDemo = { | ||
| type: 'code', | ||
| code: `<Toggle.Group defaultValue={["center"]} aria-label="Text alignment"> | ||
| <Toggle value="left" aria-label="Align left"> | ||
| <TextAlignLeftIcon /> | ||
| </Toggle> | ||
| <Toggle value="center" aria-label="Align center"> | ||
| <TextAlignCenterIcon /> | ||
| </Toggle> | ||
| <Toggle value="right" aria-label="Align right"> | ||
| <TextAlignRightIcon /> | ||
| </Toggle> | ||
| </Toggle.Group>` | ||
| }; | ||
|
|
||
| export const multipleDemo = { | ||
| type: 'code', | ||
| code: `<Toggle.Group multiple aria-label="Text formatting"> | ||
| <Toggle value="bold" aria-label="Bold"> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle value="italic" aria-label="Italic"> | ||
| <FontItalicIcon /> | ||
| </Toggle> | ||
| <Toggle value="underline" aria-label="Underline"> | ||
| <UnderlineIcon /> | ||
| </Toggle> | ||
| </Toggle.Group>` | ||
| }; | ||
|
|
||
| export const controlledDemo = { | ||
| type: 'code', | ||
| code: `function ControlledToggle() { | ||
| const [pressed, setPressed] = React.useState(false); | ||
| return ( | ||
| <Toggle | ||
| aria-label="Star" | ||
| pressed={pressed} | ||
| onPressedChange={setPressed} | ||
| > | ||
| {pressed ? "★" : "☆"} | ||
| </Toggle> | ||
| ); | ||
| }` | ||
| }; | ||
|
|
||
| export const sizeDemo = { | ||
| type: 'code', | ||
| code: `<Flex gap="large" align="center"> | ||
| <Toggle size={1} aria-label="Bold"> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle size={2} aria-label="Bold"> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle size={3} aria-label="Bold"> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| <Toggle size={4} aria-label="Bold"> | ||
| <FontBoldIcon /> | ||
| </Toggle> | ||
| </Flex>` | ||
| }; | ||
|
|
||
| export const disabledDemo = { | ||
| type: 'code', | ||
| tabs: [ | ||
| { | ||
| name: 'Single', | ||
| code: `<Toggle aria-label="Bold" disabled> | ||
| <b>B</b> | ||
| </Toggle>` | ||
| }, | ||
| { | ||
| name: 'Group', | ||
| code: `<Toggle.Group disabled aria-label="Text alignment"> | ||
| <Toggle value="left" aria-label="Align left"> | ||
| <TextAlignLeftIcon /> | ||
| </Toggle> | ||
| <Toggle value="center" aria-label="Align center"> | ||
| <TextAlignCenterIcon /> | ||
| </Toggle> | ||
| </Toggle.Group>` | ||
| } | ||
| ] | ||
| }; | ||
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,83 @@ | ||
| --- | ||
| title: Toggle | ||
| description: A two-state button that can be toggled on or off, used standalone or within a group. | ||
| source: packages/raystack/components/toggle | ||
| tag: new | ||
| --- | ||
|
|
||
| import { playground, preview, groupDemo, multipleDemo, sizeDemo, controlledDemo, disabledDemo } from "./demo.ts"; | ||
|
|
||
| <Demo data={playground} /> | ||
|
|
||
| ## Anatomy | ||
|
|
||
| Import and use the component: | ||
|
|
||
| ```tsx | ||
| import { Toggle } from "@raystack/apsara"; | ||
|
|
||
| <Toggle aria-label="Bold">B</Toggle> | ||
| ``` | ||
|
|
||
| Use `Toggle.Group` to manage a set of toggles with shared state: | ||
|
|
||
| ```tsx | ||
| <Toggle.Group defaultValue={["left"]}> | ||
| <Toggle value="left" aria-label="Align left">L</Toggle> | ||
| <Toggle value="center" aria-label="Align center">C</Toggle> | ||
| <Toggle value="right" aria-label="Align right">R</Toggle> | ||
| </Toggle.Group> | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### Root | ||
|
|
||
| A two-state button that can be toggled on or off. | ||
|
|
||
| <auto-type-table path="./props.ts" name="ToggleProps" /> | ||
|
|
||
| ### Group | ||
|
|
||
| Groups multiple toggle buttons and manages their shared pressed state. | ||
|
|
||
| <auto-type-table path="./props.ts" name="ToggleGroupProps" /> | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Group | ||
|
|
||
| Use `Toggle.Group` for exclusive or multi-select toggle sets. By default, only one toggle can be pressed at a time. | ||
|
|
||
| <Demo data={groupDemo} /> | ||
|
|
||
| ### Multiple Selection | ||
|
|
||
| Set `multiple` on the group to allow more than one toggle to be pressed simultaneously. | ||
|
|
||
| <Demo data={multipleDemo} /> | ||
|
|
||
| ### Size | ||
|
|
||
| The toggle supports four sizes matching the icon button sizes. | ||
|
|
||
| <Demo data={sizeDemo} /> | ||
|
|
||
| ### Controlled | ||
|
|
||
| Control the pressed state programmatically. | ||
|
|
||
| <Demo data={controlledDemo} /> | ||
|
|
||
| ### Disabled | ||
|
|
||
| Disable individual toggles or the entire group. | ||
|
|
||
| <Demo data={disabledDemo} /> | ||
|
|
||
| ## Accessibility | ||
|
|
||
| - Uses native `<button>` element with `aria-pressed` for toggle state | ||
| - Supports keyboard activation with Space and Enter keys | ||
| - `Toggle.Group` renders with `role="group"` and supports arrow key navigation | ||
| - Use `aria-label` on each toggle to provide an accessible name when using icon-only content |
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.
Uh oh!
There was an error while loading. Please reload this page.