-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add segmented control radio and tabs #1101
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
nats12
wants to merge
4
commits into
main
Choose a base branch
from
codex/segmented-control
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9477af2
feat: add segmented control radio and tabs
nats12 c9465ce
docs: deprecate compound tabs API
nats12 75fe802
Merge remote-tracking branch 'origin/main' into codex/segmented-control
nats12 f8ee8fe
fix: address segmented control review feedback
nats12 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,14 @@ | ||
| export type { | ||
| AriaDescriptionProps, | ||
| AriaLabelProps, | ||
| SegmentedControlOption, | ||
| SegmentedControlProps, | ||
| SegmentedControlRadioOption, | ||
| SegmentedControlRadioProps, | ||
| } from './segmented-control' | ||
| export { SegmentedControl, SegmentedControlRadio } from './segmented-control' | ||
| export type { | ||
| SegmentedControlTabsOption, | ||
| SegmentedControlTabsProps, | ||
| } from './segmented-control-tabs' | ||
| export { SegmentedControlTabs } from './segmented-control-tabs' | ||
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,12 @@ | ||
| import type { BoxJustifyContent } from '../box' | ||
|
|
||
| const justifyContentByAlignment = { | ||
| start: 'flexStart', | ||
| center: 'center', | ||
| end: 'flexEnd', | ||
| } as const satisfies Record<string, BoxJustifyContent> | ||
|
|
||
| type SegmentedControlAlignment = keyof typeof justifyContentByAlignment | ||
|
|
||
| export { justifyContentByAlignment } | ||
| export type { SegmentedControlAlignment } |
153 changes: 153 additions & 0 deletions
153
src/segmented-control/segmented-control-tabs.stories.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,153 @@ | ||
| import * as React from 'react' | ||
|
|
||
| import { Box } from '../box' | ||
|
|
||
| import { SegmentedControlTabs } from './segmented-control-tabs' | ||
|
|
||
| import type { Meta, StoryObj } from '@storybook/react' | ||
|
|
||
| const meta = { | ||
| title: '📑 Menus & tabs/Segmented Control/Tabs', | ||
| component: SegmentedControlTabs, | ||
| parameters: { | ||
| badges: ['accessible'], | ||
| figma: { | ||
| url: 'https://www.figma.com/design/AH9ioCnZZLfiA5XcHFabOr/-Refresh--Product-%E2%80%93-Web?node-id=5-1187', | ||
| path: 'Refresh - Product Web', | ||
| }, | ||
| }, | ||
| } satisfies Meta<typeof SegmentedControlTabs> | ||
|
|
||
| export default meta | ||
|
|
||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| function ControlledExample({ width }: { width?: 'maxContent' | 'full' }) { | ||
| const [selectedOptionId, setSelectedOptionId] = React.useState('all') | ||
|
|
||
| return ( | ||
| <SegmentedControlTabs | ||
| aria-label="Notifications" | ||
| width={width} | ||
| contentOffset="medium" | ||
| selectedOptionId={selectedOptionId} | ||
| onSelectedOptionChange={setSelectedOptionId} | ||
| options={[ | ||
| { id: 'all', label: 'All', tabContent: 'All notifications content' }, | ||
| { id: 'unread', label: 'Unread', tabContent: 'Unread notifications content' }, | ||
| ]} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| function UpgradeIcon() { | ||
| return ( | ||
| <svg aria-hidden="true" width="14" height="14" viewBox="0 0 14 14" fill="none"> | ||
| <path | ||
| d="m7 1 1.7 3.6L13 5.1 9.8 8l.8 4.2L7 10.1l-3.6 2.1L4.2 8 1 5.1l4.3-.5z" | ||
| fill="currentColor" | ||
| /> | ||
| </svg> | ||
| ) | ||
| } | ||
|
|
||
| export const Default: Story = { | ||
| render: () => <ControlledExample />, | ||
| } | ||
|
|
||
| export const FullWidth: Story = { | ||
| render: () => ( | ||
| <Box width="small"> | ||
|
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. Same here, potentially it limits the full width |
||
| <ControlledExample width="full" /> | ||
| </Box> | ||
| ), | ||
| } | ||
|
|
||
| export const FullWidthWithWrappingLabels: Story = { | ||
| render: () => ( | ||
| <Box width="small"> | ||
|
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. This could be limiting the full width |
||
| <SegmentedControlTabs | ||
| aria-label="Developer options" | ||
| width="full" | ||
| initialSelectedOptionId="testdata" | ||
| options={[ | ||
| { id: 'testdata', label: 'Test Data', tabContent: 'Test data content' }, | ||
| { id: 'tooltips', label: 'Tooltips', tabContent: 'Tooltips content' }, | ||
| { id: 'syncengine', label: 'Sync Engine', tabContent: 'Sync engine content' }, | ||
| { id: 'appversion', label: 'App Version', tabContent: 'App version content' }, | ||
| { id: 'frozen', label: 'Frozen Data', tabContent: 'Frozen data content' }, | ||
| { id: 'persistence', label: 'Persistence', tabContent: 'Persistence content' }, | ||
| { | ||
| id: 'featureflags', | ||
| label: 'Feature Flags', | ||
| tabContent: 'Feature flags content', | ||
| }, | ||
| ]} | ||
| /> | ||
| </Box> | ||
| ), | ||
| } | ||
|
|
||
| export const DisabledOption: Story = { | ||
| render: () => ( | ||
| <SegmentedControlTabs | ||
| aria-label="Notifications" | ||
| initialSelectedOptionId="all" | ||
| options={[ | ||
| { id: 'all', label: 'All', tabContent: 'All notifications content' }, | ||
| { | ||
| id: 'unread', | ||
| label: 'Unread', | ||
| tabContent: 'Unread notifications content', | ||
| disabled: true, | ||
| }, | ||
| ]} | ||
| /> | ||
| ), | ||
| } | ||
|
|
||
| export const WithExtraContent: Story = { | ||
| render: () => ( | ||
| <SegmentedControlTabs | ||
| aria-label="Reminder type" | ||
| initialSelectedOptionId="date" | ||
| options={[ | ||
| { | ||
| id: 'date', | ||
| label: 'Date & time', | ||
| extraIcon: <UpgradeIcon />, | ||
| tabContent: 'Date and time settings', | ||
| }, | ||
| { | ||
| id: 'location', | ||
| label: 'Location', | ||
| extraLabel: '2', | ||
| tabContent: 'Location settings', | ||
| }, | ||
| ]} | ||
| /> | ||
| ), | ||
| } | ||
|
|
||
| export const WithLabelsAsLinks: Story = { | ||
| render: () => ( | ||
| <SegmentedControlTabs | ||
| aria-label="Integrations" | ||
| initialSelectedOptionId="installed" | ||
| options={[ | ||
| { | ||
| id: 'installed', | ||
| label: 'Installed', | ||
| tabContent: 'Installed integrations', | ||
| tabRender: <a href="#installed" />, | ||
| }, | ||
| { | ||
| id: 'browse', | ||
| label: 'Browse', | ||
| tabContent: 'Browse integrations', | ||
| tabRender: <a href="#browse" />, | ||
| }, | ||
| ]} | ||
| /> | ||
| ), | ||
| } | ||
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,140 @@ | ||
| import * as React from 'react' | ||
|
|
||
| import { render, screen } from '@testing-library/react' | ||
| import userEvent from '@testing-library/user-event' | ||
| import { axe } from 'jest-axe' | ||
|
|
||
| import { flushAnimationFrames, flushMicrotasks, TestIcon } from '../utils/test-helpers' | ||
|
|
||
| import { SegmentedControlTabs } from './segmented-control-tabs' | ||
|
|
||
| async function renderSegmentedControlTabs(element: React.ReactElement) { | ||
| const result = render(element) | ||
| await flushAnimationFrames() | ||
| return result | ||
| } | ||
|
|
||
| describe('SegmentedControlTabs', () => { | ||
| it('renders option metadata and switches controlled panels', async () => { | ||
| const onSelectedOptionChange = jest.fn() | ||
| const user = userEvent.setup() | ||
|
|
||
| function Example() { | ||
| const [selectedOptionId, setSelectedOptionId] = React.useState('all') | ||
|
|
||
| return ( | ||
| <SegmentedControlTabs | ||
| aria-label="Notifications" | ||
| selectedOptionId={selectedOptionId} | ||
| onSelectedOptionChange={(id) => { | ||
| onSelectedOptionChange(id) | ||
| setSelectedOptionId(id) | ||
| }} | ||
| options={[ | ||
| { id: 'all', label: 'All', tabContent: 'All notifications' }, | ||
| { | ||
| id: 'unread', | ||
| label: 'Unread', | ||
| extraLabel: '2', | ||
| extraIcon: <TestIcon />, | ||
| tabContent: 'Unread notifications', | ||
| }, | ||
| ]} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| await renderSegmentedControlTabs(<Example />) | ||
|
|
||
| expect(await screen.findByRole('tabpanel', { name: 'All' })).toBeVisible() | ||
| expect(screen.getByRole('tab', { name: 'Unread 2' })).toBeVisible() | ||
|
|
||
| await user.click(screen.getByRole('tab', { name: 'Unread 2' })) | ||
| await flushMicrotasks() | ||
|
|
||
| expect(onSelectedOptionChange).toHaveBeenCalledWith('unread') | ||
| expect(screen.getByRole('tabpanel', { name: 'Unread 2' })).toBeVisible() | ||
| expect(screen.getByText('Unread notifications')).toBeVisible() | ||
| }) | ||
|
|
||
| it('renders tabs as links and supports content wrappers', async () => { | ||
| function ContentWrapper({ children }: { children: React.ReactNode }) { | ||
| return <span title="Wrapped tab">{children}</span> | ||
| } | ||
|
|
||
| await renderSegmentedControlTabs( | ||
| <SegmentedControlTabs | ||
| aria-label="Integrations" | ||
| initialSelectedOptionId="installed" | ||
| options={[ | ||
| { | ||
| id: 'installed', | ||
| label: 'Installed', | ||
| tabContent: 'Installed integrations', | ||
| tabRender: <a href="#installed" />, | ||
| }, | ||
| { | ||
| id: 'browse', | ||
| label: 'Browse', | ||
| tabContent: 'Browse integrations', | ||
| Wrapper: ContentWrapper, | ||
| }, | ||
| ]} | ||
| />, | ||
| ) | ||
|
|
||
| expect(await screen.findByRole('tab', { name: 'Installed' })).toHaveAttribute( | ||
| 'href', | ||
| '#installed', | ||
| ) | ||
| expect(screen.getByTitle('Wrapped tab')).toBeVisible() | ||
| }) | ||
|
|
||
| it('removes disabled tabs from keyboard navigation', async () => { | ||
|
nats12 marked this conversation as resolved.
|
||
| const user = userEvent.setup() | ||
|
|
||
| await renderSegmentedControlTabs( | ||
| <SegmentedControlTabs | ||
| aria-label="Notifications" | ||
| initialSelectedOptionId="all" | ||
| options={[ | ||
| { id: 'all', label: 'All', tabContent: 'All notifications' }, | ||
| { | ||
| id: 'unread', | ||
| label: 'Unread', | ||
| tabContent: 'Unread notifications', | ||
| disabled: true, | ||
| }, | ||
| { id: 'mentions', label: 'Mentions', tabContent: 'Mentions' }, | ||
| ]} | ||
| />, | ||
| ) | ||
|
|
||
| await screen.findByRole('tabpanel', { name: 'All' }) | ||
| await user.tab() | ||
| expect(screen.getByRole('tab', { name: 'All' })).toHaveFocus() | ||
|
|
||
| await user.keyboard('{ArrowRight}') | ||
| await flushMicrotasks() | ||
|
|
||
| expect(screen.getByRole('tab', { name: 'Mentions' })).toHaveFocus() | ||
| expect(screen.getByRole('tab', { name: 'Unread' })).toBeDisabled() | ||
| expect(screen.getByRole('tabpanel', { name: 'Mentions' })).toBeVisible() | ||
| }) | ||
|
|
||
| it('has no automated accessibility violations', async () => { | ||
| const { container } = await renderSegmentedControlTabs( | ||
| <SegmentedControlTabs | ||
| aria-label="Notifications" | ||
| initialSelectedOptionId="all" | ||
| options={[ | ||
| { id: 'all', label: 'All', tabContent: 'All notifications' }, | ||
| { id: 'unread', label: 'Unread', tabContent: 'Unread notifications' }, | ||
| ]} | ||
| />, | ||
| ) | ||
|
|
||
| await screen.findByRole('tabpanel', { name: 'All' }) | ||
| expect(await axe(container)).toHaveNoViolations() | ||
| }) | ||
| }) | ||
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.
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.
My original reaction was that the name is too generic, but then I checked and it actually describes it very well. But I feel like we should move it somewhere else, maybe in
src/utils/common-types.ts?