Skip to content
Star

Zoom bar

Undo, redo, zoom and fit in one floating bar for a canvas. Free and MIT licensed, as HTML, a typed React component or an Astro component, in three styles with light and dark mode.

$npx designme add zoom-bar
$npx shadcn@latest add https://designme.dev/r/shadcn/zoom-bar.json

Need this designed into your product? Book a call

01

Install

Add it to your project with the CLI.

HTML, CSS and JS

$npx designme add zoom-bar

React

$npx designme add zoom-bar --react

Astro

$npx designme add zoom-bar --astro

shadcn, React

$npx shadcn@latest add https://designme.dev/r/shadcn/zoom-bar.json

shadcn, Astro

$npx shadcn@latest add https://designme.dev/r/shadcn/astro/zoom-bar.json

The CLI copies the source into your project, so you own it and can change anything. Where each version lands, what it needs and the shadcn theme bridge: Installation.

02

About

What it is for and what it does.

Zoom bar is the floating view bar from the Whiteboard template: Undo and Redo, then Zoom out, the zoom level and Zoom in, then Fit to screen, in one raised pill. Minus and plus step the level it shows, a press on the level sets it back to 100%, and Undo, Redo and Fit press and call you, so your canvas does the work.

  • When a board or canvas needs its undo history and its zoom in one place, out of the way in a corner.
  • When people should always see how far in they are zoomed, and get back to 100% in one press.
  • Next to Tool rail, which holds the tools while this holds the view.
  • Not when the canvas already has a full toolbar with zoom in it: Canvas toolbar has its own.
03

Usage

Import it in React and drive it from your own state, or place it in an Astro page.

React

import { ZoomBar } from '@/components/designme/zoom-bar/ZoomBar';

React example

The demo above as a React component, 19 lines you can copy.

'use client';
// The animated preview from designme.dev, rebuilt from the React component: an example of driving it from your own state.
import { useRef, useState } from 'react';
import { ZoomBar } from './ZoomBar';
import { Demo } from '../lib/Demo';
import { useStory } from '../lib/useScript';

/* ---------- Zoom bar: zoom in twice, out once, undo, fit, and back to 100% ---------- */
export default function ZoomBarDemo() {
  const ref = useRef<HTMLDivElement>(null);
  const [zoom, setZoom] = useState(1);
  useStory(ref, async ({ wait, press }) => {
    await wait(1000);
    for (const b of ['Zoom in', 'Zoom in', 'Zoom out', 'Undo', 'Fit to screen']) { await wait(1000); press(`button[aria-label="${b}"]`); }
    await wait(1000); press('.dm-zmb-z');
  });
  return <Demo ref={ref} switcher={null}><ZoomBar zoom={zoom} onZoomChange={setZoom} /></Demo>;
}

Astro

In a page in src/pages. It plays the demo above; change its zoom-bar.js to show your own data. Attributes like class go on its root element.

---
import ZoomBar from '../components/designme/zoom-bar/ZoomBar.astro';
---
<ZoomBar />
<ZoomBar data-style="soft" class="my-class" />
04

Props

Everything ZoomBar takes.

PropTypeWhat it does
zoomnumberThe zoom level, 1 being 100%, when you keep it in your state
defaultZoomnumberThe level at first, when the bar keeps it. Defaults to 1
onZoomChange(zoom: number) => voidCalled with the level Zoom out, Zoom in or a press on the level (100%) sets
stepsnumber[]The levels to step through, smallest first. Defaults to ZOOM_STEPS
onUndo() => void
onRedo() => void
onFit() => void
canUndobooleanFalse greys Undo out. Defaults to true
canRedobooleanFalse greys Redo out. Defaults to true
classNamestring
05

Code

The full source, as the CLI installs it.

ZoomBar.tsxReact · 65 lines
'use client';
import { useState, type KeyboardEvent as ReactKeyboardEvent } from 'react';
import { cx } from '../lib/cx';
import { useControllable } from '../lib/hooks';
import { Icon } from '../lib/icons';
import { Tip } from '../lib/motion';
import '../designme.css';
import './zoom-bar.css';

/** The levels Zoom out and Zoom in step through, as the Whiteboard's. 1 is 100% */
export const ZOOM_STEPS = [0.25, 0.33, 0.5, 0.67, 0.75, 0.9, 1, 1.25, 1.5, 2, 3, 4];

export interface ZoomBarProps {
  /** The zoom level, 1 being 100%, when you keep it in your state */
  zoom?: number;
  /** The level at first, when the bar keeps it. Defaults to 1 */
  defaultZoom?: number;
  /** Called with the level Zoom out, Zoom in or a press on the level (100%) sets */
  onZoomChange?: (zoom: number) => void;
  /** The levels to step through, smallest first. Defaults to ZOOM_STEPS */
  steps?: number[];
  onUndo?: () => void;
  onRedo?: () => void;
  onFit?: () => void;
  /** False greys Undo out. Defaults to true */
  canUndo?: boolean;
  /** False greys Redo out. Defaults to true */
  canRedo?: boolean;
  className?: string;
}

/** Undo, redo, zoom and fit in one floating bar for a canvas, from the Whiteboard template. Minus and plus step the level,
    a press on the level sets it to 100%; Undo, Redo and Fit call you */
export function ZoomBar({ zoom: zoomProp, defaultZoom = 1, onZoomChange, steps = ZOOM_STEPS, onUndo, onRedo, onFit, canUndo = true, canRedo = true, className }: ZoomBarProps) {
  const [zoom, setZoom] = useControllable(zoomProp, defaultZoom, onZoomChange);
  const [said, setSaid] = useState('');
  const pct = Math.round(zoom * 100) + '%';
  const lo = zoom <= steps[0] + 1e-3, hi = zoom >= steps[steps.length - 1] - 1e-3;
  const set = (z: number) => { setZoom(z); setSaid('Zoom ' + Math.round(z * 100) + '%'); };
  const step = (d: 1 | -1) => { const n = d > 0 ? steps.find(x => x > zoom + 1e-3) : [...steps].reverse().find(x => x < zoom - 1e-3); if (n !== undefined) set(n); };
  // minus, plus and Shift 0 step and reset the level while the focus is in the bar
  const onKey = (e: ReactKeyboardEvent) => {
    if (e.metaKey || e.ctrlKey || e.altKey) return;
    if (e.key === '-' || e.key === '−') { e.preventDefault(); step(-1); }
    else if (e.key === '+' || e.key === '=') { e.preventDefault(); step(1); }
    else if (e.shiftKey && (e.key === ')' || e.code === 'Digit0')) { e.preventDefault(); set(1); }
  };
  const off = (on: boolean, fn?: () => void) => () => { if (on) fn?.(); };
  return (
    <div className={cx('dm-zmb', className)} data-dm="">
      <div className="dm-zmb-bar" role="toolbar" aria-label="View" onKeyDown={onKey}>
        <Tip label="Undo" kbd="⌘Z"><button type="button" className="dm-zmb-b dm-mx-press" aria-label="Undo" aria-disabled={!canUndo} onClick={off(canUndo, onUndo)}><Icon name="undo" /></button></Tip>
        <Tip label="Redo" kbd="⇧⌘Z"><button type="button" className="dm-zmb-b dm-mx-press" aria-label="Redo" aria-disabled={!canRedo} onClick={off(canRedo, onRedo)}><Icon name="redo" /></button></Tip>
        <span className="dm-zmb-sep" aria-hidden="true" />
        <Tip label="Zoom out" kbd="−"><button type="button" className="dm-zmb-b dm-mx-press" aria-label="Zoom out" aria-disabled={lo} onClick={() => step(-1)}><Icon name="minus" /></button></Tip>
        <Tip label="Zoom to 100%" kbd="⇧0"><button type="button" className="dm-zmb-z dm-mx-press dm-num" aria-label={`Zoom ${pct}, zoom to 100%`} onClick={() => set(1)}>{pct}</button></Tip>
        <Tip label="Zoom in" kbd="+"><button type="button" className="dm-zmb-b dm-mx-press" aria-label="Zoom in" aria-disabled={hi} onClick={() => step(1)}><Icon name="plus" /></button></Tip>
        <span className="dm-zmb-sep" aria-hidden="true" />
        <Tip label="Fit to screen" kbd="⇧1"><button type="button" className="dm-zmb-b dm-mx-press" aria-label="Fit to screen" onClick={onFit}><Icon name="fit" /></button></Tip>
      </div>
      <p className="dm-zmb-live" aria-live="polite">{said}</p>
    </div>
  );
}
zoom-bar.cssReact · 16 lines
/* Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync. */
/* ---------- Zoom bar ---------- */
/* the Whiteboard template's view bar (owner, 27 Nov): undo and redo, zoom out, the level and zoom in, and fit, as Canvas
   toolbar's bar. Minus and plus step the level shown between them, the level resets it to 100%; undo, redo and fit only
   press. At 25% and 400% the step that has nowhere to go greys out and keeps its place */
.dm-zmb{position:relative;display:flex;justify-content:center}
.dm-zmb-live{position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
.dm-zmb-bar{display:inline-flex;align-items:center;padding:var(--dm-s-1);border-radius:var(--dm-r-pill);background:var(--dm-raised);box-shadow:var(--dm-shadow-pop);white-space:nowrap}
.dm-zmb-bar :focus-visible{border-radius:var(--dm-r-pill)}
.dm-zmb-b{width:32px;height:32px;flex:none;display:grid;place-items:center;border-radius:var(--dm-r-pill);color:var(--dm-fg2);transition:background var(--dm-t-fast),color var(--dm-t-fast)}
.dm-zmb-z{min-width:48px;height:32px;padding:0 var(--dm-s-1);flex:none;border-radius:var(--dm-r-pill);font-size:12.5px;color:var(--dm-fg2);transition:background var(--dm-t-fast),color var(--dm-t-fast)}
@media (hover:hover) and (pointer:fine){.dm-zmb-b:hover,.dm-zmb-z:hover{background:var(--dm-row);color:var(--dm-fg)}}
.dm-zmb-b[aria-disabled="true"],.dm-zmb-b[aria-disabled="true"]:active{color:var(--dm-dis);background:none;cursor:not-allowed}
@media (hover:hover) and (pointer:fine){.dm-zmb-b[aria-disabled="true"]:hover{color:var(--dm-dis);background:none;cursor:not-allowed}}
.dm-zmb-sep{width:1px;height:16px;margin:0 var(--dm-s-1);flex:none;background:var(--dm-line)}
ZoomBar.astroAstro · 36 lines
---
// Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync.
// Zoom bar: Undo, redo, zoom and fit in one floating bar for a canvas.
// The markup, CSS and script are designme.dev's own, run by the shared core in ../core, which mounts the component when it scrolls
// into view and plays its demo. To show your own data, change zoom-bar.js. The markup is kept exactly as it is (is:raw).
import type { HTMLAttributes } from 'astro/types';
import '../core/designme.css';
import './zoom-bar.css';

export interface Props extends HTMLAttributes<'div'> {
  /** "soft" or "studio" draws it in that style. Leave it out for Blueprint, or for the style set on <html> (data-style="soft" or "studio") */
  'data-style'?: 'soft' | 'studio';
}
const { class: className, ...rest } = Astro.props;
---
<div class:list={['card', className]} data-c="zoom-bar" {...rest}>
  <div class="stage-in"><Fragment is:raw>
<div class="zmb">
  <div class="zmb-bar" role="toolbar" aria-label="View">
    <button class="zmb-b mx-press zmb-undo" aria-label="Undo" data-tip="Undo" data-tip-k="⌘Z"><svg class="i s"><use href="#i-undo"/></svg></button>
    <button class="zmb-b mx-press zmb-redo" aria-label="Redo" data-tip="Redo" data-tip-k="⇧⌘Z"><svg class="i s"><use href="#i-redo"/></svg></button>
    <span class="zmb-sep" aria-hidden="true"></span>
    <button class="zmb-b mx-press zmb-zo" aria-label="Zoom out" data-tip="Zoom out" data-tip-k="−"><svg class="i s"><use href="#i-minus"/></svg></button>
    <button class="zmb-z mx-press num zmb-zl" aria-label="Zoom 100%, zoom to 100%" data-tip="Zoom to 100%" data-tip-k="⇧0">100%</button>
    <button class="zmb-b mx-press zmb-zi" aria-label="Zoom in" data-tip="Zoom in" data-tip-k="+"><svg class="i s"><use href="#i-plus"/></svg></button>
    <span class="zmb-sep" aria-hidden="true"></span>
    <button class="zmb-b mx-press zmb-fit" aria-label="Fit to screen" data-tip="Fit to screen" data-tip-k="⇧1"><svg class="i s"><use href="#i-fit"/></svg></button>
  </div>
  <p class="zmb-live" aria-live="polite"></p>
</div>
  </Fragment></div>
</div>
<script>
  import './zoom-bar.js';
</script>
zoom-bar.htmlHTML · 18 lines
<div class="card" data-c="zoom-bar">
  <div class="stage-in">
    <div class="zmb">
      <div class="zmb-bar" role="toolbar" aria-label="View">
        <button class="zmb-b mx-press zmb-undo" aria-label="Undo" data-tip="Undo" data-tip-k="⌘Z"><svg class="i s"><use href="#i-undo"/></svg></button>
        <button class="zmb-b mx-press zmb-redo" aria-label="Redo" data-tip="Redo" data-tip-k="⇧⌘Z"><svg class="i s"><use href="#i-redo"/></svg></button>
        <span class="zmb-sep" aria-hidden="true"></span>
        <button class="zmb-b mx-press zmb-zo" aria-label="Zoom out" data-tip="Zoom out" data-tip-k="−"><svg class="i s"><use href="#i-minus"/></svg></button>
        <button class="zmb-z mx-press num zmb-zl" aria-label="Zoom 100%, zoom to 100%" data-tip="Zoom to 100%" data-tip-k="⇧0">100%</button>
        <button class="zmb-b mx-press zmb-zi" aria-label="Zoom in" data-tip="Zoom in" data-tip-k="+"><svg class="i s"><use href="#i-plus"/></svg></button>
        <span class="zmb-sep" aria-hidden="true"></span>
        <button class="zmb-b mx-press zmb-fit" aria-label="Fit to screen" data-tip="Fit to screen" data-tip-k="⇧1"><svg class="i s"><use href="#i-fit"/></svg></button>
      </div>
      <p class="zmb-live" aria-live="polite"></p>
    </div>
  </div>
</div>
zoom-bar.cssHTML · 15 lines
/* ---------- Zoom bar ---------- */
/* the Whiteboard template's view bar (owner, 27 Nov): undo and redo, zoom out, the level and zoom in, and fit, as Canvas
   toolbar's bar. Minus and plus step the level shown between them, the level resets it to 100%; undo, redo and fit only
   press. At 25% and 400% the step that has nowhere to go greys out and keeps its place */
.zmb{position:relative;display:flex;justify-content:center}
.zmb-live{position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
.zmb-bar{display:inline-flex;align-items:center;padding:var(--dm-s-1);border-radius:var(--dm-r-pill);background:var(--dm-raised);box-shadow:var(--dm-shadow-pop);white-space:nowrap}
.zmb-bar :focus-visible{border-radius:var(--dm-r-pill)}
.zmb-b{width:32px;height:32px;flex:none;display:grid;place-items:center;border-radius:var(--dm-r-pill);color:var(--dm-fg2);transition:background var(--dm-t-fast),color var(--dm-t-fast)}
.zmb-z{min-width:48px;height:32px;padding:0 var(--dm-s-1);flex:none;border-radius:var(--dm-r-pill);font-size:12.5px;color:var(--dm-fg2);transition:background var(--dm-t-fast),color var(--dm-t-fast)}
@media (hover:hover) and (pointer:fine){.zmb-b:hover,.zmb-z:hover{background:var(--dm-row);color:var(--dm-fg)}}
.zmb-b[aria-disabled="true"],.zmb-b[aria-disabled="true"]:active{color:var(--dm-dis);background:none;cursor:not-allowed}
@media (hover:hover) and (pointer:fine){.zmb-b[aria-disabled="true"]:hover{color:var(--dm-dis);background:none;cursor:not-allowed}}
.zmb-sep{width:1px;height:16px;margin:0 var(--dm-s-1);flex:none;background:var(--dm-line)}
zoom-bar.jsHTML · 30 lines
DM.def('zoom-bar', (r, c) => {
  r.innerHTML = r.innerHTML.replace(/\{([\w-]+)\}/g, (m, k) => I(k));
  const zo = $('.zmb-zo', r), zi = $('.zmb-zi', r), zl = $('.zmb-zl', r), live = $('.zmb-live', r);
  // the levels minus and plus step through, as the Whiteboard's
  const STEPS = [.25, .33, .5, .67, .75, .9, 1, 1.25, 1.5, 2, 3, 4];
  let z = 1;
  const paint = say => { const p = Math.round(z * 100) + '%';
    zl.textContent = p; zl.setAttribute('aria-label', `Zoom ${p}, zoom to 100%`);
    zo.setAttribute('aria-disabled', String(z <= STEPS[0])); zi.setAttribute('aria-disabled', String(z >= STEPS[STEPS.length - 1]));
    if (say) live.textContent = 'Zoom ' + p; };
  const step = d => { const n = d > 0 ? STEPS.find(x => x > z + .001) : [...STEPS].reverse().find(x => x < z - .001); if (n){ z = n; paint(true); } };
  c.on(zo, 'click', () => step(-1)); c.on(zi, 'click', () => step(1)); c.on(zl, 'click', () => { z = 1; paint(true); });
  // minus, plus and Shift 0 step and reset the level while the focus is in the bar
  c.on($('.zmb-bar', r), 'keydown', e => {
    if (e.metaKey || e.ctrlKey || e.altKey) return;
    const a = e.key === '-' || e.key === '−' ? () => step(-1) : e.key === '+' || e.key === '=' ? () => step(1) : e.shiftKey && (e.key === ')' || e.code === 'Digit0') ? () => { z = 1; paint(true); } : null;
    if (a){ e.preventDefault(); a(); }
  });
  paint();
  // the preview's story: zoom in twice, out once, undo, fit, and back to 100%
  c.story(async s => {
    await s.beat(2);
    await s.tap(zi); await s.beat(1.5); await s.tap(zi); await s.beat(2);
    await s.tap(zo); await s.beat(2);
    await s.point($('.zmb-undo', r)); await s.beat(1); await s.tap($('.zmb-undo', r)); await s.beat(2);
    await s.tap($('.zmb-fit', r)); await s.beat(2);
    await s.tap(zl); s.away(); await s.beat(2);
  });
});
06

Questions

What people ask about it.

How do I connect it to my canvas?
In React, keep the level in your state with zoom and onZoomChange, and scale your canvas by it; onFit is where you work out the level that fits and set it. In HTML, read the level from the .zmb-zl button's text and listen for clicks on .zmb-undo, .zmb-redo and .zmb-fit.
Can I use other zoom steps?
In React pass steps, a list of levels where 1 is 100%. In HTML change STEPS in its script.
Why are Undo and Redo never greyed out?
The bar doesn't know your history. In React pass canUndo={false} or canRedo={false} to grey one out when there is nothing to undo or redo.
07

More in Workspace

Navigation, search, settings, models and automations around the agent.

Tool railA board's tools in one vertical rail: pick one, and its name and key show on hover.
Run drawerA workflow's run under its canvas: the steps, the log and the data.
Command paletteJump anywhere or hand a task to the agent from one box.
Team memoryWhat the agent remembers about your team, editable and removable.
Tools and connectionsThe tools the agent can reach, and how much it may do with each.
Knowledge sourcesThe files, sites and folders the agent knows, and how far each is indexed.
Recurring jobsTasks the agent runs on a schedule, with their last result and a switch.
File explorerProject files, with the ones the agent touched marked.