Last active
October 18, 2025 06:01
-
-
Save iamrobert/1c001bf1e5d247cc13467d2a2178ff3c to your computer and use it in GitHub Desktop.
Baseline grid Toggle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (() => { | |
| const CLASS_B = 'bgrid'; // full baseline grid | |
| const CLASS_BH = 'bgridh'; // half-height baseline grid (CSS handles the size) | |
| const STORAGE_KEY = 'baselineGrid:last'; // stores 'bgrid' or 'bgridh' | |
| const body = document.body; | |
| const has = (c) => body.classList.contains(c); | |
| const getCurrent = () => (has(CLASS_B) ? CLASS_B : has(CLASS_BH) ? CLASS_BH : ''); | |
| const clearClasses = () => body.classList.remove(CLASS_B, CLASS_BH); | |
| const applyMode = (mode /* 'bgrid'|'bgridh'|'' */) => { | |
| clearClasses(); | |
| if (mode) body.classList.add(mode); | |
| if (mode) { | |
| try { localStorage.setItem(STORAGE_KEY, mode); } catch {} | |
| } | |
| }; | |
| const getLast = () => { | |
| try { | |
| const v = localStorage.getItem(STORAGE_KEY) || ''; | |
| return v === CLASS_B || v === CLASS_BH ? v : ''; | |
| } catch { return ''; } | |
| }; | |
| // --- Read URL param (do not modify URL) --- | |
| const getGridParamMode = () => { | |
| try { | |
| const url = new URL(location.href); | |
| const hasParam = url.searchParams.has('grid'); | |
| if (!hasParam) return ''; // not present → ignore | |
| const val = (url.searchParams.get('grid') ?? '').toLowerCase(); | |
| if (val === '' || val === 'bgrid') return CLASS_B; // ?grid or ?grid= | |
| if (val === 'bgridh') return CLASS_BH; // ?grid=bgridh | |
| return ''; // unknown value → ignore | |
| } catch { return ''; } | |
| }; | |
| // --- Init: respect body; else honor ?grid; else stay empty --- | |
| const initial = getCurrent(); | |
| if (initial) { | |
| applyMode(initial); | |
| } else { | |
| const fromParam = getGridParamMode(); | |
| if (fromParam) applyMode(fromParam); | |
| // else: do nothing (stay empty) | |
| } | |
| // Toggle rule: | |
| // - If same mode key pressed while active -> clear. | |
| // - If other mode active -> clear (don't switch). | |
| // - If empty -> restore last; if none, use the key's default. | |
| const handleToggle = (desired, defaultMode) => { | |
| const cur = getCurrent(); | |
| const last = getLast(); | |
| if (cur === desired) { applyMode(''); return; } | |
| if (cur && cur !== desired) { applyMode(''); return; } | |
| applyMode(last || defaultMode); | |
| }; | |
| // --- Shortcuts (Ctrl or Cmd) --- | |
| document.addEventListener('keydown', (e) => { | |
| const mod = e.ctrlKey || e.metaKey; | |
| if (!mod) return; | |
| if (e.key === ';') { | |
| handleToggle(CLASS_B, CLASS_B); // default: bgrid | |
| e.preventDefault(); | |
| } | |
| if (e.key === "'") { | |
| handleToggle(CLASS_BH, CLASS_BH); // default: bgridh | |
| e.preventDefault(); | |
| } | |
| }); | |
| })(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add a Baseline Grid
Instructions
Toggle Baseline Grid with Keyboard Shortcut:
Ctrl + ;to toggle the baseline grid on and off.Add
bgridClass to Body (optional):bgridclass is used to show the baseline grid..bgridclass to display the grid.Persistent Baseline Grid with URL Parameter (optional):
?gridto the URL.?gridis present, the baseline grid will be displayed.Baseline Grid CSS Styles
CSS Variables
--step-0clamp(1rem, 0.9565rem + 0.2174vi, 1.125rem)--lh1.7Baseline CSS