A floating bar that offers to undo the last action for a few seconds before committing it.
Undo Bar 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.
import { TrashIcon } from "@phosphor-icons/react"import * as React from "react"import { UndoBarProvider, useUndoBar } from "@/components/interactions/undo-bar"import { Button } from "@/components/ui/button"const initialNotes = [ { id: 1, title: "Groceries", preview: "Milk, eggs, bread, coffee" }, { id: 2, title: "Trip ideas", preview: "Lisbon, Porto, the coast road" }, { id: 3, title: "Book list", preview: "Three novels for the summer" }, { id: 4, title: "Meeting notes", preview: "Ship the beta on Friday" },]type Note = (typeof initialNotes)[number]function NotesList() { const [notes, setNotes] = React.useState(initialNotes)
UndoBarProvider renders a floating bar at the bottom of the screen and provides useUndoBar. Calling show displays a title and an Undo button with a countdown ring. Pressing Undo, or Ctrl+Z or Cmd+Z outside text fields, runs onUndo; when the timer runs out, the bar closes and runs onCommit. Showing a new entry commits the previous one first.
The countdown pauses while the pointer hovers the bar, while it has focus, and while the page is hidden. The Undo button is a suiss/ui Button. Use it to make destructive actions, such as deleting from a list, immediate but reversible.
Installation
$ pnpm dlx shadcn@latest add @esuiss/undo-bar
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
A timeout of 10 seconds keeps the bar on screen longer before committing.
import { TrashIcon } from "@phosphor-icons/react"import * as React from "react"import { UndoBarProvider, useUndoBar } from "@/components/interactions/undo-bar"import { Button } from "@/components/ui/button"const initialNotes = [ { id: 1, title: "Groceries", preview: "Milk, eggs, bread, coffee" }, { id: 2, title: "Trip ideas", preview: "Lisbon, Porto, the coast road" }, { id: 3, title: "Book list", preview: "Three novels for the summer" }, { id: 4, title: "Meeting notes", preview: "Ship the beta on Friday" },]type Note = (typeof initialNotes)[number]function NotesList() { const [notes, setNotes] = React.useState(initialNotes)
With swipe actions
Notes deleted from SwipeActions, including a full swipe, can be restored from the bar.
import { TrashIcon } from "@phosphor-icons/react"import * as React from "react"import { SwipeAction, SwipeActions, SwipeActionsActions, SwipeActionsContent,} from "@/components/interactions/swipe-actions"import { UndoBarProvider, useUndoBar } from "@/components/interactions/undo-bar"const initialNotes = [ { id: 1, title: "Groceries", preview: "Milk, eggs, bread, coffee" }, { id: 2, title: "Trip ideas", preview: "Lisbon, Porto, the coast road" }, { id: 3, title: "Book list", preview: "Three novels for the summer" }, { id: 4, title: "Meeting notes", preview: "Ship the beta on Friday" },]
Accessibility
The bar sits in a polite live region, so its title is announced when it appears.
The bar has role="group" and is labelled by its title.
Ctrl+Z or Cmd+Z triggers undo while the bar is visible, except when focus is in an editable field.
The countdown pauses while the bar has keyboard focus, giving people time to reach the button.
API Reference
UndoBarProvider
Provides useUndoBar and renders the floating undo bar.
Prop
Type
Default
timeout
number
5000
className
string
-
children
React.ReactNode
-
Wrap the app or screen in UndoBarProvider and call show({ title, actionLabel, onUndo, onCommit }) from useUndoBar after performing the action. Apply the change optimistically, restore it in onUndo, and finalize it in onCommit. actionLabel defaults to Undo. useUndoBar also returns undo and dismiss to settle the current entry from code.
timeout is the number of milliseconds the bar stays visible before committing. className styles the fixed region that positions the bar, which sits at the bottom of the viewport above the safe-area inset. Each part sets a data-slot, such as undo-bar or undo-bar-action, for styling. The bar sets data-paused while the countdown is paused and data-closed while closing.
Web
Web preview
import { TrashIcon } from "phosphor-react-native"import * as React from "react"import { View } from "react-native"import { UndoBarProvider, useUndoBar } from "@/components/interactions/undo-bar"import { Button } from "@/components/ui/button"import { Icon } from "@/components/ui/icon"import { Text } from "@/components/ui/text"const initialNotes = [ { id: 1, title: "Groceries", preview: "Milk, eggs, bread, coffee" }, { id: 2, title: "Trip ideas", preview: "Lisbon, Porto, the coast road" }, { id: 3, title: "Book list", preview: "Three novels for the summer" }, { id: 4, title: "Meeting notes", preview: "Ship the beta on Friday" },]type Note = (typeof initialNotes)[number]
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
import PhosphorSwiftimport SwiftUIstruct UndoBarExample: View { var body: some View { UndoBarStage(timeout: .seconds(5)) { UndoBarNotes() } }}struct UndoBarNote: Identifiable, Equatable { let id: Int let title: String let preview: String}@MainActor let undoBarInitialNotes = [
UndoBarProvider shows a pill-shaped bar above the bottom safe area when show is called from useUndoBar. It holds a message, a countdown ring and an Undo button, and slides up into view.
If the user presses Undo, onUndo runs. If the countdown runs out, onCommit runs. Only one bar shows at a time: calling show again commits the previous action first. The countdown pauses while the app is in the background and resumes with the time left. Use it after deleting, archiving or moving items.
import * as React from "react"
import { HapticsContext } from "./haptics"
const DEFAULT_PORTAL_HOST = "INTERNAL_PRIMITIVE_DEFAULT_HOST_NAME"
type PortalEntries = ReadonlyMap<string, React.ReactNode>
const EMPTY_ENTRIES: PortalEntries = new Map()
let hosts: ReadonlyMap<string, PortalEntries> = new Map()
const listeners = new Set<() => void>()
function emit() {
for (const listener of listeners) listener()
}
Copy and paste the following code into components/interactions/undo-bar.tsx:
uniwind/components/interactions/undo-bar.tsx
import { cn } from "cn"import * as React from "react"import { AccessibilityInfo, AppState, I18nManager, useWindowDimensions, View,} from "react-native"import { cancelAnimation, Easing, Keyframe, ReduceMotion, type SharedValue, useAnimatedStyle,
Update the import paths to match your project setup.
import * as React from "react"
import { HapticsContext } from "./haptics"
const DEFAULT_PORTAL_HOST = "INTERNAL_PRIMITIVE_DEFAULT_HOST_NAME"
type PortalEntries = ReadonlyMap<string, React.ReactNode>
const EMPTY_ENTRIES: PortalEntries = new Map()
let hosts: ReadonlyMap<string, PortalEntries> = new Map()
const listeners = new Set<() => void>()
function emit() {
for (const listener of listeners) listener()
}
Copy and paste the following code into components/interactions/undo-bar.tsx:
nativewind/components/interactions/undo-bar.tsx
import { cn } from "cn"import * as React from "react"import { AccessibilityInfo, AppState, I18nManager, useWindowDimensions, View,} from "react-native"import { cancelAnimation, Easing, Keyframe, ReduceMotion, type SharedValue, useAnimatedStyle,
Update the import paths to match your project setup.
import * as React from "react"
import { HapticsContext } from "./haptics"
const DEFAULT_PORTAL_HOST = "INTERNAL_PRIMITIVE_DEFAULT_HOST_NAME"
type PortalEntries = ReadonlyMap<string, React.ReactNode>
const EMPTY_ENTRIES: PortalEntries = new Map()
let hosts: ReadonlyMap<string, PortalEntries> = new Map()
const listeners = new Set<() => void>()
function emit() {
for (const listener of listeners) listener()
}
Copy and paste the following code into components/interactions/undo-bar.tsx:
stylesheet/components/interactions/undo-bar.tsx
import * as React from "react"import { AccessibilityInfo, AppState, I18nManager, type StyleProp, StyleSheet, useWindowDimensions, View, type ViewStyle,} from "react-native"import { cancelAnimation, Easing, Keyframe, ReduceMotion,
Update the import paths to match your project setup.
import CoreTextimport SwiftUIimport UIKitstruct RGBA: Sendable { let red: CGFloat let green: CGFloat let blue: CGFloat let alpha: CGFloat init(_ hex: UInt32, _ alpha: CGFloat = 1) { red = CGFloat((hex >> 16) & 0xFF) / 255 green = CGFloat((hex >> 8) & 0xFF) / 255 blue = CGFloat(hex & 0xFF) / 255 self.alpha = alpha }
swift/Theme/Tokens.swift
import SwiftUIpublic enum EsuissColor { public static let background = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x000000)) public static let foreground = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7)) public static let card = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x1D1D1F)) public static let cardForeground = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7)) public static let popover = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x1D1D1F)) public static let popoverForeground = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7)) public static let primary = Color.adaptive(light: .init(0x0071E3), dark: .init(0x0A84FF)) public static let primaryForeground = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0xFFFFFF)) public static let secondary = Color.adaptive(light: .init(0x000000, 0.04), dark: .init(0xFFFFFF, 0.1)) public static let secondaryForeground = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7)) public static let muted = Color.adaptive(light: .init(0xF5F5F7), dark: .init(0x161617)) public static let mutedForeground = Color.adaptive(light: .init(0x6E6E73), dark: .init(0x86868B)) public static let accent = Color.adaptive(light: .init(0x000000, 0.06), dark: .init(0xFFFFFF, 0.1))
Copy the source and every file it uses:
swift/Interactions/UndoBar.swift
import Observationimport SwiftUIpublic struct UndoBarEntry: Identifiable, Sendable { public let id: UUID public let title: LocalizedStringResource public let actionLabel: LocalizedStringResource? let onUndo: @MainActor @Sendable () -> Void let onCommit: (@MainActor @Sendable () -> Void)?}@Observablepublic final class UndoBarManager: @unchecked Sendable { public static let shared = UndoBarManager() public private(set) var entry: UndoBarEntry?
swift/Components/Button.swift
import SwiftUIpublic struct VariantButtonStyle: ButtonStyle { public enum Variant: CaseIterable, Sendable { case `default`, outline, secondary, tinted, ghost, plain, destructive, link } public enum Size: CaseIterable, Sendable { case `default`, xs, sm, lg, xl, form, icon, iconXs, iconSm, iconLg, iconXl var height: CGFloat { switch self { case .xs, .iconXs: Metrics.Height.buttonXs case .sm, .iconSm: Metrics.Height.buttonSm case .default, .icon: Metrics.Height.button case .lg, .iconLg: Metrics.Height.buttonLg
swift/Components/ButtonGroup.swift
import SwiftUIextension EnvironmentValues { @Entry var buttonGroup: Axis? = nil @Entry var buttonGroupSize: CGSize = .zero}enum ButtonGroupSpace: Hashable { case group}struct ButtonGroupFrame { let bounds: Anchor<CGRect> let outlined: Bool}
swift/Interactions/SelectionList.swift
import PhosphorSwiftimport SwiftUIstruct SelectionModel<Value: Hashable> { var editing = false var selected: Set<Value> = [] var order: [Value] = [] mutating func toggle(_ value: Value) { if selected.contains(value) { selected.remove(value) } else { selected.insert(value) } }
Add the files to your app target.
Apply the Compose compiler plugin, enable Compose, set compileSdk to 37 or higher and minSdk to 24 or higher, and add the dependencies:
package com.esuiss.ui.themeimport androidx.compose.runtime.Immutableimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.text.font.FontWeightimport androidx.compose.ui.unit.dp@Immutabledata class Colors( val background: Color, val foreground: Color, val card: Color, val cardForeground: Color, val popover: Color, val popoverForeground: Color, val primary: Color,
The bar shown right after deleting a note, with a long timeout.
React Native
import { TrashIcon } from "phosphor-react-native"import * as React from "react"import { View } from "react-native"import { UndoBarProvider, useUndoBar } from "@/components/interactions/undo-bar"import { Button } from "@/components/ui/button"import { Icon } from "@/components/ui/icon"import { Text } from "@/components/ui/text"const initialNotes = [ { id: 1, title: "Groceries", preview: "Milk, eggs, bread, coffee" }, { id: 2, title: "Trip ideas", preview: "Lisbon, Porto, the coast road" }, { id: 3, title: "Book list", preview: "Three novels for the summer" }, { id: 4, title: "Meeting notes", preview: "Ship the beta on Friday" },]type Note = (typeof initialNotes)[number]
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
import PhosphorSwiftimport SwiftUIstruct UndoBarShowingExample: View { var body: some View { UndoBarStage(timeout: .seconds(60)) { UndoBarNotes(deleteFirst: true) } }}struct UndoBarNote: Identifiable, Equatable { let id: Int let title: String let preview: String}@MainActor let undoBarInitialNotes = [
import { TrashIcon } from "phosphor-react-native"import * as React from "react"import { View } from "react-native"import { UndoBarProvider, useUndoBar } from "@/components/interactions/undo-bar"import { Button } from "@/components/ui/button"import { Icon } from "@/components/ui/icon"import { Text } from "@/components/ui/text"const initialNotes = [ { id: 1, title: "Groceries", preview: "Milk, eggs, bread, coffee" }, { id: 2, title: "Trip ideas", preview: "Lisbon, Porto, the coast road" }, { id: 3, title: "Book list", preview: "Three novels for the summer" }, { id: 4, title: "Meeting notes", preview: "Ship the beta on Friday" },]type Note = (typeof initialNotes)[number]
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
import PhosphorSwiftimport SwiftUIstruct UndoBarLongerTimeoutExample: View { var body: some View { UndoBarStage(timeout: .seconds(10)) { UndoBarNotes() } }}struct UndoBarNote: Identifiable, Equatable { let id: Int let title: String let preview: String}@MainActor let undoBarInitialNotes = [
Shows the bar after a swipe-to-delete in a list with swipe actions.
Web
Web preview
import { TrashIcon } from "phosphor-react-native"import * as React from "react"import { View } from "react-native"import { SwipeAction, SwipeActions, SwipeActionsActions, SwipeActionsContent,} from "@/components/interactions/swipe-actions"import { UndoBarProvider, useUndoBar } from "@/components/interactions/undo-bar"import { Icon } from "@/components/ui/icon"import { Text } from "@/components/ui/text"const initialNotes = [ { id: 1, title: "Groceries", preview: "Milk, eggs, bread, coffee" }, { id: 2, title: "Trip ideas", preview: "Lisbon, Porto, the coast road" },
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
import PhosphorSwiftimport SwiftUIstruct UndoBarWithSwipeActionsExample: View { var body: some View { UndoBarStage(timeout: .seconds(5)) { UndoBarNotes(swipe: true) } }}struct UndoBarNote: Identifiable, Equatable { let id: Int let title: String let preview: String}@MainActor let undoBarInitialNotes = [
The bar has the group role and is a polite live region.
The countdown ring is hidden from screen readers.
API Reference
UndoBarProvider
Holds the current undo entry and renders the bar in a portal.
Prop
Type
Default
timeout
number
5000
className
string
-
portalHost
string
-
children
React.ReactNode
-
Wrap the screen or app in UndoBarProvider. After removing an item from the UI, call show({ title, onUndo, onCommit?, actionLabel? }) from useUndoBar: restore the item in onUndo, and do the permanent work, such as a server delete, in onCommit. useUndoBar also returns undo, which runs the current undo, and dismiss, which commits and hides the bar.
timeout is the countdown length in milliseconds. className styles the bar, and portalHost names a portal host to render into. The bar is up to 384 points wide and centered. The ring drains linearly and runs counterclockwise in right-to-left layouts. Slide animations respect the system reduce motion setting; the countdown always runs.
UndoBarEntry
Property
Type
id
UUID
title
LocalizedStringResource
actionLabel
LocalizedStringResource?
UndoBarManager
Takes no parameters.
Property
Type
static shared
UndoBarManager
entry
UndoBarEntry?
timeout
Duration
UndoBarManager.show
Parameter
Type
Default
_ย title
LocalizedStringResource
-
actionLabel
LocalizedStringResource?
nil
onUndo
() -> Void
-
onCommit
(() -> Void)?
nil
UndoBarManager.undo
Takes no parameters.
UndoBarManager.dismiss
Takes no parameters.
.undoBar
Parameter
Type
Default
_ย manager
UndoBarManager
.shared
timeout
Duration
.seconds(5)
UndoBarState
Takes no parameters.
UndoBarState.show
Parameter
Type
Default
title
String
-
onUndo
() -> Unit
-
actionLabel
String?
null
onCommit
(() -> Unit)?
null
UndoBarState.undo
Takes no parameters.
UndoBarState.dismiss
Takes no parameters.
rememberUndoBarState
Takes no parameters.
Returns UndoBarState.
UndoBarProvider
Holds the current undo entry and renders the bar in a portal.