@gjsify/adwaita-web 0.40.0 → 0.41.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.
@@ -77,6 +77,7 @@
77
77
  @use 'password_entry_row';
78
78
  @use 'toast';
79
79
  @use 'toolbar_view';
80
+ @use 'scrolling';
80
81
  @use 'wrap_box';
81
82
  @use 'panes';
82
83
  @use 'overlay_split_view';
@@ -9,7 +9,12 @@
9
9
  // element sets it as an inline style precisely so it stays overridable.
10
10
  import { describe, expect, it } from '@gjsify/unit';
11
11
 
12
- import { CAROUSEL_PAGE_AT_POSITION_VECTORS, CAROUSEL_PAGE_LIST_VECTORS } from '@gjsify/adwaita-core/conformance';
12
+ import type { CarouselPageMeasurement } from '@gjsify/adwaita-core';
13
+ import {
14
+ CAROUSEL_PAGE_ALLOCATION_VECTORS,
15
+ CAROUSEL_PAGE_AT_POSITION_VECTORS,
16
+ CAROUSEL_PAGE_LIST_VECTORS,
17
+ } from '@gjsify/adwaita-core/conformance';
13
18
  import type { CarouselPageOp } from '@gjsify/adwaita-core/conformance';
14
19
 
15
20
  /** The carousel surface this suite drives — the element's own public API. */
@@ -62,6 +67,45 @@ function mount(labels: readonly string[], attributes: Record<string, string> = {
62
67
  return { carousel, host, track };
63
68
  }
64
69
 
70
+ /**
71
+ * One page that MEASURES what a conformance row says it does. `expand` is spelled
72
+ * `width: 100%`, the CSS of hexpand; `flex: none` keeps the slot's own flex layout from
73
+ * shrinking a natural size that is wider than the carousel before it can be measured.
74
+ */
75
+ function sizedCard(page: CarouselPageMeasurement, index: number): HTMLElement {
76
+ const element = document.createElement('div');
77
+ element.dataset.label = `p${index}`;
78
+ const width = page.expand ? '100%' : `${page.natural}px`;
79
+ element.style.cssText = `flex:none;height:100%;width:${width};`;
80
+ return element;
81
+ }
82
+
83
+ /** Mount a carousel `available` px wide whose pages measure what `pages` says. */
84
+ function mountSized(available: number, pages: readonly CarouselPageMeasurement[]) {
85
+ const host = document.createElement('div');
86
+ host.style.cssText = `width:${available}px;height:80px;`;
87
+ document.body.appendChild(host);
88
+
89
+ const carousel = document.createElement('adw-carousel') as CarouselElement;
90
+ carousel.style.cssText = `display:block;width:${available}px;height:80px;`;
91
+ pages.forEach((page, index) => carousel.appendChild(sizedCard(page, index)));
92
+ host.appendChild(carousel);
93
+
94
+ const track = carousel.querySelector('.adw-carousel-track') as HTMLElement;
95
+ // Instant scrolls; snapping stays ON here, because where a page SNAPS is half of
96
+ // what these tests are about.
97
+ track.style.setProperty('scroll-behavior', 'auto');
98
+ return { carousel, host, track };
99
+ }
100
+
101
+ /** Each page's left edge relative to the carousel, rounded off the subpixel noise. */
102
+ function pageEdges(carousel: HTMLElement): number[] {
103
+ const origin = carousel.getBoundingClientRect().left;
104
+ return Array.from(carousel.querySelectorAll('.adw-carousel-page')).map((slot) =>
105
+ Math.round(slot.getBoundingClientRect().left - origin),
106
+ );
107
+ }
108
+
65
109
  /** The page labels in DOM order — what the user actually sees, not what the model says. */
66
110
  function domOrder(carousel: HTMLElement): string[] {
67
111
  return Array.from(carousel.querySelectorAll('.adw-carousel-page')).map(
@@ -159,6 +203,102 @@ export const AdwCarouselTest = async () => {
159
203
  }
160
204
  });
161
205
 
206
+ await describe('adw-carousel page allocation (libadwaita conformance vectors)', async () => {
207
+ // The rows the DOM can express. The element measures a RENDERED width, so a page
208
+ // whose MINIMUM exceeds the carousel has already been resolved by CSS before the
209
+ // element sees it, and a carousel with no pages has nothing to measure at all.
210
+ const measurable = CAROUSEL_PAGE_ALLOCATION_VECTORS.filter(
211
+ (vector) => vector.pages.length > 0 && vector.pages.every((page) => page.minimum === undefined),
212
+ );
213
+
214
+ for (const vector of measurable) {
215
+ await it(vector.rule, async () => {
216
+ const { carousel, host, track } = mountSized(vector.available, vector.pages);
217
+ await flush();
218
+
219
+ const widths = Array.from(carousel.querySelectorAll('.adw-carousel-page')).map((slot) =>
220
+ Math.round(slot.getBoundingClientRect().width),
221
+ );
222
+ expect(widths).toStrictEqual(vector.pages.map(() => vector.pageSize));
223
+ // The half-gap is the track's own padding, which is what lets the page
224
+ // strip start inside the carousel instead of flush against its edge.
225
+ expect(Math.round(Number.parseFloat(getComputedStyle(track).paddingInlineStart))).toBe(
226
+ vector.leadingInset,
227
+ );
228
+ host.remove();
229
+ });
230
+ }
231
+
232
+ await it('shows the previous and next page at the edges', async () => {
233
+ // THE REPORTED DEFECT. With `flex: 0 0 100%` the pages of the Carousel story
234
+ // (440px cards in a 480px carousel) filled the width and both neighbours sat
235
+ // entirely off screen, where GTK leaves 20px of each showing.
236
+ const pages: CarouselPageMeasurement[] = [{ natural: 440 }, { natural: 440 }, { natural: 440 }];
237
+ const { carousel, host } = mountSized(480, pages);
238
+ await flush();
239
+
240
+ // At rest on page 0: centred, with page 1 starting 20px before the right edge.
241
+ expect(pageEdges(carousel)).toStrictEqual([20, 460, 900]);
242
+
243
+ carousel.scrollToPage(1);
244
+ await flush();
245
+ // Page 1 centred, 20px of page 0 showing at the left and 20px of page 2 at the right.
246
+ expect(pageEdges(carousel)).toStrictEqual([-420, 20, 460]);
247
+ host.remove();
248
+ });
249
+
250
+ await it('leaves the LAST page room to centre as well', async () => {
251
+ // The trailing half-gap is track padding too. A scroll container that dropped
252
+ // its end padding would stop `leadingInset` short of the last snap point, and
253
+ // the last page would never reach the middle.
254
+ const pages: CarouselPageMeasurement[] = [{ natural: 440 }, { natural: 440 }, { natural: 440 }];
255
+ const { carousel, host, track } = mountSized(480, pages);
256
+ await flush();
257
+
258
+ carousel.scrollToPage(2);
259
+ await flush();
260
+ expect(Math.round(track.scrollLeft)).toBe(880);
261
+ expect(carousel.currentPage).toBe(2);
262
+ expect(pageEdges(carousel)).toStrictEqual([-860, -420, 20]);
263
+ host.remove();
264
+ });
265
+
266
+ await it('re-aims the scroll in flight instead of rewinding it', async () => {
267
+ // Mounted inside a HIDDEN ancestor, which is how real markup reaches this:
268
+ // `connectedCallback` measures 0, so the declared position waits for a
269
+ // layout, and revealing the host delivers the pending-position observer and
270
+ // the layout observer in ONE resize pass. The first fires a scroll to page
271
+ // 2 at the fallback pitch; the second narrows the pages and has to put the
272
+ // offset back, and `state.position` is still 0 there, because the `scroll`
273
+ // event that will teach it 2 fires a frame later. Restoring THAT parks the
274
+ // carousel on page 0 and swallows `position="2"`. GTK re-aims instead: it
275
+ // pushes the recomputed snap point into the running animation (:786-788).
276
+ //
277
+ // A carousel already laid out when it is mounted does NOT reach this: it
278
+ // allocates during `connectedCallback` and the pending observer never
279
+ // arms, which is why the mounted-visible spelling of this test passes
280
+ // either way and is not the one kept.
281
+ const host = document.createElement('div');
282
+ host.style.cssText = 'width:480px;height:80px;display:none;';
283
+ document.body.appendChild(host);
284
+
285
+ const carousel = document.createElement('adw-carousel') as CarouselElement;
286
+ carousel.style.cssText = 'display:block;width:480px;height:80px;';
287
+ carousel.setAttribute('position', '2');
288
+ for (let index = 0; index < 3; index++) carousel.appendChild(sizedCard({ natural: 440 }, index));
289
+ host.appendChild(carousel);
290
+ await flush();
291
+
292
+ host.style.display = 'block';
293
+ await flush();
294
+
295
+ const track = carousel.querySelector('.adw-carousel-track') as HTMLElement;
296
+ expect(Math.round(track.scrollLeft)).toBe(880);
297
+ expect(carousel.currentPage).toBe(2);
298
+ host.remove();
299
+ });
300
+ });
301
+
162
302
  await describe('adw-carousel page lookup (libadwaita conformance vectors)', async () => {
163
303
  for (const vector of CAROUSEL_PAGE_AT_POSITION_VECTORS) {
164
304
  // Only rows whose snap points are a settled page list are reachable through
@@ -36,6 +36,47 @@ function centerWidget(text: string): HTMLElement {
36
36
  /** Remove every header bar a test mounted. */
37
37
  function unmountAll(): void {
38
38
  for (const bar of Array.from(document.querySelectorAll('adw-header-bar'))) bar.remove();
39
+ for (const host of Array.from(document.querySelectorAll('[data-header-bar-host]'))) host.remove();
40
+ }
41
+
42
+ /** The long title both geometry tests use, so the narrow bar really has to give way. */
43
+ const LONG_TITLE = 'A very very long document title indeed';
44
+
45
+ /**
46
+ * A header bar of a fixed width, one start button and two end buttons.
47
+ *
48
+ * The asymmetry is the point: it is what tells "centred on the BAR" apart from
49
+ * "centred in the space the buttons left over".
50
+ */
51
+ function mountSizedHeaderBar(width: number): HTMLElement {
52
+ const host = document.createElement('div');
53
+ host.setAttribute('data-header-bar-host', '');
54
+ host.style.width = `${width}px`;
55
+ document.body.appendChild(host);
56
+
57
+ const bar = document.createElement('adw-header-bar');
58
+ bar.setAttribute('title', LONG_TITLE);
59
+ for (const [slot, count] of [
60
+ ['start', 1],
61
+ ['end', 2],
62
+ ] as const) {
63
+ for (let i = 0; i < count; i++) {
64
+ const button = document.createElement('button');
65
+ button.className = 'adw-header-btn';
66
+ button.setAttribute('slot', slot);
67
+ button.textContent = '+';
68
+ bar.appendChild(button);
69
+ }
70
+ }
71
+ // Appended LAST: the sections are built in `connectedCallback`, so the slotted
72
+ // children have to be in place before the bar enters the document.
73
+ host.appendChild(bar);
74
+ return bar;
75
+ }
76
+
77
+ /** The rect of one part of a mounted header bar. */
78
+ function rectOf(bar: HTMLElement, selector: string): DOMRect {
79
+ return (bar.querySelector(selector) as HTMLElement).getBoundingClientRect();
39
80
  }
40
81
 
41
82
  export const AdwHeaderBarTest = async () => {
@@ -113,4 +154,55 @@ export const AdwHeaderBarTest = async () => {
113
154
  unmountAll();
114
155
  });
115
156
  });
157
+
158
+ // `Adw.HeaderBar` lays its three sections out with a `GtkCenterBox`
159
+ // (adw-header-bar.c:992), whose two rules pull in opposite directions and were
160
+ // each broken on their own once. Both are asserted here, because a fix for
161
+ // either one is exactly what breaks the other:
162
+ //
163
+ // - the centre is centred on the WHOLE bar, not in the space the sides left
164
+ // over, so a bar with unequal sides still shows its title in the middle;
165
+ // - `shrink_center_last` is FALSE, so the centre is clamped to
166
+ // `size - (start.natural + end.natural)` and pushed in from either side
167
+ // (gtkcenterlayout.c:155-157). The TITLE gives way, never the buttons.
168
+ await describe('<adw-header-bar> centre placement (GtkCenterBox)', async () => {
169
+ await it('centres the title on the BAR even when the two sides differ', () => {
170
+ const bar = mountSizedHeaderBar(600);
171
+ const barBox = bar.getBoundingClientRect();
172
+ const start = rectOf(bar, '.adw-header-bar-start');
173
+ const end = rectOf(bar, '.adw-header-bar-end');
174
+ const title = rectOf(bar, '.adw-window-title-title');
175
+
176
+ // The discriminator: with equal sides, centring on the bar and centring
177
+ // between the buttons are the same answer and this proves nothing.
178
+ expect(Math.round(end.width) > Math.round(start.width)).toBe(true);
179
+
180
+ const off = Math.abs((title.left + title.right) / 2 - (barBox.left + barBox.right) / 2);
181
+ expect(off <= 1).toBe(true);
182
+ unmountAll();
183
+ });
184
+
185
+ await it('ellipsizes instead of painting the title over the buttons', () => {
186
+ const bar = mountSizedHeaderBar(260);
187
+ const barBox = bar.getBoundingClientRect();
188
+ const start = rectOf(bar, '.adw-header-bar-start');
189
+ const end = rectOf(bar, '.adw-header-bar-end');
190
+ const centre = rectOf(bar, '.adw-header-bar-center');
191
+ const titleEl = bar.querySelector('.adw-window-title-title') as HTMLElement;
192
+
193
+ // The discriminator: the untruncated title really is wider than the whole
194
+ // bar, so there is a collision to avoid in the first place.
195
+ expect(titleEl.scrollWidth > Math.round(barBox.width)).toBe(true);
196
+
197
+ // A centred grid item is sized `fit-content`, whose floor is its own
198
+ // min-content — the whole `nowrap` string. That drew the centre section
199
+ // 277px wide in a 260px bar, starting outside it and running over all
200
+ // three buttons. Stretched into its track it can only be the leftover.
201
+ expect(centre.left >= start.right - 0.5).toBe(true);
202
+ expect(centre.right <= end.left + 0.5).toBe(true);
203
+ expect(centre.left >= barBox.left - 0.5 && centre.right <= barBox.right + 0.5).toBe(true);
204
+ expect(titleEl.scrollWidth > titleEl.clientWidth).toBe(true);
205
+ unmountAll();
206
+ });
207
+ });
116
208
  };
@@ -8,6 +8,10 @@ import { describe, expect, it } from '@gjsify/unit';
8
8
 
9
9
  import type { AdwTabView } from './elements/adw-tab-view.js';
10
10
 
11
+ function chips(view: AdwTabView): HTMLButtonElement[] {
12
+ return Array.from(view.querySelectorAll('.adw-tab')) as HTMLButtonElement[];
13
+ }
14
+
11
15
  function makeTabView(attrs = ''): AdwTabView {
12
16
  const host = document.createElement('div');
13
17
  host.innerHTML = `<adw-tab-view ${attrs}>
@@ -111,4 +115,151 @@ export const AdwTabViewTest = async () => {
111
115
  view.parentElement?.remove();
112
116
  });
113
117
  });
118
+
119
+ // Switching a tab scrolls the TAB STRIP and nothing else. In C that is not a rule
120
+ // anyone has to keep, it is the shape of the API: `scroll_to_tab_full` writes
121
+ // `self->adjustment`, the strip's own adjustment
122
+ // (refs/libadwaita/src/adw-tab-box.c:928-961), and `select_page` is its only caller
123
+ // on a selection (:1728). The DOM has no equally narrow move, because `focus()` and
124
+ // `scrollIntoView()` both walk every scrollable ancestor up to the window, so the
125
+ // port handed the document a 6567px jump on a single ArrowRight, measured in a
126
+ // documentation page long enough to have somewhere to jump to.
127
+ await describe('adw-tab-view keyboard selection scrolls the STRIP, not the page', async () => {
128
+ const BAR_WIDTH = 220;
129
+ const CHIP_WIDTH = 90;
130
+
131
+ /** A view parked far below the fold, so a window scroll is the only way to reveal it. */
132
+ function makeFarBelowTheFold(count = 6): { view: AdwTabView; host: HTMLElement } {
133
+ const host = document.createElement('div');
134
+ const spacer = document.createElement('div');
135
+ spacer.style.height = '4000px';
136
+ host.appendChild(spacer);
137
+ const inner = document.createElement('div');
138
+ inner.style.width = `${BAR_WIDTH}px`;
139
+ host.appendChild(inner);
140
+ document.body.appendChild(host);
141
+
142
+ const view = document.createElement('adw-tab-view') as AdwTabView;
143
+ inner.appendChild(view);
144
+ for (const id of 'abcdefgh'.slice(0, count)) view.appendPage({ id, title: `Page ${id}` });
145
+ return { view, host };
146
+ }
147
+
148
+ await it('ArrowRight moves focus without moving the window', async () => {
149
+ const { view, host } = makeFarBelowTheFold();
150
+ window.scrollTo(0, 0);
151
+ // The precondition IS the test: with the widget already on screen the
152
+ // browser would not scroll either way, and this would pass on the bug.
153
+ expect(view.getBoundingClientRect().top > window.innerHeight).toBe(true);
154
+
155
+ const before = window.scrollY;
156
+ chips(view)[0].dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight', bubbles: true }));
157
+
158
+ expect(view.selectedId).toBe('b');
159
+ // Focus still travels with the roving tabindex; what changed is where the
160
+ // browser may scroll, not whether the chip is focused.
161
+ expect(document.activeElement).toBe(chips(view)[1]);
162
+ expect(window.scrollY).toBe(before);
163
+
164
+ host.remove();
165
+ window.scrollTo(0, 0);
166
+ });
167
+
168
+ await it('End brings the last chip into the bar by scrolling the BAR', async () => {
169
+ const { view, host } = makeFarBelowTheFold();
170
+ window.scrollTo(0, 0);
171
+ const bar = view.querySelector('.adw-tab-bar') as HTMLElement;
172
+ // Pin the chip widths: a chip is a flex item that shrinks to whatever the
173
+ // strip has, so without this the six of them fit and `scroll_to_tab_full`
174
+ // has nothing to do, and the assertions below would hold vacuously.
175
+ for (const chip of chips(view)) chip.style.flex = '0 0 90px';
176
+ expect(bar.scrollWidth > bar.clientWidth).toBe(true);
177
+ expect(bar.scrollLeft).toBe(0);
178
+
179
+ const before = window.scrollY;
180
+ chips(view)[0].dispatchEvent(new KeyboardEvent('keydown', { key: 'End', bubbles: true }));
181
+
182
+ expect(view.selectedId).toBe('f');
183
+ expect(bar.scrollLeft > 0).toBe(true);
184
+ const last = chips(view)[5].getBoundingClientRect();
185
+ const box = bar.getBoundingClientRect();
186
+ expect(Math.round(last.right) <= Math.round(box.right)).toBe(true);
187
+ expect(window.scrollY).toBe(before);
188
+
189
+ host.remove();
190
+ window.scrollTo(0, 0);
191
+ });
192
+
193
+ // The two above both press a key, so both would still pass if the strip scroll
194
+ // lived in the key handler and the padding term were dropped. Neither is what C
195
+ // does: `notify::selected-page` reaches `select_page` through the bar
196
+ // (adw-tab-bar.c:934 -> adw-tab-box.c:1728), so EVERY selection scrolls the
197
+ // strip, and `scroll_to_tab_full` parks the chip `padding` short of the edge
198
+ // rather than flush against it (adw-tab-box.c:952-959).
199
+ await it('a programmatic selection parks the chip a padding short of the edge', async () => {
200
+ const { view, host } = makeFarBelowTheFold(8);
201
+ window.scrollTo(0, 0);
202
+ const bar = view.querySelector('.adw-tab-bar') as HTMLElement;
203
+ for (const chip of chips(view)) {
204
+ // `min-width: 0` as well as the basis: a flex item's automatic minimum
205
+ // size is its MIN-CONTENT size, and the selected chip carries a close
206
+ // affordance the others hide, so without this the six-point-something
207
+ // pixels it adds land in the middle of the arithmetic under test.
208
+ chip.style.flex = `0 0 ${CHIP_WIDTH}px`;
209
+ chip.style.minWidth = '0';
210
+ }
211
+ expect(bar.scrollLeft).toBe(0);
212
+
213
+ const before = window.scrollY;
214
+ // No keypress and no click: the model is driven directly.
215
+ expect(view.setSelectedPage('d')).toBe(true);
216
+
217
+ const pageSize = bar.clientWidth;
218
+ // `padding = MIN (tab_width, page_size - tab_width) / 2` (adw-tab-box.c:952).
219
+ const padding = Math.min(CHIP_WIDTH, pageSize - CHIP_WIDTH) / 2;
220
+ // Chip 4 of 8 is mid-strip, so the computed offset is inside the adjustment's
221
+ // range: an assertion the clamp could satisfy on its own proves nothing.
222
+ expect(bar.scrollLeft > 0 && bar.scrollLeft < bar.scrollWidth - pageSize).toBe(true);
223
+ const chip = chips(view)[3].getBoundingClientRect();
224
+ expect(Math.round(bar.getBoundingClientRect().right - chip.right)).toBe(padding);
225
+ expect(window.scrollY).toBe(before);
226
+
227
+ host.remove();
228
+ window.scrollTo(0, 0);
229
+ });
230
+ });
231
+
232
+ // The same coordinate space, one call site over, and it was wrong there too.
233
+ // `update_visible` (adw-tab-box.c:769-797) decides `fully_visible` from `pos` against
234
+ // the adjustment, exactly as `scroll_to_tab_full` does, and the close affordance
235
+ // hangs off it: `show_close = (hovering && fully_visible) || selected || dragging`
236
+ // (adw-tab.c:124). Measured with `offsetLeft` instead, the comparison ran against
237
+ // whichever positioned ancestor the HOST page happened to have, so a tab view that
238
+ // merely sat indented reported every chip clipped and swallowed the close button on
239
+ // hover.
240
+ await describe('adw-tab-view close affordance is measured against the BAR', async () => {
241
+ await it('hovering an indented but fully visible chip shows its close button', async () => {
242
+ const host = document.createElement('div');
243
+ // The whole defect: an offset between the chip's offsetParent and the bar.
244
+ // Nothing in the widget is positioned, so this lands in `offsetLeft`.
245
+ host.style.marginLeft = '300px';
246
+ host.style.width = '400px';
247
+ document.body.appendChild(host);
248
+ const view = document.createElement('adw-tab-view') as AdwTabView;
249
+ host.appendChild(view);
250
+ for (const id of ['a', 'b', 'c']) view.appendPage({ id, title: `Page ${id}` });
251
+
252
+ const bar = view.querySelector('.adw-tab-bar') as HTMLElement;
253
+ expect(bar.scrollWidth <= bar.clientWidth).toBe(true);
254
+
255
+ // Chip 1, not chip 0: `selected` shows the close button on its own, which
256
+ // would mask the `hovering && fullyVisible` term under test.
257
+ const chip = chips(view)[1];
258
+ expect(chip.classList.contains('active')).toBe(false);
259
+ chip.dispatchEvent(new PointerEvent('pointerenter', { bubbles: false }));
260
+ expect((chip.querySelector('.adw-tab-close') as HTMLElement).hidden).toBe(false);
261
+
262
+ host.remove();
263
+ });
264
+ });
114
265
  };
@@ -201,6 +201,93 @@ export const AdwBottomSheetTest = async () => {
201
201
  sheet.remove();
202
202
  });
203
203
 
204
+ await it('a dismissed sheet re-opens, through either toggle spelling', () => {
205
+ // Reported as "the sheet never comes back": a preview that mounts open, gets
206
+ // dismissed by a scrim click, and then stays closed. Every other case here
207
+ // stops at its FIRST close, so a close path that latched (a listener torn
208
+ // down and never re-attached, a state that only travels one way) could have
209
+ // kept the suite green. The two spellings are the two a host actually
210
+ // writes: `sheet.open = !sheet.open`, and `toggleAttribute('open')` (what the
211
+ // storybook story uses), which only works while the element keeps reflecting
212
+ // the state back onto the attribute.
213
+ const sheet = document.createElement('adw-bottom-sheet') as AdwBottomSheet;
214
+ // `open="true"`, not a bare `open`: that is what Astro's MDX compiler emits
215
+ // for the documentation preview, so it is the markup that gets upgraded.
216
+ sheet.setAttribute('open', 'true');
217
+ sheet.innerHTML =
218
+ '<adw-bottom-sheet-content><button id="under">Under</button></adw-bottom-sheet-content>' +
219
+ '<adw-bottom-sheet-sheet><button id="inside">Inside</button></adw-bottom-sheet-sheet>';
220
+ document.body.appendChild(sheet);
221
+ expect(sheet.open).toBe(true);
222
+
223
+ const payloads: boolean[] = [];
224
+ sheet.addEventListener('notify::open', (event) => {
225
+ payloads.push((event as CustomEvent).detail.open);
226
+ });
227
+
228
+ dimming(sheet).dispatchEvent(new MouseEvent('click', { bubbles: true }));
229
+ expect(sheet.open).toBe(false);
230
+ expect(sheet.hasAttribute('open')).toBe(false);
231
+
232
+ sheet.open = true;
233
+ expect(sheet.open).toBe(true);
234
+ expect(sheet.hasAttribute('open')).toBe(true);
235
+ expect(sheet.classList.contains('open')).toBe(true);
236
+ // The scrim has to become interactive again, or the sheet is dismissible
237
+ // exactly once even though the state reopened.
238
+ expect(dimming(sheet).classList.contains('visible')).toBe(true);
239
+
240
+ sheet.toggleAttribute('open');
241
+ expect(sheet.open).toBe(false);
242
+ sheet.toggleAttribute('open');
243
+ expect(sheet.open).toBe(true);
244
+
245
+ expect(payloads).toStrictEqual([false, true, false, true]);
246
+ sheet.remove();
247
+ });
248
+
249
+ await it('survives the template-clone mount the docs preview uses, and reopens from it', () => {
250
+ // Every other case here builds the sheet with createElement, so the element
251
+ // is upgraded before it has children. The documentation site never does
252
+ // that: it clones an inert <template> and appends the whole subtree in ONE
253
+ // insertion, so the sheet is constructed, gets its attributes and is
254
+ // connected while the same walk is still upgrading its descendants, and its
255
+ // connectedCallback moves those descendants into the content/sheet layers
256
+ // mid-walk. That is the mount the reported "the sheet never comes back" was
257
+ // seen in, and the toggle button has to come back out of it wired and
258
+ // reachable or the preview has no way back from a scrim dismissal.
259
+ const tpl = document.createElement('template');
260
+ tpl.innerHTML =
261
+ '<adw-bottom-sheet open="true" modal="true" can-close="true">' +
262
+ '<adw-bottom-sheet-content><adw-button pill>Toggle sheet</adw-button></adw-bottom-sheet-content>' +
263
+ '<adw-bottom-sheet-sheet><button id="inside">Inside</button></adw-bottom-sheet-sheet>' +
264
+ '</adw-bottom-sheet>';
265
+ const stage = document.createElement('div');
266
+ document.body.appendChild(stage);
267
+ stage.append(tpl.content.cloneNode(true));
268
+
269
+ const sheet = stage.querySelector('adw-bottom-sheet') as AdwBottomSheet;
270
+ expect(sheet.open).toBe(true);
271
+
272
+ const toggle = sheet.querySelector('.adw-bottom-sheet-content adw-button') as HTMLElement;
273
+ expect(toggle).toBeTruthy();
274
+ expect(insideSheet(sheet).id).toBe('inside');
275
+ // What the page binds: a listener on the custom element, driven by a click
276
+ // on the inner native button.
277
+ toggle.addEventListener('click', () => sheet.toggleAttribute('open'));
278
+ const inner = toggle.querySelector('button') as HTMLElement;
279
+
280
+ dimming(sheet).dispatchEvent(new MouseEvent('click', { bubbles: true }));
281
+ expect(sheet.open).toBe(false);
282
+
283
+ inner.dispatchEvent(new MouseEvent('click', { bubbles: true }));
284
+ expect(sheet.open).toBe(true);
285
+ expect(sheet.classList.contains('open')).toBe(true);
286
+ expect(dimming(sheet).classList.contains('visible')).toBe(true);
287
+
288
+ stage.remove();
289
+ });
290
+
204
291
  await it('a dimming click on a CLOSED sheet does nothing and signals nothing', () => {
205
292
  // The scrim is `can_target = open`, so it is not reachable at all — unlike
206
293
  // Escape, it does not signal.