@hussmartinez/ui 0.6.0 → 0.7.0

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.
package/dist/react.d.ts CHANGED
@@ -22,6 +22,7 @@ import { ReactNode } from 'react';
22
22
  import { Ref } from 'react';
23
23
  import { RefAttributes } from 'react';
24
24
  import { RefObject } from 'react';
25
+ import type { ShortcutPart } from '@hussmartinez/sdk-core';
25
26
  import type { SVGProps } from 'react';
26
27
 
27
28
  /**
@@ -993,6 +994,48 @@ export declare interface StyleProps<Part extends string> {
993
994
  readonly unstyled?: boolean | undefined;
994
995
  }
995
996
 
997
+ /**
998
+ * A single-line text input, styled like the family's other fields. Inside a
999
+ * `Field`, it takes the field's label, description and error.
1000
+ *
1001
+ * @example
1002
+ * ```tsx
1003
+ * <Field label={t("export.fileName")} error={nameError}>
1004
+ * <TextField value={name} onValueChange={setName} autoComplete="off" />
1005
+ * </Field>
1006
+ * ```
1007
+ *
1008
+ * @public
1009
+ */
1010
+ export declare const TextField: ForwardRefExoticComponent<TextFieldProps & RefAttributes<HTMLInputElement>>;
1011
+
1012
+ /**
1013
+ * Props of {@link TextField}. Native input props are passed through, so it
1014
+ * works controlled with `value` and `onChange`, or uncontrolled with
1015
+ * `defaultValue`.
1016
+ *
1017
+ * @public
1018
+ */
1019
+ export declare interface TextFieldProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "className" | "type">, StyleProps<"root"> {
1020
+ /** Defaults to `text`. */
1021
+ readonly type?: TextFieldType | undefined;
1022
+ /** Called with the new text as it changes, as well as `onChange`. */
1023
+ readonly onValueChange?: ((value: string) => void) | undefined;
1024
+ /**
1025
+ * The accessible name, such as "File name". Leave it out inside a
1026
+ * `Field`, or when a visible `<label>` points at the field's `id`, or
1027
+ * `aria-labelledby` names it.
1028
+ */
1029
+ readonly label?: string | undefined;
1030
+ }
1031
+
1032
+ /**
1033
+ * The kinds of single-line text a {@link TextField} takes.
1034
+ *
1035
+ * @public
1036
+ */
1037
+ export declare type TextFieldType = "text" | "search" | "email" | "url" | "tel" | "password";
1038
+
996
1039
  /**
997
1040
  * A validated theme. Create it with `createTheme`; its values are
998
1041
  * frozen and safe to write into CSS.
@@ -1253,6 +1296,77 @@ export declare const ToolbarSeparator: ForwardRefExoticComponent<ToolbarSeparato
1253
1296
  */
1254
1297
  export declare type ToolbarSeparatorProps = Omit<HTMLAttributes<HTMLHRElement>, "children">;
1255
1298
 
1299
+ /**
1300
+ * A tooltip: short text about a control, shown while the pointer rests on it
1301
+ * or while it has keyboard focus (WAI-ARIA tooltip pattern, WCAG 1.4.13).
1302
+ *
1303
+ * - It shows after `delay` on hover, and at once on keyboard focus, never
1304
+ * on a mouse click's focus or on touch.
1305
+ * - The pointer can move onto it without it closing, and it stays until the
1306
+ * pointer and focus leave. Escape closes it without moving focus, and
1307
+ * doesn't close a surrounding dialog.
1308
+ * - **While it's open, the first Escape belongs to the tooltip.** It calls
1309
+ * `preventDefault()`, so an editor's own Escape action, such as cancelling
1310
+ * a crop, runs on the second press. sdk-core's shortcut registry already
1311
+ * skips handled events; a handler of your own should check
1312
+ * `event.defaultPrevented` too.
1313
+ * - It's in the top layer, through the Popover API, so no stacking context
1314
+ * can hide it, and it's placed above the trigger, or below when there's no
1315
+ * room, without a positioning library (docs/adr/0006).
1316
+ *
1317
+ * @example
1318
+ * ```tsx
1319
+ * <Tooltip type="label" content={t("toolbar.undo")} shortcut={shortcuts.describe("pdf.undo")[0]}>
1320
+ * <IconButton label={t("toolbar.undo")} icon={<UndoIcon />} onClick={undo} />
1321
+ * </Tooltip>
1322
+ * ```
1323
+ *
1324
+ * @public
1325
+ */
1326
+ export declare const Tooltip: ForwardRefExoticComponent<TooltipProps & RefAttributes<HTMLDivElement>>;
1327
+
1328
+ /**
1329
+ * The parts of a tooltip that `classNames` can target.
1330
+ *
1331
+ * @public
1332
+ */
1333
+ export declare type TooltipPart = "root" | "content" | "shortcut";
1334
+
1335
+ /**
1336
+ * Props of {@link Tooltip}.
1337
+ *
1338
+ * @public
1339
+ */
1340
+ export declare interface TooltipProps extends StyleProps<TooltipPart> {
1341
+ /** The text. Use text from your message catalogue. */
1342
+ readonly content: ReactNode;
1343
+ /**
1344
+ * How the text reaches assistive technology. `description`, the default,
1345
+ * describes the trigger (`aria-describedby`). `label` names it
1346
+ * (`aria-labelledby`): use it for an icon-only button whose tooltip says
1347
+ * what its label would, so screen readers don't hear it twice.
1348
+ */
1349
+ readonly type?: "description" | "label" | undefined;
1350
+ /**
1351
+ * The trigger's keyboard shortcut, as sdk-core's shortcut registry
1352
+ * describes it: `registry.describe(toolId)[0]`. The tooltip shows it in the
1353
+ * platform's notation, such as ⌘⇧Z or Ctrl+Shift+Z, and the trigger gets
1354
+ * `aria-keyshortcuts`.
1355
+ */
1356
+ readonly shortcut?: readonly ShortcutPart[] | undefined;
1357
+ /** Where it shows, when there's room: `top` (the default) or `bottom`. */
1358
+ readonly placement?: "top" | "bottom" | undefined;
1359
+ /** How long the pointer rests on the trigger before it shows, in milliseconds. Defaults to 500. */
1360
+ readonly delay?: number | undefined;
1361
+ /**
1362
+ * The trigger: one element that forwards its ref, such as `IconButton`.
1363
+ * A disabled trigger should use `focusableWhenDisabled`: a natively
1364
+ * disabled button gets neither focus nor pointer events, so its tooltip
1365
+ * can't show.
1366
+ */
1367
+ readonly children: ReactElement;
1368
+ }
1369
+
1256
1370
  /**
1257
1371
  * The `trim-end` icon: Lucide's `arrow-left-from-line`.
1258
1372
  *
package/dist/react.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import { a as uiMessages, i as warnInDevelopment, n as cssWriterOf, o as UiError, t as isThemePair } from "./shared/pair-DLVFV6vU.js";
3
3
  import { createMessages, platformOf, sdkCoreMessages } from "@hussmartinez/sdk-core";
4
- import { createContext, forwardRef, useCallback, useContext, useEffect, useId, useMemo, useRef, useState, useSyncExternalStore } from "react";
4
+ import { cloneElement, createContext, forwardRef, isValidElement, useCallback, useContext, useEffect, useId, useMemo, useRef, useState, useSyncExternalStore, version } from "react";
5
5
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6
6
  import { flushSync } from "react-dom";
7
7
  // src/internal/single-copy.ts
@@ -9,7 +9,7 @@ var REGISTRY = /* @__PURE__ */ Symbol.for("@hussmartinez/ui");
9
9
  var THIS_COPY = {};
10
10
  var checked = false;
11
11
  function ownVersion() {
12
- return "0.6.0";
12
+ return "0.7.0";
13
13
  }
14
14
  function checkSingleCopy() {
15
15
  if (checked || !(typeof process !== "undefined" && process.env.NODE_ENV !== "production")) return;
@@ -800,6 +800,27 @@ var Spinner = /* @__PURE__ */ forwardRef(function Spinner({ label, className, cl
800
800
  "data-hm-unstyled": unstyledAttribute(unstyled)
801
801
  });
802
802
  });
803
+ // src/components/text-field/TextField.tsx
804
+ var TextField = /* @__PURE__ */ forwardRef(function TextField({ type = "text", onValueChange, onChange, label, className, classNames, unstyled, ...props }, ref) {
805
+ const fieldProps = useFieldProps(props, label);
806
+ if (label === void 0 && !props["aria-label"] && !props["aria-labelledby"] && fieldProps.id === void 0) {
807
+ if (typeof process !== "undefined" && process.env.NODE_ENV !== "production") warnInDevelopment("TextField needs an accessible name: pass label, or put it in a Field. Use text from your message catalogue.");
808
+ }
809
+ const handleChange = (event) => {
810
+ onChange?.(event);
811
+ onValueChange?.(event.currentTarget.value);
812
+ };
813
+ return /* @__PURE__ */ jsx("input", {
814
+ ...props,
815
+ ...fieldProps,
816
+ ref,
817
+ type,
818
+ "aria-label": label ?? props["aria-label"],
819
+ className: cx("hm-text-field", classNames?.root, className),
820
+ "data-hm-unstyled": unstyledAttribute(unstyled),
821
+ onChange: handleChange
822
+ });
823
+ });
803
824
  // src/components/toggle-button/ToggleButton.tsx
804
825
  var ToggleButton = /* @__PURE__ */ forwardRef(function ToggleButton({ pressed: controlled, defaultPressed = false, onPressedChange, onClick, icon, label, className, ...props }, ref) {
805
826
  const [uncontrolled, setUncontrolled] = useState(defaultPressed);
@@ -950,6 +971,155 @@ var ToolbarSeparator = /* @__PURE__ */ forwardRef(function ToolbarSeparator({ cl
950
971
  className: cx("hm-toolbar-separator", className)
951
972
  });
952
973
  });
974
+ // src/react/use-platform.ts
975
+ var subscribe = () => () => void 0;
976
+ var onClient = () => platformOf(globalThis.navigator);
977
+ var onServer = () => "other";
978
+ function usePlatform() {
979
+ return useSyncExternalStore(subscribe, onClient, onServer);
980
+ }
981
+ // src/components/tooltip/Tooltip.tsx
982
+ var GRACE = 100;
983
+ var ariaKey = (part) => part.value === " " ? "Space" : part.value;
984
+ function place(trigger, tooltip, preferred) {
985
+ const anchor = trigger.getBoundingClientRect();
986
+ const box = tooltip.getBoundingClientRect();
987
+ const viewport = document.documentElement;
988
+ const above = anchor.top;
989
+ const below = viewport.clientHeight - anchor.bottom;
990
+ const fits = (side) => (side === "top" ? above : below) >= box.height;
991
+ const other = preferred === "top" ? "bottom" : "top";
992
+ const side = fits(preferred) || !fits(other) ? preferred : other;
993
+ const left = Math.min(Math.max(anchor.left + anchor.width / 2 - box.width / 2, 0), Math.max(viewport.clientWidth - box.width, 0));
994
+ tooltip.style.left = `${left}px`;
995
+ tooltip.style.top = `${side === "top" ? anchor.top - box.height : anchor.bottom}px`;
996
+ tooltip.dataset["hmPlacement"] = side;
997
+ }
998
+ function refOf(element) {
999
+ if (version.startsWith("18")) return element.ref;
1000
+ return element.props.ref;
1001
+ }
1002
+ function setRef(ref, value) {
1003
+ if (typeof ref === "function") ref(value);
1004
+ else if (ref) ref.current = value;
1005
+ }
1006
+ var Tooltip = /* @__PURE__ */ forwardRef(function Tooltip({ content, type = "description", shortcut, placement = "top", delay = 500, className, classNames, unstyled, children }, ref) {
1007
+ const id = useId();
1008
+ const contentId = `${id}-content`;
1009
+ const platform = usePlatform();
1010
+ const messages = useUiMessages();
1011
+ const [open, setOpen] = useState(false);
1012
+ const trigger = useRef(null);
1013
+ const tooltip = useRef(null);
1014
+ const timer = useRef(void 0);
1015
+ const dismissed = useRef(false);
1016
+ const clear = () => {
1017
+ clearTimeout(timer.current);
1018
+ timer.current = void 0;
1019
+ };
1020
+ const show = useCallback((after) => {
1021
+ clearTimeout(timer.current);
1022
+ if (dismissed.current) return;
1023
+ timer.current = setTimeout(() => setOpen(true), after);
1024
+ }, []);
1025
+ const hide = useCallback((after) => {
1026
+ clearTimeout(timer.current);
1027
+ timer.current = setTimeout(() => setOpen(false), after);
1028
+ }, []);
1029
+ useEffect(() => () => clearTimeout(timer.current), []);
1030
+ useEffect(() => {
1031
+ const element = tooltip.current;
1032
+ if (!element || typeof element.showPopover !== "function") return;
1033
+ if (!open) {
1034
+ if (element.matches(":popover-open")) element.hidePopover();
1035
+ return;
1036
+ }
1037
+ element.showPopover();
1038
+ if (trigger.current) place(trigger.current, element, placement);
1039
+ const close = () => setOpen(false);
1040
+ const onKeyDown = (event) => {
1041
+ if (event.key === "Escape") {
1042
+ event.preventDefault();
1043
+ dismissed.current = true;
1044
+ close();
1045
+ }
1046
+ };
1047
+ document.addEventListener("keydown", onKeyDown, true);
1048
+ window.addEventListener("scroll", close, true);
1049
+ window.addEventListener("resize", close);
1050
+ return () => {
1051
+ document.removeEventListener("keydown", onKeyDown, true);
1052
+ window.removeEventListener("scroll", close, true);
1053
+ window.removeEventListener("resize", close);
1054
+ };
1055
+ }, [open, placement]);
1056
+ if (!isValidElement(children)) {
1057
+ if (typeof process !== "undefined" && process.env.NODE_ENV !== "production") warnInDevelopment("Tooltip needs one element as its child, such as an IconButton.");
1058
+ return children;
1059
+ }
1060
+ const child = children;
1061
+ const own = child.props;
1062
+ const childRef = refOf(child);
1063
+ const keyshortcuts = shortcut && shortcut.length > 0 ? shortcut.map(ariaKey).join("+") : own["aria-keyshortcuts"];
1064
+ const text = messages.t;
1065
+ const triggerProps = {
1066
+ ref: (node) => {
1067
+ trigger.current = node;
1068
+ setRef(childRef, node);
1069
+ },
1070
+ ...type === "label" ? { "aria-labelledby": joinIds(contentId, own["aria-labelledby"]) } : { "aria-describedby": joinIds(own["aria-describedby"], contentId) },
1071
+ "aria-keyshortcuts": keyshortcuts,
1072
+ onPointerEnter: (event) => {
1073
+ own.onPointerEnter?.(event);
1074
+ if (event.pointerType !== "touch") show(open ? 0 : delay);
1075
+ },
1076
+ onPointerLeave: (event) => {
1077
+ own.onPointerLeave?.(event);
1078
+ dismissed.current = false;
1079
+ hide(GRACE);
1080
+ },
1081
+ onPointerDown: (event) => {
1082
+ own.onPointerDown?.(event);
1083
+ clear();
1084
+ setOpen(false);
1085
+ },
1086
+ onFocus: (event) => {
1087
+ own.onFocus?.(event);
1088
+ if (event.currentTarget.matches(":focus-visible") && !dismissed.current) {
1089
+ clear();
1090
+ setOpen(true);
1091
+ }
1092
+ },
1093
+ onBlur: (event) => {
1094
+ own.onBlur?.(event);
1095
+ dismissed.current = false;
1096
+ clear();
1097
+ setOpen(false);
1098
+ }
1099
+ };
1100
+ return /* @__PURE__ */ jsxs(Fragment, { children: [cloneElement(child, triggerProps), /* @__PURE__ */ jsxs("div", {
1101
+ ref: (node) => {
1102
+ tooltip.current = node;
1103
+ setRef(ref, node);
1104
+ },
1105
+ id,
1106
+ role: "tooltip",
1107
+ popover: "manual",
1108
+ className: cx("hm-tooltip", classNames?.root, className),
1109
+ "data-hm-unstyled": unstyledAttribute(unstyled),
1110
+ onPointerEnter: () => clear(),
1111
+ onPointerLeave: () => hide(GRACE),
1112
+ children: [/* @__PURE__ */ jsx("span", {
1113
+ id: contentId,
1114
+ className: cx("hm-tooltip-content", classNames?.content),
1115
+ children: content
1116
+ }), shortcut && shortcut.length > 0 ? /* @__PURE__ */ jsx("span", {
1117
+ className: cx("hm-tooltip-shortcut", classNames?.shortcut),
1118
+ "aria-hidden": "true",
1119
+ children: shortcut.map((part, index) => /* @__PURE__ */ jsxs("span", { children: [index > 0 && platform === "other" ? "+" : null, /* @__PURE__ */ jsx("kbd", { children: part.message ? text(part.message) : part.value })] }, index))
1120
+ }) : null]
1121
+ })] });
1122
+ });
953
1123
  // src/components/visually-hidden/VisuallyHidden.tsx
954
1124
  var VisuallyHidden = /* @__PURE__ */ forwardRef(function VisuallyHidden({ as: Element = "span", className, ...props }, ref) {
955
1125
  return /* @__PURE__ */ jsx(Element, {
@@ -1348,13 +1518,6 @@ function ThemeProvider(props) {
1348
1518
  }), children]
1349
1519
  });
1350
1520
  }
1351
- // src/react/use-platform.ts
1352
- var subscribe = () => () => void 0;
1353
- var onClient = () => platformOf(globalThis.navigator);
1354
- var onServer = () => "other";
1355
- function usePlatform() {
1356
- return useSyncExternalStore(subscribe, onClient, onServer);
1357
- }
1358
- export { AddIcon, AnnouncerProvider, Button, CheckIcon, ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon, CloseIcon, CropIcon, DeleteIcon, Dialog, DownloadIcon, FadeInIcon, FadeOutIcon, Field, FileDropZone, FitIcon, FlipHorizontalIcon, FlipVerticalIcon, HighlighterIcon, IconButton, InfoIcon, LoopIcon, MenuIcon, MoreIcon, MutedIcon, NumberField, OpenIcon, PageGridIcon, PageSingleIcon, PanIcon, PauseIcon, PenIcon, PlayIcon, ProgressBar, RedoIcon, RemoveIcon, RotateLeftIcon, RotateRightIcon, SearchIcon, SegmentedControl, SelectIcon, SkipBackIcon, SkipForwardIcon, Slider, SnapIcon, Spinner, SplitIcon, StopIcon, ThemeProvider, ToggleButton, Toolbar, ToolbarGroup, ToolbarSeparator, TrimEndIcon, TrimStartIcon, UiMessagesProvider, UndoIcon, UploadIcon, VisuallyHidden, VolumeIcon, WarningIcon, ZoomInIcon, ZoomOutIcon, uiMessages, useAnnounce, usePlatform, useUiMessages };
1521
+ export { AddIcon, AnnouncerProvider, Button, CheckIcon, ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon, CloseIcon, CropIcon, DeleteIcon, Dialog, DownloadIcon, FadeInIcon, FadeOutIcon, Field, FileDropZone, FitIcon, FlipHorizontalIcon, FlipVerticalIcon, HighlighterIcon, IconButton, InfoIcon, LoopIcon, MenuIcon, MoreIcon, MutedIcon, NumberField, OpenIcon, PageGridIcon, PageSingleIcon, PanIcon, PauseIcon, PenIcon, PlayIcon, ProgressBar, RedoIcon, RemoveIcon, RotateLeftIcon, RotateRightIcon, SearchIcon, SegmentedControl, SelectIcon, SkipBackIcon, SkipForwardIcon, Slider, SnapIcon, Spinner, SplitIcon, StopIcon, TextField, ThemeProvider, ToggleButton, Toolbar, ToolbarGroup, ToolbarSeparator, Tooltip, TrimEndIcon, TrimStartIcon, UiMessagesProvider, UndoIcon, UploadIcon, VisuallyHidden, VolumeIcon, WarningIcon, ZoomInIcon, ZoomOutIcon, uiMessages, useAnnounce, usePlatform, useUiMessages };
1359
1522
 
1360
1523
  //# sourceMappingURL=react.js.map