@milktop/react-ui 0.3.10 → 0.3.12

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.
@@ -9,9 +9,9 @@
9
9
  transition: background-color 120ms, border-color 120ms;
10
10
 
11
11
  &:hover { background: var(--ui-hover); }
12
- &:active { translate: 0 1px; }
12
+ &:active { filter: brightness(0.96); }
13
13
  &:focus-visible { outline: 2px solid var(--ui-ring-soft); border-color: var(--ui-ring); }
14
- &:disabled { opacity: 0.5; cursor: not-allowed; translate: none; }
14
+ &:disabled { opacity: 0.5; cursor: not-allowed; filter: none; }
15
15
 
16
16
  &[data-variant=primary] { background: var(--ui-accent); border-color: var(--ui-accent); color: var(--ui-accent-text);
17
17
  &:hover { filter: brightness(1.1); } }
@@ -25,7 +25,7 @@
25
25
  &[data-variant=link] { height: auto; padding: 0; background: transparent; border-color: transparent; color: var(--ui-accent);
26
26
  font-size: inherit; font-weight: inherit; line-height: inherit; white-space: normal; text-align: start;
27
27
  &:hover { background: transparent; text-decoration: underline; }
28
- &:active { translate: none; } }
28
+ &:active { filter: none; } }
29
29
 
30
30
  &[data-size=sm] { height: var(--ui-control-height-sm); padding: 0 0.625rem; font-size: 12px; gap: 0.375rem; }
31
31
  &[data-size=lg] { height: var(--ui-control-height-lg); padding: 0 1.25rem; font-size: 16px; }
@@ -23,11 +23,15 @@
23
23
  * `navigate`; by default a link is clicked
24
24
  * - `placeholder`, `emptyText`, `label`; `hints`: false hides the keyboard
25
25
  * hints along the bottom
26
+ * - `scope`: what the search is narrowed to, shown before the input (a pill,
27
+ * a menu); give it its own `load`, `items` and `placeholder`. A new `load`
28
+ * searches again for what's typed. `onScopeClear`: Backspace in the empty
29
+ * input
26
30
  *
27
31
  * `formatHotkey('k')` gives how the shortcut reads here ('⌘K' or 'Ctrl K'),
28
32
  * for a trigger's hint.
29
33
  */
30
- import { type ReactElement } from 'react';
34
+ import { type ReactElement, type ReactNode } from 'react';
31
35
  import { Dialog } from '@ark-ui/react/dialog';
32
36
  import '../icons.js';
33
37
  import { type Classes } from '../utils.js';
@@ -60,7 +64,9 @@ export type CommandProps = Omit<Dialog.ContentProps, 'onSelect'> & {
60
64
  emptyText?: string;
61
65
  loadingText?: string;
62
66
  hints?: boolean;
67
+ scope?: ReactNode;
68
+ onScopeClear?: () => void;
63
69
  classes?: Classes<'backdrop' | 'positioner' | 'content' | 'search' | 'searchIcon' | 'input' | 'list' | 'status' | 'group' | 'groupLabel' | 'item' | 'icon' | 'text' | 'label' | 'description' | 'shortcut' | 'hints' | 'key'>;
64
70
  };
65
71
  export declare function formatHotkey(key?: string): string;
66
- export declare function Command({ items, load, debounce, onSelect, trigger, open: openProp, defaultOpen, onOpenChange, hotkey, navigate, label: labelProp, placeholder: placeholderProp, emptyText: emptyTextProp, loadingText: loadingTextProp, hints, className, classes, ...rest }: CommandProps): import("react").JSX.Element;
72
+ export declare function Command({ items, load, debounce, onSelect, trigger, open: openProp, defaultOpen, onOpenChange, hotkey, navigate, label: labelProp, placeholder: placeholderProp, emptyText: emptyTextProp, loadingText: loadingTextProp, hints, scope, onScopeClear, className, classes, ...rest }: CommandProps): import("react").JSX.Element;
@@ -24,6 +24,10 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
24
24
  * `navigate`; by default a link is clicked
25
25
  * - `placeholder`, `emptyText`, `label`; `hints`: false hides the keyboard
26
26
  * hints along the bottom
27
+ * - `scope`: what the search is narrowed to, shown before the input (a pill,
28
+ * a menu); give it its own `load`, `items` and `placeholder`. A new `load`
29
+ * searches again for what's typed. `onScopeClear`: Backspace in the empty
30
+ * input
27
31
  *
28
32
  * `formatHotkey('k')` gives how the shortcut reads here ('⌘K' or 'Ctrl K'),
29
33
  * for a trigger's hint.
@@ -57,7 +61,9 @@ function go(href) {
57
61
  link.click();
58
62
  link.remove();
59
63
  }
60
- export function Command({ items = [], load = null, debounce = 200, onSelect, trigger = null, open: openProp, defaultOpen = false, onOpenChange, hotkey = 'k', navigate = null, label: labelProp, placeholder: placeholderProp, emptyText: emptyTextProp, loadingText: loadingTextProp, hints = true, className, classes, ...rest }) {
64
+ // Zag's own scrolling, off: the Command does its own (see below).
65
+ const noScroll = () => { };
66
+ export function Command({ items = [], load = null, debounce = 200, onSelect, trigger = null, open: openProp, defaultOpen = false, onOpenChange, hotkey = 'k', navigate = null, label: labelProp, placeholder: placeholderProp, emptyText: emptyTextProp, loadingText: loadingTextProp, hints = true, scope = null, onScopeClear, className, classes, ...rest }) {
61
67
  const t = useMessages();
62
68
  const label = labelProp ?? t.command.label;
63
69
  const placeholder = placeholderProp ?? t.command.placeholder;
@@ -111,8 +117,7 @@ export function Command({ items = [], load = null, debounce = 200, onSelect, tri
111
117
  }
112
118
  // Keeps the highlighted item in view, scrolling only the list, and showing
113
119
  // a group's heading along with its first item.
114
- function scrollToItem({ getElement }) {
115
- const el = getElement();
120
+ function scrollToItem(el) {
116
121
  const box = listEl.current;
117
122
  if (!el || !box)
118
123
  return;
@@ -129,10 +134,15 @@ export function Command({ items = [], load = null, debounce = 200, onSelect, tri
129
134
  }
130
135
  // The value stays empty, so picking the same command again still counts.
131
136
  const list = useListbox({
132
- collection, value: [], loopFocus: true, scrollToIndexFn: scrollToItem, onSelect: (details) => choose(details.value),
137
+ collection, value: [], loopFocus: true, scrollToIndexFn: noScroll, onSelect: (details) => choose(details.value),
133
138
  });
134
139
  const api = useRef(list);
135
140
  useEffect(() => { api.current = list; });
141
+ // Zag scrolls only a list that was there when it started, and this one comes with the dialog: ours, on each highlight.
142
+ // An item highlighted by the pointer is already in view, so it doesn't move.
143
+ useEffect(() => {
144
+ scrollToItem(listEl.current?.querySelector('[data-part=item][data-highlighted]') ?? null);
145
+ }, [list.highlightedValue]);
136
146
  // Each time it opens (from the trigger, the hotkey or `open`): an empty
137
147
  // search over all the commands.
138
148
  const latestSearch = useRef(search);
@@ -143,6 +153,22 @@ export function Command({ items = [], load = null, debounce = 200, onSelect, tri
143
153
  setLoaded([]);
144
154
  latestSearch.current('');
145
155
  }, [open]);
156
+ // A new scope's `load`: its results for what's typed, and back to the input
157
+ // (a menu in `scope` would otherwise keep the focus).
158
+ const latestQuery = useRef(query);
159
+ useEffect(() => { latestQuery.current = query; });
160
+ const firstLoad = useRef(load);
161
+ useEffect(() => {
162
+ if (load === firstLoad.current)
163
+ return;
164
+ firstLoad.current = load;
165
+ if (!open)
166
+ return;
167
+ setLoaded([]);
168
+ latestSearch.current(latestQuery.current);
169
+ const id = setTimeout(() => input.current?.focus(), 0);
170
+ return () => clearTimeout(id);
171
+ }, [load, open]);
146
172
  // The first result stays highlighted as the results change, so Enter picks it.
147
173
  const resultKeys = results.map(keyOf).join('\u0000');
148
174
  useEffect(() => {
@@ -163,7 +189,8 @@ export function Command({ items = [], load = null, debounce = 200, onSelect, tri
163
189
  addEventListener('keydown', keydown);
164
190
  return () => removeEventListener('keydown', keydown);
165
191
  }, [hotkey, open, setOpen]);
166
- return (_jsxs(Dialog.Root, { open: open, onOpenChange: (details) => setOpen(details.open), lazyMount: true, unmountOnExit: true, initialFocusEl: () => input.current, children: [trigger && _jsx(Dialog.Trigger, { asChild: true, children: trigger }), _jsxs(Portal, { children: [_jsx(Dialog.Backdrop, { ...part('backdrop') }), _jsx(Dialog.Positioner, { ...part('positioner'), children: _jsxs(Dialog.Content, { ...rest, ...part('content'), className: cx(className, classes?.content), children: [_jsx(Dialog.Title, { ...part('title'), hidden: true, children: label }), _jsxs(Listbox.RootProvider, { value: list, children: [_jsx(Listbox.Label, { hidden: true, children: label }), _jsxs("div", { ...part('search'), children: [_jsx("iconify-icon", { ...part('search-icon'), icon: "lucide:search", "aria-hidden": "true" }), _jsx(Listbox.Input, { ref: input, className: classes?.input, autoHighlight: true, type: "text", placeholder: placeholder, value: query, onChange: (e) => search(e.target.value) }), _jsx("kbd", { ...part('key'), "data-esc": "", children: t.command.esc })] }), _jsx(Listbox.Content, { ref: listEl, className: classes?.list, children: !results.length
192
+ return (_jsxs(Dialog.Root, { open: open, onOpenChange: (details) => setOpen(details.open), lazyMount: true, unmountOnExit: true, initialFocusEl: () => input.current, children: [trigger && _jsx(Dialog.Trigger, { asChild: true, children: trigger }), _jsxs(Portal, { children: [_jsx(Dialog.Backdrop, { ...part('backdrop') }), _jsx(Dialog.Positioner, { ...part('positioner'), children: _jsxs(Dialog.Content, { ...rest, ...part('content'), className: cx(className, classes?.content), children: [_jsx(Dialog.Title, { ...part('title'), hidden: true, children: label }), _jsxs(Listbox.RootProvider, { value: list, children: [_jsx(Listbox.Label, { hidden: true, children: label }), _jsxs("div", { ...part('search'), children: [_jsx("iconify-icon", { ...part('search-icon'), icon: "lucide:search", "aria-hidden": "true" }), scope, _jsx(Listbox.Input, { ref: input, className: classes?.input, autoHighlight: true, type: "text", placeholder: placeholder, value: query, onChange: (e) => search(e.target.value), onKeyDown: (e) => { if (e.key === 'Backspace' && !query)
193
+ onScopeClear?.(); } }), _jsx("kbd", { ...part('key'), "data-esc": "", children: t.command.esc })] }), _jsx(Listbox.Content, { ref: listEl, className: classes?.list, children: !results.length
167
194
  ? _jsx("div", { ...part('status'), children: loading ? loadingText : emptyText })
168
195
  : groups.map((group) => (_jsxs(Listbox.ItemGroup, { className: classes?.group, children: [group.name && _jsx(Listbox.ItemGroupLabel, { className: classes?.groupLabel, children: group.name }), group.items.map((item) => (_jsxs(Listbox.Item, { item: item, highlightOnHover: true, className: classes?.item, children: [item.icon && _jsx("iconify-icon", { ...part('icon'), icon: item.icon, "aria-hidden": "true" }), _jsxs("span", { ...part('text'), children: [_jsx("span", { ...part('label'), children: item.label }), item.description && _jsx("span", { ...part('description'), children: item.description })] }), item.shortcut && _jsx("kbd", { ...part('shortcut'), children: item.shortcut })] }, keyOf(item))))] }, group.name))) }), hints && (_jsxs("div", { ...part('hints'), "aria-hidden": "true", children: [_jsxs("span", { children: [_jsx("kbd", { ...part('key'), children: "\u2191" }), _jsx("kbd", { ...part('key'), children: "\u2193" }), " ", t.command.toNavigate] }), _jsxs("span", { children: [_jsx("kbd", { ...part('key'), children: "\u21B5" }), " ", t.command.toSelect] })] }))] })] }) })] })] }));
169
196
  }
@@ -11,7 +11,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
11
11
  * - `maxRows`: grow up to this many lines, then scroll
12
12
  * - `autogrow`: false for a fixed height the user can resize
13
13
  */
14
- import { useCallback, useId, useLayoutEffect, useRef } from 'react';
14
+ import { useCallback, useEffect, useId, useLayoutEffect, useRef } from 'react';
15
15
  import { Field, useFieldContext } from '@ark-ui/react/field';
16
16
  import { cx, partsOf } from '../utils.js';
17
17
  import '../theme.css';
@@ -22,10 +22,11 @@ export function Textarea({ label = null, rows = 3, maxRows = null, autogrow = tr
22
22
  const ownId = useId();
23
23
  const id = rest.id ?? `${ownId}-textarea`;
24
24
  const el = useRef(null);
25
- // Fits the height to the content, capped at maxRows.
25
+ // Fits the height to the content, capped at maxRows. Not while it isn't laid out (in a closed
26
+ // popover, a hidden tab): it would measure 0 and stay flat.
26
27
  const fit = useCallback(() => {
27
28
  const area = el.current;
28
- if (!autogrow || !area)
29
+ if (!autogrow || !area || !area.clientWidth)
29
30
  return;
30
31
  const line = parseFloat(getComputedStyle(area).lineHeight);
31
32
  const max = maxRows ? line * maxRows : Infinity;
@@ -34,6 +35,21 @@ export function Textarea({ label = null, rows = 3, maxRows = null, autogrow = tr
34
35
  area.style.overflowY = area.scrollHeight > max ? 'auto' : 'hidden';
35
36
  }, [autogrow, maxRows]);
36
37
  useLayoutEffect(fit, [fit, value]);
38
+ // Again when its width changes: once it's shown, and when the lines rewrap.
39
+ useEffect(() => {
40
+ const area = el.current;
41
+ if (!autogrow || !area)
42
+ return;
43
+ let width = area.clientWidth;
44
+ const observer = new ResizeObserver(() => {
45
+ if (area.clientWidth === width)
46
+ return;
47
+ width = area.clientWidth;
48
+ fit();
49
+ });
50
+ observer.observe(area);
51
+ return () => observer.disconnect();
52
+ }, [autogrow, fit]);
37
53
  function change(e) {
38
54
  onChange?.(e);
39
55
  onValueChange?.(e.target.value);
@@ -33,7 +33,10 @@ import '../theme.css';
33
33
  import './tooltip.css';
34
34
  function TooltipShortcut({ content = null, placement = 'top', openDelay = 0, closeDelay, interactive = false, disabled = false, open, defaultOpen, onOpenChange, className, classes, children, ...rest }) {
35
35
  const part = partsOf('tooltip', classes);
36
- return (_jsxs(Ark.Root, { openDelay: openDelay, interactive: interactive, disabled: disabled, lazyMount: true, unmountOnExit: true, positioning: { placement, strategy: 'fixed', gutter: 8 }, ...(closeDelay !== undefined && { closeDelay }), ...(open !== undefined && { open }), ...(defaultOpen !== undefined && { defaultOpen }), ...on('onOpenChange', onOpenChange, (details) => details.open), children: [isValidElement(children) && !children.props.disabled
36
+ const anchored = isValidElement(children) && !children.props.disabled;
37
+ // A trigger that's already another's (a menu's, a popover's) keeps that one's id: the tooltip finds it by it
38
+ const triggerId = anchored ? children.props.id : undefined;
39
+ return (_jsxs(Ark.Root, { openDelay: openDelay, ...(triggerId && { ids: { trigger: triggerId } }), interactive: interactive, disabled: disabled, lazyMount: true, unmountOnExit: true, positioning: { placement, strategy: 'fixed', gutter: 8 }, ...(closeDelay !== undefined && { closeDelay }), ...(open !== undefined && { open }), ...(defaultOpen !== undefined && { defaultOpen }), ...on('onOpenChange', onOpenChange, (details) => details.open), children: [anchored
37
40
  ? _jsx(Ark.Trigger, { asChild: true, children: children })
38
41
  : _jsx(Ark.Trigger, { asChild: true, children: _jsx("span", { ...part('trigger'), children: children }) }), _jsx(Portal, { children: _jsx(Ark.Positioner, { ...part('positioner'), children: _jsxs(Ark.Content, { ...rest, ...part('content'), className: cx(className, classes?.content), children: [_jsx(Ark.Arrow, { ...part('arrow'), children: _jsx(Ark.ArrowTip, { ...part('arrow-tip') }) }), content] }) }) })] }));
39
42
  }
@@ -2,9 +2,9 @@
2
2
  [data-scope=tooltip][data-part=trigger] { display: inline-flex; }
3
3
  /* The positioner carries the z-index itself. Zag sets it inline as
4
4
  var(--z-index) but doesn't always fill in the variable, so set both. */
5
- [data-scope=tooltip][data-part=positioner] { z-index: 60; --z-index: 60; }
5
+ [data-scope=tooltip][data-part=positioner] { z-index: 110; --z-index: 110; } /* above dialogs and sheets (100) */
6
6
  [data-scope=tooltip][data-part=content] {
7
- z-index: 60; max-width: 16rem; padding: 0.25rem 0.5rem; white-space: normal; font-weight: 400;
7
+ z-index: 110; max-width: 16rem; padding: 0.25rem 0.5rem; white-space: normal; font-weight: 400;
8
8
  background: var(--ui-text); color: var(--ui-surface); font-family: var(--ui-font); font-size: 12px; line-height: 1.4;
9
9
  border-radius: calc(var(--ui-radius) - 2px); box-shadow: var(--ui-shadow);
10
10
  --arrow-size: 8px; --arrow-background: var(--ui-text);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milktop/react-ui",
3
- "version": "0.3.10",
3
+ "version": "0.3.12",
4
4
  "description": "React components on Ark UI (Zag state machines), with imba-ui's API and design",
5
5
  "license": "MIT",
6
6
  "author": "André Goldstein",