@apliteni/apliteni-ui 0.142.0 → 0.142.1

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.
@@ -19,6 +19,12 @@ import { safeUrl } from '../html.js';
19
19
  import { avatar } from './avatar.js';
20
20
  // The fade's own length, read off the stylesheet rather than copied. why: src/motion.js
21
21
  import { transitionMs, prefersReducedMotion } from '../motion.js';
22
+ import {
23
+ DD_MENU_FLOOR, filterPanelRow, filterPanelFit, dropdownHeightFit, dropdownLift, dropdownReveal, dropdownRest, dropdownSnap, dropdownViewportFit, ddViewOf, ddGap, ddMin, ddViewWidth, ddLifted, DD_EXIT_SLACK_MS, ddWriteCeiling, ddInFilterRow,
24
+ } from '../logic/dropdown-placement.js';
25
+ export {
26
+ DD_MENU_FLOOR, filterPanelRow, filterPanelFit, dropdownHeightFit, dropdownLift, dropdownReveal, dropdownRest, dropdownSnap, dropdownViewportFit,
27
+ } from '../logic/dropdown-placement.js';
22
28
  const cx = (...a) => a.filter(Boolean).join(' ');
23
29
 
24
30
  // A trailing status badge. `badge` is the text shown, in the case it is written
@@ -280,15 +286,6 @@ const ddHostOf = (node) => {
280
286
  return root.nodeType === 9 ? root.body : root;
281
287
  };
282
288
 
283
- /** The window a box is measured in. A panel in a frame is laid out against the
284
- * frame's viewport, not the top page's. */
285
- const ddViewOf = (node) => node.ownerDocument?.defaultView || window;
286
-
287
- // The trigger-to-panel offset is --ui-dropdown-gap in src/styles/dropdown.css.
288
- // This is the fallback for a document that has not loaded the sheet;
289
- // src/components/dropdown.test.js pins the two to each other.
290
- const DD_GAP = 9;
291
-
292
289
  // A portalled panel is no longer a descendant of its container, so everything
293
290
  // below asks the container for its panel rather than querying inside it.
294
291
  const ddPanelOf = (dd) => dd.__ddPanel || dd.querySelector('[data-dropdown-panel]');
@@ -304,51 +301,6 @@ const ddTriggerOff = (dd) => {
304
301
  return !!trigger && (trigger.disabled === true || trigger.getAttribute('aria-disabled') === 'true');
305
302
  };
306
303
 
307
- function ddGap(panel) {
308
- const declared = parseFloat(ddViewOf(panel).getComputedStyle(panel).getPropertyValue('--ui-dropdown-gap'));
309
- return Number.isFinite(declared) ? declared : DD_GAP;
310
- }
311
-
312
- // The least a panel keeps between itself and the edge of its view. The number is
313
- // --ui-dropdown-edge in src/styles/dropdown.css, and this is the fallback for a
314
- // document that has not loaded the sheet, as DD_GAP is for the offset.
315
- const DD_EDGE = 8;
316
-
317
- function ddEdge(panel) {
318
- const declared = parseFloat(ddViewOf(panel).getComputedStyle(panel).getPropertyValue('--ui-dropdown-edge'));
319
- return Number.isFinite(declared) ? declared : DD_EDGE;
320
- }
321
-
322
- // The room a panel that opens down keeps between its end and the end of the Modal
323
- // or Drawer body that holds it, so its shadow fades before the body clips it. The
324
- // number is --ui-dropdown-shade in src/styles/dropdown.css.
325
- const DD_SHADE = 48;
326
-
327
- function ddShade(panel) {
328
- const declared = parseFloat(ddViewOf(panel).getComputedStyle(panel).getPropertyValue('--ui-dropdown-shade'));
329
- return Number.isFinite(declared) ? declared : DD_SHADE;
330
- }
331
-
332
- // The least height a capped panel keeps, below which it would rather spend the
333
- // edge inset than shrink further. The number is --ui-dropdown-min in
334
- // src/styles/dropdown.css. #489
335
- const DD_MIN = 120;
336
-
337
- function ddMin(panel) {
338
- const declared = parseFloat(ddViewOf(panel).getComputedStyle(panel).getPropertyValue('--ui-dropdown-min'));
339
- return Number.isFinite(declared) ? declared : DD_MIN;
340
- }
341
-
342
- /** The width a panel has to stay inside: the LAYOUT viewport, which is what both
343
- * `position: fixed` and a page's scrollable width are measured against. A classic
344
- * scrollbar is in `innerWidth` and not in the box either of those means, so an
345
- * end-anchored panel written from `innerWidth` sat a scrollbar's width inside its
346
- * trigger. The fallback is for a view that lays nothing out, JSDOM among them. */
347
- const ddViewWidth = (panel) => {
348
- const view = ddViewOf(panel);
349
- return view.document?.documentElement?.clientWidth || view.innerWidth || 0;
350
- };
351
-
352
304
  function ddItemsOf(dd) {
353
305
  const panel = ddPanelOf(dd);
354
306
  if (!panel) return [];
@@ -408,89 +360,6 @@ function ddResetSearch(dd, panel, search) {
408
360
  if (panel.offsetWidth) panel.style.minWidth = `${panel.offsetWidth}px`;
409
361
  }
410
362
 
411
- /* The kit's menu floor, the one `.ui-dropdown__panel` writes. The stylesheets
412
- * carry the same number; stories/filter-bar-fit.test.js reads all of them and
413
- * fails when one drifts. why: src/styles/dropdown.css */
414
- export const DD_MENU_FLOOR = 240;
415
-
416
- /** The floor a panel asks for in the sheet, or null. A separate name from the
417
- * floor this fit writes back: one that read its own output would pin a menu at
418
- * whatever a narrow row once allowed. why: src/styles/filter-bar.css */
419
- function ddPanelAsk(panel) {
420
- const view = panel?.ownerDocument?.defaultView;
421
- if (!panel || typeof view?.getComputedStyle !== 'function') return null;
422
- const asked = Number.parseFloat(view.getComputedStyle(panel).getPropertyValue('--ui-filter-panel-ask'));
423
- return Number.isFinite(asked) && asked > 0 ? asked : null;
424
- }
425
-
426
- /** The `.ui-dropdown__panel` inside a dropdown, by class rather than by data
427
- * attribute: the fit's subjects are the panels the stylesheet's rules match, and
428
- * filter-panel-fit.test.js hands this function hand-made elements. */
429
- const ddFitPanelOf = (dd) => (typeof dd?.querySelector === 'function'
430
- ? dd.querySelector('.ui-dropdown__panel') : null);
431
-
432
- /**
433
- * The filter row an anchored menu is measured inside, or null outside one.
434
- *
435
- * Two panels in a row are anchored at a trigger of their own: a chip's values, and
436
- * the add control's catalogue, which says so by asking for a width. The slide
437
- * filterPanelFit() computes is measured from that trigger's offset along the row,
438
- * which is arithmetic for either. A panel anchored to the row itself is neither,
439
- * and sizes itself.
440
- *
441
- * Published because `<Dropdown>` asks the same question — the same row to observe,
442
- * the same panels to hold through a close — and a second copy of this rule would
443
- * drift from the stylesheet. why: src/styles/filter-bar.css
444
- *
445
- * @param {Element} dd a `.ui-dropdown` that may be inside a filter row
446
- * @returns {Element|null} the `.ui-filter-bar` this menu's fit is measured in
447
- */
448
- export function filterPanelRow(dd) {
449
- if (typeof dd?.closest !== 'function') return null;
450
- const chip = dd.closest('.ui-filter-bar__chip');
451
- if (chip) return chip.closest('.ui-filter-bar');
452
- return ddPanelAsk(ddFitPanelOf(dd)) ? dd.closest('.ui-filter-bar') : null;
453
- }
454
-
455
- /**
456
- * Where a filter chip's menu may sit. #484 bounds the panel to its trigger, so a
457
- * chip printing its value alone (#536) left a 48px menu breaking words
458
- * mid-letter — #549. An OPEN panel takes the floor instead and slides along the
459
- * row when the room on the side it opens from is short; shut, it keeps the
460
- * trigger's width, which is why #467 needs no measuring at all.
461
- *
462
- * A panel is anchored at one edge of its dropdown — its inline end when it
463
- * carries `is-end` — so room is measured from that edge and the slide goes the
464
- * other way. Measuring an end-anchored panel forwards put one off the page.
465
- * why: docs/components.md#a-filter-row-holds-its-panels
466
- *
467
- * A menu that is not a chip's takes this arithmetic by asking, on its own panel,
468
- * for a width: `--ui-filter-panel-ask`. why: src/styles/filter-bar.css
469
- *
470
- * @param {Element} dd a `.ui-dropdown` that may be inside a filter row
471
- * @param {number} [floor] the width to reach for; the panel's own ask, else the kit's
472
- * @returns {{room: number, shift: number, floor: number, end: boolean}|null}
473
- */
474
- export function filterPanelFit(dd, floor) {
475
- const panel = ddFitPanelOf(dd);
476
- // Which menus are this function's subjects is filterPanelRow()'s question.
477
- const bar = filterPanelRow(dd);
478
- if (!bar || typeof bar.getBoundingClientRect !== 'function') return null;
479
- const row = bar.getBoundingClientRect();
480
- const box = dd.getBoundingClientRect();
481
- const end = !!panel?.classList?.contains('is-end');
482
- // A row narrower than the floor decides the width; nothing may leave the row.
483
- const want = Math.min(floor ?? ddPanelAsk(panel) ?? DD_MENU_FLOOR, row.width);
484
- const ahead = end
485
- ? Math.max(0, box.right - row.left) // an end-anchored panel grows backwards
486
- : Math.max(0, row.right - box.left);
487
- const slack = end
488
- ? Math.max(0, row.right - box.right) // …and slides towards the row's end
489
- : Math.max(0, box.left - row.left);
490
- const shift = Math.min(slack, Math.max(0, want - ahead));
491
- return { room: ahead + shift, shift, floor: want, end };
492
- }
493
-
494
363
  /** Write the fit onto the panel, and say whether any of the three numbers moved.
495
364
  * All three, because the stylesheet reads all three; it falls back to the
496
365
  * trigger's width when they are unset, so a panel opened before this runs is
@@ -610,457 +479,6 @@ function ddUnwatchRow(dd) {
610
479
  * taken for the middle of an animation, is stated with the rule.
611
480
  * why: docs/components.md#the-dropdown-panel */
612
481
 
613
- /**
614
- * How tall a panel may be: the room between its trigger and the viewport edge,
615
- * from dropdownAvail() in src/logic/dropdown.js — one calculation, so the
616
- * vanilla wiring and the React `<Dropdown>` cannot disagree about where a panel
617
- * ends. Measured from the trigger, not the panel, the way dropdownViewportFit()
618
- * is measured from the panel and not the trigger: each is the box the number it
619
- * answers actually depends on. `null` when there is nothing to measure.
620
- * why: docs/components.md#the-dropdown-panel
621
- *
622
- * @param {Element} dd the `[data-dropdown]` container the trigger lives in
623
- * @param {Element} panel a `[data-dropdown-panel]`, for its gap/edge/min and its
624
- * `is-up` direction
625
- */
626
- export function dropdownHeightFit(dd, panel) {
627
- const trigger = dd?.querySelector?.('[data-dropdown-trigger]');
628
- if (!trigger || typeof trigger.getBoundingClientRect !== 'function' || !panel) return null;
629
- const t = trigger.getBoundingClientRect();
630
- const view = ddViewOf(panel);
631
- // Inside a scrolling body the panel may take the body less its field: dropdownReveal()
632
- // scrolls the body to show it, so the room is the body's, not what is left under the
633
- // trigger now. A lifted panel is measured against the viewport, as a portalled one is.
634
- const fit = ddLifted(panel) ? null : ddBodyFit(dd, panel);
635
- if (fit && !fit.lift) return fit.room;
636
- // A lifted panel is measured from where the body is about to bring its trigger.
637
- const by = ddLifted(panel) && !ddUp(panel) ? dd.__ddLiftBy || 0 : 0;
638
- return dropdownAvail({
639
- anchorTop: t.top - by,
640
- anchorBottom: t.bottom - by,
641
- viewport: view.innerHeight,
642
- gap: ddGap(panel),
643
- inset: ddEdge(panel),
644
- min: ddMin(panel),
645
- up: ddUp(panel),
646
- });
647
- }
648
-
649
- /* A panel the wiring lifted out of a body too short to hold it: `position: fixed`, so
650
- * it opens over the body the way a portalled panel does. The value is the side it
651
- * opens on. why: docs/components.md#the-dropdown-panel */
652
- const ddLifted = (panel) => typeof panel?.hasAttribute === 'function' && panel.hasAttribute('data-dd-lifted');
653
- const ddUp = (panel) => (ddLifted(panel)
654
- ? panel.getAttribute('data-dd-lifted') === 'up'
655
- : !!panel?.classList?.contains('is-up'));
656
-
657
- /** Every ancestor that scrolls the block axis, from a wrapper to the Modal or Drawer
658
- * body round it, with the part of them a reader can see: their padding boxes clipped
659
- * to each other and to the viewport. `el` is the one whose end is that part's end,
660
- * which is the one that has to scroll. `height` is the least any of them shows, which
661
- * is the room a panel has whatever they are scrolled to. `null` for a portalled panel
662
- * (it is not inside them), a filter row (its row already bounds its panels) and a
663
- * trigger with none. */
664
- function ddScrollBox(dd, panel) {
665
- if (dd.__ddPanel || ddInFilterRow(panel)) return null;
666
- const view = ddViewOf(panel);
667
- const els = [];
668
- let top = 0;
669
- let bottom = view.innerHeight;
670
- let height = view.innerHeight;
671
- let el = null;
672
- for (let at = dd.parentElement; at && at !== at.ownerDocument.body; at = at.parentElement) {
673
- const overflow = view.getComputedStyle(at).overflowY;
674
- if (overflow !== 'auto' && overflow !== 'scroll') continue;
675
- const start = at.getBoundingClientRect().top + at.clientTop;
676
- const end = start + at.clientHeight;
677
- els.push(at);
678
- top = Math.max(top, start);
679
- if (end <= bottom) { bottom = end; el = at; }
680
- height = Math.min(height, at.clientHeight);
681
- }
682
- if (!els.length || !(bottom > top)) return null;
683
- return { el: el || els[0], els, top, bottom, height };
684
- }
685
-
686
- /* The top of what has to stay in view over the panel: the field's label when the
687
- * trigger has one above it, so the reader still sees what the list is for. */
688
- function ddAnchorTop(dd, t) {
689
- const label = dd.closest?.('.ui-field')?.querySelector(':scope > .ui-field__label');
690
- const top = label?.getBoundingClientRect?.().top;
691
- return Number.isFinite(top) && top < t.top ? top : t.top;
692
- }
693
-
694
- /* The height a panel's rows ask for, uncapped: its own box plus whatever it and its
695
- * list hold past their ends. */
696
- function ddContentHeight(panel) {
697
- let h = panel.offsetHeight + panel.scrollHeight - panel.clientHeight;
698
- const list = panel.querySelector?.('.ui-dropdown__list');
699
- if (list) h += list.scrollHeight - list.clientHeight;
700
- return h;
701
- }
702
-
703
- /**
704
- * How a panel fits the body it opens in. The body has to hold the field's label and
705
- * trigger, the gap, the panel and the room under it. That room is 48px where the body
706
- * can spare it, so the shadow fades before the body clips it, and it is spent down to
707
- * the 8px edge where the body cannot. Where even then the body cannot hold the floor
708
- * (or the whole list, when that is shorter), the panel is lifted out of the body.
709
- * Measured from the heights alone, so the answer is the same however far the body is
710
- * scrolled. `null` with no scrolling body.
711
- */
712
- function ddBodyFit(dd, panel) {
713
- const trigger = dd.querySelector('[data-dropdown-trigger]');
714
- const box = ddScrollBox(dd, panel);
715
- if (!box || !trigger) return null;
716
- const t = trigger.getBoundingClientRect();
717
- const edge = ddEdge(panel);
718
- const gap = ddGap(panel);
719
- const need = Math.min(ddMin(panel), ddContentHeight(panel));
720
- if (ddUp(panel)) {
721
- const room = box.height - 2 * edge - t.height - gap;
722
- return room >= need ? { box, end: edge, room, anchor: t.top } : { box, lift: true };
723
- }
724
- const anchor = ddAnchorTop(dd, t);
725
- const spare = box.height - edge - (t.bottom - anchor) - gap - need;
726
- const end = Math.max(edge, Math.min(ddShade(panel), spare));
727
- const room = box.height - edge - (t.bottom - anchor) - gap - end;
728
- return room >= need ? { box, end, room, anchor } : { box, lift: true };
729
- }
730
-
731
- /**
732
- * Lift an open panel out of a body too short to hold it, or put a lifted one back.
733
- * A lifted panel is `position: fixed` against the viewport and opens over the body,
734
- * below its trigger or, where the viewport has more room above, over it, the way a
735
- * portalled panel opens. It follows its trigger while the body scrolls. Call it with
736
- * the panel open and laid out, before sizing it. Returns whether the panel is lifted.
737
- * why: docs/components.md#the-dropdown-panel
738
- */
739
- export function dropdownLift(dd, panel) {
740
- if (!dd || !panel?.style || dd.__ddPanel) return false;
741
- const lift = !!ddBodyFit(dd, panel)?.lift;
742
- if (!lift) {
743
- if (ddLifted(panel)) {
744
- panel.removeAttribute('data-dd-lifted');
745
- for (const side of ['top', 'bottom', 'left', 'right']) panel.style.removeProperty(side);
746
- panel.style.removeProperty('inline-size');
747
- }
748
- return false;
749
- }
750
- dd.__ddLiftBy = ddLiftPlan(dd, panel);
751
- ddPlaceLifted(dd, panel);
752
- return true;
753
- }
754
-
755
- /* How far the body scrolls to give a lifted panel that opens down the viewport's room
756
- * under its trigger: no further than the field's label reaching the body's top, and no
757
- * further than the panel needs. 0 for a panel that opens up. The label stays in view,
758
- * where a panel lifted over it would cover it. */
759
- function ddLiftPlan(dd, panel) {
760
- const trigger = dd.querySelector('[data-dropdown-trigger]');
761
- const box = ddScrollBox(dd, panel);
762
- if (!trigger || !box || panel.classList.contains('is-up')) return 0;
763
- const t = trigger.getBoundingClientRect();
764
- const edge = ddEdge(panel);
765
- const rise = Math.max(0, ddAnchorTop(dd, t) - edge - box.top);
766
- const want = Math.min(ddContentHeight(panel), ddCeilingOf(panel));
767
- const over = t.bottom + ddGap(panel) + want + edge - ddViewOf(panel).innerHeight;
768
- return Math.max(0, Math.min(rise, Math.ceil(over)));
769
- }
770
-
771
- /* The tallest a panel may be before the room has a say: the cap a consumer asked for,
772
- * which under a search field is the list's and the field over it, or the standard cap. */
773
- function ddCeilingOf(panel) {
774
- const view = ddViewOf(panel);
775
- const list = panel.querySelector?.('.ui-dropdown__list');
776
- const own = parseFloat((list || panel).style?.getPropertyValue('--ui-dropdown-cap'));
777
- if (Number.isFinite(own)) return own + (list ? panel.offsetHeight - list.offsetHeight : 0);
778
- const max = parseFloat(view.getComputedStyle(panel).getPropertyValue('--ui-dropdown-max'));
779
- return Number.isFinite(max) ? max : Infinity;
780
- }
781
-
782
- function ddPlaceLifted(dd, panel) {
783
- const trigger = dd.querySelector('[data-dropdown-trigger]');
784
- if (!trigger) return;
785
- const view = ddViewOf(panel);
786
- const t = trigger.getBoundingClientRect();
787
- const gap = ddGap(panel);
788
- // Below is counted after the body has scrolled the field up (ddLiftPlan()).
789
- const below = view.innerHeight - t.bottom - gap + (dd.__ddLiftBy || 0);
790
- const above = t.top - gap;
791
- // The configured side, unless it cannot hold the floor and the other side has more.
792
- const configured = panel.classList.contains('is-up');
793
- const mine = configured ? above : below;
794
- const other = configured ? below : above;
795
- const up = mine < Math.min(ddMin(panel), ddContentHeight(panel)) && other > mine ? !configured : configured;
796
- const s = panel.style;
797
- s.translate = '';
798
- if (panel.getAttribute('data-dd-lifted') !== (up ? 'up' : 'down')) {
799
- const held = s.transition;
800
- s.transition = 'none';
801
- panel.setAttribute('data-dd-lifted', up ? 'up' : 'down');
802
- view.getComputedStyle(panel).transform;
803
- s.transition = held;
804
- }
805
- ddWriteCeiling(panel);
806
- if (dd.parentElement?.classList.contains('ui-field')) s.inlineSize = `${t.width}px`;
807
- const width = panel.getBoundingClientRect().width;
808
- const end = panel.classList.contains('is-end');
809
- const shift = dropdownViewportFit(panel, { left: end ? t.right - width : t.left, width })?.shift || 0;
810
- const left = (end ? t.right - width : t.left) + shift;
811
- s.right = 'auto';
812
- s.left = `${left}px`;
813
- if (up) { s.top = 'auto'; s.bottom = `${view.innerHeight - t.top + gap}px`; }
814
- else { s.bottom = 'auto'; s.top = `${t.bottom + gap}px`; }
815
- /* An ancestor with a transform holds a fixed box too, and then the numbers above are
816
- * read from its corner. Measured and taken back, so the panel lands where they say. */
817
- const r = panel.getBoundingClientRect();
818
- if (!(r.width > 0)) return;
819
- const dx = r.left - left;
820
- const dy = up ? r.bottom - (t.top - gap) : r.top - (t.bottom + gap);
821
- if (Math.abs(dx) > 0.5) s.left = `${left - dx}px`;
822
- if (Math.abs(dy) > 0.5) {
823
- if (up) s.bottom = `${view.innerHeight - t.top + gap + dy}px`;
824
- else s.top = `${t.bottom + gap - dy}px`;
825
- }
826
- }
827
-
828
- /**
829
- * Scroll the body an open panel sits in the least distance that shows the whole
830
- * panel, the room under it and the field's label over it (dropdownHeightFit() makes
831
- * the panel no taller than that). A body that already shows them does not move. A
832
- * wrapper inside the body that does not scroll is passed over: the ancestor whose end
833
- * is the end of what the reader sees is the one scrolled. Instant under
834
- * `prefers-reduced-motion`. Returns the distance scrolled.
835
- * why: docs/components.md#the-dropdown-panel
836
- */
837
- export function dropdownReveal(dd, panel) {
838
- const by = ddRevealOnce(dd, panel);
839
- /* A shut panel is held 6px off its place and the body cannot scroll past where
840
- * it ends, so the first scroll falls up to 6px short. One more look once the
841
- * panel has settled. */
842
- if (by && panel.style) {
843
- setTimeout(() => { if (dd.classList.contains('open')) ddRevealOnce(dd, panel); },
844
- transitionMs(panel) + DD_EXIT_SLACK_MS);
845
- }
846
- return by;
847
- }
848
-
849
- /* A body cannot scroll past the end of what it holds, and a panel is the last thing
850
- * in it: the panel's own bottom is the end. Without more below it, the room the
851
- * reveal keeps cannot be reached at the end of the body. The sheet draws a 1px spot
852
- * at the distance written here, below the trigger. `end` is that room: the body's,
853
- * or on a page the 8px edge, which the page's own fit keeps, so opening a panel on a
854
- * page that does not scroll makes it scroll by nothing. why: src/styles/dropdown.css */
855
- function ddReserveEnd(dd, panel, end) {
856
- if (!dd.style || ddUp(panel) || ddLifted(panel)) return;
857
- const next = `${ddGap(panel) + panel.offsetHeight + end - 1}px`;
858
- if (dd.style.getPropertyValue('--ui-dropdown-reach') !== next) dd.style.setProperty('--ui-dropdown-reach', next);
859
- }
860
-
861
- /* A shut panel is still laid out, and a box that is laid out counts towards the scroll
862
- * of what holds it: the end of a Modal or Drawer body, or of a page, scrolled to blank
863
- * space as tall as the panel. So a shut panel is collapsed, once its fade is over, and
864
- * its room under it goes with it. The body cannot scroll past where it ends, so
865
- * collapsing under a reader who is looking at that room would slide the field down by
866
- * the whole of it: the box stays until the reader has scrolled out of the room, and the
867
- * field never moves. The panel is given its box back before anything measures it.
868
- * why: docs/components.md#the-dropdown-panel */
869
- function ddScrollerOf(dd, panel) {
870
- return ddScrollBox(dd, panel)?.el ?? dd.ownerDocument.scrollingElement;
871
- }
872
-
873
- function ddCollapse(dd, panel) {
874
- const scroller = ddScrollerOf(dd, panel);
875
- const before = scroller?.scrollTop ?? 0;
876
- const reach = dd.style?.getPropertyValue('--ui-dropdown-reach') ?? '';
877
- panel.setAttribute('data-dd-shut', '');
878
- dd.style?.removeProperty('--ui-dropdown-reach');
879
- if (!scroller || scroller.scrollTop === before) return true;
880
- panel.removeAttribute('data-dd-shut');
881
- if (reach) dd.style.setProperty('--ui-dropdown-reach', reach);
882
- if (typeof scroller.scrollTo === 'function') scroller.scrollTo({ top: before, behavior: 'instant' });
883
- else scroller.scrollTop = before;
884
- return false;
885
- }
886
-
887
- /**
888
- * Give a panel its box while it is open, and take the box away once it has shut and
889
- * its fade is over. Returns a function that cancels a collapse still waiting.
890
- * Does nothing for a portalled panel, which is `position: fixed` and adds no scroll,
891
- * and for a panel in a filter row, which its row already bounds.
892
- * why: docs/components.md#the-dropdown-panel
893
- *
894
- * @param {Element} dd the `[data-dropdown]` container
895
- * @param {Element} panel its `[data-dropdown-panel]`
896
- * @param {boolean} open whether the panel is opening (`true`) or has shut (`false`)
897
- * @param {number} [wait] ms to wait before collapsing; the panel's own fade by default
898
- */
899
- export function dropdownRest(dd, panel, open, wait) {
900
- if (!dd || !panel?.style || dd.__ddPanel || ddInFilterRow(panel)) return () => {};
901
- dd.__ddRest?.();
902
- if (open) {
903
- panel.removeAttribute('data-dd-shut');
904
- return () => {};
905
- }
906
- let timer = null;
907
- let off = null;
908
- const cancel = () => {
909
- clearTimeout(timer);
910
- off?.();
911
- dd.__ddRest = null;
912
- };
913
- const attempt = () => {
914
- const done = !dd.isConnected || dd.classList.contains('open') || ddCollapse(dd, panel);
915
- if (done) cancel();
916
- return done;
917
- };
918
- const start = () => {
919
- timer = null;
920
- if (attempt()) return;
921
- const scroller = ddScrollerOf(dd, panel);
922
- const target = scroller === dd.ownerDocument.scrollingElement ? ddViewOf(panel) : scroller;
923
- target.addEventListener('scroll', attempt, { passive: true });
924
- off = () => target.removeEventListener('scroll', attempt);
925
- };
926
- dd.__ddRest = cancel;
927
- const ms = wait ?? transitionMs(panel) + DD_EXIT_SLACK_MS;
928
- if (ms > 0) timer = setTimeout(start, ms);
929
- else start();
930
- return cancel;
931
- }
932
-
933
- function ddRevealOnce(dd, panel) {
934
- const trigger = dd?.querySelector?.('[data-dropdown-trigger]');
935
- if (!trigger || typeof trigger.getBoundingClientRect !== 'function' || !panel) return 0;
936
- if (ddLifted(panel)) return ddRevealLifted(dd, panel, trigger);
937
- const fit = ddBodyFit(dd, panel);
938
- if (!fit) {
939
- // A page is held the same way: the shut panel lifts 6px, which would end the page 6px
940
- // sooner under a reader at its end.
941
- if (!dd.__ddPanel && !ddInFilterRow(panel)) ddReserveEnd(dd, panel, ddEdge(panel));
942
- // Focusing End can scroll only far enough to show the row. Finish the page
943
- // scroll so the open panel keeps its edge and the row's outline has room.
944
- if (!dd.__ddPanel && !ddLifted(panel) && !ddInFilterRow(panel)
945
- && panel.contains(dd.ownerDocument.activeElement?.closest?.('[data-dd-item]'))) {
946
- const view = ddViewOf(panel);
947
- const by = Math.ceil(panel.getBoundingClientRect().bottom + ddEdge(panel) - view.innerHeight);
948
- if (by > 0) { view.scrollBy({ top: by, behavior: 'instant' }); return by; }
949
- }
950
- return 0;
951
- }
952
- if (fit.lift) return 0;
953
- const { box, end } = fit;
954
- ddReserveEnd(dd, panel, end);
955
- const t = trigger.getBoundingClientRect();
956
- const edge = ddEdge(panel);
957
- const h = panel.offsetHeight;
958
- const up = ddUp(panel);
959
- const top = up ? t.top - ddGap(panel) - h - edge : ddAnchorTop(dd, t) - edge;
960
- const bottom = (up ? t.bottom : t.bottom + ddGap(panel) + h) + end;
961
- let by = 0;
962
- if (bottom > box.bottom) by = Math.min(bottom - box.bottom, top - box.top);
963
- else if (top < box.top) by = top - box.top;
964
- by = Math.round(by);
965
- if (!by) return 0;
966
- const behavior = prefersReducedMotion() ? 'auto' : 'smooth';
967
- /* The ancestor whose end the reader sees takes the scroll. What it has no room to
968
- * take goes to the others, nearest first, each up to the room it has. */
969
- const move = (el, d) => {
970
- if (typeof el.scrollBy === 'function') el.scrollBy({ top: d, behavior });
971
- else el.scrollTop += d;
972
- };
973
- const room = (el, d) => (d > 0 ? el.scrollHeight - el.clientHeight - el.scrollTop : -el.scrollTop);
974
- const first = room(box.el, by);
975
- move(box.el, by);
976
- let left = by > 0 ? by - Math.max(0, Math.min(by, first)) : by - Math.min(0, Math.max(by, first));
977
- for (const el of box.els) {
978
- if (el === box.el || !left) continue;
979
- const can = left > 0 ? Math.max(0, Math.min(left, room(el, left))) : Math.min(0, Math.max(left, room(el, left)));
980
- if (can) { move(el, can); left -= can; }
981
- }
982
- return by;
983
- }
984
-
985
- /* The scroll ddLiftPlan() asked for. The body may hold too little to scroll that far, so
986
- * the reserved spot is written to where the body's end then has to be. */
987
- function ddRevealLifted(dd, panel, trigger) {
988
- const by = dd.__ddLiftBy || 0;
989
- const box = by && !ddUp(panel) ? ddScrollBox(dd, panel) : null;
990
- if (!box) return 0;
991
- const t = trigger.getBoundingClientRect();
992
- dd.style?.setProperty('--ui-dropdown-reach', `${Math.max(0, box.bottom + by - t.bottom - 1)}px`);
993
- const behavior = prefersReducedMotion() ? 'auto' : 'smooth';
994
- if (typeof box.el.scrollBy === 'function') box.el.scrollBy({ top: by, behavior });
995
- else box.el.scrollTop += by;
996
- return by;
997
- }
998
-
999
- /**
1000
- * Stop an open panel's height where it cuts a row through the middle. A height that
1001
- * falls between two rows, or in a row's padding, shows a complete last label and the
1002
- * list looks whole; a half row says it goes on. The height is the one the panel gets:
1003
- * the smaller of its cap and the room the wiring measured, so a panel the room limits
1004
- * is cut the same way as one the cap limits. The rows are measured as they are drawn,
1005
- * so every row height the kit has, with a description or without and under a search
1006
- * field or not, stops at its own half row. A cap a consumer asked for (`scroll: <n>`)
1007
- * is kept to its number where it is the smaller, and a panel that fits has nothing to
1008
- * cut. Call it with the panel laid out and its room written.
1009
- * why: docs/components.md#the-dropdown-panel
1010
- */
1011
- export function dropdownSnap(dd, panel) {
1012
- if (!panel?.style) return;
1013
- const view = ddViewOf(panel);
1014
- const set = (v) => {
1015
- const next = v == null ? '' : `${v}px`;
1016
- if (panel.style.getPropertyValue('--ui-dropdown-fit') === next) return;
1017
- if (next) panel.style.setProperty('--ui-dropdown-fit', next);
1018
- else panel.style.removeProperty('--ui-dropdown-fit');
1019
- };
1020
- const rows = Array.from(panel.querySelectorAll('[data-dd-item]')).filter((el) => el.getClientRects().length);
1021
- let clip = panel;
1022
- for (let el = rows[0]?.parentElement; el && el !== panel; el = el.parentElement) {
1023
- const overflow = view.getComputedStyle(el).overflowY;
1024
- if (overflow === 'auto' || overflow === 'scroll') { clip = el; break; }
1025
- }
1026
- // The consumer's cap is on the panel, or on the list under a search field, where the
1027
- // panel is that cap and the field over it.
1028
- const own = parseFloat((clip === panel ? panel : clip).style.getPropertyValue('--ui-dropdown-cap'));
1029
- const chrome = clip === panel ? 0 : panel.offsetHeight - clip.offsetHeight;
1030
- const ceiling = ddCeilingOf(panel);
1031
- const room = parseFloat(panel.style.getPropertyValue('--ui-dropdown-avail'));
1032
- /* In a body, the room under the panel may be spent down to the 8px edge to reach the
1033
- * next half row up, rather than stop a whole row short of the room. */
1034
- const fit = room < ceiling && !ddLifted(panel) ? ddBodyFit(dd, panel) : null;
1035
- const spare = fit && !fit.lift ? fit.end - ddEdge(panel) : 0;
1036
- const height = Number.isFinite(room) ? Math.min(ceiling, room) : ceiling;
1037
- if (!rows.length || !Number.isFinite(height)) { set(null); return; }
1038
- // Kept to the consumer's number where it is the one that binds.
1039
- if (Number.isFinite(own) && !(room < ceiling)) { set(chrome ? ceiling : null); return; }
1040
- const box = panel.getBoundingClientRect();
1041
- const clipBox = clip.getBoundingClientRect();
1042
- if (!(box.height > 0)) return;
1043
- let scrolled = 0;
1044
- for (let el = rows[0].parentElement; el; el = el.parentElement) {
1045
- scrolled += el.scrollTop;
1046
- if (el === panel) break;
1047
- }
1048
- const edge = clip === panel ? panel.offsetHeight - panel.clientHeight - panel.clientTop : box.bottom - clipBox.bottom;
1049
- const lead = clip === panel ? panel.clientTop : clipBox.top - box.top;
1050
- const stop = dropdownStop({
1051
- rows: rows.map((el) => {
1052
- const r = el.getBoundingClientRect();
1053
- return { top: r.top - box.top + scrolled, height: r.height };
1054
- }),
1055
- below: edge,
1056
- content: lead + clip.scrollHeight + edge,
1057
- ceiling: height,
1058
- reach: Math.min(ceiling, height + spare),
1059
- });
1060
- set(stop ?? (chrome && Number.isFinite(own) ? ceiling : null));
1061
- if (stop > room) panel.style.setProperty('--ui-dropdown-avail', `${stop}px`);
1062
- }
1063
-
1064
482
  function ddSizeHeight(dd, panel) {
1065
483
  if (!panel?.style) return;
1066
484
  /* Decided when the panel opens and kept while the page or the body scrolls under it,
@@ -1103,10 +521,6 @@ function ddPlace(dd) {
1103
521
  ddSizeHeight(dd, panel);
1104
522
  }
1105
523
 
1106
- /* A few frames past the sheet's own end, so the timer never cuts the fade's last
1107
- * frame. The number the dialogs' exit uses. */
1108
- const DD_EXIT_SLACK_MS = 50;
1109
-
1110
524
  /* How long the settle fallback waits: past --dur-slow (400ms), the longest motion
1111
525
  * the kit ships, plus the same slack. A motion longer than this is answered by its
1112
526
  * own end event; this timer is for the end event that never comes.
@@ -1193,78 +607,6 @@ function ddSetUp(panel, up) {
1193
607
  panel.style.transition = held;
1194
608
  }
1195
609
 
1196
- /** The shift a panel is already carrying, the one ddFitViewport() wrote. Taken
1197
- * off before the next measurement, so asking twice answers the same as asking
1198
- * once — the fit is measured from where the sheet puts the panel, not from where
1199
- * the last fit left it. */
1200
- const ddShiftOf = (panel) => parseFloat(panel.style?.translate) || 0;
1201
-
1202
- /** The widest a panel may be: the view it is laid out in, less the keep-out it owes
1203
- * each edge. It is the bound a PORTALLED panel needs, whose room is the whole view
1204
- * because it is `position: fixed`; an in-place one is bounded by its trigger's own
1205
- * block, once `overflow-wrap` has let its long tokens break.
1206
- * why: src/styles/dropdown.css */
1207
- const ddCeiling = (panel) => Math.max(0, ddViewWidth(panel) - 2 * ddEdge(panel));
1208
-
1209
- /** Write the ceiling, BEFORE the panel is measured: capping a panel's width
1210
- * changes the width a shift would be calculated from. The sheet reads the
1211
- * property and keeps its own 240px floor where the two disagree, which is a view
1212
- * narrower than 2 × edge + 240. why: src/styles/dropdown.css */
1213
- function ddWriteCeiling(panel) {
1214
- const max = ddCeiling(panel);
1215
- panel.style?.setProperty('--ui-dropdown-ceiling', max > 0 ? `${max}px` : '');
1216
- }
1217
-
1218
- /** A panel a filter row bounds to the row itself. It is nothing the viewport fit
1219
- * has to measure, and nothing the wiring has to watch for it.
1220
- * why: docs/components.md#a-filter-row-holds-its-panels */
1221
- const ddInFilterRow = (panel) => typeof panel?.closest === 'function' && !!panel.closest('.ui-filter-bar');
1222
-
1223
- /**
1224
- * How wide a panel may be and how far along the inline axis it has to move to stay
1225
- * inside the view it is laid out in: `left` at or after the edge, `right` at or
1226
- * before the far one.
1227
- *
1228
- * `align` decides the edge of the trigger a panel hangs from, and that is all it
1229
- * decides — on a phone it moves the clipping from one side to the other rather
1230
- * than removing it, which is #572. So the preferred edge is measured and kept
1231
- * whenever it fits, and overridden only when it does not: the same shape
1232
- * `direction: 'auto'` gives the block axis.
1233
- *
1234
- * Measured, not derived from the classes: the panel's own box is where the sheet
1235
- * has actually put it, so one answer covers `left: 0`, `.is-end`'s `right: 0` and
1236
- * the topbar's bespoke menus alike, and a rule nobody here has read about cannot
1237
- * send it the wrong way.
1238
- *
1239
- * A panel inside a `.ui-filter-bar` is not this function's subject: a filter row
1240
- * bounds its panels to the row itself, which already holds them inside the view.
1241
- * why: docs/components.md#a-filter-row-holds-its-panels
1242
- *
1243
- * @param {Element} panel a `[data-dropdown-panel]`, as the sheet has placed it
1244
- * @param {{left: number, width: number}} [at] the box the caller is about to
1245
- * write, for a panel it places itself — the portal, whose own box is still the
1246
- * one the last call left it at
1247
- * @returns {{shift: number, left: number, width: number, view: number, edge: number,
1248
- * max: number}|null}
1249
- */
1250
- export function dropdownViewportFit(panel, at) {
1251
- if (!panel || typeof panel.getBoundingClientRect !== 'function') return null;
1252
- if (!at && ddInFilterRow(panel)) return null;
1253
- const view = ddViewWidth(panel);
1254
- const box = panel.getBoundingClientRect();
1255
- const width = at ? at.width : box.width;
1256
- const left = at ? at.left : box.left - ddShiftOf(panel);
1257
- if (!(view > 0) || !(width > 0) || !Number.isFinite(left)) return null;
1258
- const slack = view - width;
1259
- /* The gap is what a panel can afford rather than what it asks for. A panel still
1260
- wider than its view — the floor, inside a view too narrow for it — has no room
1261
- for one and cannot honour both edges at all; it keeps its start, so the reader
1262
- meets the beginning of the rows and not the end. */
1263
- const edge = Math.max(0, Math.min(ddEdge(panel), slack / 2));
1264
- const want = Math.max(edge, Math.min(left, slack - edge));
1265
- return { shift: want - left, left: want, width, view, edge, max: ddCeiling(panel) };
1266
- }
1267
-
1268
610
  /**
1269
611
  * Keep a panel in place inside its view, by moving it along the inline axis.
1270
612
  *