Bruno DelorenceBruno
Appearance
Appearance

Choose your reading theme.

Contact

View switcher

New

The saved views of a records page: a menu that scales from its button with a band that glides between rows, each view a table, a board or a chart of its own.

view-switcher/view-switcher.tsx
Live · interactive

People

Everyone you’ve emailed or met, kept up to date from your inbox and calendar.Beta
Name · Contact addedContact added, filled in from your email and calendar
Maren Okafor2 hours ago
Priya RamanYesterday, 1:32 pm
Theo LindqvistYesterday, 12:41 am
Caio MendesMarch 27
Ines ValtonenMarch 21
Rafael DuarteMarch 21
Oskar BrandtFebruary 21
Hana KowalFebruary 21

The live component, numbered part by part. Every measurement is read from it as it is drawn. Point at a row to outline its part, or at a part to find its row.

The choices it offers, each value drawn live and the default marked, with how to pick one.

When to use

  • A records page in a CRM, a tracker or an admin tool where people keep a few saved ways of looking at the same list: a filter, a sort and a layout under one name.
  • Views that differ in kind, not only in filters: the same people as rows, as cards grouped by a status, or as a chart of the groups.
  • Lists people return to every day, where switching between “mine”, “recent” and “needs attention” should take one press and the keyboard.
  • Teams that name their own views: rename, duplicate and delete them in place, with an undo, without a settings page.

When not to use

  • Two or three fixed layouts nobody renames. Use a segmented control or tabs; a menu hides choices that could all show.
  • One-off filtering. Use filter chips over the list; a saved view is worth its name only when people come back to it.
  • Moving between different pages or records. Use navigation; views change how one list is drawn, not where you are.
  • Dozens of views. Add search to the menu, or group them by owner; a flat menu stops scanning well past ten or so.

Saving views

  • A view is a name, a type, filter rules, a date column and a sort. The component keeps its own list, starting from views, which only counts the first time.
  • onViewsChange gets every view after each create, rename, duplicate, delete, undo, or change of rules or sort. Save the list from there.
  • onViewChange hears the view that opens, also when a deletion moves to its neighbour or Undo brings it back. Store its id and pass it as defaultView next time.
  • A new view is named in place; left blank, it is called Untitled table (or board, or chart). A duplicate is called … copy and opens in rename.

Deleting and undo

  • Delete sits last in a view’s ⋮ menu, in red after a rule. It is off while only one view is left, so the page always has one.
  • The view goes at once, and its neighbour opens if it was the active one. A dark toast offers Undo: its six seconds wait while the views menu is still open and while the pointer or the focus is on the toast. Undo, or ⌘Z or Ctrl+Z from anywhere in the component, puts the view back in its place.
  • onViewsChange fires the moment a view is deleted, before the undo’s time is up, and again on Undo with the view back in the list. Nothing tells you when the toast has gone, so if a deletion on your server can’t be taken back, keep it soft or delay it until Undo can no longer come.

Each one drawn with the component itself, used well or badly.

The page title says what the records are; each view’s name says which of them.

  • The page title (title): the records, plural, in a word: People, Contacts. It also names the info button, “About People”.
  • View names: sentence case, no full stop, a word or three for who is in the list. The button leaves about 130 px for the name, near the width of Strong connection; longer names end in an ellipsis.
  • Not the rules, not the layout: the Filter button counts and lists the rules, and the type tile shows table, board or chart.
  • Rows and lanes use the data’s own words: full names, and dates as people say them, like Yesterday, 4:00 pm or 3 weeks ago.

You can add this component with the shadcn CLI or by hand:

Into components/view-switcher of any React project, with no setup:

Terminal
npx shadcn@latest add https://brunodelorence.com/r/view-switcher.json

Either way it needs React 19 and a bundler that supports CSS Modules, such as Next.js or Vite. It has a dark colour scheme: it follows your page’s color-scheme, light unless the page sets one, or its colorScheme prop, with nothing to configure. Browsers without light-dark() show it light.

Usage

people-page.tsx
import { ViewSwitcher } from './view-switcher';export function PeoplePage() {  return (    <ViewSwitcher      title="People"      defaultView="activity"      onViewChange={(view) => console.log('Showing', view.name)}      onViewsChange={(views) => console.log('Saved views', views)}      autoplay={false}    />  );}

Each state below is a live instance, locked so you can study it.

The pieces it is built from, each with its measurements read live.

Below are variations of this component, each live, with its code.

Opened on the board

The same page opened on Slipping away: everyone quiet for two weeks, in lanes by how long, the longest first.

Your own views

A Contacts page with four views of its own, open on a Directory table sorted by name, with a board of who to reach out to and a chart of the whole team.

Renaming in place

The menu open with Strong connection being renamed to Close circle in its row.

Selection and filters

Strong connection with two people checked and its Filter panel open on its three rules.

The People page of a CRM, with its saved views behind one button. The menu lists them with a coloured tile for their type, the active one in blue, and a grey band that glides to the row under the pointer or the focus. Picking one changes what is below: Activity is a table of everyone added, newest first, under seven rules; Strong connection keeps the people you were in touch with this month; Slipping away is a board of who has gone quiet, in lanes by how long; Contact overview is a chart of the same buckets. Each row’s ⋮ menu renames it in place (F2 too), duplicates it or deletes it with an undo, and Create new names a view and picks its type without leaving the menu. Filter lists the view’s rules to remove or add, and Sort orders it. Light and dark.

  • A view is data: a name, a type (table, board or chart), the rules that filter the people, the date it shows and its sort. The body is drawn from the active view each time, so picking a view, removing a rule or changing the sort all redraw the same way, and the Filter badge counts the active view’s rules.
  • The menu stays mounted and hidden, so it can animate both ways: it scales up from the corner under its button on a spring, fades out faster than it came, and its chevron turns over. Hidden, it is out of the accessibility tree and the tab order.
  • One band sits under the rows and moves with the `translate` property to the row the pointer or the focus is on, measured from the row’s own offset, so it glides instead of jumping and stays right inside a scaled-down card. When nothing is under it, it fades where it was and glides from there next time.
  • The views button follows the menu button pattern: Enter, Space or ↓ open on the active view, ↑ on the last row; ↑ ↓ Home and End move, letters jump to the next name that starts with them, F2 renames, → or Shift+F10 opens the row’s ⋮ menu, Escape closes and gives the focus back.
  • Renaming lays a field over the name in its row; Enter or leaving it keeps the name, Escape puts the old one back. Duplicate adds “… copy” under the original with its name already selected. Delete removes the view and, if it was open, opens its neighbour; Undo puts it back in its place for six seconds.
  • Create new turns its row into a name field, a radio group of the three types and Create view. The new view starts with one rule and opens at once.
  • The table’s rows, the board’s cards and the chart’s bars come in one after the other when the view changes, staggered from a custom property; the Filter count rolls to its new number.
  • While on screen and untouched, a demo pointer opens the menu, glides past Activity to Strong connection and picks it, opens Slipping away and Contact overview, then creates a board named Weekly check-ins. Any real input hands it over; it comes back after 8 s of quiet.
PropTypeDefaultDescription
titlestring'People'The page’s title, in the head.
viewsView[]four viewsThe saved views at first: an id, a name, a type (table, board or chart), filter rules, the date column (added or last contact) and the sort.
peoplePerson[]sixteen peopleThe records the views filter: a name, a tint, when they were added and last contacted, connection strength, owner, email, archive and source.
defaultViewstring—The id of the view open at first; the first view when left out.
defaultSelectedstring[][]Ids of the people checked at first.
defaultStateViewSwitcherState—Opens locked in a moment, with the demo off: the view, a popover open, the row under the band, a ⋮ menu, a rename or a new view in progress, or a view just deleted.
onViewChange(view: View) => void—Called with the view picked, created, or opened after a delete.
onViewsChange(views: View[]) => void—Called with every view when one is created, renamed, duplicated, deleted, restored or has its rules or sort changed.
onSelectionChange(ids: string[]) => void—Called with the checked people’s ids when they change.
autoplaybooleantrueWhile on screen and untouched, a demo pointer shows the main interactions. Any real input stops it.
colorScheme'light' | 'dark' | 'system'—Light or dark colours, or the reader’s; without it, the page’s color-scheme.
classNamestring—Classes for the stage.

Keyboard

The views button opens its menu with Enter, Space or Down, and Up opens it on the last row. In the menu, Up and Down move and wrap round, Home and End jump to the ends, typing jumps to a name, and Enter opens the view. F2 renames in place; Right, Shift+F10 or the Menu key opens the row’s ⋮ menu, and Left or Escape comes back. Escape closes the menu and puts focus back on its button; Tab closes it. After a delete, ⌘Z or Ctrl+Z undoes it from anywhere in the component (not inside a text field), and the toast’s Undo button is also in the Tab order. Focus shows as a blue ring, or as a blue bar at the start of a views menu row.

Screen readers

  • The views button is a menu button that names the view and its type: “View: Activity, Table”. The rows are radio items, the active one checked, and each ⋮ is “Options for Activity”.
  • Filter and Sort say their state in their names: “Filter, 7 rules”, “Sort, newest first”. The count badge itself is hidden.
  • A polite live region says what changed: the view opened with its type and count, a rename, a duplicate, a deletion with how to undo it (“Undo with Command Z or Control Z”), a restore, a sort or a filter.

Undo timing

The undo toast’s six seconds only start once the views menu has closed, and stop while the pointer or the focus is on the toast; they go on from where they stopped when it leaves. That way nobody loses Undo while reaching for it (WCAG 2.2.1 Timing Adjustable, A). Focus stays in the menu after a delete, so ⌘Z or Ctrl+Z is the keyboard’s way back.

Colour

Text meets 4.5:1 and icons, checkbox edges and focus rings 3:1 against what they sit on, in light and dark (WCAG 1.4.3 and 1.4.11, AA). The avatars’ initials and the bars’ fills only repeat a name or a number written beside them.

Motion

Under reduced motion the menu, the band and the chevron change at once, the rows, cards, bars, counts and toast don’t animate in, and the demo never plays.

In this build

  • The views button is a menu button (aria-haspopup, aria-expanded, aria-controls) labelled with the active view and its type. The views are menuitemradio items, the open one checked; each ⋮ is a menuitem with its own menu, reached with → or Shift+F10 as well as the pointer.
  • In the menu ↑ ↓ Home and End move the focus, typed letters jump to a name, Enter or Space picks, F2 renames, and Escape closes with the focus back on the button. The ⋮ menu closes with Escape or ← back to its row. The band follows the focus as well as the pointer, and a focused row also shows an accent edge.
  • Filter opens a non-modal dialog whose remove buttons name their rule; Sort is a menu of radio items. The info button’s tooltip is tied to it with aria-describedby and shows on hover and focus; Escape hides it.
  • Create new is a group: a labelled name field and a radio group of types moved with the arrow keys. The rename field is labelled with the view it renames.
  • The table has column headers and each name is its row’s header; checkboxes are role="checkbox" buttons, the header one mixed while some are checked. The board is a labelled group of lanes, and on a phone it scrolls sideways and takes the focus. Each chart bar says its bucket and count in words.
  • A polite live region says which view opened and how many people it shows, renames, duplicates, deletes and their undo, and rule and sort changes. Under reduced motion nothing scales, glides or rolls and the demo never plays; the demo pointer is decorative.

The complete source behind this demo. Open source under the MIT license, © 2026 Bruno Delorence: copy it, change it, ship it.

view-switcher.tsx
// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import {  useEffect,  useEffectEvent,  useId,  useLayoutEffect,  useMemo,  useRef,  useState,  type KeyboardEvent as ReactKeyboardEvent,} from 'react';import {  PEOPLE,  RULES,  RULE_IDS,  SORT_LABEL,  TYPE_LABEL,  VIEWS,  peopleFor,  type Person,  type RuleId,
views.tsx
// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import type { CSSProperties } from 'react';import { BUCKETS, COLUMN_LABEL, ago, bucketOf, when, type Person, type View } from './data';import { Avatar, Sparkle } from './glyphs';import styles from './view-switcher.module.css';/* * What a view draws under the toolbar: the people as a table, as a board of cards grouped by how * long since you were in touch, or as bars of the same buckets. Each mounts afresh when the view * changes, so its rows come in one after the other (--i staggers them). */const nth = (i: number) => ({ '--i': i }) as CSSProperties;export function TableView({  view,  people,  selected,  onToggle,  onToggleAll,}: {
data.ts
// Copyright (c) 2026 Bruno Delorence. MIT License./* * The view switcher's data: the people of a records page, the rules a view filters them by, the * views saved by default, and how dates are written. Dates are counted from a fixed "now", in UTC, * so the server and the browser write the same words. */export type ViewType = 'table' | 'board' | 'chart';export type Strength = 'strong' | 'good' | 'weak';export type Tint = 'amber' | 'green' | 'pink' | 'blue' | 'violet' | 'teal' | 'orange' | 'red';export type Person = {  id: string;  name: string;  tint: Tint;  /** When the contact was added, `YYYY-MM-DDTHH:MM` (UTC). */  added: string;  /** When you were last in touch, or null if never. */  contact: string | null;  strength: Strength;  owner: 'me' | 'team';  email: boolean;  bounced: boolean;
view-switcher.module.css
/* View switcher (view-switcher.tsx): a records page, People, on a grey stage. The head names the   page; the toolbar holds the views button, Filter and Sort; the body draws the active view as a   table, a board or a chart. The views menu hangs from its button over the body.   Under 560 px of stage, Filter and Sort keep their icons only, the views button takes the rest of   the row, and the table's columns tighten.   Colours are the tokens below, light-dark(light, dark) where the two differ: the component takes   the color-scheme it inherits from the page, or the one its colorScheme prop sets   (data-color-scheme). */.root {  --stage: #ececee;  --surface: #ffffff;  --surface-2: #f6f6f7;  --line: #ebebed;  --edge: #e3e3e6;  --ink: #141416;  --ink-2: #66666e;  --ink-3: #6e6e76;  --accent: #1b67d8;  --accent-ink: #ffffff;  --accent-soft: #edf3fe;  --halo: #d6e5fc;
glyphs.tsx
// Copyright (c) 2026 Bruno Delorence. MIT License.import type { ReactNode } from 'react';import type { Tint, ViewType } from './data';import styles from './view-switcher.module.css';/* * The drawings: line icons on a 20-unit grid in the text's colour, the view types' coloured tiles, * and the people's initials. All of them are decorative; the buttons around them carry the words. */const PATHS = {  chevron: <path d="m6 8 4 4 4-4" />,  filter: <path d="M3.5 4.5h13l-5 6.2v4.8l-3 1.5v-6.3z" />,  sort: <path d="M6.5 15.5v-11m0 0L4 7m2.5-2.5L9 7m4.5-2.5v11m0 0L11 13m2.5 2.5L16 13" />,  info: (    <>      <circle cx="10" cy="10" r="7" />      <path d="M10 9v4.5" />      <path d="M10 6.4v.1" strokeWidth="2.2" />    </>  ),  more: (    <>
demo-pointer.tsx
// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import type { CSSProperties, Ref } from 'react';/* * The demo's pointer, and the steps that move it. A demo is a script of awaited steps: wait, or * glide the pointer somewhere. Stopping the demo (the reader takes over, or it scrolls away) ends * the step in progress at once: its promise rejects, which ends the script right there, and the * pointer fades out. * * Each glide is a curved flight played by the Web Animations API, so nothing runs frame by frame. * The points of the flight are handed to whoever wants to move along with it. The arrow and the * hand are HTML boxes * that swap by their visibility: a change of style inside an SVG would lay the SVG out again. */export type Point = { x: number; y: number };/** A glide's path and timing, for an animation that plays along with it. */export type Flight = { points: Point[]; timing: KeyframeAnimationOptions };export type Demo = {
use-autoplay.ts
// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import { useCallback, useEffect, useRef, useState } from 'react';/* * When a demo may play on its own: while part of it is on screen and the tab is visible, never * under reduced motion, and not while the reader is using it. `take()` hands it to the reader; it * takes back over `idle` ms after their last input. */export function useAutoplay<T extends HTMLElement>(idle = 6000) {  const ref = useRef<T>(null);  const [visible, setVisible] = useState(false);  const [reduced, setReduced] = useState(true);  const [taken, setTaken] = useState(false);  const timer = useRef(0);  useEffect(() => {    const element = ref.current;    if (!element) return;    let onScreen = false;    const update = () => setVisible(onScreen && !document.hidden);    const observer = new IntersectionObserver(