This repository was archived by the owner on Jul 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: segmented button #268
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00c9326
feat: button
adaam2 2c2d3a4
style: Update styling in SegmentedButton component
adaam2 2413f88
style: Update button hover and tap scales
adaam2 3d56680
style: update transition damping to 80
adaam2 eba67aa
style: Remove unnecessary motion animations
adaam2 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,19 @@ | ||
| .active { | ||
| border-radius: 50px; | ||
| background: #f8f8f8; | ||
| box-shadow: | ||
| 0 0 4px 0 rgba(0, 0, 0, 0.1), | ||
| 0 2px 1px 0 #fff inset, | ||
| 0 -2px 1px 0 rgba(0, 0, 0, 0.05) inset; | ||
| } | ||
|
|
||
| :global(.dark) { | ||
| .active { | ||
| background: #121212; | ||
| box-shadow: | ||
| 0 0 6px 0 rgba(0, 0, 0, 0.35), | ||
| 0 2px 1px 0 rgba(255, 255, 255, 0.08) inset, | ||
| 0 -2px 1px 0 rgba(0, 0, 0, 0.45) inset; | ||
| } | ||
| } | ||
|
|
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,104 @@ | ||
| import { Meta, StoryObj } from '@storybook/react' | ||
| import { | ||
| SegmentedButton, | ||
| SegmentedButtonItemProps, | ||
| SegmentedButtonProps, | ||
| } from '.' | ||
| import { useState } from 'react' | ||
| import React from 'react' | ||
|
|
||
| type Story = StoryObj<typeof SegmentedButton> | ||
|
|
||
| const meta: Meta<typeof SegmentedButton> = { | ||
| component: SegmentedButton, | ||
| tags: ['autodocs'], | ||
| } | ||
|
|
||
| export default meta | ||
|
|
||
| const StoryRenderer = (args: SegmentedButtonProps) => { | ||
| const getInitialActiveId = () => { | ||
| let foundActiveId: string | null = null | ||
| React.Children.forEach(args.children, (child) => { | ||
| if ( | ||
| foundActiveId === null && | ||
| React.isValidElement(child) && | ||
| child.props.active && | ||
| child.props.id | ||
| ) { | ||
| foundActiveId = child.props.id as string | ||
| } | ||
| }) | ||
|
|
||
| if (foundActiveId) return foundActiveId | ||
|
|
||
| let firstId: string | null = null | ||
| React.Children.forEach(args.children, (child) => { | ||
| if (firstId === null && React.isValidElement(child) && child.props.id) { | ||
| firstId = child.props.id as string | ||
| } | ||
| }) | ||
|
|
||
| return firstId | ||
| } | ||
|
|
||
| const [activeId, setActiveId] = useState<string | null>(() => | ||
| getInitialActiveId() | ||
| ) | ||
| return ( | ||
| <SegmentedButton {...args}> | ||
| {React.Children.map(args.children, (child) => { | ||
| if (!React.isValidElement(child)) { | ||
| return child | ||
| } | ||
|
|
||
| const props: SegmentedButtonItemProps = { | ||
| id: child.props.id ?? '', | ||
| active: child.props.id === activeId, | ||
| children: child.props.children, | ||
| onClick: () => setActiveId(child.props.id ?? ''), | ||
| } | ||
| return React.cloneElement(child, props) | ||
| })} | ||
| </SegmentedButton> | ||
| ) | ||
| } | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| children: [ | ||
| <SegmentedButton.Item id="chat">Chat</SegmentedButton.Item>, | ||
| <SegmentedButton.Item id="split" active> | ||
| Split | ||
| </SegmentedButton.Item>, | ||
| <SegmentedButton.Item id="code">Code</SegmentedButton.Item>, | ||
| ], | ||
| }, | ||
| render: (args) => <StoryRenderer {...args} />, | ||
| } | ||
|
|
||
| export const TwoItems: Story = { | ||
| args: { | ||
| children: [ | ||
| <SegmentedButton.Item id="chat">Chat</SegmentedButton.Item>, | ||
| <SegmentedButton.Item id="split" active> | ||
| Split | ||
| </SegmentedButton.Item>, | ||
| ], | ||
| }, | ||
| render: (args) => <StoryRenderer {...args} />, | ||
| } | ||
|
|
||
| export const FourItems: Story = { | ||
| args: { | ||
| children: [ | ||
| <SegmentedButton.Item id="chat">Chat</SegmentedButton.Item>, | ||
| <SegmentedButton.Item id="split" active> | ||
| Split | ||
| </SegmentedButton.Item>, | ||
| <SegmentedButton.Item id="code">Code</SegmentedButton.Item>, | ||
| <SegmentedButton.Item id="beast-mode">Beast Mode</SegmentedButton.Item>, | ||
| ], | ||
| }, | ||
| render: (args) => <StoryRenderer {...args} />, | ||
| } |
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,108 @@ | ||
| import { cn } from '@/lib/utils' | ||
| import { motion, MotionProps } from 'framer-motion' | ||
| import React from 'react' | ||
| import styles from './index.module.css' | ||
|
|
||
| export interface SegmentedButtonProps { | ||
| children: React.ReactNode | ||
| className?: string | ||
| } | ||
|
|
||
| const SegmentedButtonBase = ({ children, className }: SegmentedButtonProps) => { | ||
| const [hoveredId, setHoveredId] = React.useState<string | null>(null) | ||
|
|
||
| const enhancedChildren = React.Children.map(children, (child) => { | ||
| if (!React.isValidElement<SegmentedButtonItemProps>(child)) return child | ||
| const childId: string | undefined = child.props.id | ||
|
|
||
| const handleMouseEnter: React.MouseEventHandler<HTMLButtonElement> = ( | ||
| e | ||
| ) => { | ||
| setHoveredId(childId ?? null) | ||
| child.props.onMouseEnter?.(e) | ||
| } | ||
|
|
||
| const handleMouseLeave: React.MouseEventHandler<HTMLButtonElement> = ( | ||
| e | ||
| ) => { | ||
| setHoveredId((current) => (current === childId ? null : current)) | ||
| child.props.onMouseLeave?.(e) | ||
| } | ||
|
|
||
| const highlighted = hoveredId ? childId === hoveredId : child.props.active | ||
|
|
||
| return React.cloneElement( | ||
| child as React.ReactElement<SegmentedButtonItemProps>, | ||
| { | ||
| highlighted, | ||
| onMouseEnter: handleMouseEnter, | ||
| onMouseLeave: handleMouseLeave, | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| return ( | ||
| <div | ||
| className={cn( | ||
| 'border-neutral-softest relative inline-flex flex-row items-center overflow-hidden rounded-full border shadow-xs backdrop-blur-md', | ||
| className | ||
| )} | ||
| onMouseLeave={() => setHoveredId(null)} | ||
| > | ||
| {enhancedChildren} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| SegmentedButtonBase.displayName = 'SegmentedButton' | ||
|
|
||
| export interface SegmentedButtonItemProps extends MotionProps { | ||
| children: React.ReactNode | ||
| className?: string | ||
| active?: boolean | ||
| highlighted?: boolean | ||
| id: string | ||
| onClick?: () => void | ||
| onMouseEnter?: React.MouseEventHandler<HTMLButtonElement> | ||
| onMouseLeave?: React.MouseEventHandler<HTMLButtonElement> | ||
| } | ||
|
|
||
| const SegmentedButtonItem = ({ | ||
| children, | ||
| className, | ||
| highlighted, | ||
| ...props | ||
| }: SegmentedButtonItemProps) => { | ||
| return ( | ||
| <motion.button | ||
| className="text-codeline-xs relative flex items-center rounded-full px-5 py-1 uppercase" | ||
| {...props} | ||
| > | ||
| {highlighted && ( | ||
| <motion.span | ||
| layoutId="segmented-highlight" | ||
| className={cn( | ||
| 'absolute inset-0 -z-10 rounded-full shadow-sm', | ||
| styles.active | ||
| )} | ||
| transition={{ type: 'spring', duration: 0.7, bounce: 0.3 }} | ||
| /> | ||
| )} | ||
| <span | ||
| className={cn( | ||
| 'text-muted relative z-10', | ||
| highlighted && 'text-highlight', | ||
| className | ||
| )} | ||
| > | ||
| {children} | ||
| </span> | ||
| </motion.button> | ||
| ) | ||
| } | ||
|
|
||
| SegmentedButtonItem.displayName = 'SegmentedButtonItem' | ||
|
|
||
| export const SegmentedButton = Object.assign(SegmentedButtonBase, { | ||
| Item: SegmentedButtonItem, | ||
| }) |
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
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.