Button group
Related actions joined on one surface: a shared highlight glides under your finger (slide across to pick), hairlines fade beside it, labels confirm in place without shifting the row, and a chevron opens more actions.
npx @swipeui/cli add button-groupWhen to use it
- A row of related actions on one item, such as Share, Duplicate and Archive, with the rest in a trailing menu.
- Compact controls with a live value between them, such as zoom out, 100% and zoom in.
- A short segmented choice, such as Day, Week and Month, by passing value and onValueChange.
- A vertical tool rail or zoom rail beside a canvas or map (orientation="vertical").
When not to use it
- Unrelated actions or a form's main and secondary actions. Space ordinary Buttons apart.
- A setting that switches the content below as a page-level control. Segmented control is the standard choice.
- Many options or long labels. Use Select or Menu.
- One main action with a few alternatives. Split button keeps the main action prominent.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add button-group- Adds
components/swipeui/ui/button-group.tsx,components/swipeui/ui/use-button-group.tsand 16 shared helpers. - Installs class-variance-authority, react-native-gesture-handler, react-native-reanimated, react-native-worklets, uniwind, clsx, tailwind-merge, expo-haptics, react-native-safe-area-context, @expo/ui, @gorhom/bottom-sheet, expo-blur, react-native-svg. In Expo apps it uses
npx expo installso versions match your SDK.
Usage
import { ButtonGroup } from '@/components/swipeui/ui/button-group';
import { Archive, Copy, Link } from '@/components/icons'; // any SVG icon set
<ButtonGroup
accessibilityLabel="Document actions"
options={[
{ value: 'share', label: 'Share', icon: Link, confirmLabel: 'Copied', onPress: copyLink },
{ value: 'duplicate', label: 'Duplicate', icon: Copy, onPress: duplicate },
{ value: 'archive', label: 'Archive', icon: Archive, onPress: archive },
]}
menu={{
label: 'More actions',
actions: [
{ id: 'pin', label: 'Pin to sidebar' },
{ id: 'delete', label: 'Delete copy', destructive: true },
],
onSelect: runAction,
}}
/>;Icons are components or render functions; each gets { color, size } for the group's surface and size.
Examples
import { Text } from '@/components/swipeui/ui/text';
import { ButtonGroup } from '@/components/swipeui/ui/button-group';
// A live value between two icon buttons. The middle segment is a display, not a button.
<ButtonGroup
size="sm"
accessibilityLabel="Zoom"
options={[
{ value: 'out', label: 'Zoom out', icon: Minus, iconOnly: true, onPress: zoomOut },
{ value: 'level', label: `Zoom ${zoom}%`, content: <Text>{zoom}%</Text> },
{ value: 'in', label: 'Zoom in', icon: Plus, iconOnly: true, onPress: zoomIn },
]}
/>;// A segmented choice: pass value and onValueChange and the highlight stays under the choice.
<ButtonGroup
variant="soft"
layout="fill"
accessibilityLabel="Range"
value={range}
onValueChange={setRange}
options={[
{ value: 'day', label: 'Day' },
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
]}
/>// The main pair of a screen, as a filled capsule. reserveLabels keeps Save as wide as Saved.
<ButtonGroup
variant="solid"
shape="pill"
accessibilityLabel="Draft"
options={[
{
value: 'save',
label: saved ? 'Saved' : 'Save',
reserveLabels: ['Save', 'Saved'],
onPress: save,
},
{ value: 'preview', label: 'Preview', onPress: preview },
]}
/>API reference
ButtonGroupProps
ButtonGroupOption
ButtonGroupMenu
Composition and behavior
- One highlight for the whole group. Touch a segment and it lands under it, growing slightly into place. Slide your finger across and it glides from segment to segment with a selection tick; lift and the segment under your finger runs. In an action group it fades once you lift; in a selection group it stays under the choice.
- Hairlines sit between segments and fade on both sides of the highlight, so it reads as one shape.
- Confirmations (
confirmLabel,confirmIcon) replace the label forconfirmDurationin theconfirmTonecolour (green by default), then the label returns. The segment keeps the width of its widest label, so nothing beside it moves; screen readers hear the confirmation once. - More actions go in
menu, a trailing chevron that turns over while its menu is open. The menu is a compact list just under the bar, lined up with its end edge, with no title or close button. A segment withmenuopens one from its own label. Rows can have icons and be destructive or disabled. - Surfaces:
outline(a bordered card, the default),soft(a muted tray),tinted(a wash of your primary colour) andsolid(filled, for a screen's main pair). The 1.x names still work. - Sizes and shapes: segments are 36, 40 or 44 points tall in a 4-point tray, with corners from your preset (or
shape="pill") and a concentric highlight.orientation="vertical"stacks them into a rail. - Selection: passing
value,defaultValueoronValueChangeturns it into a tab list.expandSelectedshows only the selected label; Expanding button group is that preset.
Button group settings
| Prop | Does | Default |
|---|---|---|
| variant | outline, soft, tinted or solid | outline |
| size | sm, md or lg | md |
| shape | preset (your control radius) or pill | preset |
| orientation | horizontal or vertical | horizontal |
| layout | fit (sized to content) or fill (equal widths) | fit |
| iconOnly | Icons only, labels spoken | false |
| dividers | Hairlines between segments | true |
| highlight | The shared gliding highlight | true |
| slideToSelect | Slide across, lift to run | true |
| expandSelected | Only the selected segment shows its label | false |
| menu | A trailing chevron with more actions | none |
| confirmDuration | How long a confirmation shows, in ms | 1600 |
| haptics | true, false or { press, slide } | true |
| disabled | Disables every segment and the menu | false |
| value / defaultValue / onValueChange | Selection mode | none |
| motion | A local motion feel or override | the app's feel |
Per segment: icon, iconOnly, content, onPress, destructive, confirmLabel, confirmIcon, confirmTone (success by default), reserveLabels, menu, menuEngine, onMenuSelect and disabled. When the row is wider than its container, labelled segments shrink and truncate so the chevron stays inside the bar.
Behavior and gestures
- Touch a segment and one shared highlight lands under it; lift and it fades in an action group, or stays under the choice in a selection group.
- Slide a finger across the group and the highlight follows from segment to segment with a selection tick; lifting runs (or selects) the segment under the finger; in an expanding group the label opens under the finger as you slide. A plain tap never moves the highlight back. slideToSelect turns sliding off.
- The hairlines on either side of the highlight fade so it reads as one shape; dividers={false} removes them and highlight={false} removes the highlight.
- A segment with confirmLabel (and confirmIcon) shows it, such as "Copied", in the confirmTone colour (green by default; warning, destructive, primary or none) for confirmDuration, then returns; reserveLabels keeps the segment as wide as its widest label so the row never shifts.
- The menu prop adds a trailing chevron that turns over and opens a compact menu just under the bar, lined up with the bar's end edge (icons, destructive and disabled rows, no title or close button); a segment with menu does the same with its label.
- content puts a live value in a segment, such as a zoom level; without onPress it is a display, not a button.
- iconOnly (group or segment) shows just icons; expandSelected shows only the selected segment's label while the others shrink to icons.
- Pressing a segment dips its content on the press spring (icon-only segments dip deeper); the group and its neighbours never move.
- haptics plays an impact on press and ticks while sliding; pass { press, slide } to switch them separately.
- When a row is wider than its container, labelled segments shrink and truncate so the chevron and icon-only segments always stay inside the bar.
Accessibility
- An action group is a toolbar of buttons; with a selection prop it becomes a tab list whose tabs report their selected state.
- Name the group with accessibilityLabel. Icon-only and content segments keep their label as the spoken name.
- A confirmation such as "Copied" is announced once and becomes the segment's name while it shows.
- The chevron is a button named by menu.label that reports expanded; its menu moves focus to its heading and returns it on close.
- Disabled segments are announced as disabled and dim; sliding skips them and display-only segments.
- Highlight and dividers use physical positions in a left-to-right layer, so right-to-left layouts mirror correctly; segments still read in reading order.
Motion
- Uses the
select,press,morphandtogglemotion roles, so it follows your app's feel. Passmotionto tune this one instance. - Select: the highlight glides between segments; when it first appears it lands in place, growing from 94%, instead of sliding in. It deepens while pressed and fades on lift in an action group.
- Press: segment content dips on your feel's press scale, deeper for icon-only segments.
- Morph: confirmation labels rise in as the old label fades; expanding segments resize on the morph spring.
- Toggle: the chevron turns over while its menu is open.
- Reduce Motion moves the highlight without travel or growth, cross-fades labels and stops the chevron turning; the press dip is removed.
Performance
- The highlight, dividers and slide run on the UI thread with shared values; React renders only when the selection, a confirmation or the menu changes.
- Segment frames are measured once per layout; rebuilding options each render is fine.
Notes for AI
- Give every segment a unique value and a label, even icon-only ones; name the group with accessibilityLabel.
- Use onPress per segment for actions; add value and onValueChange only when one segment should stay chosen.
- Put three or four segments in a group and the rest in menu; use confirmLabel for quick feedback such as Copied.
- Pass icons as render functions so they get the right colour and size: icon: ({ color, size }) => <Share color={color} size={size} />.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.