aiellie

Search

Find a component, template or page

GitHub
Design

History Buttons

Back and forward buttons for stepping through what was opened, each with a tooltip naming its shortcut.

Release notes
Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/history-buttons"

shadcn needs the @aiellie registry in components.json first. npx aiellie add history-buttons adds it on its first run, then installs the same way.

History buttons are the back and forward pair for moving through what someone has opened, the way a browser does. They show whether each way is open and the keys that do the same; the history itself, and listening for those keys, stays with you.

Getting started

  1. Wire them to the router

    In Next.js the router already keeps the history. Track how far back you are to know when each button is open.

    components/app-header.tsx
    "use client"
    
    import { useRouter } from "next/navigation"
    
    import { HistoryButtons } from "@/components/ui/history-buttons"
    
    export function AppHeader({
      canGoBack,
      canGoForward,
    }: {
      canGoBack: boolean
      canGoForward: boolean
    }) {
      const router = useRouter()
    
      return (
        <HistoryButtons
          canGoBack={canGoBack}
          canGoForward={canGoForward}
          onBack={() => router.back()}
          onForward={() => router.forward()}
        />
      )
    }
  2. Listen for the keys

    The tooltips show ⌘[ and ⌘], but the buttons don't bind them. Add the listener where the history lives, so the keys work wherever focus is.

    React.useEffect(() => {
      const onKey = (event: KeyboardEvent) => {
        if (!(event.metaKey || event.ctrlKey)) return
        if (event.key === "[") router.back()
        if (event.key === "]") router.forward()
      }
      window.addEventListener("keydown", onKey)
      return () => window.removeEventListener("keydown", onKey)
    }, [router])

Anatomy

<div data-slot="history-buttons">
  <button /> {/* Back, a TooltipIconButton, disabled without canGoBack */}
  <button /> {/* Forward, disabled without canGoForward */}
</div>

Each button is a 28px TooltipIconButton whose tooltip carries its shortcut. The arrows flip in a right-to-left layout, where back points right.

Examples

With the keys bound

Press ⌘[ or ⌘] with the preview in focus, or use the buttons.

Release notes

Other shortcuts

Change what the tooltips show with backShortcut and forwardShortcut, to match whatever you listen for.

<HistoryButtons backShortcut="Alt ←" forwardShortcut="Alt →" {...history} />

API reference

HistoryButtons

PropTypeDefaultDescription
canGoBackbooleanrequiredWhether Back is open. It is disabled when not.
canGoForwardbooleanrequiredWhether Forward is open.
onBack() => voidrequiredRuns when Back is pressed.
onForward() => voidrequiredRuns when Forward is pressed.
backShortcutstring"⌘["Shown in Back's tooltip. The keys themselves are yours to listen for.
forwardShortcutstring"⌘]"Shown in Forward's tooltip.
classNamestringMerged onto the root.

All other div props are forwarded to the root.