@milktop/react-ui 0.3.10 → 0.3.11

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,7 @@ 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
+ 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
65
  const t = useMessages();
62
66
  const label = labelProp ?? t.command.label;
63
67
  const placeholder = placeholderProp ?? t.command.placeholder;
@@ -143,6 +147,22 @@ export function Command({ items = [], load = null, debounce = 200, onSelect, tri
143
147
  setLoaded([]);
144
148
  latestSearch.current('');
145
149
  }, [open]);
150
+ // A new scope's `load`: its results for what's typed, and back to the input
151
+ // (a menu in `scope` would otherwise keep the focus).
152
+ const latestQuery = useRef(query);
153
+ useEffect(() => { latestQuery.current = query; });
154
+ const firstLoad = useRef(load);
155
+ useEffect(() => {
156
+ if (load === firstLoad.current)
157
+ return;
158
+ firstLoad.current = load;
159
+ if (!open)
160
+ return;
161
+ setLoaded([]);
162
+ latestSearch.current(latestQuery.current);
163
+ const id = setTimeout(() => input.current?.focus(), 0);
164
+ return () => clearTimeout(id);
165
+ }, [load, open]);
146
166
  // The first result stays highlighted as the results change, so Enter picks it.
147
167
  const resultKeys = results.map(keyOf).join('\u0000');
148
168
  useEffect(() => {
@@ -163,7 +183,8 @@ export function Command({ items = [], load = null, debounce = 200, onSelect, tri
163
183
  addEventListener('keydown', keydown);
164
184
  return () => removeEventListener('keydown', keydown);
165
185
  }, [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
186
+ 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)
187
+ onScopeClear?.(); } }), _jsx("kbd", { ...part('key'), "data-esc": "", children: t.command.esc })] }), _jsx(Listbox.Content, { ref: listEl, className: classes?.list, children: !results.length
167
188
  ? _jsx("div", { ...part('status'), children: loading ? loadingText : emptyText })
168
189
  : 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
190
  }
@@ -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.11",
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",