menhir 0.1.1 → 0.1.3

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.
Files changed (176) hide show
  1. package/README.md +2 -2
  2. package/dist/alert-dialog/index.js +2 -0
  3. package/dist/alert-dialog/index.parts.js +7 -0
  4. package/dist/alert-dialog/popup/AlertDialogPopup.js +13 -0
  5. package/dist/core/createControllableSignal.js +4 -3
  6. package/dist/core/slot.js +30 -0
  7. package/dist/dialog/close/DialogClose.js +33 -0
  8. package/dist/dialog/description/DialogDescription.js +27 -0
  9. package/dist/dialog/index.js +2 -0
  10. package/dist/dialog/index.parts.js +7 -0
  11. package/dist/dialog/popup/DialogPopup.js +13 -0
  12. package/dist/dialog/popup/renderDialogPopup.js +71 -0
  13. package/dist/dialog/root/DialogRoot.js +46 -0
  14. package/dist/dialog/root/DialogRootContext.js +10 -0
  15. package/dist/dialog/title/DialogTitle.js +27 -0
  16. package/dist/dialog/trigger/DialogTrigger.js +33 -0
  17. package/dist/floating/dismissal/dialogClose.js +67 -0
  18. package/dist/floating/dismissal/escape.js +43 -0
  19. package/dist/floating/dismissal/outsidePress.js +52 -0
  20. package/dist/floating/dismissal/popoverClose.js +74 -0
  21. package/dist/floating/dismissal/reason.js +0 -0
  22. package/dist/floating/focus/initialFocus.js +33 -0
  23. package/dist/floating/focus/returnFocus.js +65 -0
  24. package/dist/floating/focus/tabWrap.js +55 -0
  25. package/dist/floating/focus/tabbable.js +50 -0
  26. package/dist/floating/focus/target.js +0 -0
  27. package/dist/floating/hover/group.js +36 -0
  28. package/dist/floating/hover/hover.js +105 -0
  29. package/dist/floating/hover/safeArea.js +64 -0
  30. package/dist/floating/positioning/arrow.js +21 -0
  31. package/dist/floating/positioning/createPosition.js +122 -0
  32. package/dist/floating/positioning/flip.js +1 -0
  33. package/dist/floating/positioning/hide.js +14 -0
  34. package/dist/floating/positioning/offset.js +1 -0
  35. package/dist/floating/positioning/shift.js +1 -0
  36. package/dist/floating/positioning/size.js +39 -0
  37. package/dist/floating/presence/presence.js +57 -0
  38. package/dist/floating/scroll-lock/scrollLock.js +125 -0
  39. package/dist/popover/arrow/PopoverArrow.js +39 -0
  40. package/dist/popover/close/PopoverClose.js +33 -0
  41. package/dist/popover/description/PopoverDescription.js +27 -0
  42. package/dist/popover/index.js +2 -0
  43. package/dist/popover/index.parts.js +8 -0
  44. package/dist/popover/popup/PopoverPopup.js +116 -0
  45. package/dist/popover/popup/PopoverPopupContext.js +10 -0
  46. package/dist/popover/root/PopoverRoot.js +50 -0
  47. package/dist/popover/root/PopoverRootContext.js +10 -0
  48. package/dist/popover/title/PopoverTitle.js +27 -0
  49. package/dist/popover/trigger/PopoverTrigger.js +44 -0
  50. package/dist/source/alert-dialog/index.d.ts +1 -0
  51. package/dist/source/alert-dialog/index.js +1 -0
  52. package/dist/source/alert-dialog/index.parts.d.ts +6 -0
  53. package/dist/source/alert-dialog/index.parts.js +7 -0
  54. package/dist/source/alert-dialog/popup/AlertDialogPopup.d.ts +10 -0
  55. package/dist/source/alert-dialog/popup/AlertDialogPopup.jsx +11 -0
  56. package/dist/source/core/createControllableSignal.d.ts +5 -4
  57. package/dist/source/core/createControllableSignal.js +4 -3
  58. package/dist/source/core/slot.d.ts +21 -0
  59. package/dist/source/core/slot.js +28 -0
  60. package/dist/source/dialog/close/DialogClose.d.ts +11 -0
  61. package/dist/source/dialog/close/DialogClose.jsx +25 -0
  62. package/dist/source/dialog/description/DialogDescription.d.ts +10 -0
  63. package/dist/source/dialog/description/DialogDescription.jsx +26 -0
  64. package/dist/source/dialog/index.d.ts +1 -0
  65. package/dist/source/dialog/index.js +1 -0
  66. package/dist/source/dialog/index.parts.d.ts +6 -0
  67. package/dist/source/dialog/index.parts.js +6 -0
  68. package/dist/source/dialog/popup/DialogPopup.d.ts +9 -0
  69. package/dist/source/dialog/popup/DialogPopup.jsx +11 -0
  70. package/dist/source/dialog/popup/renderDialogPopup.d.ts +40 -0
  71. package/dist/source/dialog/popup/renderDialogPopup.jsx +73 -0
  72. package/dist/source/dialog/root/DialogRoot.d.ts +20 -0
  73. package/dist/source/dialog/root/DialogRoot.jsx +39 -0
  74. package/dist/source/dialog/root/DialogRootContext.d.ts +32 -0
  75. package/dist/source/dialog/root/DialogRootContext.js +14 -0
  76. package/dist/source/dialog/title/DialogTitle.d.ts +10 -0
  77. package/dist/source/dialog/title/DialogTitle.jsx +26 -0
  78. package/dist/source/dialog/trigger/DialogTrigger.d.ts +10 -0
  79. package/dist/source/dialog/trigger/DialogTrigger.jsx +27 -0
  80. package/dist/source/floating/dismissal/dialogClose.d.ts +26 -0
  81. package/dist/source/floating/dismissal/dialogClose.js +69 -0
  82. package/dist/source/floating/dismissal/escape.d.ts +23 -0
  83. package/dist/source/floating/dismissal/escape.js +47 -0
  84. package/dist/source/floating/dismissal/outsidePress.d.ts +26 -0
  85. package/dist/source/floating/dismissal/outsidePress.js +50 -0
  86. package/dist/source/floating/dismissal/popoverClose.d.ts +28 -0
  87. package/dist/source/floating/dismissal/popoverClose.js +82 -0
  88. package/dist/source/floating/dismissal/reason.d.ts +30 -0
  89. package/dist/source/floating/dismissal/reason.js +1 -0
  90. package/dist/source/floating/focus/initialFocus.d.ts +24 -0
  91. package/dist/source/floating/focus/initialFocus.js +38 -0
  92. package/dist/source/floating/focus/returnFocus.d.ts +27 -0
  93. package/dist/source/floating/focus/returnFocus.js +81 -0
  94. package/dist/source/floating/focus/tabWrap.d.ts +21 -0
  95. package/dist/source/floating/focus/tabWrap.js +69 -0
  96. package/dist/source/floating/focus/tabbable.d.ts +16 -0
  97. package/dist/source/floating/focus/tabbable.js +69 -0
  98. package/dist/source/floating/focus/target.d.ts +8 -0
  99. package/dist/source/floating/focus/target.js +1 -0
  100. package/dist/source/floating/hover/group.d.ts +39 -0
  101. package/dist/source/floating/hover/group.js +38 -0
  102. package/dist/source/floating/hover/hover.d.ts +35 -0
  103. package/dist/source/floating/hover/hover.js +130 -0
  104. package/dist/source/floating/hover/safeArea.d.ts +23 -0
  105. package/dist/source/floating/hover/safeArea.js +67 -0
  106. package/dist/source/floating/positioning/arrow.d.ts +13 -0
  107. package/dist/source/floating/positioning/arrow.js +20 -0
  108. package/dist/source/floating/positioning/createPosition.d.ts +53 -0
  109. package/dist/source/floating/positioning/createPosition.js +138 -0
  110. package/dist/source/floating/positioning/flip.d.ts +1 -0
  111. package/dist/source/floating/positioning/flip.js +3 -0
  112. package/dist/source/floating/positioning/hide.d.ts +8 -0
  113. package/dist/source/floating/positioning/hide.js +12 -0
  114. package/dist/source/floating/positioning/offset.d.ts +1 -0
  115. package/dist/source/floating/positioning/offset.js +3 -0
  116. package/dist/source/floating/positioning/shift.d.ts +1 -0
  117. package/dist/source/floating/positioning/shift.js +2 -0
  118. package/dist/source/floating/positioning/size.d.ts +12 -0
  119. package/dist/source/floating/positioning/size.js +35 -0
  120. package/dist/source/floating/presence/presence.d.ts +24 -0
  121. package/dist/source/floating/presence/presence.js +73 -0
  122. package/dist/source/floating/scroll-lock/scrollLock.d.ts +27 -0
  123. package/dist/source/floating/scroll-lock/scrollLock.js +151 -0
  124. package/dist/source/popover/arrow/PopoverArrow.d.ts +11 -0
  125. package/dist/source/popover/arrow/PopoverArrow.jsx +32 -0
  126. package/dist/source/popover/close/PopoverClose.d.ts +11 -0
  127. package/dist/source/popover/close/PopoverClose.jsx +25 -0
  128. package/dist/source/popover/description/PopoverDescription.d.ts +10 -0
  129. package/dist/source/popover/description/PopoverDescription.jsx +26 -0
  130. package/dist/source/popover/index.d.ts +1 -0
  131. package/dist/source/popover/index.js +1 -0
  132. package/dist/source/popover/index.parts.d.ts +7 -0
  133. package/dist/source/popover/index.parts.js +7 -0
  134. package/dist/source/popover/popup/PopoverPopup.d.ts +47 -0
  135. package/dist/source/popover/popup/PopoverPopup.jsx +120 -0
  136. package/dist/source/popover/popup/PopoverPopupContext.d.ts +14 -0
  137. package/dist/source/popover/popup/PopoverPopupContext.js +11 -0
  138. package/dist/source/popover/root/PopoverRoot.d.ts +20 -0
  139. package/dist/source/popover/root/PopoverRoot.jsx +42 -0
  140. package/dist/source/popover/root/PopoverRootContext.d.ts +39 -0
  141. package/dist/source/popover/root/PopoverRootContext.js +13 -0
  142. package/dist/source/popover/title/PopoverTitle.d.ts +10 -0
  143. package/dist/source/popover/title/PopoverTitle.jsx +26 -0
  144. package/dist/source/popover/trigger/PopoverTrigger.d.ts +12 -0
  145. package/dist/source/popover/trigger/PopoverTrigger.jsx +40 -0
  146. package/dist/source/tooltip/arrow/TooltipArrow.d.ts +11 -0
  147. package/dist/source/tooltip/arrow/TooltipArrow.jsx +32 -0
  148. package/dist/source/tooltip/group/TooltipGroup.d.ts +20 -0
  149. package/dist/source/tooltip/group/TooltipGroup.jsx +18 -0
  150. package/dist/source/tooltip/group/TooltipGroupContext.d.ts +2 -0
  151. package/dist/source/tooltip/group/TooltipGroupContext.js +5 -0
  152. package/dist/source/tooltip/index.d.ts +1 -0
  153. package/dist/source/tooltip/index.js +1 -0
  154. package/dist/source/tooltip/index.parts.d.ts +5 -0
  155. package/dist/source/tooltip/index.parts.js +5 -0
  156. package/dist/source/tooltip/popup/TooltipPopup.d.ts +31 -0
  157. package/dist/source/tooltip/popup/TooltipPopup.jsx +77 -0
  158. package/dist/source/tooltip/popup/TooltipPopupContext.d.ts +14 -0
  159. package/dist/source/tooltip/popup/TooltipPopupContext.js +11 -0
  160. package/dist/source/tooltip/root/TooltipRoot.d.ts +31 -0
  161. package/dist/source/tooltip/root/TooltipRoot.jsx +80 -0
  162. package/dist/source/tooltip/root/TooltipRootContext.d.ts +35 -0
  163. package/dist/source/tooltip/root/TooltipRootContext.js +13 -0
  164. package/dist/source/tooltip/trigger/TooltipTrigger.d.ts +12 -0
  165. package/dist/source/tooltip/trigger/TooltipTrigger.jsx +52 -0
  166. package/dist/tooltip/arrow/TooltipArrow.js +39 -0
  167. package/dist/tooltip/group/TooltipGroup.js +26 -0
  168. package/dist/tooltip/group/TooltipGroupContext.js +4 -0
  169. package/dist/tooltip/index.js +2 -0
  170. package/dist/tooltip/index.parts.js +6 -0
  171. package/dist/tooltip/popup/TooltipPopup.js +86 -0
  172. package/dist/tooltip/popup/TooltipPopupContext.js +10 -0
  173. package/dist/tooltip/root/TooltipRoot.js +79 -0
  174. package/dist/tooltip/root/TooltipRootContext.js +10 -0
  175. package/dist/tooltip/trigger/TooltipTrigger.js +54 -0
  176. package/package.json +24 -1
@@ -0,0 +1,35 @@
1
+ import type { OpenChangeDetails } from "../dismissal/reason.ts";
2
+ import type { HoverGroup } from "./group.ts";
3
+ /**
4
+ * Opens a popup when the pointer rests on its trigger for `delay`
5
+ * milliseconds (default 600), and closes it `closeDelay` milliseconds
6
+ * (default 0) after the pointer has left both the trigger and the popup:
7
+ *
8
+ * ```ts
9
+ * createHover({
10
+ * get trigger() { return trigger; },
11
+ * get popup() { return popup; },
12
+ * get open() { return open(); },
13
+ * get delay() { return props.delay; },
14
+ * onOpenChange: (open, details) => setOpen(open, details),
15
+ * });
16
+ * ```
17
+ *
18
+ * Both report `trigger-hover`. The pointer may cross from the trigger to the
19
+ * popup, and back, through the safe area between them (`trackSafeArea`).
20
+ * A mouse or a pen opens it; a touch does not. While `disabled`, hovering
21
+ * opens nothing. In a `group` (`createHoverGroup`), it opens at once while
22
+ * another member is open, and closes that one. A change of `open`, from
23
+ * here or elsewhere, and disposal clear a pending delay; disposal also
24
+ * takes the popup out of its group. Nothing runs on the server.
25
+ */
26
+ export declare function createHover(options: {
27
+ readonly trigger: HTMLElement | undefined;
28
+ readonly popup: HTMLElement | undefined;
29
+ readonly open: boolean;
30
+ readonly delay?: number;
31
+ readonly closeDelay?: number;
32
+ readonly disabled?: boolean;
33
+ readonly group?: HoverGroup;
34
+ onOpenChange(open: boolean, details: OpenChangeDetails): void;
35
+ }): void;
@@ -0,0 +1,130 @@
1
+ import { createEffect, createSignal, onCleanup, onSettled } from "solid-js";
2
+ import { trackSafeArea } from "./safeArea.js";
3
+ /**
4
+ * Opens a popup when the pointer rests on its trigger for `delay`
5
+ * milliseconds (default 600), and closes it `closeDelay` milliseconds
6
+ * (default 0) after the pointer has left both the trigger and the popup:
7
+ *
8
+ * ```ts
9
+ * createHover({
10
+ * get trigger() { return trigger; },
11
+ * get popup() { return popup; },
12
+ * get open() { return open(); },
13
+ * get delay() { return props.delay; },
14
+ * onOpenChange: (open, details) => setOpen(open, details),
15
+ * });
16
+ * ```
17
+ *
18
+ * Both report `trigger-hover`. The pointer may cross from the trigger to the
19
+ * popup, and back, through the safe area between them (`trackSafeArea`).
20
+ * A mouse or a pen opens it; a touch does not. While `disabled`, hovering
21
+ * opens nothing. In a `group` (`createHoverGroup`), it opens at once while
22
+ * another member is open, and closes that one. A change of `open`, from
23
+ * here or elsewhere, and disposal clear a pending delay; disposal also
24
+ * takes the popup out of its group. Nothing runs on the server.
25
+ */
26
+ export function createHover(options) {
27
+ let timer;
28
+ let stopSafeArea;
29
+ // Where the pointer last was over the trigger or the popup.
30
+ let last;
31
+ const cancel = () => {
32
+ clearTimeout(timer);
33
+ timer = undefined;
34
+ stopSafeArea?.();
35
+ stopSafeArea = undefined;
36
+ };
37
+ /** Opens or closes after `delay`, or now if it is 0. */
38
+ const schedule = (open, event, delay) => {
39
+ cancel();
40
+ const set = () => {
41
+ timer = undefined;
42
+ // Disabled, or removed, while the delay was pending.
43
+ if (open && (options.disabled || !options.trigger?.isConnected))
44
+ return;
45
+ if (open)
46
+ options.group?.opened(member, event);
47
+ options.onOpenChange(open, { reason: "trigger-hover", event });
48
+ };
49
+ if (delay > 0)
50
+ timer = setTimeout(set, delay);
51
+ else
52
+ set();
53
+ };
54
+ const close = (event) => schedule(false, event, options.closeDelay ?? 0);
55
+ const member = {
56
+ close: (event) => schedule(false, event, 0),
57
+ };
58
+ onCleanup(() => {
59
+ cancel();
60
+ // Closed with its owner: an open popup that the group still counted
61
+ // would keep the group open, and be closed by the next member.
62
+ options.group?.closed(member);
63
+ });
64
+ // A touch fires pointer events too, which are ignored, as `trackSafeArea`
65
+ // ignores them; the mouse events a browser emulates after it are not
66
+ // pointer events, so they never reach here.
67
+ const listen = (element, other, onEnter) => {
68
+ const enter = (event) => {
69
+ if (event.pointerType === "touch")
70
+ return;
71
+ move(event);
72
+ cancel();
73
+ onEnter(event);
74
+ };
75
+ const move = (event) => {
76
+ if (event.pointerType !== "touch")
77
+ last = event;
78
+ };
79
+ const leave = (event) => {
80
+ if (event.pointerType === "touch")
81
+ return;
82
+ cancel();
83
+ if (!options.open)
84
+ return;
85
+ const to = other();
86
+ // A leave at the pointer's last position is the page moving under a
87
+ // still pointer, such as a scroll, and one with nothing to go to is
88
+ // the pointer leaving the window: no move follows to end a safe area.
89
+ if (to &&
90
+ event.relatedTarget &&
91
+ (event.clientX !== last?.clientX || event.clientY !== last.clientY))
92
+ stopSafeArea = trackSafeArea(event, to, close);
93
+ else
94
+ close(event);
95
+ };
96
+ element.addEventListener("pointerenter", enter);
97
+ element.addEventListener("pointermove", move);
98
+ element.addEventListener("pointerleave", leave);
99
+ return () => {
100
+ element.removeEventListener("pointerenter", enter);
101
+ element.removeEventListener("pointermove", move);
102
+ element.removeEventListener("pointerleave", leave);
103
+ };
104
+ };
105
+ // The trigger usually comes from a ref, assigned while the component
106
+ // renders, after an effect's first compute: it is read once mounted.
107
+ const [mounted, setMounted] = createSignal(false);
108
+ onSettled(() => {
109
+ setMounted(true);
110
+ });
111
+ createEffect(() => mounted() && options.trigger, (trigger) => {
112
+ if (!trigger)
113
+ return;
114
+ return listen(trigger, () => options.popup, (event) => {
115
+ if (options.open || options.disabled)
116
+ return;
117
+ const group = options.group;
118
+ schedule(true, event, (group ? group.delay(options.delay) : options.delay) ?? 600);
119
+ });
120
+ });
121
+ createEffect(() => options.open, (open) => {
122
+ cancel();
123
+ if (!open)
124
+ options.group?.closed(member);
125
+ const popup = options.popup;
126
+ if (!open || !popup)
127
+ return;
128
+ return listen(popup, () => options.trigger, () => { });
129
+ });
130
+ }
@@ -0,0 +1,23 @@
1
+ /** A point in the viewport, as a pointer event gives it. */
2
+ export interface ViewportPoint {
3
+ readonly clientX: number;
4
+ readonly clientY: number;
5
+ }
6
+ /**
7
+ * Follows the pointer once it has left one element on its way to another,
8
+ * such as from a trigger to its popup across the gap between them, and
9
+ * calls `onLeave` with the event once it moves out of the way:
10
+ *
11
+ * ```ts
12
+ * trigger.addEventListener("pointerleave", (event) => {
13
+ * stop = trackSafeArea(event, popup, () => close());
14
+ * });
15
+ * ```
16
+ *
17
+ * The safe area is every point on a line from `exit`, where the pointer
18
+ * left, to the box of `element`, read again on each move. A move outside
19
+ * it, or past the element, ends it, and so does the pointer leaving the
20
+ * window. A touch is ignored. Returns what stops following; call it when
21
+ * the pointer reaches the element. Plain DOM, with no timers and no Solid.
22
+ */
23
+ export declare function trackSafeArea(exit: ViewportPoint, element: Element, onLeave: (event: PointerEvent) => void): () => void;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Follows the pointer once it has left one element on its way to another,
3
+ * such as from a trigger to its popup across the gap between them, and
4
+ * calls `onLeave` with the event once it moves out of the way:
5
+ *
6
+ * ```ts
7
+ * trigger.addEventListener("pointerleave", (event) => {
8
+ * stop = trackSafeArea(event, popup, () => close());
9
+ * });
10
+ * ```
11
+ *
12
+ * The safe area is every point on a line from `exit`, where the pointer
13
+ * left, to the box of `element`, read again on each move. A move outside
14
+ * it, or past the element, ends it, and so does the pointer leaving the
15
+ * window. A touch is ignored. Returns what stops following; call it when
16
+ * the pointer reaches the element. Plain DOM, with no timers and no Solid.
17
+ */
18
+ export function trackSafeArea(exit, element, onLeave) {
19
+ const check = (event) => {
20
+ if (
21
+ // A touch, such as a tap elsewhere while the mouse is in the gap.
22
+ event.pointerType === "touch" ||
23
+ (event.type === "pointerout"
24
+ ? event.relatedTarget
25
+ : inSafeArea(exit, event, element.getBoundingClientRect())))
26
+ return;
27
+ stop();
28
+ onLeave(event);
29
+ };
30
+ const stop = () => {
31
+ document.removeEventListener("pointermove", check);
32
+ document.removeEventListener("pointerout", check);
33
+ };
34
+ document.addEventListener("pointermove", check);
35
+ // Out of the window: a `pointerout` with nothing to go to.
36
+ document.addEventListener("pointerout", check);
37
+ return stop;
38
+ }
39
+ /**
40
+ * Whether `point` is on a line from `exit` to `box`: whether the ray from
41
+ * `exit` through `point` meets the box at or beyond `point`. Each axis
42
+ * gives the stretch of the ray, in multiples of `exit` to `point`, that is
43
+ * within the box's extent on it; the point is inside if the stretches of
44
+ * both axes overlap beyond 1.
45
+ */
46
+ function inSafeArea(exit, point, box) {
47
+ let from = 1;
48
+ let to = Infinity;
49
+ for (const [start, end, min, max] of [
50
+ [exit.clientX, point.clientX, box.left, box.right],
51
+ [exit.clientY, point.clientY, box.top, box.bottom],
52
+ ]) {
53
+ const step = end - start;
54
+ if (step) {
55
+ const a = (min - start) / step;
56
+ const b = (max - start) / step;
57
+ from = Math.max(from, Math.min(a, b));
58
+ to = Math.min(to, Math.max(a, b));
59
+ }
60
+ else if (start < min || start > max) {
61
+ // Still on this axis, and outside the box on it, unless the pointer
62
+ // has not moved at all: the move that left is at `exit` itself.
63
+ return point.clientX === exit.clientX && point.clientY === exit.clientY;
64
+ }
65
+ }
66
+ return from <= to;
67
+ }
@@ -0,0 +1,13 @@
1
+ import type { Padding } from "@floating-ui/dom";
2
+ import type { PositionMiddleware } from "./createPosition.ts";
3
+ /**
4
+ * Places `element`, an absolutely positioned child of the popup, so that it
5
+ * points at the anchor's center: its `left` on the top and bottom sides,
6
+ * its `top` on the left and right. `padding` keeps it that far from the
7
+ * popup's corners. The other offset, out of the popup's edge, is the
8
+ * consumer's CSS, by `data-side`. Goes after `shift`.
9
+ */
10
+ export declare function arrow(options: {
11
+ element: HTMLElement;
12
+ padding?: Padding;
13
+ }): PositionMiddleware;
@@ -0,0 +1,20 @@
1
+ import { arrow as measure } from "@floating-ui/dom";
2
+ /**
3
+ * Places `element`, an absolutely positioned child of the popup, so that it
4
+ * points at the anchor's center: its `left` on the top and bottom sides,
5
+ * its `top` on the left and right. `padding` keeps it that far from the
6
+ * popup's corners. The other offset, out of the popup's edge, is the
7
+ * consumer's CSS, by `data-side`. Goes after `shift`.
8
+ */
9
+ export function arrow(options) {
10
+ return {
11
+ ...measure(options),
12
+ write(_popup, data) {
13
+ // Set by this middleware, which ran before its write.
14
+ const { x, y } = data.arrow;
15
+ const style = options.element.style;
16
+ style.left = x == null ? "" : `${x}px`;
17
+ style.top = y == null ? "" : `${y}px`;
18
+ },
19
+ };
20
+ }
@@ -0,0 +1,53 @@
1
+ import type { Alignment, Middleware, MiddlewareData, Side, VirtualElement } from "@floating-ui/dom";
2
+ import type { Accessor } from "solid-js";
3
+ export type { Side, VirtualElement };
4
+ /** Where along the anchor's side the popup sits. */
5
+ export type Align = Alignment | "center";
6
+ /**
7
+ * A middleware of this folder, or of `@floating-ui/dom` through it. `write`
8
+ * puts the middleware's result on the popup, and runs only for a result
9
+ * that is not stale.
10
+ */
11
+ export type PositionMiddleware = Middleware & {
12
+ write?(popup: HTMLElement, data: MiddlewareData): void;
13
+ };
14
+ /** A placement that was written, as reactive state. */
15
+ export interface Position {
16
+ readonly side: Side;
17
+ readonly align: Align;
18
+ /** The popup's left and top in the viewport, as written. */
19
+ readonly x: number;
20
+ readonly y: number;
21
+ readonly middlewareData: MiddlewareData;
22
+ }
23
+ /**
24
+ * Places `popup` against `anchor` while `open`, with the strategy `fixed` of
25
+ * a popover in the top layer. Pass the options as getters, so a change to
26
+ * one restarts the placement:
27
+ *
28
+ * ```ts
29
+ * const position = createPosition({
30
+ * get anchor() { return trigger; },
31
+ * get popup() { return popup; },
32
+ * get open() { return open(); },
33
+ * get side() { return props.side; },
34
+ * get middleware() { return [offset(8), flip(), shift()]; },
35
+ * });
36
+ * ```
37
+ *
38
+ * Show the popup before placing it: create the effect that calls
39
+ * `showPopover()` before calling this, so that the popup can be measured,
40
+ * and is placed before the browser paints it. Writes on the popup its
41
+ * `inset`, rounded to device pixels, `data-side`, `data-align`,
42
+ * `--anchor-width`, `--anchor-height` and `--transform-origin`, then what
43
+ * each middleware writes. Returns the last placement written. Nothing runs
44
+ * on the server.
45
+ */
46
+ export declare function createPosition(options: {
47
+ readonly anchor: Element | VirtualElement | undefined;
48
+ readonly popup: HTMLElement | undefined;
49
+ readonly open: boolean;
50
+ readonly side?: Side;
51
+ readonly align?: Align;
52
+ readonly middleware?: PositionMiddleware[];
53
+ }): Accessor<Position | undefined>;
@@ -0,0 +1,138 @@
1
+ import { autoUpdate, computePosition } from "@floating-ui/dom";
2
+ import { createEffect, createSignal, onSettled } from "solid-js";
3
+ /**
4
+ * Places `popup` against `anchor` while `open`, with the strategy `fixed` of
5
+ * a popover in the top layer. Pass the options as getters, so a change to
6
+ * one restarts the placement:
7
+ *
8
+ * ```ts
9
+ * const position = createPosition({
10
+ * get anchor() { return trigger; },
11
+ * get popup() { return popup; },
12
+ * get open() { return open(); },
13
+ * get side() { return props.side; },
14
+ * get middleware() { return [offset(8), flip(), shift()]; },
15
+ * });
16
+ * ```
17
+ *
18
+ * Show the popup before placing it: create the effect that calls
19
+ * `showPopover()` before calling this, so that the popup can be measured,
20
+ * and is placed before the browser paints it. Writes on the popup its
21
+ * `inset`, rounded to device pixels, `data-side`, `data-align`,
22
+ * `--anchor-width`, `--anchor-height` and `--transform-origin`, then what
23
+ * each middleware writes. Returns the last placement written. Nothing runs
24
+ * on the server.
25
+ */
26
+ export function createPosition(options) {
27
+ const [position, setPosition] = createSignal();
28
+ // The elements usually come from refs, which are assigned while the
29
+ // component renders, after this effect's first compute. Placing starts
30
+ // once the owner has mounted.
31
+ const [mounted, setMounted] = createSignal(false);
32
+ onSettled(() => {
33
+ setMounted(true);
34
+ });
35
+ // Each placement takes the next number, and closing takes one too. A
36
+ // result is written only if it still has the latest number, so a
37
+ // placement that resolves late, such as one that flipped and measured
38
+ // twice, never overwrites a newer one.
39
+ let generation = 0;
40
+ // The newest result written while open, to write again over what a
41
+ // stale run's middleware wrote while it measured, as `size` does.
42
+ let written;
43
+ const update = (run) => {
44
+ const current = ++generation;
45
+ void computePosition(run.anchor, run.popup, {
46
+ placement: run.placement,
47
+ strategy: "fixed",
48
+ middleware: [...run.middleware, anchorBox],
49
+ }).then((result) => {
50
+ if (current !== generation) {
51
+ if (written && written.generation > current)
52
+ written.write();
53
+ return;
54
+ }
55
+ const [side, align = "center"] = result.placement.split("-");
56
+ const x = roundToDevicePixels(result.x);
57
+ const y = roundToDevicePixels(result.y);
58
+ const popup = run.popup;
59
+ const anchor = result.middlewareData.anchor;
60
+ const write = () => {
61
+ popup.style.inset = `${y}px auto auto ${x}px`;
62
+ popup.setAttribute("data-side", side);
63
+ popup.setAttribute("data-align", align);
64
+ popup.style.setProperty("--anchor-width", `${anchor.width}px`);
65
+ popup.style.setProperty("--anchor-height", `${anchor.height}px`);
66
+ popup.style.setProperty("--transform-origin", anchor.origin);
67
+ for (const middleware of run.middleware)
68
+ middleware.write?.(popup, result.middlewareData);
69
+ };
70
+ write();
71
+ written = { generation: current, write };
72
+ setPosition({
73
+ side,
74
+ align,
75
+ x,
76
+ y,
77
+ middlewareData: result.middlewareData,
78
+ });
79
+ });
80
+ };
81
+ createEffect(() => {
82
+ if (!mounted() || !options.open)
83
+ return undefined;
84
+ const { anchor, popup } = options;
85
+ if (!anchor || !popup)
86
+ return undefined;
87
+ const side = options.side ?? "bottom";
88
+ const align = options.align ?? "center";
89
+ return {
90
+ anchor,
91
+ popup,
92
+ placement: align === "center" ? side : `${side}-${align}`,
93
+ middleware: options.middleware ?? [],
94
+ };
95
+ }, (run) => {
96
+ if (!run)
97
+ return;
98
+ const stop = autoUpdate(run.anchor, run.popup, () => update(run));
99
+ return () => {
100
+ generation++;
101
+ written = undefined;
102
+ stop();
103
+ };
104
+ });
105
+ return position;
106
+ }
107
+ /**
108
+ * Runs last, after every middleware has moved the popup: the anchor's size,
109
+ * and the point of the popup nearest the anchor's center, on the edge that
110
+ * faces the anchor, for `transform-origin`.
111
+ */
112
+ const anchorBox = {
113
+ name: "anchor",
114
+ fn({ x, y, placement, rects: { reference, floating } }) {
115
+ const side = placement.split("-")[0];
116
+ const clamp = (value, max) => Math.min(Math.max(value, 0), max);
117
+ const centerX = clamp(reference.x + reference.width / 2 - x, floating.width);
118
+ const centerY = clamp(reference.y + reference.height / 2 - y, floating.height);
119
+ const [originX, originY] = side === "top"
120
+ ? [centerX, floating.height]
121
+ : side === "bottom"
122
+ ? [centerX, 0]
123
+ : side === "left"
124
+ ? [floating.width, centerY]
125
+ : [0, centerY];
126
+ const data = {
127
+ width: reference.width,
128
+ height: reference.height,
129
+ origin: `${originX}px ${originY}px`,
130
+ };
131
+ return { data };
132
+ },
133
+ };
134
+ /** Snaps to the device's pixel grid, so the popup's edges stay sharp. */
135
+ function roundToDevicePixels(value) {
136
+ const ratio = window.devicePixelRatio || 1;
137
+ return Math.round(value * ratio) / ratio;
138
+ }
@@ -0,0 +1 @@
1
+ export { flip } from "@floating-ui/dom";
@@ -0,0 +1,3 @@
1
+ // Moves the popup to the opposite side of the anchor when it does not fit
2
+ // on its own. Goes before `shift`.
3
+ export { flip } from "@floating-ui/dom";
@@ -0,0 +1,8 @@
1
+ import type { HideOptions } from "@floating-ui/dom";
2
+ import type { PositionMiddleware } from "./createPosition.ts";
3
+ /**
4
+ * Sets `data-anchor-hidden` on the popup while its anchor is scrolled out
5
+ * of view, clipped by a scrolling ancestor, so that the consumer's CSS can
6
+ * hide the popup too. Goes last.
7
+ */
8
+ export declare function hide(options?: Omit<HideOptions, "strategy">): PositionMiddleware;
@@ -0,0 +1,12 @@
1
+ import { hide as measure } from "@floating-ui/dom";
2
+ /**
3
+ * Sets `data-anchor-hidden` on the popup while its anchor is scrolled out
4
+ * of view, clipped by a scrolling ancestor, so that the consumer's CSS can
5
+ * hide the popup too. Goes last.
6
+ */
7
+ export function hide(options = {}) {
8
+ return {
9
+ ...measure(options),
10
+ write: (popup, data) => popup.toggleAttribute("data-anchor-hidden", !!data.hide?.referenceHidden),
11
+ };
12
+ }
@@ -0,0 +1 @@
1
+ export { offset } from "@floating-ui/dom";
@@ -0,0 +1,3 @@
1
+ // The gap between anchor and popup (`mainAxis`), and a shift along the
2
+ // anchor's side (`alignmentAxis`). Goes first in the middleware list.
3
+ export { offset } from "@floating-ui/dom";
@@ -0,0 +1 @@
1
+ export { shift } from "@floating-ui/dom";
@@ -0,0 +1,2 @@
1
+ // Slides the popup along the anchor's side to keep it in view.
2
+ export { shift } from "@floating-ui/dom";
@@ -0,0 +1,12 @@
1
+ import type { SizeOptions } from "@floating-ui/dom";
2
+ import type { PositionMiddleware } from "./createPosition.ts";
3
+ /**
4
+ * Writes the space between the anchor and the viewport's edges, less
5
+ * `padding`, as `--available-width` and `--available-height`, for the
6
+ * popup's `max-width` and `max-height`. Goes after `flip` and `shift`.
7
+ *
8
+ * It writes them while it measures as well, so that a popup they limit is
9
+ * measured at its limited size in the same placement. If that placement
10
+ * turns out stale, `createPosition` writes the newest one again.
11
+ */
12
+ export declare function size(options?: Omit<SizeOptions, "apply">): PositionMiddleware;
@@ -0,0 +1,35 @@
1
+ import { size as measure } from "@floating-ui/dom";
2
+ function writeAvailable(popup, available) {
3
+ popup.style.setProperty("--available-width", `${available.width}px`);
4
+ popup.style.setProperty("--available-height", `${available.height}px`);
5
+ }
6
+ /**
7
+ * Writes the space between the anchor and the viewport's edges, less
8
+ * `padding`, as `--available-width` and `--available-height`, for the
9
+ * popup's `max-width` and `max-height`. Goes after `flip` and `shift`.
10
+ *
11
+ * It writes them while it measures as well, so that a popup they limit is
12
+ * measured at its limited size in the same placement. If that placement
13
+ * turns out stale, `createPosition` writes the newest one again.
14
+ */
15
+ export function size(options = {}) {
16
+ return {
17
+ name: "size",
18
+ options,
19
+ async fn(state) {
20
+ let available;
21
+ const result = await measure({
22
+ ...options,
23
+ apply({ elements, availableWidth, availableHeight }) {
24
+ available = {
25
+ width: Math.max(0, availableWidth),
26
+ height: Math.max(0, availableHeight),
27
+ };
28
+ writeAvailable(elements.floating, available);
29
+ },
30
+ }).fn(state);
31
+ return { ...result, data: available };
32
+ },
33
+ write: (popup, data) => writeAvailable(popup, data.size),
34
+ };
35
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Shows a popup when `open` turns true, and hides it once its exit
3
+ * animations have ended after `open` turns false:
4
+ *
5
+ * ```ts
6
+ * createPresence({
7
+ * get popup() { return dialog; },
8
+ * get open() { return open(); },
9
+ * });
10
+ * ```
11
+ *
12
+ * A `<dialog>` is shown with `showModal()` and hidden with `close()`; any
13
+ * other element is a popover, shown with `showPopover()` and hidden with
14
+ * `hidePopover()`. On closing it waits for the animations and transitions
15
+ * running in the popup, its descendants and its `::backdrop`, the ones its
16
+ * `data-closed` starts, and hides it at once if none is running; infinite
17
+ * and paused ones do not count. Meanwhile the popup is `inert`, unless it is
18
+ * a popover that holds focus, which only returns focus to its trigger from
19
+ * inside. Opening again cancels the exit. Nothing runs on the server.
20
+ */
21
+ export declare function createPresence(options: {
22
+ readonly popup: HTMLElement | undefined;
23
+ readonly open: boolean;
24
+ }): void;