menhir 0.1.1 → 0.1.2

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.
@@ -0,0 +1,21 @@
1
+ import { arrow as arrow$1 } from "@floating-ui/dom";
2
+ // src/floating/positioning/arrow.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
+ function arrow(options) {
11
+ return {
12
+ ...arrow$1(options),
13
+ write(_popup, data) {
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
+ }
21
+ export { arrow };
@@ -0,0 +1,122 @@
1
+ import { createEffect, createSignal, onSettled } from "solid-js";
2
+ import { autoUpdate, computePosition } from "@floating-ui/dom";
3
+ // src/floating/positioning/createPosition.ts
4
+ /**
5
+ * Places `popup` against `anchor` while `open`, with the strategy `fixed` of
6
+ * a popover in the top layer. Pass the options as getters, so a change to
7
+ * one restarts the placement:
8
+ *
9
+ * ```ts
10
+ * const position = createPosition({
11
+ * get anchor() { return trigger; },
12
+ * get popup() { return popup; },
13
+ * get open() { return open(); },
14
+ * get side() { return props.side; },
15
+ * get middleware() { return [offset(8), flip(), shift()]; },
16
+ * });
17
+ * ```
18
+ *
19
+ * Show the popup before placing it: create the effect that calls
20
+ * `showPopover()` before calling this, so that the popup can be measured,
21
+ * and is placed before the browser paints it. Writes on the popup its
22
+ * `inset`, rounded to device pixels, `data-side`, `data-align`,
23
+ * `--anchor-width`, `--anchor-height` and `--transform-origin`, then what
24
+ * each middleware writes. Returns the last placement written. Nothing runs
25
+ * on the server.
26
+ */
27
+ function createPosition(options) {
28
+ const [position, setPosition] = createSignal();
29
+ const [mounted, setMounted] = createSignal(false);
30
+ onSettled(() => {
31
+ setMounted(true);
32
+ });
33
+ let generation = 0;
34
+ let written;
35
+ const update = (run) => {
36
+ const current = ++generation;
37
+ computePosition(run.anchor, run.popup, {
38
+ placement: run.placement,
39
+ strategy: "fixed",
40
+ middleware: [...run.middleware, anchorBox]
41
+ }).then((result) => {
42
+ if (current !== generation) {
43
+ if (written && written.generation > current) written.write();
44
+ return;
45
+ }
46
+ const [side, align = "center"] = result.placement.split("-");
47
+ const x = roundToDevicePixels(result.x);
48
+ const y = roundToDevicePixels(result.y);
49
+ const popup = run.popup;
50
+ const anchor = result.middlewareData.anchor;
51
+ const write = () => {
52
+ popup.style.inset = `${y}px auto auto ${x}px`;
53
+ popup.setAttribute("data-side", side);
54
+ popup.setAttribute("data-align", align);
55
+ popup.style.setProperty("--anchor-width", `${anchor.width}px`);
56
+ popup.style.setProperty("--anchor-height", `${anchor.height}px`);
57
+ popup.style.setProperty("--transform-origin", anchor.origin);
58
+ for (const middleware of run.middleware) middleware.write?.(popup, result.middlewareData);
59
+ };
60
+ write();
61
+ written = {
62
+ generation: current,
63
+ write
64
+ };
65
+ setPosition({
66
+ side,
67
+ align,
68
+ x,
69
+ y,
70
+ middlewareData: result.middlewareData
71
+ });
72
+ });
73
+ };
74
+ createEffect(() => {
75
+ if (!mounted() || !options.open) return void 0;
76
+ const { anchor, popup } = options;
77
+ if (!anchor || !popup) return void 0;
78
+ const side = options.side ?? "bottom";
79
+ const align = options.align ?? "center";
80
+ return {
81
+ anchor,
82
+ popup,
83
+ placement: align === "center" ? side : `${side}-${align}`,
84
+ middleware: options.middleware ?? []
85
+ };
86
+ }, (run) => {
87
+ if (!run) return;
88
+ const stop = autoUpdate(run.anchor, run.popup, () => update(run));
89
+ return () => {
90
+ generation++;
91
+ written = void 0;
92
+ stop();
93
+ };
94
+ });
95
+ return position;
96
+ }
97
+ /**
98
+ * Runs last, after every middleware has moved the popup: the anchor's size,
99
+ * and the point of the popup nearest the anchor's center, on the edge that
100
+ * faces the anchor, for `transform-origin`.
101
+ */
102
+ var anchorBox = {
103
+ name: "anchor",
104
+ fn({ x, y, placement, rects: { reference, floating } }) {
105
+ const side = placement.split("-")[0];
106
+ const clamp = (value, max) => Math.min(Math.max(value, 0), max);
107
+ const centerX = clamp(reference.x + reference.width / 2 - x, floating.width);
108
+ const centerY = clamp(reference.y + reference.height / 2 - y, floating.height);
109
+ const [originX, originY] = side === "top" ? [centerX, floating.height] : side === "bottom" ? [centerX, 0] : side === "left" ? [floating.width, centerY] : [0, centerY];
110
+ return { data: {
111
+ width: reference.width,
112
+ height: reference.height,
113
+ origin: `${originX}px ${originY}px`
114
+ } };
115
+ }
116
+ };
117
+ /** Snaps to the device's pixel grid, so the popup's edges stay sharp. */
118
+ function roundToDevicePixels(value) {
119
+ const ratio = window.devicePixelRatio || 1;
120
+ return Math.round(value * ratio) / ratio;
121
+ }
122
+ export { createPosition };
@@ -0,0 +1 @@
1
+ export { flip } from "@floating-ui/dom";
@@ -0,0 +1,14 @@
1
+ import { hide as hide$1 } from "@floating-ui/dom";
2
+ // src/floating/positioning/hide.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
+ function hide(options = {}) {
9
+ return {
10
+ ...hide$1(options),
11
+ write: (popup, data) => popup.toggleAttribute("data-anchor-hidden", !!data.hide?.referenceHidden)
12
+ };
13
+ }
14
+ export { hide };
@@ -0,0 +1 @@
1
+ export { offset } from "@floating-ui/dom";
@@ -0,0 +1 @@
1
+ export { shift } from "@floating-ui/dom";
@@ -0,0 +1,39 @@
1
+ import { size as size$1 } from "@floating-ui/dom";
2
+ // src/floating/positioning/size.ts
3
+ function writeAvailable(popup, available) {
4
+ popup.style.setProperty("--available-width", `${available.width}px`);
5
+ popup.style.setProperty("--available-height", `${available.height}px`);
6
+ }
7
+ /**
8
+ * Writes the space between the anchor and the viewport's edges, less
9
+ * `padding`, as `--available-width` and `--available-height`, for the
10
+ * popup's `max-width` and `max-height`. Goes after `flip` and `shift`.
11
+ *
12
+ * It writes them while it measures as well, so that a popup they limit is
13
+ * measured at its limited size in the same placement. If that placement
14
+ * turns out stale, `createPosition` writes the newest one again.
15
+ */
16
+ function size(options = {}) {
17
+ return {
18
+ name: "size",
19
+ options,
20
+ async fn(state) {
21
+ let available;
22
+ return {
23
+ ...await size$1({
24
+ ...options,
25
+ apply({ elements, availableWidth, availableHeight }) {
26
+ available = {
27
+ width: Math.max(0, availableWidth),
28
+ height: Math.max(0, availableHeight)
29
+ };
30
+ writeAvailable(elements.floating, available);
31
+ }
32
+ }).fn(state),
33
+ data: available
34
+ };
35
+ },
36
+ write: (popup, data) => writeAvailable(popup, data.size)
37
+ };
38
+ }
39
+ export { size };
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "menhir",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
5
5
  "keywords": [
6
6
  "solid",
@@ -102,6 +102,9 @@
102
102
  "imports": {
103
103
  "#test-utils": "./test/index.ts"
104
104
  },
105
+ "dependencies": {
106
+ "@floating-ui/dom": "^1.8.0"
107
+ },
105
108
  "peerDependencies": {
106
109
  "@solidjs/web": "^2.0.0",
107
110
  "solid-js": "^2.0.0"