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.
Need this designed into your product? Book a call
Install
Add it to your project with the CLI.
HTML, CSS and JS
npx designme add zoom-barReact
npx designme add zoom-bar --reactAstro
npx designme add zoom-bar --astroshadcn, 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.
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.
- Zoom out and Zoom in step through 25%, 33%, 50%, 67%, 75%, 90%, 100%, 125%, 150%, 200%, 300% and 400%. At 25% Zoom out greys out and at 400% Zoom in does, each keeping its place.
- The level between them shows the zoom as a percentage in tabular figures, and a press on it sets it back to 100%.
- Undo, Redo and Fit to screen show their hover and press and nothing more in HTML; in React
onUndo,onRedoandonFitcall you. - Tooltips name each button with its shortcut (⌘Z, ⇧⌘Z, −, ⇧0, + and ⇧1). While the focus is in the bar, minus and plus step the level and Shift 0 resets it; bind the others in your app.
- Hover is the quiet row fill in the button's own shape, and a press shows at once as a deeper fill, with no scale.
- In React,
ZoomBartakeszoomordefaultZoom(1 is 100%) and callsonZoomChangewith the new level.
- The bar is a
toolbar, and every button has anaria-label; the level's says the zoom and that a press sets it to 100%. - A greyed step is
aria-disabled, so it stays in the tab order and keeps the focus where it was. - Each new level is read out through a polite live region.
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" />Props
Everything ZoomBar takes.
| Prop | Type | What it does |
|---|---|---|
zoom | number | The zoom level, 1 being 100%, when you keep it in your state |
defaultZoom | number | The level at first, when the bar keeps it. Defaults to 1 |
onZoomChange | (zoom: number) => void | Called with the level Zoom out, Zoom in or a press on the level (100%) sets |
steps | number[] | The levels to step through, smallest first. Defaults to ZOOM_STEPS |
onUndo | () => void | |
onRedo | () => void | |
onFit | () => void | |
canUndo | boolean | False greys Undo out. Defaults to true |
canRedo | boolean | False greys Redo out. Defaults to true |
className | string |
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);
});
});
Questions
What people ask about it.
- How do I connect it to my canvas?
- In React, keep the level in your state with
zoomandonZoomChange, and scale your canvas by it;onFitis where you work out the level that fits and set it. In HTML, read the level from the.zmb-zlbutton's text and listen for clicks on.zmb-undo,.zmb-redoand.zmb-fit. - Can I use other zoom steps?
- In React pass
steps, a list of levels where 1 is 100%. In HTML changeSTEPSin its script. - Why are Undo and Redo never greyed out?
- The bar doesn't know your history. In React pass
canUndo={false}orcanRedo={false}to grey one out when there is nothing to undo or redo.
More in Workspace
Navigation, search, settings, models and automations around the agent.