suiss/ui ยท Interaction
Swipe Actions
A list row that reveals action buttons when swiped sideways, with optional full swipe to run an action.
Swipe Actions is an open-source interaction from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
SwipeActions recreates iOS swipe actions for list rows. Dragging SwipeActionsContent sideways reveals the buttons in SwipeActionsActions on that side; releasing past half their width snaps the row open, otherwise it springs closed. Dragging past the actions meets increasing resistance, unless an action has fullSwipe: then dragging past 60% of the row width expands that action across the row, and releasing collapses the row and runs it.
Only one row stays open at a time. Tapping the content or pressing elsewhere closes an open row, and choosing an action closes it after the action runs. Use it for quick per-row actions such as archive, delete, or mark as unread.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a SwipeActions:
Examples
Both sides
A row with a leading Unread action on one side and trailing Archive and Delete actions on the other. Add a SwipeActionsActions for each side and set its side to leading or trailing.
Variants
Use the variant prop on SwipeAction to set the action color: default, neutral, archive or destructive.
Accessibility
- The row has
role="group", and actions are native buttons that stay in the tab order. - Moving focus to an action opens its side of the row so the button is visible; moving focus out of the row closes it.
Escapecloses an open row.- Leading and trailing sides follow the writing direction, so swipes mirror in right-to-left layouts.
API Reference
SwipeActions
The row container that tracks the swipe and manages open state.
Also accepts the props of React.ComponentProps<"div">.
Place SwipeActionsContent inside SwipeActions, with a SwipeActionsActions for each side that has actions. Gestures are detected with pointer events and locked to the horizontal axis after a few pixels, so vertical scrolling still works. useSwipeActions returns close for closing the row from custom controls.
The root sets data-slot="swipe-actions", data-open (leading or trailing), data-full during a full swipe and data-removing while the row collapses. The --swipe-actions-offset custom property holds the current offset.
SwipeActionsContent
The visible row content that moves with the swipe.
Also accepts the props of React.ComponentProps<"div">.
SwipeActionsActions
The group of actions revealed on one side of the row.
| Prop | Type | Default |
|---|---|---|
side | SwipeActionsSide | "trailing" |
Also accepts the props of React.ComponentProps<"div">.
Action groups expose data-side.
SwipeAction
A single action button revealed by the swipe.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "destructive" | "archive" | "neutral" | "default" |
fullSwipe | boolean | false |
Also accepts the props of React.ComponentProps<"button">.
Handle each action with onClick. fullSwipe lets a long swipe expand the action across the row and run it by clicking it, so the same handler covers both. Full-swipe actions set data-full-swipe. swipeActionVariants is exported for styling other elements like an action.
Web preview
SwipeActions wraps a row so it can be dragged left or right to reveal SwipeAction buttons on the leading or trailing side. The buttons stretch to fill the revealed space, and opening one row closes any other open row.
When an action has fullSwipe, dragging past 60% of the row width expands that action over the others with a medium impact haptic. Releasing there slides the row off, collapses its height, then runs the action, which suits delete or archive. Use it for mail, message or task lists.
Installation
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/swipe-actions.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/swipe-actions.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/swipe-actions.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Accessibility
- Every action is offered as an accessibility action on
SwipeActionsContent, labelled by itsaccessibilityLabelor text, so screen reader users can run it without swiping. - Hidden actions are removed from the accessibility tree until their side is open.
- The row has the
grouprole and each action thebuttonrole.
API Reference
SwipeActions
The root that handles the swipe gesture.
| Prop | Type | Default |
|---|---|---|
onOpenChange | (side: SwipeActionsSide | null) => void | - |
Also accepts the props of ViewProps.
onOpenChange is called with leading, trailing or null when the row opens or closes. useSwipeActions returns close for closing the row from inside it.
SwipeActionsContent
The row content that slides.
Also accepts the props of ViewProps.
SwipeActionsActions
The group of actions for one side.
| Prop | Type | Default |
|---|---|---|
side | SwipeActionsSide | - |
children | React.ReactNode | - |
Also accepts the props of ViewProps.
Put the row in SwipeActionsContent, and the buttons in one SwipeActionsActions per side, with side set to leading or trailing (the default). Leading actions are on the left in left-to-right layouts and on the right in right-to-left layouts.
SwipeAction
One action button.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "destructive" | "archive" | "neutral" | "default" |
fullSwipe | boolean | false |
onPress | () => void | - |
Also accepts the props of PressableProps, except onPress, which takes no arguments.
Pressing an action runs onPress and closes the row. For a fullSwipe action, onPress runs after the row collapses, so remove the item from your data there. The full-swipe haptic plays only if the app provides an adapter through HapticsProvider. swipeActionVariants is also exported.
SwipeActionVariant
Values: .default, .destructive, .archive, .neutral.
SwipeActions
The root that handles the swipe gesture.
| Parameter | Type | Default |
|---|---|---|
background | Color | EsuissColor.background |
content | () -> Content | - |
leading | () -> Leading | - |
trailing | () -> Trailing | - |
Overloads: init(background:content:leading:trailing:), init(background:content:trailing:), init(background:content:leading:).
SwipeAction
One action button.
| Parameter | Type | Default |
|---|---|---|
_ย title | LocalizedStringKey | - |
variant | SwipeActionVariant: .default | .destructive | .archive | .neutral | .default |
fullSwipe | Bool | false |
action | () -> Void | - |
label | () -> Label | - |
icon | Image | - |
Overloads: init(_:variant:fullSwipe:action:label:), init(_:icon:variant:fullSwipe:action:), init(_:variant:fullSwipe:action:).
SwipeActionsSide
Values: Leading, Trailing.
SwipeActionVariant
Values: Default, Destructive, Archive, Neutral.
SwipeAction
| Parameter | Type | Default |
|---|---|---|
label | String | - |
onClick | () -> Unit | - |
icon | ImageVector? | null |
variant | SwipeActionVariant: Default | Destructive | Archive | Neutral | SwipeActionVariant.Default |
fullSwipe | Boolean | false |
SwipeActionsState
| Property | Type |
|---|---|
openSide | SwipeActionsSide? |
SwipeActionsState.close
Takes no parameters.
SwipeActionsState.open
| Parameter | Type | Default |
|---|---|---|
side | SwipeActionsSide: Leading | Trailing | - |
rememberSwipeActionsState
Takes no parameters.
Returns SwipeActionsState.
SwipeActions
The root that handles the swipe gesture.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
leading | List<SwipeAction> | emptyList() |
trailing | List<SwipeAction> | emptyList() |
state | SwipeActionsState | rememberSwipeActionsState() |
enabled | Boolean | true |
background | Color | EsuissTheme.colors.background |
content | @Composable () -> Unit | - |