menhir 0.1.2 → 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 (155) 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/presence/presence.js +57 -0
  31. package/dist/floating/scroll-lock/scrollLock.js +125 -0
  32. package/dist/popover/arrow/PopoverArrow.js +39 -0
  33. package/dist/popover/close/PopoverClose.js +33 -0
  34. package/dist/popover/description/PopoverDescription.js +27 -0
  35. package/dist/popover/index.js +2 -0
  36. package/dist/popover/index.parts.js +8 -0
  37. package/dist/popover/popup/PopoverPopup.js +116 -0
  38. package/dist/popover/popup/PopoverPopupContext.js +10 -0
  39. package/dist/popover/root/PopoverRoot.js +50 -0
  40. package/dist/popover/root/PopoverRootContext.js +10 -0
  41. package/dist/popover/title/PopoverTitle.js +27 -0
  42. package/dist/popover/trigger/PopoverTrigger.js +44 -0
  43. package/dist/source/alert-dialog/index.d.ts +1 -0
  44. package/dist/source/alert-dialog/index.js +1 -0
  45. package/dist/source/alert-dialog/index.parts.d.ts +6 -0
  46. package/dist/source/alert-dialog/index.parts.js +7 -0
  47. package/dist/source/alert-dialog/popup/AlertDialogPopup.d.ts +10 -0
  48. package/dist/source/alert-dialog/popup/AlertDialogPopup.jsx +11 -0
  49. package/dist/source/core/createControllableSignal.d.ts +5 -4
  50. package/dist/source/core/createControllableSignal.js +4 -3
  51. package/dist/source/core/slot.d.ts +21 -0
  52. package/dist/source/core/slot.js +28 -0
  53. package/dist/source/dialog/close/DialogClose.d.ts +11 -0
  54. package/dist/source/dialog/close/DialogClose.jsx +25 -0
  55. package/dist/source/dialog/description/DialogDescription.d.ts +10 -0
  56. package/dist/source/dialog/description/DialogDescription.jsx +26 -0
  57. package/dist/source/dialog/index.d.ts +1 -0
  58. package/dist/source/dialog/index.js +1 -0
  59. package/dist/source/dialog/index.parts.d.ts +6 -0
  60. package/dist/source/dialog/index.parts.js +6 -0
  61. package/dist/source/dialog/popup/DialogPopup.d.ts +9 -0
  62. package/dist/source/dialog/popup/DialogPopup.jsx +11 -0
  63. package/dist/source/dialog/popup/renderDialogPopup.d.ts +40 -0
  64. package/dist/source/dialog/popup/renderDialogPopup.jsx +73 -0
  65. package/dist/source/dialog/root/DialogRoot.d.ts +20 -0
  66. package/dist/source/dialog/root/DialogRoot.jsx +39 -0
  67. package/dist/source/dialog/root/DialogRootContext.d.ts +32 -0
  68. package/dist/source/dialog/root/DialogRootContext.js +14 -0
  69. package/dist/source/dialog/title/DialogTitle.d.ts +10 -0
  70. package/dist/source/dialog/title/DialogTitle.jsx +26 -0
  71. package/dist/source/dialog/trigger/DialogTrigger.d.ts +10 -0
  72. package/dist/source/dialog/trigger/DialogTrigger.jsx +27 -0
  73. package/dist/source/floating/dismissal/dialogClose.d.ts +26 -0
  74. package/dist/source/floating/dismissal/dialogClose.js +69 -0
  75. package/dist/source/floating/dismissal/escape.d.ts +23 -0
  76. package/dist/source/floating/dismissal/escape.js +47 -0
  77. package/dist/source/floating/dismissal/outsidePress.d.ts +26 -0
  78. package/dist/source/floating/dismissal/outsidePress.js +50 -0
  79. package/dist/source/floating/dismissal/popoverClose.d.ts +28 -0
  80. package/dist/source/floating/dismissal/popoverClose.js +82 -0
  81. package/dist/source/floating/dismissal/reason.d.ts +30 -0
  82. package/dist/source/floating/dismissal/reason.js +1 -0
  83. package/dist/source/floating/focus/initialFocus.d.ts +24 -0
  84. package/dist/source/floating/focus/initialFocus.js +38 -0
  85. package/dist/source/floating/focus/returnFocus.d.ts +27 -0
  86. package/dist/source/floating/focus/returnFocus.js +81 -0
  87. package/dist/source/floating/focus/tabWrap.d.ts +21 -0
  88. package/dist/source/floating/focus/tabWrap.js +69 -0
  89. package/dist/source/floating/focus/tabbable.d.ts +16 -0
  90. package/dist/source/floating/focus/tabbable.js +69 -0
  91. package/dist/source/floating/focus/target.d.ts +8 -0
  92. package/dist/source/floating/focus/target.js +1 -0
  93. package/dist/source/floating/hover/group.d.ts +39 -0
  94. package/dist/source/floating/hover/group.js +38 -0
  95. package/dist/source/floating/hover/hover.d.ts +35 -0
  96. package/dist/source/floating/hover/hover.js +130 -0
  97. package/dist/source/floating/hover/safeArea.d.ts +23 -0
  98. package/dist/source/floating/hover/safeArea.js +67 -0
  99. package/dist/source/floating/presence/presence.d.ts +24 -0
  100. package/dist/source/floating/presence/presence.js +73 -0
  101. package/dist/source/floating/scroll-lock/scrollLock.d.ts +27 -0
  102. package/dist/source/floating/scroll-lock/scrollLock.js +151 -0
  103. package/dist/source/popover/arrow/PopoverArrow.d.ts +11 -0
  104. package/dist/source/popover/arrow/PopoverArrow.jsx +32 -0
  105. package/dist/source/popover/close/PopoverClose.d.ts +11 -0
  106. package/dist/source/popover/close/PopoverClose.jsx +25 -0
  107. package/dist/source/popover/description/PopoverDescription.d.ts +10 -0
  108. package/dist/source/popover/description/PopoverDescription.jsx +26 -0
  109. package/dist/source/popover/index.d.ts +1 -0
  110. package/dist/source/popover/index.js +1 -0
  111. package/dist/source/popover/index.parts.d.ts +7 -0
  112. package/dist/source/popover/index.parts.js +7 -0
  113. package/dist/source/popover/popup/PopoverPopup.d.ts +47 -0
  114. package/dist/source/popover/popup/PopoverPopup.jsx +120 -0
  115. package/dist/source/popover/popup/PopoverPopupContext.d.ts +14 -0
  116. package/dist/source/popover/popup/PopoverPopupContext.js +11 -0
  117. package/dist/source/popover/root/PopoverRoot.d.ts +20 -0
  118. package/dist/source/popover/root/PopoverRoot.jsx +42 -0
  119. package/dist/source/popover/root/PopoverRootContext.d.ts +39 -0
  120. package/dist/source/popover/root/PopoverRootContext.js +13 -0
  121. package/dist/source/popover/title/PopoverTitle.d.ts +10 -0
  122. package/dist/source/popover/title/PopoverTitle.jsx +26 -0
  123. package/dist/source/popover/trigger/PopoverTrigger.d.ts +12 -0
  124. package/dist/source/popover/trigger/PopoverTrigger.jsx +40 -0
  125. package/dist/source/tooltip/arrow/TooltipArrow.d.ts +11 -0
  126. package/dist/source/tooltip/arrow/TooltipArrow.jsx +32 -0
  127. package/dist/source/tooltip/group/TooltipGroup.d.ts +20 -0
  128. package/dist/source/tooltip/group/TooltipGroup.jsx +18 -0
  129. package/dist/source/tooltip/group/TooltipGroupContext.d.ts +2 -0
  130. package/dist/source/tooltip/group/TooltipGroupContext.js +5 -0
  131. package/dist/source/tooltip/index.d.ts +1 -0
  132. package/dist/source/tooltip/index.js +1 -0
  133. package/dist/source/tooltip/index.parts.d.ts +5 -0
  134. package/dist/source/tooltip/index.parts.js +5 -0
  135. package/dist/source/tooltip/popup/TooltipPopup.d.ts +31 -0
  136. package/dist/source/tooltip/popup/TooltipPopup.jsx +77 -0
  137. package/dist/source/tooltip/popup/TooltipPopupContext.d.ts +14 -0
  138. package/dist/source/tooltip/popup/TooltipPopupContext.js +11 -0
  139. package/dist/source/tooltip/root/TooltipRoot.d.ts +31 -0
  140. package/dist/source/tooltip/root/TooltipRoot.jsx +80 -0
  141. package/dist/source/tooltip/root/TooltipRootContext.d.ts +35 -0
  142. package/dist/source/tooltip/root/TooltipRootContext.js +13 -0
  143. package/dist/source/tooltip/trigger/TooltipTrigger.d.ts +12 -0
  144. package/dist/source/tooltip/trigger/TooltipTrigger.jsx +52 -0
  145. package/dist/tooltip/arrow/TooltipArrow.js +39 -0
  146. package/dist/tooltip/group/TooltipGroup.js +26 -0
  147. package/dist/tooltip/group/TooltipGroupContext.js +4 -0
  148. package/dist/tooltip/index.js +2 -0
  149. package/dist/tooltip/index.parts.js +6 -0
  150. package/dist/tooltip/popup/TooltipPopup.js +86 -0
  151. package/dist/tooltip/popup/TooltipPopupContext.js +10 -0
  152. package/dist/tooltip/root/TooltipRoot.js +79 -0
  153. package/dist/tooltip/root/TooltipRootContext.js +10 -0
  154. package/dist/tooltip/trigger/TooltipTrigger.js +54 -0
  155. package/package.json +21 -1
@@ -0,0 +1,20 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { OpenChangeDetails } from "../../floating/dismissal/reason.ts";
3
+ export interface DialogRootProps {
4
+ /** Whether the dialog is open, when controlled. */
5
+ open?: boolean;
6
+ /** Whether the dialog is open at first, when uncontrolled. */
7
+ defaultOpen?: boolean;
8
+ /**
9
+ * Called with the new value, and why, when the user opens or closes the
10
+ * dialog. When controlled, the dialog changes only if `open` does.
11
+ */
12
+ onOpenChange?: (open: boolean, details: OpenChangeDetails) => void;
13
+ children?: JSX.Element;
14
+ }
15
+ /**
16
+ * A modal dialog: holds whether it is open and shares it with
17
+ * `Dialog.Trigger`, `Dialog.Popup` and the parts inside it. Renders no
18
+ * element of its own.
19
+ */
20
+ export declare function DialogRoot(props: DialogRootProps): JSX.Element;
@@ -0,0 +1,39 @@
1
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
2
+ import { createIdSlot } from "../../core/slot.js";
3
+ import { DialogRootContext } from "./DialogRootContext.js";
4
+ /**
5
+ * A modal dialog: holds whether it is open and shares it with
6
+ * `Dialog.Trigger`, `Dialog.Popup` and the parts inside it. Renders no
7
+ * element of its own.
8
+ */
9
+ export function DialogRoot(props) {
10
+ const [open, setOpen] = createControllableSignal({
11
+ get value() {
12
+ return props.open;
13
+ },
14
+ get defaultValue() {
15
+ return props.defaultOpen ?? false;
16
+ },
17
+ onChange: (value, details) => props.onOpenChange?.(value, details),
18
+ });
19
+ const attributes = {
20
+ get "data-open"() {
21
+ return open() ? "" : undefined;
22
+ },
23
+ get "data-closed"() {
24
+ return open() ? undefined : "";
25
+ },
26
+ };
27
+ const [titleId, registerTitleId] = createIdSlot();
28
+ const [descriptionId, registerDescriptionId] = createIdSlot();
29
+ const context = {
30
+ open,
31
+ setOpen,
32
+ titleId,
33
+ registerTitleId,
34
+ descriptionId,
35
+ registerDescriptionId,
36
+ attributes,
37
+ };
38
+ return (<DialogRootContext value={context}>{props.children}</DialogRootContext>);
39
+ }
@@ -0,0 +1,32 @@
1
+ import type { OpenChangeDetails } from "../../floating/dismissal/reason.ts";
2
+ /**
3
+ * The dialog's state as `data-*` attributes, each `""` when it applies and
4
+ * absent otherwise: one of `data-open` and `data-closed`. Every part merges
5
+ * the same object, so its getters are read where each element uses them.
6
+ */
7
+ export type DialogStateAttributes = {
8
+ readonly "data-open": "" | undefined;
9
+ readonly "data-closed": "" | undefined;
10
+ };
11
+ /** What the Root shares with its parts. */
12
+ export interface DialogRootContextValue {
13
+ /** Whether the dialog is open. */
14
+ open(): boolean;
15
+ /** Asks to open or close the dialog, saying why. */
16
+ setOpen(open: boolean, details: OpenChangeDetails): void;
17
+ /** The `id` of the rendered Title, which the Popup names, if any. */
18
+ titleId(): string | undefined;
19
+ /**
20
+ * Registers the `id` of a mounted Title, as a getter. Returns what
21
+ * unregisters it, for the Title's disposal.
22
+ */
23
+ registerTitleId(id: () => string | false): () => void;
24
+ /** The `id` of the rendered Description, which the Popup names, if any. */
25
+ descriptionId(): string | undefined;
26
+ /** As `registerTitleId`, for the Description. */
27
+ registerDescriptionId(id: () => string | false): () => void;
28
+ /** The `data-*` attributes every part carries. */
29
+ readonly attributes: DialogStateAttributes;
30
+ }
31
+ export declare const DialogRootContext: import("solid-js").Context<DialogRootContextValue | null>;
32
+ export declare function useDialogRootContext(): DialogRootContextValue;
@@ -0,0 +1,14 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside a Root, so that the part can name what is missing. Solid's
4
+ // own error for a context with no provider and no default does not.
5
+ export const DialogRootContext = createContext(null);
6
+ export function useDialogRootContext() {
7
+ const context = useContext(DialogRootContext);
8
+ // Production builds drop the check, and its message, with `isDev`.
9
+ if (isDev && !context) {
10
+ // Alert Dialog's parts are Dialog's (#32), and cannot tell which.
11
+ throw new Error("[menhir] Dialog parts must be placed within <Dialog.Root>, and Alert Dialog parts within <AlertDialog.Root>.");
12
+ }
13
+ return context;
14
+ }
@@ -0,0 +1,10 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface DialogTitleProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The dialog's title, which names the Popup in `aria-labelledby`: an
8
+ * `<h2>`. Place it inside `Dialog.Popup`.
9
+ */
10
+ export declare function DialogTitle(props: DialogTitleProps): JSX.Element;
@@ -0,0 +1,26 @@
1
+ import { createUniqueId, merge, omit, onSettled } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useDialogRootContext } from "../root/DialogRootContext.js";
5
+ /**
6
+ * The dialog's title, which names the Popup in `aria-labelledby`: an
7
+ * `<h2>`. Place it inside `Dialog.Popup`.
8
+ */
9
+ export function DialogTitle(props) {
10
+ const root = useDialogRootContext();
11
+ const generated = createUniqueId();
12
+ // The consumer's `id`, else the generated one. Kept out of the merge,
13
+ // where a wrapper's `id={undefined}` would remove it ("`undefined` is a
14
+ // value", PLAN.md section 8).
15
+ const id = () => props.id ?? generated;
16
+ // Named once mounted, so the Popup names only a Title that is rendered,
17
+ // and none in the server HTML (README, "Decisions").
18
+ onSettled(() => root.registerTitleId(id));
19
+ const ownProps = merge(root.attributes, {
20
+ get id() {
21
+ return id();
22
+ },
23
+ });
24
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "id"));
25
+ return renderElement(props, elementProps, (p) => <h2 {...p}/>);
26
+ }
@@ -0,0 +1,10 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface DialogTriggerProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The button that opens the dialog: a `<button type="button"
8
+ * aria-haspopup="dialog">`. Place it inside `Dialog.Root`.
9
+ */
10
+ export declare function DialogTrigger(props: DialogTriggerProps): JSX.Element;
@@ -0,0 +1,27 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { merge, omit } from "solid-js";
3
+ import { warnUnlessButton } from "../../core/button.js";
4
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
5
+ import { renderElement } from "../../core/renderElement.jsx";
6
+ import { useDialogRootContext } from "../root/DialogRootContext.js";
7
+ /**
8
+ * The button that opens the dialog: a `<button type="button"
9
+ * aria-haspopup="dialog">`. Place it inside `Dialog.Root`.
10
+ */
11
+ export function DialogTrigger(props) {
12
+ const root = useDialogRootContext();
13
+ // No `aria-expanded`: the Trigger is inert while the dialog is open. No
14
+ // `aria-controls`: the closed dialog is not shown (README, "Decisions").
15
+ const ownProps = merge(root.attributes, {
16
+ type: "button",
17
+ "aria-haspopup": "dialog",
18
+ onClick: (event) => root.setOpen(true, { reason: "trigger-press", event }),
19
+ ref: (element) => {
20
+ // Production builds drop the check with `isDev`.
21
+ if (isDev)
22
+ warnUnlessButton(element, "Dialog.Trigger");
23
+ },
24
+ });
25
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
26
+ return renderElement(props, elementProps, (p) => <button {...p}/>);
27
+ }
@@ -0,0 +1,26 @@
1
+ import type { OpenChangeDetails } from "./reason.ts";
2
+ /**
3
+ * Reports a modal `<dialog>` that the browser closes, while `open`: Escape
4
+ * and other close requests (`cancel`), and a `<form method="dialog">`
5
+ * (`close`). The popup closes through its own state:
6
+ *
7
+ * ```ts
8
+ * createDialogClose({
9
+ * get dialog() { return dialog; },
10
+ * get open() { return open(); },
11
+ * onDismiss: (details) => setOpen(false, details),
12
+ * });
13
+ * ```
14
+ *
15
+ * `cancel` is always prevented, so the state decides. The browser closes
16
+ * the dialog anyway when `cancel` cannot be prevented, such as on a second
17
+ * Escape: that close is reported once, and if the state still says open
18
+ * once it has settled, the dialog is opened again with `showModal()`. A
19
+ * close the popup made itself, after its state turned false, is not
20
+ * reported.
21
+ */
22
+ export declare function createDialogClose(options: {
23
+ readonly dialog: HTMLDialogElement | undefined;
24
+ readonly open: boolean;
25
+ onDismiss(details: OpenChangeDetails): void;
26
+ }): void;
@@ -0,0 +1,69 @@
1
+ import { createEffect, onFlush } from "solid-js";
2
+ /**
3
+ * Reports a modal `<dialog>` that the browser closes, while `open`: Escape
4
+ * and other close requests (`cancel`), and a `<form method="dialog">`
5
+ * (`close`). The popup closes through its own state:
6
+ *
7
+ * ```ts
8
+ * createDialogClose({
9
+ * get dialog() { return dialog; },
10
+ * get open() { return open(); },
11
+ * onDismiss: (details) => setOpen(false, details),
12
+ * });
13
+ * ```
14
+ *
15
+ * `cancel` is always prevented, so the state decides. The browser closes
16
+ * the dialog anyway when `cancel` cannot be prevented, such as on a second
17
+ * Escape: that close is reported once, and if the state still says open
18
+ * once it has settled, the dialog is opened again with `showModal()`. A
19
+ * close the popup made itself, after its state turned false, is not
20
+ * reported.
21
+ */
22
+ export function createDialogClose(options) {
23
+ createEffect(() => options.open, (open) => {
24
+ const dialog = options.dialog;
25
+ if (!open || !dialog)
26
+ return;
27
+ // The browser fires `cancel` while it handles the key, in the same
28
+ // task, so an Escape of this task is the reason. A timer forgets it.
29
+ let escapeKey;
30
+ const keydown = (event) => {
31
+ if (event.key !== "Escape")
32
+ return;
33
+ escapeKey = event;
34
+ setTimeout(() => {
35
+ if (escapeKey === event)
36
+ escapeKey = undefined;
37
+ });
38
+ };
39
+ // Set when the `close` that follows a `cancel` that could not be
40
+ // prevented has been reported already.
41
+ let reported = false;
42
+ const cancel = (event) => {
43
+ event.preventDefault();
44
+ reported = !event.cancelable;
45
+ options.onDismiss(escapeKey
46
+ ? { reason: "escape-key", event: escapeKey }
47
+ : { reason: "close-request", event });
48
+ };
49
+ const close = (event) => {
50
+ if (!reported)
51
+ options.onDismiss({ reason: "close-request", event });
52
+ reported = false;
53
+ // After the popup's own write (`createControllableSignal` defers it
54
+ // with `onFlush` too), so a refused close is the only one left open.
55
+ onFlush(() => {
56
+ if (options.open && !dialog.open)
57
+ dialog.showModal();
58
+ });
59
+ };
60
+ document.addEventListener("keydown", keydown, true);
61
+ dialog.addEventListener("cancel", cancel);
62
+ dialog.addEventListener("close", close);
63
+ return () => {
64
+ document.removeEventListener("keydown", keydown, true);
65
+ dialog.removeEventListener("cancel", cancel);
66
+ dialog.removeEventListener("close", close);
67
+ };
68
+ });
69
+ }
@@ -0,0 +1,23 @@
1
+ import type { OpenChangeDetails } from "./reason.ts";
2
+ /**
3
+ * Escape for a popup the browser does not close on Escape, a `manual`
4
+ * popover such as Tooltip's. While `open`, an Escape key calls `onDismiss`
5
+ * with the reason `escape-key`:
6
+ *
7
+ * ```ts
8
+ * createEscape({
9
+ * get open() { return open(); },
10
+ * onDismiss: (details) => setOpen(false, details),
11
+ * });
12
+ * ```
13
+ *
14
+ * Only the popup opened last closes, one per key. The key is marked
15
+ * handled (`preventDefault()`), so the browser does not also close a dialog
16
+ * or popover below, and carries on to the page's own listeners. A key
17
+ * already handled, or one pressed while an input method is composing, does
18
+ * nothing.
19
+ */
20
+ export declare function createEscape(options: {
21
+ readonly open: boolean;
22
+ onDismiss(details: OpenChangeDetails): void;
23
+ }): void;
@@ -0,0 +1,47 @@
1
+ import { createEffect } from "solid-js";
2
+ /**
3
+ * Escape for a popup the browser does not close on Escape, a `manual`
4
+ * popover such as Tooltip's. While `open`, an Escape key calls `onDismiss`
5
+ * with the reason `escape-key`:
6
+ *
7
+ * ```ts
8
+ * createEscape({
9
+ * get open() { return open(); },
10
+ * onDismiss: (details) => setOpen(false, details),
11
+ * });
12
+ * ```
13
+ *
14
+ * Only the popup opened last closes, one per key. The key is marked
15
+ * handled (`preventDefault()`), so the browser does not also close a dialog
16
+ * or popover below, and carries on to the page's own listeners. A key
17
+ * already handled, or one pressed while an input method is composing, does
18
+ * nothing.
19
+ */
20
+ export function createEscape(options) {
21
+ createEffect(() => options.open, (open) => {
22
+ if (!open)
23
+ return;
24
+ // Listeners of one target and phase run in the order they were
25
+ // added, and each popup adds its own when it opens. In the capture
26
+ // phase, the one opened last claims the key last; in the bubble
27
+ // phase, after the page's listeners, only the claim holder closes.
28
+ const claim = (event) => {
29
+ event.menhirEscapeClaim = claim;
30
+ };
31
+ const close = (event) => {
32
+ if (event.key !== "Escape" ||
33
+ event.isComposing ||
34
+ event.defaultPrevented ||
35
+ event.menhirEscapeClaim !== claim)
36
+ return;
37
+ event.preventDefault();
38
+ options.onDismiss({ reason: "escape-key", event });
39
+ };
40
+ document.addEventListener("keydown", claim, true);
41
+ document.addEventListener("keydown", close);
42
+ return () => {
43
+ document.removeEventListener("keydown", claim, true);
44
+ document.removeEventListener("keydown", close);
45
+ };
46
+ });
47
+ }
@@ -0,0 +1,26 @@
1
+ import type { OpenChangeDetails } from "./reason.ts";
2
+ /**
3
+ * Reports a press on a modal `<dialog>`'s backdrop, while `open`, with the
4
+ * reason `outside-press`. The popup closes through its own state:
5
+ *
6
+ * ```ts
7
+ * createOutsidePress({
8
+ * get dialog() { return dialog; },
9
+ * get open() { return open(); },
10
+ * onDismiss: (details) => setOpen(false, details),
11
+ * });
12
+ * ```
13
+ *
14
+ * A press on the backdrop targets the `<dialog>` itself, as one on its
15
+ * padding or its own scrollbar does, so the backdrop is where the target is
16
+ * the dialog and the point is outside its border box. It closes on release,
17
+ * as the browser's light dismiss does: on the `click` of a primary-button
18
+ * press that also started on the backdrop. So a press dragged out of the
19
+ * dialog, a secondary button (no `click`) and a touch that scrolls
20
+ * (`pointercancel`, no `click`) do not close it.
21
+ */
22
+ export declare function createOutsidePress(options: {
23
+ readonly dialog: HTMLDialogElement | undefined;
24
+ readonly open: boolean;
25
+ onDismiss(details: OpenChangeDetails): void;
26
+ }): void;
@@ -0,0 +1,50 @@
1
+ import { createEffect } from "solid-js";
2
+ /**
3
+ * Reports a press on a modal `<dialog>`'s backdrop, while `open`, with the
4
+ * reason `outside-press`. The popup closes through its own state:
5
+ *
6
+ * ```ts
7
+ * createOutsidePress({
8
+ * get dialog() { return dialog; },
9
+ * get open() { return open(); },
10
+ * onDismiss: (details) => setOpen(false, details),
11
+ * });
12
+ * ```
13
+ *
14
+ * A press on the backdrop targets the `<dialog>` itself, as one on its
15
+ * padding or its own scrollbar does, so the backdrop is where the target is
16
+ * the dialog and the point is outside its border box. It closes on release,
17
+ * as the browser's light dismiss does: on the `click` of a primary-button
18
+ * press that also started on the backdrop. So a press dragged out of the
19
+ * dialog, a secondary button (no `click`) and a touch that scrolls
20
+ * (`pointercancel`, no `click`) do not close it.
21
+ */
22
+ export function createOutsidePress(options) {
23
+ createEffect(() => options.open, (open) => {
24
+ const dialog = options.dialog;
25
+ if (!open || !dialog)
26
+ return;
27
+ const onBackdrop = (event) => {
28
+ if (event.target !== dialog)
29
+ return false;
30
+ const { left, top, right, bottom } = dialog.getBoundingClientRect();
31
+ const { clientX: x, clientY: y } = event;
32
+ return x < left || x > right || y < top || y > bottom;
33
+ };
34
+ let pressed = false;
35
+ const pointerdown = (event) => {
36
+ pressed = event.button === 0 && onBackdrop(event);
37
+ };
38
+ const click = (event) => {
39
+ if (pressed && onBackdrop(event))
40
+ options.onDismiss({ reason: "outside-press", event });
41
+ pressed = false;
42
+ };
43
+ dialog.addEventListener("pointerdown", pointerdown);
44
+ dialog.addEventListener("click", click);
45
+ return () => {
46
+ dialog.removeEventListener("pointerdown", pointerdown);
47
+ dialog.removeEventListener("click", click);
48
+ };
49
+ });
50
+ }
@@ -0,0 +1,28 @@
1
+ import type { OpenChangeDetails } from "./reason.ts";
2
+ /**
3
+ * Reports an `auto` popover that the browser closes, while `open`, with the
4
+ * reason. The popup closes through its own state:
5
+ *
6
+ * ```ts
7
+ * createPopoverClose({
8
+ * get popup() { return popup; },
9
+ * get trigger() { return trigger; },
10
+ * get open() { return open(); },
11
+ * onDismiss: (details) => setOpen(false, details),
12
+ * });
13
+ * ```
14
+ *
15
+ * The reason is the input event of the same task: an Escape key
16
+ * (`escape-key`), a pointer released outside the popover (`outside-press`,
17
+ * the browser's light dismiss) or a release or click on the trigger
18
+ * (`trigger-press`, its `popovertarget`); else `close-request`. The
19
+ * browser's close cannot be refused, so if the state still says open once
20
+ * it has settled, the popover is shown again. A close the popup made
21
+ * itself, after its state turned false, is not reported.
22
+ */
23
+ export declare function createPopoverClose(options: {
24
+ readonly popup: HTMLElement | undefined;
25
+ readonly trigger: Element | undefined;
26
+ readonly open: boolean;
27
+ onDismiss(details: OpenChangeDetails): void;
28
+ }): void;
@@ -0,0 +1,82 @@
1
+ import { createEffect } from "solid-js";
2
+ /**
3
+ * Reports an `auto` popover that the browser closes, while `open`, with the
4
+ * reason. The popup closes through its own state:
5
+ *
6
+ * ```ts
7
+ * createPopoverClose({
8
+ * get popup() { return popup; },
9
+ * get trigger() { return trigger; },
10
+ * get open() { return open(); },
11
+ * onDismiss: (details) => setOpen(false, details),
12
+ * });
13
+ * ```
14
+ *
15
+ * The reason is the input event of the same task: an Escape key
16
+ * (`escape-key`), a pointer released outside the popover (`outside-press`,
17
+ * the browser's light dismiss) or a release or click on the trigger
18
+ * (`trigger-press`, its `popovertarget`); else `close-request`. The
19
+ * browser's close cannot be refused, so if the state still says open once
20
+ * it has settled, the popover is shown again. A close the popup made
21
+ * itself, after its state turned false, is not reported.
22
+ */
23
+ export function createPopoverClose(options) {
24
+ createEffect(() => options.open, (open) => {
25
+ const popup = options.popup;
26
+ if (!open || !popup)
27
+ return;
28
+ // The browser fires `beforetoggle` while it handles the key or press
29
+ // that closes the popover, in the same task, so the input event of
30
+ // this task is the reason. A timer forgets it.
31
+ let cause;
32
+ const note = (reason, event) => {
33
+ const noted = { reason, event };
34
+ cause = noted;
35
+ setTimeout(() => {
36
+ if (cause === noted)
37
+ cause = undefined;
38
+ });
39
+ };
40
+ const keydown = (event) => {
41
+ if (event.key === "Escape")
42
+ note("escape-key", event);
43
+ };
44
+ // Light dismiss closes on `pointerup`. `popovertarget` toggles on
45
+ // `click`, which a key fires too, so a click counts only on the
46
+ // trigger: a key on another popover's trigger is no press outside.
47
+ const pointerup = (event) => {
48
+ const target = event.target;
49
+ if (options.trigger?.contains(target))
50
+ note("trigger-press", event);
51
+ else if (!popup.contains(target))
52
+ note("outside-press", event);
53
+ };
54
+ const click = (event) => {
55
+ if (options.trigger?.contains(event.target))
56
+ note("trigger-press", event);
57
+ };
58
+ const beforetoggle = (event) => {
59
+ if (event.newState !== "closed" || !options.open)
60
+ return;
61
+ options.onDismiss(cause ?? { reason: "close-request", event });
62
+ };
63
+ // `toggle` comes a task after the popover is hidden, when the popup's
64
+ // own write has landed, so a refused close is the only one still open.
65
+ const toggle = () => {
66
+ if (options.open && !popup.matches(":popover-open"))
67
+ popup.showPopover();
68
+ };
69
+ document.addEventListener("keydown", keydown, true);
70
+ document.addEventListener("pointerup", pointerup, true);
71
+ document.addEventListener("click", click, true);
72
+ popup.addEventListener("beforetoggle", beforetoggle);
73
+ popup.addEventListener("toggle", toggle);
74
+ return () => {
75
+ document.removeEventListener("keydown", keydown, true);
76
+ document.removeEventListener("pointerup", pointerup, true);
77
+ document.removeEventListener("click", click, true);
78
+ popup.removeEventListener("beforetoggle", beforetoggle);
79
+ popup.removeEventListener("toggle", toggle);
80
+ };
81
+ });
82
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Why a popup opened or closed, the `reason` of `onOpenChange`'s details
3
+ * (PLAN.md section 8, "API conventions"). Later components add reasons;
4
+ * they do not rename these.
5
+ *
6
+ * - `escape-key`: Escape.
7
+ * - `outside-press`: a press outside the popup, on a dialog's backdrop or
8
+ * in a popover's light dismiss.
9
+ * - `trigger-press`: the trigger, pressed by pointer or keyboard.
10
+ * - `close-request`: the browser closed the popup for another reason, such
11
+ * as the back gesture on Android, a `<form method="dialog">` or another
12
+ * popover opening.
13
+ * - `trigger-hover`: the pointer rested on the trigger, or left the trigger
14
+ * and the popup (`floating/hover`).
15
+ * - `close-press`: the popup's own close button, pressed by pointer or
16
+ * keyboard (Dialog's `Close`).
17
+ * - `trigger-focus`: the trigger received keyboard focus, or lost it
18
+ * (Tooltip).
19
+ */
20
+ export type OpenChangeReason = "escape-key" | "outside-press" | "trigger-press" | "close-request" | "trigger-hover" | "close-press" | "trigger-focus";
21
+ /**
22
+ * The second argument of `onOpenChange`. `event` is the event that caused
23
+ * the change: the key or pointer event when there was one, else the
24
+ * browser's own `cancel`, `close` or `beforetoggle`. There is no `cancel()`:
25
+ * a consumer keeps a popup open by controlling `open`.
26
+ */
27
+ export interface OpenChangeDetails {
28
+ readonly reason: OpenChangeReason;
29
+ readonly event: Event;
30
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ import type { FocusTarget } from "./target.ts";
2
+ /**
3
+ * Moves focus to `initialFocus` once the popup has opened, instead of where
4
+ * the browser puts it: the element with `autofocus`, else a dialog's first
5
+ * focusable element, while a popover leaves focus on its trigger. Without
6
+ * `initialFocus` the browser decides.
7
+ *
8
+ * ```ts
9
+ * createInitialFocus({
10
+ * get popup() { return dialog; },
11
+ * get open() { return open(); },
12
+ * get initialFocus() { return props.initialFocus; },
13
+ * });
14
+ * ```
15
+ *
16
+ * A function is called on each opening with the element the browser focused
17
+ * inside the popup, or `null` when it focused none. The browser focuses
18
+ * first, so focus moves twice, in the same task, before anything is painted.
19
+ */
20
+ export declare function createInitialFocus(options: {
21
+ readonly popup: HTMLElement | undefined;
22
+ readonly open: boolean;
23
+ readonly initialFocus?: FocusTarget;
24
+ }): void;
@@ -0,0 +1,38 @@
1
+ import { createEffect } from "solid-js";
2
+ /**
3
+ * Moves focus to `initialFocus` once the popup has opened, instead of where
4
+ * the browser puts it: the element with `autofocus`, else a dialog's first
5
+ * focusable element, while a popover leaves focus on its trigger. Without
6
+ * `initialFocus` the browser decides.
7
+ *
8
+ * ```ts
9
+ * createInitialFocus({
10
+ * get popup() { return dialog; },
11
+ * get open() { return open(); },
12
+ * get initialFocus() { return props.initialFocus; },
13
+ * });
14
+ * ```
15
+ *
16
+ * A function is called on each opening with the element the browser focused
17
+ * inside the popup, or `null` when it focused none. The browser focuses
18
+ * first, so focus moves twice, in the same task, before anything is painted.
19
+ */
20
+ export function createInitialFocus(options) {
21
+ createEffect(() => options.open, (open) => {
22
+ const popup = options.popup;
23
+ if (!open || !popup)
24
+ return;
25
+ // After the popup's own `showModal()` or `showPopover()` in this
26
+ // flush, whichever effect runs first. A microtask, not `onFlush`,
27
+ // which is for event handlers: this runs inside the flush.
28
+ queueMicrotask(() => {
29
+ if (!popup.matches("[open], :popover-open"))
30
+ return;
31
+ const active = document.activeElement;
32
+ const target = options.initialFocus;
33
+ (typeof target === "function"
34
+ ? target(popup.contains(active) ? active : null)
35
+ : target)?.focus();
36
+ });
37
+ });
38
+ }