@warmio/ui 1.3.0 → 1.5.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,239 @@
1
+ import {
2
+ FLUID_SPRING,
3
+ useReducedMotion
4
+ } from "./chunk-JXSJTIX7.js";
5
+ import {
6
+ cn
7
+ } from "./chunk-VIB7OX7D.js";
8
+
9
+ // ../ui/src/lib/fluid-menu-highlight.tsx
10
+ import * as React from "react";
11
+ import { motion } from "motion/react";
12
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
13
+ var itemSelectors = {
14
+ menu: '[role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"]',
15
+ select: '[role="option"]',
16
+ sidebar: '[data-sidebar="menu-button"], [data-sidebar="menu-sub-button"]'
17
+ };
18
+ var SIDEBAR_HOVER_DEBOUNCE_MS = 0;
19
+ function sameHighlights(previous, next) {
20
+ return previous.length === next.length && previous.every((item, index) => {
21
+ const other = next[index];
22
+ return other && item.key === other.key && item.element === other.element && item.left === other.left && item.top === other.top && item.width === other.width && item.height === other.height && item.selected === other.selected && item.destructive === other.destructive;
23
+ });
24
+ }
25
+ function MenuHighlight({ target, kind }) {
26
+ const reduceMotion = useReducedMotion();
27
+ const previousElement = React.useRef(target.element);
28
+ const movedToItem = previousElement.current !== target.element;
29
+ React.useLayoutEffect(() => {
30
+ previousElement.current = target.element;
31
+ }, [target.element]);
32
+ const Element2 = kind === "sidebar" ? motion.li : motion.div;
33
+ const geometry = { x: target.left, y: target.top, width: target.width, height: target.height };
34
+ return /* @__PURE__ */ jsx(
35
+ Element2,
36
+ {
37
+ "aria-hidden": "true",
38
+ role: "presentation",
39
+ "data-slot": "fluid-menu-highlight",
40
+ className: cn(
41
+ "pointer-events-none absolute top-0 left-0 -z-10 list-none rounded-lg",
42
+ target.destructive ? "bg-destructive/10 dark:bg-destructive/20" : kind === "select" ? "bg-accent" : "bg-sidebar-accent"
43
+ ),
44
+ initial: false,
45
+ style: reduceMotion ? {
46
+ left: target.left,
47
+ top: target.top,
48
+ width: target.width,
49
+ height: target.height
50
+ } : {},
51
+ animate: reduceMotion ? { opacity: target.selected ? 1 : kind === "sidebar" ? 0.5 : 1 } : {
52
+ ...geometry,
53
+ opacity: target.selected ? 1 : kind === "sidebar" ? 0.5 : 1
54
+ },
55
+ transition: reduceMotion || !movedToItem ? { duration: 0 } : kind === "sidebar" ? FLUID_SPRING.moderate : FLUID_SPRING.fast
56
+ },
57
+ reduceMotion ? "still" : "moving"
58
+ );
59
+ }
60
+ function FluidMenuHighlights({ kind }) {
61
+ const anchorRef = React.useRef(null);
62
+ const [highlights, setHighlights] = React.useState([]);
63
+ const Anchor = kind === "sidebar" ? "li" : "span";
64
+ React.useLayoutEffect(() => {
65
+ const container = anchorRef.current?.parentElement;
66
+ if (!container) return;
67
+ const selector = itemSelectors[kind];
68
+ let items = [];
69
+ let hovered = null;
70
+ let focused = null;
71
+ let hoverTimer;
72
+ let frame;
73
+ let nextId = 0;
74
+ const ids = /* @__PURE__ */ new WeakMap();
75
+ const idFor = (element) => {
76
+ let id = ids.get(element);
77
+ if (id === void 0) {
78
+ id = nextId++;
79
+ ids.set(element, id);
80
+ }
81
+ return id;
82
+ };
83
+ const enabled = (item) => !item.matches(
84
+ ':disabled, [disabled], [aria-disabled="true"], [data-disabled], [data-fluid-menu-disabled]'
85
+ ) && item.getClientRects().length > 0 && item.offsetWidth > 0 && item.offsetHeight > 0;
86
+ const measure = () => {
87
+ frame = void 0;
88
+ const visibleItems = items.filter(enabled);
89
+ const selected = visibleItems.filter(
90
+ (item) => item.matches(
91
+ '[aria-selected="true"], [aria-checked="true"], [data-active], [data-popup-open], [data-open]'
92
+ )
93
+ );
94
+ const active = kind === "sidebar" ? hovered && visibleItems.includes(hovered) ? hovered : focused && visibleItems.includes(focused) ? focused : null : visibleItems.find((item) => item.hasAttribute("data-highlighted"));
95
+ const targets = [];
96
+ const add = (element, key, isSelected) => {
97
+ const containerRect = container.getBoundingClientRect();
98
+ const elementRect = element.getBoundingClientRect();
99
+ targets.push({
100
+ key,
101
+ element,
102
+ left: elementRect.left - containerRect.left + container.scrollLeft,
103
+ top: elementRect.top - containerRect.top + container.scrollTop,
104
+ width: elementRect.width,
105
+ height: elementRect.height,
106
+ selected: isSelected,
107
+ destructive: element.dataset["variant"] === "destructive"
108
+ });
109
+ };
110
+ if (kind === "sidebar") {
111
+ const selectedItem = selected[0];
112
+ const hoverItem = active ?? selectedItem;
113
+ if (hoverItem) add(hoverItem, "hover", false);
114
+ if (selectedItem) add(selectedItem, "sidebar", true);
115
+ } else {
116
+ const occurrences = /* @__PURE__ */ new Map();
117
+ selected.forEach((item) => {
118
+ const group = item.closest('[role="group"], [data-sidebar="menu-sub"]') ?? container;
119
+ const occurrence = occurrences.get(group) ?? 0;
120
+ occurrences.set(group, occurrence + 1);
121
+ const key = item.getAttribute("role") === "menuitemcheckbox" || kind === "select" && selected.length > 1 ? `selection-item-${idFor(item)}` : `selection-${idFor(group)}-${occurrence}`;
122
+ add(item, key, true);
123
+ });
124
+ if (active) add(active, "hover", false);
125
+ }
126
+ setHighlights((previous) => sameHighlights(previous, targets) ? previous : targets);
127
+ };
128
+ const schedule = () => {
129
+ if (frame === void 0) frame = requestAnimationFrame(measure);
130
+ };
131
+ const resizeObserver = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(schedule);
132
+ const register = () => {
133
+ resizeObserver?.disconnect();
134
+ items.forEach((item) => item.removeAttribute("data-fluid-menu-item"));
135
+ items = Array.from(container.querySelectorAll(selector)).filter(
136
+ (item) => item.closest("[data-fluid-menu]") === container
137
+ );
138
+ items.forEach((item) => {
139
+ item.setAttribute("data-fluid-menu-item", "");
140
+ resizeObserver?.observe(item);
141
+ });
142
+ resizeObserver?.observe(container);
143
+ };
144
+ const observer = new MutationObserver((records) => {
145
+ if (records.some(
146
+ (record) => record.type === "childList" && [...record.addedNodes, ...record.removedNodes].some(
147
+ (node) => node instanceof Element && !node.matches('[data-slot="fluid-menu-highlight"]')
148
+ )
149
+ ))
150
+ register();
151
+ schedule();
152
+ });
153
+ register();
154
+ observer.observe(container, {
155
+ subtree: true,
156
+ childList: true,
157
+ attributes: true,
158
+ attributeFilter: [
159
+ "data-highlighted",
160
+ "aria-selected",
161
+ "aria-checked",
162
+ "data-active",
163
+ "data-popup-open",
164
+ "data-open",
165
+ "disabled",
166
+ "aria-disabled",
167
+ "data-disabled",
168
+ "data-fluid-menu-disabled",
169
+ "hidden",
170
+ "data-state"
171
+ ]
172
+ });
173
+ const pointerMove = (event) => {
174
+ if (event.pointerType === "touch") return;
175
+ const nextHovered = event.target instanceof Element ? event.target.closest(selector) : null;
176
+ clearTimeout(hoverTimer);
177
+ hoverTimer = window.setTimeout(() => {
178
+ hovered = nextHovered;
179
+ hoverTimer = void 0;
180
+ schedule();
181
+ }, SIDEBAR_HOVER_DEBOUNCE_MS);
182
+ };
183
+ const pointerLeave = () => {
184
+ clearTimeout(hoverTimer);
185
+ hoverTimer = void 0;
186
+ hovered = null;
187
+ schedule();
188
+ };
189
+ const focusIn = (event) => {
190
+ clearTimeout(hoverTimer);
191
+ hoverTimer = void 0;
192
+ const item = event.target instanceof Element ? event.target.closest(selector) : null;
193
+ focused = item?.matches(":focus-visible") ? item : null;
194
+ if (focused) hovered = null;
195
+ schedule();
196
+ };
197
+ const focusOut = () => {
198
+ focused = null;
199
+ schedule();
200
+ };
201
+ if (kind === "sidebar") {
202
+ container.addEventListener("pointermove", pointerMove);
203
+ container.addEventListener("pointerleave", pointerLeave);
204
+ container.addEventListener("focusin", focusIn);
205
+ container.addEventListener("focusout", focusOut);
206
+ }
207
+ container.addEventListener("scroll", schedule, true);
208
+ measure();
209
+ return () => {
210
+ observer.disconnect();
211
+ resizeObserver?.disconnect();
212
+ if (frame !== void 0) cancelAnimationFrame(frame);
213
+ clearTimeout(hoverTimer);
214
+ items.forEach((item) => item.removeAttribute("data-fluid-menu-item"));
215
+ container.removeEventListener("pointermove", pointerMove);
216
+ container.removeEventListener("pointerleave", pointerLeave);
217
+ container.removeEventListener("focusin", focusIn);
218
+ container.removeEventListener("focusout", focusOut);
219
+ container.removeEventListener("scroll", schedule, true);
220
+ };
221
+ }, [kind]);
222
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
223
+ /* @__PURE__ */ jsx(
224
+ Anchor,
225
+ {
226
+ ref: (node) => {
227
+ anchorRef.current = node;
228
+ },
229
+ hidden: true,
230
+ "aria-hidden": "true"
231
+ }
232
+ ),
233
+ highlights.map((target) => /* @__PURE__ */ jsx(MenuHighlight, { target, kind }, target.key))
234
+ ] });
235
+ }
236
+
237
+ export {
238
+ FluidMenuHighlights
239
+ };
@@ -0,0 +1,129 @@
1
+ // ../design/src/button.tsx
2
+ import * as React from "react";
3
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
4
+ var presentationToPrivateSize = {
5
+ assistantIcon: "icon-xs",
6
+ assistantRoundIcon: "icon-round",
7
+ composerIcon: "icon-sm",
8
+ default: "default",
9
+ headerAction: "icon-xs",
10
+ compact: "sm",
11
+ controlPeer: "default",
12
+ editPill: "sm",
13
+ hero: "lg",
14
+ menuOption: "option",
15
+ promptCategory: "default",
16
+ promptOption: "option",
17
+ sidebarIcon: "icon",
18
+ tableHeader: "default",
19
+ tableHeaderLink: "default",
20
+ threadMore: "icon-xs",
21
+ threadNew: "sm",
22
+ touch: "default"
23
+ };
24
+ var presentationAliases = {
25
+ controlPeer: "default",
26
+ menuOption: "promptOption",
27
+ touch: "default"
28
+ };
29
+ function resolvePresentation(presentation) {
30
+ return presentationAliases[presentation] ?? presentation;
31
+ }
32
+ function resolveButton(presentation, size) {
33
+ const privateSize = presentationToPrivateSize[presentation];
34
+ const resolvedSize = size ?? (privateSize === "lg" ? "jumbo" : privateSize === "sm" ? "compact" : privateSize === "icon" || privateSize === "icon-round" || privateSize === "icon-sm" || privateSize === "icon-xs" ? "icon" : "default");
35
+ return { resolvedSize, explicitSize: size ? resolvedSize : void 0 };
36
+ }
37
+ function withArrow(variant, children) {
38
+ if (variant !== "arrow") return children;
39
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
40
+ children,
41
+ /* @__PURE__ */ jsxs(
42
+ "svg",
43
+ {
44
+ "aria-hidden": "true",
45
+ "data-icon": "inline-end",
46
+ width: "16",
47
+ height: "16",
48
+ viewBox: "0 0 24 24",
49
+ fill: "none",
50
+ stroke: "currentColor",
51
+ strokeWidth: "2",
52
+ strokeLinecap: "round",
53
+ strokeLinejoin: "round",
54
+ children: [
55
+ /* @__PURE__ */ jsx("path", { d: "M7 7h10v10" }),
56
+ /* @__PURE__ */ jsx("path", { d: "M7 17L17 7" })
57
+ ]
58
+ }
59
+ )
60
+ ] });
61
+ }
62
+ var Button = React.forwardRef(function Button2({
63
+ children,
64
+ className: _className,
65
+ presentation = "default",
66
+ render: _render,
67
+ size,
68
+ style: _style,
69
+ type,
70
+ variant = "default",
71
+ width,
72
+ asChild: _asChild,
73
+ ...props
74
+ }, ref) {
75
+ const { resolvedSize, explicitSize } = resolveButton(presentation, size);
76
+ return /* @__PURE__ */ jsx(
77
+ "button",
78
+ {
79
+ ...props,
80
+ ref,
81
+ type: type ?? "button",
82
+ "data-warm-button": "",
83
+ "data-slot": "button",
84
+ "data-presentation": resolvePresentation(presentation),
85
+ "data-variant": variant,
86
+ "data-size": resolvedSize,
87
+ "data-explicit-size": explicitSize,
88
+ "data-width": width,
89
+ children: withArrow(variant, children)
90
+ }
91
+ );
92
+ });
93
+ Button.displayName = "WarmButton";
94
+ var ButtonLink = React.forwardRef(function ButtonLink2({
95
+ children,
96
+ className: _className,
97
+ disabled: _disabled,
98
+ presentation = "default",
99
+ render: _render,
100
+ size,
101
+ style: _style,
102
+ variant = "default",
103
+ width,
104
+ asChild: _asChild,
105
+ ...props
106
+ }, ref) {
107
+ const { resolvedSize, explicitSize } = resolveButton(presentation, size);
108
+ return /* @__PURE__ */ jsx(
109
+ "a",
110
+ {
111
+ ...props,
112
+ ref,
113
+ "data-warm-button": "",
114
+ "data-slot": "button",
115
+ "data-presentation": resolvePresentation(presentation),
116
+ "data-variant": variant,
117
+ "data-size": resolvedSize,
118
+ "data-explicit-size": explicitSize,
119
+ "data-width": width,
120
+ children: withArrow(variant, children)
121
+ }
122
+ );
123
+ });
124
+ ButtonLink.displayName = "WarmButtonLink";
125
+
126
+ export {
127
+ Button,
128
+ ButtonLink
129
+ };