| 1 | // chrome/chrome.js: optional behaviour for estilo-bar and the one top-level |
| 2 | // menu group. Load it with <script defer>. Everything already works |
| 3 | // without it; this only adds what <details> and the platform cannot do for |
| 4 | // themselves: |
| 5 | // |
| 6 | // - Escape closes the open member of the group (name="estilo-menus": the |
| 7 | // bar's Menu, switcher and account, and every menubar menu) and returns |
| 8 | // focus to its title; |
| 9 | // - a click outside the open member closes it; |
| 10 | // - Escape in the editable title restores the saved value and leaves |
| 11 | // the field; |
| 12 | // - a row or button that opens a dialog (command="show-modal") closes |
| 13 | // the menu it sat in, returns focus to that menu's title when the |
| 14 | // dialog closes, and opens the dialog itself on an engine without |
| 15 | // invoker commands; |
| 16 | // - a stacked bar's title field (the one with a size attribute) is |
| 17 | // fitted to its title: the size the server rendered is the first |
| 18 | // paint's guess, and this measures the field and corrects it; |
| 19 | // - Appearance switches in place: the page takes the scheme at once and |
| 20 | // the choice is saved by the same POST, sent in the background, with |
| 21 | // no navigation and no spinner. With scripts off the form posts and |
| 22 | // redirects as it always has. |
| 23 | // |
| 24 | // The group is one group in every app, so closing it is estilo's job, not |
| 25 | // each app's: Sheets' menubar lost Escape and outside-click entirely when |
| 26 | // it left rastrillo's dropdown. |
| 27 | // |
| 28 | // Both listen in the capture phase. A document app has to stop key |
| 29 | // events from propagating out of its menus — Sheets' grid library acts on |
| 30 | // every key it hears on document, so an arrow in an open menu would walk |
| 31 | // the cell cursor too — and a bubble-phase listener on document never |
| 32 | // hears the Escape such an app stops. Capture hears it first. An app that |
| 33 | // closes its own menu on Escape (Docs' menubar) finds nothing open and |
| 34 | // does nothing, so nothing is handled twice. |
| 35 | ;(() => { |
| 36 | const group = 'details[name="estilo-menus"][open]' |
| 37 | const openPanel = () => document.querySelector(group) |
| 38 | |
| 39 | // Focus returns to a menu's title; in a menubar with a roving tab stop |
| 40 | // (one title tabbable, the rest tabindex=-1) the stop moves with it, so |
| 41 | // Tab then Shift+Tab comes back to the menu you were in, not to File |
| 42 | // (Astra, estilo #11). A menubar without one is left without one. |
| 43 | const returnTo = (title) => { |
| 44 | title.focus() |
| 45 | const bar = title.closest('[estilo-menubar]') |
| 46 | if (!bar) return |
| 47 | const titles = bar.querySelectorAll('[estilo-menubar-title]') |
| 48 | if (!Array.prototype.some.call(titles, (t) => t.hasAttribute('tabindex'))) return |
| 49 | Array.prototype.forEach.call(titles, (t) => { t.tabIndex = t === title ? 0 : -1 }) |
| 50 | } |
| 51 | |
| 52 | document.addEventListener('keydown', (e) => { |
| 53 | if (e.key !== 'Escape' || e.defaultPrevented) return |
| 54 | const input = e.target.closest && e.target.closest('[estilo-bar-title-input]') |
| 55 | if (input) { |
| 56 | input.value = input.defaultValue |
| 57 | if (input.hasAttribute('size')) fit(input) |
| 58 | input.blur() |
| 59 | e.preventDefault() |
| 60 | return |
| 61 | } |
| 62 | const open = openPanel() |
| 63 | if (!open) return |
| 64 | open.open = false |
| 65 | const summary = open.querySelector('summary') |
| 66 | if (summary) returnTo(summary) |
| 67 | e.preventDefault() |
| 68 | }, true) |
| 69 | |
| 70 | document.addEventListener('click', (e) => { |
| 71 | const open = openPanel() |
| 72 | if (open && !open.contains(e.target)) open.open = false |
| 73 | }, true) |
| 74 | |
| 75 | const invokers = typeof HTMLButtonElement !== 'undefined' && 'command' in HTMLButtonElement.prototype |
| 76 | document.addEventListener('click', (e) => { |
| 77 | const button = e.target.closest && e.target.closest('button[commandfor][command="show-modal"]') |
| 78 | if (!button || button.disabled) return |
| 79 | const dialog = document.getElementById(button.getAttribute('commandfor')) |
| 80 | const holder = button.closest('details[name="estilo-menus"]') |
| 81 | if (holder) { |
| 82 | holder.open = false |
| 83 | const summary = holder.querySelector('summary') |
| 84 | if (dialog && summary) dialog.addEventListener('close', () => returnTo(summary), { once: true }) |
| 85 | } |
| 86 | if (invokers || !dialog || typeof dialog.showModal !== 'function' || dialog.open) return |
| 87 | dialog.showModal() |
| 88 | e.preventDefault() |
| 89 | }) |
| 90 | |
| 91 | // The stacked bar's title field is as wide as its title (Paul: "It |
| 92 | // should grow according to the title, not start out full-width"). The |
| 93 | // field measures itself, at zero width, where its scroll width is its |
| 94 | // own text as it draws it. The engines measure that in different boxes: |
| 95 | // WebKit's scroll and client widths are the padding box, so its scroll |
| 96 | // width holds both paddings; Chromium's are the content box (its inner |
| 97 | // editor), so its scroll width is the text alone and its client width |
| 98 | // 0. Measured on Chromium 158 and WebKit 27.2, "Brightwater Labs": |
| 99 | // scroll width 127 and 141, client width 0 and 13. Adding only the |
| 100 | // borders, as this once did, cut the last letter off in Chromium (the |
| 101 | // field 12.8px short: both paddings). So whatever of the field lies |
| 102 | // outside the box the scroll width is measured in goes on top, |
| 103 | // offsetWidth less clientWidth: the borders in WebKit, the borders and |
| 104 | // paddings in Chromium. Then 2px for the caret at the end of the text. |
| 105 | // field-sizing: content, a canvas and a mirror span in the same font |
| 106 | // all came out short in WebKit; this did not. CSS caps the width at the |
| 107 | // room the field has (max-inline-size, which from 800px allows for the |
| 108 | // field's pulled-back start margin), so a longer title still scrolls |
| 109 | // inside the field. Nothing after the title on its row moves when it |
| 110 | // changes: the stack takes the room. make test-browser holds it in |
| 111 | // both engines (title_browser_test.go). |
| 112 | const FIT = '[estilo-bar-title-input][size]' |
| 113 | const CARET = 2 |
| 114 | const fit = (input) => { |
| 115 | input.style.inlineSize = '0px' |
| 116 | const w = input.scrollWidth + input.offsetWidth - input.clientWidth |
| 117 | input.style.inlineSize = Math.ceil(w + CARET) + 'px' |
| 118 | } |
| 119 | const fitAll = () => { Array.prototype.forEach.call(document.querySelectorAll(FIT), fit) } |
| 120 | document.addEventListener('input', (e) => { |
| 121 | const input = e.target.closest && e.target.closest(FIT) |
| 122 | if (input) fit(input) |
| 123 | }) |
| 124 | // Again on resize, once everything has loaded, and once fonts settle: |
| 125 | // the first measure, at script time, can come out a few pixels short. |
| 126 | window.addEventListener('resize', fitAll) |
| 127 | window.addEventListener('load', fitAll) |
| 128 | if (document.fonts && document.fonts.ready) document.fonts.ready.then(fitAll) |
| 129 | fitAll() |
| 130 | |
| 131 | // Below 800px a stacked bar is the one-row bar with the app's menus as a |
| 132 | // second row, and that row wraps where the menus do not fit (English |
| 133 | // at 320px). CSS publishes the one-row-of-menus height; this publishes |
| 134 | // the header's real one, so whatever sticks under it (Docs' strip) |
| 135 | // sticks under all of it. Only below 800px: from there the header's |
| 136 | // height is the variable itself, and publishing its measure back would |
| 137 | // hold it at whatever it measured last. |
| 138 | const stacked = document.querySelector('[estilo-bar~="stacked"]') |
| 139 | const narrow = window.matchMedia && window.matchMedia('(max-width: 799.98px)') |
| 140 | if (stacked && narrow && typeof ResizeObserver === 'function') { |
| 141 | const root = document.documentElement |
| 142 | const publish = () => { |
| 143 | const h = stacked.getBoundingClientRect().height |
| 144 | if (narrow.matches && h > 0) root.style.setProperty('--estilo-bar-block-size', h + 'px') |
| 145 | else root.style.removeProperty('--estilo-bar-block-size') |
| 146 | } |
| 147 | new ResizeObserver(publish).observe(stacked) |
| 148 | if (narrow.addEventListener) narrow.addEventListener('change', publish) |
| 149 | } |
| 150 | |
| 151 | // Appearance switches at once (Paul: "They're not doing an action ... |
| 152 | // they should just switch immediately"). The page takes the scheme as |
| 153 | // chrome.ThemeAttr writes it, data-theme="light" or "dark" on <html> and |
| 154 | // no attribute for System; whatever was mid-transition lands in it at |
| 155 | // once; the pressed segment moves; and the same form data is POSTed to |
| 156 | // the form's action in the background to save it, without following |
| 157 | // the server's redirect back to the page. The form is |
| 158 | // data-busy="false", so the busy rule holds nothing. If the POST fails |
| 159 | // the page stays switched for this visit and says nothing; the next load |
| 160 | // shows the saved choice (spec, P3). Each switch aborts the save before |
| 161 | // it, so two quick clicks cannot reach the server in the wrong order |
| 162 | // and leave the first choice saved. Capture phase, like the rest: an |
| 163 | // app's own submit handler cannot swallow it. |
| 164 | // |
| 165 | // land() is rastrillo's shim's, for its own scheme menu: a change of |
| 166 | // scheme would otherwise retarget running colour transitions while the |
| 167 | // text changes at once. getAnimations() flushes style, which starts the |
| 168 | // transitions the change causes, and cancelling one lands it on its end |
| 169 | // value. Transitions only, told by transitionProperty: cancelling an |
| 170 | // animation would stop a spinner for good. |
| 171 | const land = () => { |
| 172 | if (!document.getAnimations) return |
| 173 | document.getAnimations().forEach((a) => { if (typeof a.transitionProperty === 'string') a.cancel() }) |
| 174 | } |
| 175 | let saving = null |
| 176 | document.addEventListener('submit', (e) => { |
| 177 | const form = e.target |
| 178 | const button = e.submitter |
| 179 | if (e.defaultPrevented || !form.hasAttribute || !form.hasAttribute('estilo-appearance')) return |
| 180 | if (!button || button.getAttribute('name') !== 'mode') return |
| 181 | const mode = button.value |
| 182 | if (mode !== 'light' && mode !== 'dark' && mode !== 'system') return |
| 183 | e.preventDefault() |
| 184 | const root = document.documentElement |
| 185 | if (mode === 'system') root.removeAttribute('data-theme') |
| 186 | else root.setAttribute('data-theme', mode) |
| 187 | land() |
| 188 | Array.prototype.forEach.call(form.querySelectorAll('button[name="mode"]'), (b) => { |
| 189 | b.setAttribute('aria-pressed', b === button ? 'true' : 'false') |
| 190 | }) |
| 191 | const body = new URLSearchParams(new FormData(form)) |
| 192 | body.set('mode', mode) |
| 193 | if (saving) saving.abort() |
| 194 | saving = typeof AbortController === 'function' ? new AbortController() : null |
| 195 | fetch(form.getAttribute('action'), { method: 'POST', body, credentials: 'same-origin', redirect: 'manual', signal: saving ? saving.signal : undefined }).catch(() => {}) |
| 196 | }, true) |
| 197 | })() |
| 198 | |