@opentf/afw-docs 0.38.0 → 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@
6
6
  //
7
7
  // Note: results only exist against a built site (`dist/`). In dev there is no index,
8
8
  // so the modal opens but reports no results — expected.
9
- import { onCleanup, onMount, router, Portal } from "@opentf/afw";
9
+ import { onCleanup, onMount, router } from "@opentf/afw";
10
10
  import { createSearch, excerptParts } from "../search.js";
11
11
 
12
12
  const search = createSearch({ base: "/_search/" });
@@ -20,6 +20,7 @@ export default function Search() {
20
20
  let error = $state("");
21
21
  let controller;
22
22
  const inputRef = $ref();
23
+ const dialogRef = $ref();
23
24
 
24
25
  let timer;
25
26
  let token = 0;
@@ -56,15 +57,17 @@ export default function Search() {
56
57
  timer = setTimeout(() => runSearch(q), 150);
57
58
  });
58
59
 
59
- const focusInput = () => {
60
- const el = inputRef;
61
- if (el && typeof requestAnimationFrame !== "undefined") requestAnimationFrame(() => el.focus());
62
- };
60
+ // A native modal <dialog>: the browser makes the page behind it inert, keeps focus
61
+ // inside, closes it on Escape (and other platform close requests) and returns focus to
62
+ // whatever opened it. `autofocus` on the field puts the cursor there on open.
63
63
  const openModal = () => {
64
+ const dialog = dialogRef;
65
+ if (!dialog || dialog.open) return;
64
66
  open = true;
65
- focusInput();
67
+ dialog.showModal();
66
68
  };
67
- const close = () => {
69
+ // Every way the dialog closes (Escape, a result, the backdrop) ends here, via `close`.
70
+ const reset = () => {
68
71
  ++token;
69
72
  controller?.abort();
70
73
  clearTimeout(timer);
@@ -75,6 +78,10 @@ export default function Search() {
75
78
  results = [];
76
79
  active = 0;
77
80
  };
81
+ const close = () => {
82
+ if (dialogRef?.open) dialogRef.close();
83
+ else reset();
84
+ };
78
85
  const go = (url) => {
79
86
  close();
80
87
  router.push(url);
@@ -91,8 +98,7 @@ export default function Search() {
91
98
  return;
92
99
  }
93
100
  if (!open) return;
94
- if (k === "Escape") close();
95
- else if (k === "ArrowDown") {
101
+ if (k === "ArrowDown") {
96
102
  e.preventDefault();
97
103
  active = results.length ? (active + 1) % results.length : 0;
98
104
  } else if (k === "ArrowUp") {
@@ -116,18 +122,20 @@ export default function Search() {
116
122
  controller?.abort();
117
123
  });
118
124
 
119
- // Portal to <body>: the navbar (our render parent) sets `backdrop-filter`, which
120
- // establishes a containing block for fixed-position descendants — so without this the
121
- // `position: fixed` overlay would collapse to the navbar's box instead of the viewport.
125
+ // A modal <dialog> renders in the top layer, above the navbar's `backdrop-filter`
126
+ // containing block, so it needs no portal. A click outside the panel lands on the
127
+ // dialog itself (it fills the viewport) and closes it.
122
128
  return (
123
- <Portal>
124
- <div
125
- class={open ? "afwd-search-modal is-open" : "afwd-search-modal"}
129
+ <dialog
130
+ ref={dialogRef}
131
+ class="afwd-search-modal"
132
+ aria-label="Search docs"
133
+ onclose={reset}
126
134
  onclick={(e) => {
127
135
  if (e.target === e.currentTarget) close();
128
136
  }}
129
137
  >
130
- <div class="afwd-search-panel" role="dialog" aria-label="Search docs">
138
+ <div class="afwd-search-panel">
131
139
  <div class="afwd-search-field">
132
140
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
133
141
  <circle cx="11" cy="11" r="7" />
@@ -139,6 +147,7 @@ export default function Search() {
139
147
  type="search"
140
148
  placeholder="Search docs…"
141
149
  aria-label="Search docs"
150
+ autofocus
142
151
  value={query}
143
152
  oninput={(e) => (query = e.target.value)}
144
153
  />
@@ -185,7 +194,6 @@ export default function Search() {
185
194
  </span>
186
195
  </div>
187
196
  </div>
188
- </div>
189
- </Portal>
197
+ </dialog>
190
198
  );
191
199
  }
@@ -13,8 +13,11 @@
13
13
  // mounted, so the burger shows only on pages that actually have one.
14
14
  // • a `afw:sidebar` CustomEvent (`{ open }`) — lets the burger mirror `aria-expanded`.
15
15
  //
16
- // The drawer closes on navigation, Escape, backdrop click, or a viewport resize up to
17
- // the desktop breakpoint, and locks body scroll while open.
16
+ // The drawer closes on navigation, Escape, backdrop click, its close button, or a viewport
17
+ // resize up to the desktop breakpoint. While it is a drawer it is a dialog (`role`,
18
+ // `aria-label`); while open it is modal: `aria-modal`, the rest of the page `inert`, focus
19
+ // moved to its close button and given back to the opener on close. Scroll stays in the
20
+ // drawer through `overscroll-behavior`, not by locking the page's overflow.
18
21
  //
19
22
  // Above the tree sits a single icon button that collapses (or re-expands) every group at
20
23
  // once — it drives the nodes through the shared `collapseAll` signal, since they are
@@ -43,7 +46,11 @@ export default function Sidebar(props) {
43
46
  // The collapse-all button is only meaningful when the tree actually has groups.
44
47
  const hasGroups = nav.some((item) => item.items && item.items.length > 0);
45
48
  let open = $state(false);
49
+ // A drawer below the desktop breakpoint; the sticky column above it (not a dialog then).
50
+ let drawer = $state(false);
46
51
  const asideRef = $ref();
52
+ const shellRef = $ref();
53
+ const closeRef = $ref();
47
54
 
48
55
  const close = () => (open = false);
49
56
  // Stable per-instance toggle so cleanup can tell whether the global still points at us.
@@ -66,13 +73,17 @@ export default function Sidebar(props) {
66
73
  // (CSS turns the element back into the sticky column; this releases the scroll lock
67
74
  // and resyncs the burger).
68
75
  const mql = window.matchMedia(DESKTOP_QUERY);
69
- const onDesktop = (e) => e.matches && close();
76
+ drawer = !mql.matches;
77
+ const onDesktop = (e) => {
78
+ drawer = !e.matches;
79
+ if (e.matches) close();
80
+ };
70
81
  mql.addEventListener?.("change", onDesktop);
71
82
 
72
83
  return () => {
73
84
  window.removeEventListener("keydown", onKey);
74
85
  mql.removeEventListener?.("change", onDesktop);
75
- document.body.style.removeProperty("overflow");
86
+ releasePage();
76
87
  root.removeAttribute("data-afw-sidebar-open");
77
88
  // Only drop the flag/globals if we're the last Sidebar and still the owner — an
78
89
  // arriving instance may already have taken over during navigation.
@@ -89,11 +100,46 @@ export default function Sidebar(props) {
89
100
  // Reflect open state: lock body scroll, mark the root (for styling), and notify the
90
101
  // burger so it can mirror `aria-expanded`. Reading `open` (not writing it) keeps this
91
102
  // effect free of the route/resize effects.
103
+ // Modality for the open drawer: everything outside the drawer's shell (its panel and
104
+ // backdrop) goes `inert` — each sibling on the way up to <body>, restored on close.
105
+ let inerted = [];
106
+ let opener = null;
107
+ function holdPage() {
108
+ const shell = shellRef;
109
+ if (!shell || inerted.length) return;
110
+ for (let node = shell; node && node !== document.body; node = node.parentElement) {
111
+ for (const sibling of node.parentElement?.children ?? []) {
112
+ if (sibling === node || sibling.inert || sibling.tagName === "SCRIPT") continue;
113
+ sibling.inert = true;
114
+ inerted.push(sibling);
115
+ }
116
+ }
117
+ }
118
+ function releasePage() {
119
+ for (const el of inerted) el.inert = false;
120
+ inerted = [];
121
+ }
122
+
123
+ // Reflect open state: modality, the root mark (for styling), and the burger's
124
+ // `aria-expanded` (via `afw:sidebar`). Reading `open` (not writing it) keeps this effect
125
+ // free of the route/resize effects.
92
126
  $effect(() => {
93
127
  const isOpen = open;
94
128
  if (typeof document === "undefined") return;
95
129
  document.documentElement.toggleAttribute("data-afw-sidebar-open", isOpen);
96
- document.body.style.overflow = isOpen ? "hidden" : "";
130
+ if (isOpen && drawer) {
131
+ opener = document.activeElement;
132
+ holdPage();
133
+ // The panel is `visibility: hidden` until its slide-in starts, and a hidden element
134
+ // cannot take focus: move focus once it renders.
135
+ const focusClose = () => closeRef?.focus({ preventScroll: true });
136
+ if (typeof requestAnimationFrame === "function") requestAnimationFrame(() => requestAnimationFrame(focusClose));
137
+ else focusClose();
138
+ } else if (inerted.length) {
139
+ releasePage();
140
+ if (opener?.isConnected) opener.focus({ preventScroll: true });
141
+ opener = null;
142
+ }
97
143
  window.dispatchEvent(new CustomEvent("afw:sidebar", { detail: { open: isOpen } }));
98
144
  });
99
145
 
@@ -125,13 +171,26 @@ export default function Sidebar(props) {
125
171
  });
126
172
 
127
173
  return (
128
- <div class="afwd-sidebar-shell">
174
+ <div class="afwd-sidebar-shell" ref={shellRef}>
129
175
  <div
130
176
  class={open ? "afwd-sidebar-backdrop is-open" : "afwd-sidebar-backdrop"}
131
177
  onclick={close}
132
178
  aria-hidden="true"
133
179
  ></div>
134
- <aside id="afw-sidebar" ref={asideRef} class={open ? "afwd-sidebar is-open" : "afwd-sidebar"}>
180
+ <aside
181
+ id="afw-sidebar"
182
+ ref={asideRef}
183
+ class={open ? "afwd-sidebar is-open" : "afwd-sidebar"}
184
+ role={drawer ? "dialog" : undefined}
185
+ aria-label={drawer ? "Navigation" : undefined}
186
+ aria-modal={drawer && open ? "true" : undefined}
187
+ >
188
+ <button type="button" class="afwd-sidebar-close" ref={closeRef} onclick={close} aria-label="Close navigation">
189
+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true">
190
+ <path d="M18 6 6 18" />
191
+ <path d="m6 6 12 12" />
192
+ </svg>
193
+ </button>
135
194
  {navLinks.length ? (
136
195
  <nav class="afwd-drawer-links" aria-label="Site">
137
196
  {navLinks.map((link) => (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentf/afw-docs",
3
- "version": "0.38.0",
3
+ "version": "0.39.0",
4
4
  "description": "Documentation and blog sites built as ordinary AFW apps.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -69,19 +69,19 @@
69
69
  "bugs": "https://github.com/Open-Tech-Foundation/afw/issues",
70
70
  "license": "MIT",
71
71
  "peerDependencies": {
72
- "@opentf/afw": "0.39.0"
72
+ "@opentf/afw": "0.40.0"
73
73
  },
74
74
  "publishConfig": {
75
75
  "access": "public"
76
76
  },
77
77
  "dependencies": {
78
- "@opentf/afw-compiler": "0.26.0",
78
+ "@opentf/afw-compiler": "0.27.0",
79
79
  "@opentf/syntax-highlighter": "0.5.0"
80
80
  },
81
81
  "devDependencies": {
82
- "@opentf/afw": "0.39.0",
83
- "@opentf/afw-test": "1.35.0",
84
- "@opentf/vite-plugin-afw": "0.8.0",
82
+ "@opentf/afw": "0.40.0",
83
+ "@opentf/afw-test": "1.36.0",
84
+ "@opentf/vite-plugin-afw": "0.9.0",
85
85
  "vite": "8.3.5"
86
86
  },
87
87
  "types": "./types/index.d.ts"
package/search.js CHANGED
@@ -83,7 +83,7 @@ function excerptFor(fragment, groups, match) {
83
83
  clean += text.slice(source, end);
84
84
  source = end;
85
85
  };
86
- for (const [from, to] of [...excerptOmit].sort((a, b) => a[0] - b[0])) {
86
+ for (const [from, to] of excerptOmit.toSorted((a, b) => a[0] - b[0])) {
87
87
  if (to <= source || from >= sectionEnd) continue;
88
88
  append(Math.max(source, from));
89
89
  source = Math.min(to, sectionEnd);
@@ -111,7 +111,7 @@ function excerptFor(fragment, groups, match) {
111
111
  export function excerptParts(excerpt, highlights = []) {
112
112
  const parts = [];
113
113
  let position = 0;
114
- for (const [start, end] of [...highlights].sort((a, b) => a[0] - b[0])) {
114
+ for (const [start, end] of highlights.toSorted((a, b) => a[0] - b[0])) {
115
115
  if (start < position || end <= start || end > excerpt.length) continue;
116
116
  if (start > position) parts.push({ text: excerpt.slice(position, start), match: false });
117
117
  parts.push({ text: excerpt.slice(start, end), match: true });
package/theme/index.css CHANGED
@@ -609,19 +609,31 @@
609
609
  .afw-search {
610
610
  display: contents;
611
611
  }
612
+ /* A modal <dialog> that fills the viewport; the panel sits inside it, and a click on
613
+ the dialog around the panel closes it. */
612
614
  .afwd-search-modal {
613
- position: fixed;
614
- inset: 0;
615
- z-index: 100;
616
- display: none;
615
+ width: 100%;
616
+ max-width: none;
617
+ height: 100%;
618
+ max-height: none;
619
+ margin: 0;
620
+ border: none;
621
+ padding: 12vh 1rem 1rem;
622
+ background: transparent;
623
+ color: inherit;
624
+ /* Scroll gestures inside the dialog, or over its backdrop, stay out of the page. */
625
+ overscroll-behavior: contain;
626
+ }
627
+ .afwd-search-modal[open] {
628
+ display: flex;
617
629
  align-items: flex-start;
618
630
  justify-content: center;
619
- padding: 12vh 1rem 1rem;
631
+ }
632
+ .afwd-search-modal::backdrop {
620
633
  background: color-mix(in srgb, #000 45%, transparent);
621
634
  backdrop-filter: blur(2px);
622
- }
623
- .afwd-search-modal.is-open {
624
- display: flex;
635
+ overflow: hidden;
636
+ overscroll-behavior: contain;
625
637
  }
626
638
  .afwd-search-panel {
627
639
  width: 100%;
@@ -800,6 +812,24 @@
800
812
  opacity: 0;
801
813
  pointer-events: none;
802
814
  transition: opacity 0.25s ease;
815
+ /* A non-scrolling scroll container, so gestures over it do not scroll the page. */
816
+ overflow: hidden;
817
+ overscroll-behavior: contain;
818
+ }
819
+ .afwd-sidebar-close {
820
+ display: flex;
821
+ margin: 0 0 0.75rem auto;
822
+ padding: 0.35rem;
823
+ border: none;
824
+ border-radius: 0.4rem;
825
+ background: none;
826
+ color: var(--afw-text-muted);
827
+ cursor: pointer;
828
+ }
829
+ .afwd-sidebar-close:hover,
830
+ .afwd-sidebar-close:focus-visible {
831
+ color: var(--afw-text);
832
+ background: var(--afw-bg-elevated);
803
833
  }
804
834
  .afwd-sidebar-backdrop.is-open {
805
835
  opacity: 1;
@@ -822,7 +852,8 @@
822
852
  transform: none;
823
853
  visibility: visible;
824
854
  }
825
- .afwd-sidebar-backdrop {
855
+ .afwd-sidebar-backdrop,
856
+ .afwd-sidebar-close {
826
857
  display: none;
827
858
  }
828
859
  }