-
Notifications
You must be signed in to change notification settings - Fork 17
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
<SegmentedPicker />
: Use Radix UI's Tabs component under the hood, and rename to <Tabs />
#1543
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ import { tab } from '../utils/typography'; | |
import { motion } from 'framer-motion'; | ||
import { useId } from 'react'; | ||
import { buttonInteractions } from '../utils/button'; | ||
import * as RadixTabs from '@radix-ui/react-tabs'; | ||
|
||
const TEN_PERCENT_OPACITY_IN_HEX = '1a'; | ||
|
||
|
@@ -25,7 +26,7 @@ const outlineColorByActionType: Record<ActionType, keyof DefaultTheme['color'][' | |
unshield: 'unshieldFocusOutline', | ||
}; | ||
|
||
const SegmentButton = styled.button<{ | ||
const Tab = styled.button<{ | ||
$actionType: ActionType; | ||
$getFocusOutlineColor: (theme: DefaultTheme) => string; | ||
$getBorderRadius: (theme: DefaultTheme) => string; | ||
|
@@ -68,37 +69,27 @@ const SelectedIndicator = styled(motion.div)` | |
z-index: -1; | ||
`; | ||
|
||
export interface SegmentedPickerOption<ValueType> { | ||
/** | ||
* The value to pass to the `onChange` handler when clicked. Must be unique | ||
* across all segments, and must be either a string, number, or an object with | ||
* a `.toString()` method so that it can be used as a React key. | ||
*/ | ||
value: ValueType; | ||
export interface TabsTab { | ||
value: string; | ||
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. Radix UI's |
||
label: string; | ||
disabled?: boolean; | ||
} | ||
|
||
export interface SegmentedPickerProps<ValueType extends { toString: () => string }> { | ||
/** | ||
* The currently selected value. Will be compared to the `options`' `value` | ||
* property using `===` to determine which segment is selected. | ||
*/ | ||
value: ValueType; | ||
onChange: (value: ValueType) => void; | ||
options: SegmentedPickerOption<ValueType>[]; | ||
export interface TabsProps { | ||
value: string; | ||
onChange: (value: string) => void; | ||
options: TabsTab[]; | ||
actionType?: ActionType; | ||
} | ||
|
||
/** | ||
* Renders a segmented picker where only one option can be selected at a time. | ||
* Functionally equivalent to a `<select>` element or a set of radio buttons, | ||
* but looks nicer when you only have a few options to choose from. (Probably | ||
* shouldn't be used with more than 5 options.) | ||
* Use tabs for switching between related pages or views. | ||
* | ||
* Built atop Radix UI's `<Tabs />` component, so it's fully accessible and | ||
* supports keyboard navigation. | ||
* | ||
* @example | ||
* ```TSX | ||
* <SegmentedPicker | ||
* <Tabs | ||
* value={value} | ||
* onChange={setValue} | ||
* options={[ | ||
|
@@ -109,29 +100,36 @@ export interface SegmentedPickerProps<ValueType extends { toString: () => string | |
* /> | ||
* ``` | ||
*/ | ||
export const SegmentedPicker = <ValueType extends { toString: () => string }>({ | ||
value, | ||
onChange, | ||
options, | ||
actionType = 'default', | ||
}: SegmentedPickerProps<ValueType>) => { | ||
export const Tabs = ({ value, onChange, options, actionType = 'default' }: TabsProps) => { | ||
const layoutId = useId(); | ||
|
||
return ( | ||
<Root> | ||
{options.map(option => ( | ||
<SegmentButton | ||
key={option.value.toString()} | ||
onClick={() => onChange(option.value)} | ||
$actionType={actionType} | ||
disabled={option.disabled} | ||
$getFocusOutlineColor={theme => theme.color.action[outlineColorByActionType[actionType]]} | ||
$getBorderRadius={theme => theme.borderRadius.xs} | ||
> | ||
{value === option.value && <SelectedIndicator layout layoutId={layoutId} />} | ||
{option.label} | ||
</SegmentButton> | ||
))} | ||
</Root> | ||
<RadixTabs.Root value={value} onValueChange={onChange}> | ||
<RadixTabs.List asChild> | ||
<Root> | ||
{options.map(option => ( | ||
<RadixTabs.Trigger | ||
value={option.value} | ||
key={option.value.toString()} | ||
disabled={option.disabled} | ||
asChild | ||
> | ||
<Tab | ||
onClick={() => onChange(option.value)} | ||
disabled={option.disabled} | ||
$actionType={actionType} | ||
$getFocusOutlineColor={theme => | ||
theme.color.action[outlineColorByActionType[actionType]] | ||
} | ||
$getBorderRadius={theme => theme.borderRadius.xs} | ||
> | ||
{value === option.value && <SelectedIndicator layout layoutId={layoutId} />} | ||
{option.label} | ||
</Tab> | ||
</RadixTabs.Trigger> | ||
))} | ||
</Root> | ||
</RadixTabs.List> | ||
</RadixTabs.Root> | ||
); | ||
}; |
This file contains 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 |
---|---|---|
|
@@ -31,10 +31,15 @@ const focusOutline = css<{ | |
* disabled button, the overlay of the disabled button would be above the | ||
* outline, making the outline appear to be partly cut off. | ||
*/ | ||
&:focus::after { | ||
&:focus-within { | ||
outline: none; | ||
} | ||
|
||
&:focus-within::after { | ||
Comment on lines
+34
to
+38
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. Radix UI's |
||
outline-color: ${props => props.$getFocusOutlineColor(props.theme)}; | ||
} | ||
|
||
&:disabled, | ||
&:disabled::after { | ||
pointer-events: none; | ||
} | ||
|
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.
suggestion (non-blocking, worth implementing): expose
TabsContent
component from Radix and somehow allow thechildren
forTabs
.If you inspect the markup of the Radix Tabs example, you can see that
Tabs.Content
components are nested inside theTabs.Root
. It allows Radix to set useful attributes likedata-state="active"
androle="tabpanel"
. Plus, there is a rendering logic involved that all[role="tabpanel"]
divs are rendered but inactive divs have empty contents. All of this corresponds to W3C standards.Developers might and probably will forget to implement this, so it would be useful to help them (and ourselves)
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 call — will look into it. I'm not sure that
<TabsContent />
will be appropriate in our case, since we won't have multiple tab contents rendered on the page at the same time —<Tabs />
will usually be used for routing, I believe. But I created a ticket to look into it nonetheless.