strata-react 1.0.2 → 1.0.4

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/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { C as Tooltip, S as readErrorBody, T as typeSteps, _ as Textarea, a as toLoad, b as describeHttpError, c as ColorSchemeChoice, d as useColorScheme, f as useSystemColorScheme, g as NativeSelect, h as Input, i as staleOf, l as ThemeProvider, m as Field, n as createQueryClient, o as useLoad, p as ChoiceGroup, r as fetchJson, s as COLOR_SCHEME_STORAGE_KEY, t as StrataQueryProvider, u as colorSchemeScript, v as HttpError, w as cn, x as fieldErrorsOf, y as describeActionError } from "./query-BVh4zv7Y.js";
1
+ import { C as readErrorBody, E as typeSteps, S as fieldErrorsOf, T as cn, _ as Textarea, a as toLoad, b as describeActionError, c as ColorSchemeChoice, d as useColorScheme, f as useSystemColorScheme, g as NativeSelect, h as Input, i as staleOf, l as ThemeProvider, m as Field, n as createQueryClient, o as useLoad, p as ChoiceGroup, r as fetchJson, s as COLOR_SCHEME_STORAGE_KEY, t as StrataQueryProvider, u as colorSchemeScript, v as useFieldControl, w as Tooltip, x as describeHttpError, y as HttpError } from "./query-gC6I6Cen.js";
2
2
  import * as React from "react";
3
- import { Autocomplete, Button as Button$1, Checkbox as Checkbox$1, Dialog as Dialog$1, DialogTrigger, Header, Heading, Input as Input$1, Keyboard, Menu as Menu$1, MenuItem, MenuSection, MenuTrigger, Modal, ModalOverlay, Popover as Popover$1, SearchField, Separator, SubmenuTrigger, Switch as Switch$1, Tab, TabList, TabPanel, Tabs as Tabs$1, Text, ToggleButton, ToggleButtonGroup, Toolbar as Toolbar$1, UNSTABLE_Toast, UNSTABLE_ToastContent, UNSTABLE_ToastQueue, UNSTABLE_ToastRegion, useFilter } from "react-aria-components";
3
+ import { Autocomplete, Button as Button$1, Checkbox as Checkbox$1, Dialog as Dialog$1, DialogTrigger, Header, Heading, I18nProvider, Input as Input$1, Keyboard, Menu as Menu$1, MenuItem, MenuSection, MenuTrigger, Modal, ModalOverlay, NumberField as NumberField$1, Popover as Popover$1, SearchField, Separator, Slider as Slider$1, SliderOutput, SliderThumb, SliderTrack, SubmenuTrigger, Switch as Switch$1, Tab, TabList, TabPanel, Tabs as Tabs$1, Text, ToggleButton, ToggleButtonGroup, Toolbar as Toolbar$1, UNSTABLE_Toast, UNSTABLE_ToastContent, UNSTABLE_ToastQueue, UNSTABLE_ToastRegion, useFilter } from "react-aria-components";
4
4
  import { cva } from "class-variance-authority";
5
5
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6
- import { ArrowDown, ArrowUp, ArrowUpDown, ArrowUpRight, Ban, Check, ChevronRight, CircleAlert, CircleCheck, History, ImageOff, Info, LoaderCircle, Minus, RotateCcw, Search, Trash2, TriangleAlert, X } from "lucide-react";
6
+ import { ArrowDown, ArrowUp, ArrowUpDown, ArrowUpRight, Ban, Check, ChevronRight, CircleAlert, CircleCheck, History, ImageOff, Info, LoaderCircle, Maximize, Minimize, Minus, RotateCcw, Scan, Search, Trash2, TriangleAlert, X, ZoomIn, ZoomOut } from "lucide-react";
7
7
  import { Link, NavLink, useSearchParams } from "react-router";
8
8
  import { createSortedRowModel, flexRender, rowSortingFeature, tableFeatures, useTable } from "@tanstack/react-table";
9
9
  import { useVirtualizer } from "@tanstack/react-virtual";
@@ -1658,6 +1658,313 @@ function Skeleton({ className }) {
1658
1658
  className: cn("rounded-control bg-muted motion-safe:animate-pulse", className)
1659
1659
  });
1660
1660
  }
1661
+ // src/lib/number-input.ts
1662
+ /** Reads `#rgb`, `#rrggbb` (with or without the #) as lower-case `#rrggbb`, or null. */
1663
+ function parseHex(text) {
1664
+ const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(text.trim());
1665
+ if (!m) return null;
1666
+ const h = m[1].toLowerCase();
1667
+ return "#" + (h.length === 3 ? [...h].map((c) => c + c).join("") : h);
1668
+ }
1669
+ function luminance(hex) {
1670
+ const v = [
1671
+ 1,
1672
+ 3,
1673
+ 5
1674
+ ].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255).map((c) => c <= .03928 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4);
1675
+ return .2126 * v[0] + .7152 * v[1] + .0722 * v[2];
1676
+ }
1677
+ /** WCAG contrast ratio between two `#rrggbb` colours, 1 to 21. */
1678
+ function contrastRatio(a, b) {
1679
+ const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
1680
+ return (hi + .05) / (lo + .05);
1681
+ }
1682
+ // src/components/value-controls.tsx
1683
+ var box = "flex h-(--control-h) w-full min-w-0 items-center rounded-control border border-input bg-background px-3 type-body text-foreground transition-state data-[disabled]:cursor-not-allowed data-[disabled]:bg-muted data-[disabled]:opacity-70 has-[input:disabled]:cursor-not-allowed has-[input:disabled]:bg-muted has-[input:disabled]:opacity-70 aria-invalid:border-danger has-[input:focus-visible]:outline-2 has-[input:focus-visible]:outline-offset-2 has-[input:focus-visible]:outline-ring";
1684
+ /** A number with its unit joined by a non-breaking space; `%` and `°` stay attached. */
1685
+ function withUnit(text, unit) {
1686
+ if (!unit) return text;
1687
+ return unit === "%" || unit === "°" ? text + unit : text + "\xA0" + unit;
1688
+ }
1689
+ var intlUnits = {
1690
+ "%": "percent",
1691
+ "°": "degree",
1692
+ ms: "millisecond",
1693
+ s: "second",
1694
+ kB: "kilobyte",
1695
+ MB: "megabyte",
1696
+ GB: "gigabyte",
1697
+ kbps: "kilobit-per-second",
1698
+ Mbps: "megabit-per-second"
1699
+ };
1700
+ function decimalsOf(n) {
1701
+ const s = String(n);
1702
+ if (s.includes("e-")) return Number(s.split("e-")[1]) + (s.split("e-")[0].split(".")[1]?.length ?? 0);
1703
+ return s.split(".")[1]?.length ?? 0;
1704
+ }
1705
+ function useNumberText(locale, places) {
1706
+ return React.useMemo(() => {
1707
+ const f = new Intl.NumberFormat(locale, {
1708
+ maximumFractionDigits: places,
1709
+ minimumFractionDigits: 0
1710
+ });
1711
+ return (n) => f.format(n);
1712
+ }, [locale, places]);
1713
+ }
1714
+ function formatOptionsFor(places, unit) {
1715
+ const base = {
1716
+ maximumFractionDigits: places,
1717
+ minimumFractionDigits: 0
1718
+ };
1719
+ const known = unit ? intlUnits[unit] : void 0;
1720
+ return known ? {
1721
+ ...base,
1722
+ style: "unit",
1723
+ unit: known,
1724
+ unitDisplay: "narrow"
1725
+ } : base;
1726
+ }
1727
+ function describeRange(min, max, show, unit) {
1728
+ const a = min === void 0 ? "" : withUnit(show(min), unit);
1729
+ const b = max === void 0 ? "" : withUnit(show(max), unit);
1730
+ if (min !== void 0 && max !== void 0) return `Enter a value from ${a} to ${b}.`;
1731
+ if (min !== void 0) return `Enter ${a} or more.`;
1732
+ if (max !== void 0) return `Enter ${b} or less.`;
1733
+ return "Enter a number.";
1734
+ }
1735
+ /** A number with a unit on React Aria's number field: typed input is validated, not clamped, so an out-of-range value is explained in the surrounding `Field` and never passed on; empty stays `null`. */
1736
+ function NumberField({ value, onChange, min, max, step = 1, places, unit, locale = format.locale, required = false, disabled, id: idProp, name, className, "aria-label": ariaLabel }) {
1737
+ const { field, report } = useFieldControl();
1738
+ const digits = places ?? Math.max(decimalsOf(step), min === void 0 ? 0 : decimalsOf(min));
1739
+ const show = useNumberText(locale, digits);
1740
+ const [own, setOwn] = React.useState(false);
1741
+ const unitId = React.useId();
1742
+ const [local, setLocal] = React.useState(value ?? NaN);
1743
+ const [seen, setSeen] = React.useState(value);
1744
+ if (seen !== value) {
1745
+ setSeen(value);
1746
+ setLocal(value ?? NaN);
1747
+ }
1748
+ const fail = (message) => {
1749
+ setOwn(true);
1750
+ report(message);
1751
+ };
1752
+ const ok = () => {
1753
+ setOwn(false);
1754
+ report(void 0);
1755
+ };
1756
+ const handle = (n) => {
1757
+ setLocal(n);
1758
+ if (Number.isNaN(n)) {
1759
+ if (required) return fail("Enter a value.");
1760
+ ok();
1761
+ if (value !== null) onChange(null);
1762
+ return;
1763
+ }
1764
+ if (min !== void 0 && n < min || max !== void 0 && n > max) return fail(describeRange(min, max, show, unit));
1765
+ ok();
1766
+ if (n !== value) onChange(n);
1767
+ };
1768
+ const invalid = field?.invalid || own;
1769
+ return /* @__PURE__ */ jsx(I18nProvider, {
1770
+ locale,
1771
+ children: /* @__PURE__ */ jsxs(NumberField$1, {
1772
+ value: local,
1773
+ onChange: handle,
1774
+ minValue: min,
1775
+ maxValue: max,
1776
+ step,
1777
+ formatOptions: {
1778
+ maximumFractionDigits: digits,
1779
+ minimumFractionDigits: 0
1780
+ },
1781
+ commitBehavior: "validate",
1782
+ isDisabled: disabled,
1783
+ isInvalid: invalid || void 0,
1784
+ name,
1785
+ "aria-label": ariaLabel ?? field?.label,
1786
+ className: cn(box, className),
1787
+ children: [/* @__PURE__ */ jsx(Input$1, {
1788
+ id: idProp ?? field?.id,
1789
+ placeholder: format.missing,
1790
+ "aria-describedby": [field?.describedBy, unit ? unitId : void 0].filter(Boolean).join(" ") || void 0,
1791
+ className: "min-w-0 flex-1 bg-transparent tabular-nums outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed"
1792
+ }), unit && /* @__PURE__ */ jsxs("span", {
1793
+ id: unitId,
1794
+ className: "ml-2 shrink-0 type-body-sm text-muted-foreground",
1795
+ children: [/* @__PURE__ */ jsx("span", {
1796
+ "aria-hidden": true,
1797
+ children: unit
1798
+ }), /* @__PURE__ */ jsx("span", {
1799
+ className: "sr-only",
1800
+ children: intlUnits[unit] ? intlUnits[unit].replace(/-/g, " ") : unit
1801
+ })]
1802
+ })]
1803
+ })
1804
+ });
1805
+ }
1806
+ /** A slider on React Aria with its current value always printed beside the track (and spoken with the unit when `Intl` knows it); `editable` adds a number field to type an exact value. */
1807
+ function Slider({ value, onChange, min, max, step = 1, unit, locale = format.locale, editable = false, hideValue = false, places, disabled, className, "aria-label": ariaLabel }) {
1808
+ const { field } = useFieldControl();
1809
+ const digits = places ?? Math.max(decimalsOf(step), decimalsOf(min));
1810
+ const show = useNumberText(locale, digits);
1811
+ const name = ariaLabel ?? field?.label ?? "Value";
1812
+ const text = withUnit(show(value), unit);
1813
+ return /* @__PURE__ */ jsx(I18nProvider, {
1814
+ locale,
1815
+ children: /* @__PURE__ */ jsxs(Slider$1, {
1816
+ value,
1817
+ onChange: (v) => onChange(v),
1818
+ minValue: min,
1819
+ maxValue: max,
1820
+ step,
1821
+ formatOptions: formatOptionsFor(digits, unit),
1822
+ isDisabled: disabled,
1823
+ "aria-label": name,
1824
+ "aria-describedby": field?.describedBy,
1825
+ className: cn("flex min-w-0 items-center gap-3", className),
1826
+ children: [/* @__PURE__ */ jsx(SliderTrack, {
1827
+ className: "relative flex h-(--control-h) min-w-0 flex-1 cursor-pointer items-center data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50",
1828
+ children: ({ state }) => /* @__PURE__ */ jsxs(Fragment, { children: [
1829
+ /* @__PURE__ */ jsx("span", {
1830
+ "aria-hidden": true,
1831
+ className: "absolute inset-x-0 h-1.5 rounded-pill bg-muted"
1832
+ }),
1833
+ /* @__PURE__ */ jsx("span", {
1834
+ "aria-hidden": true,
1835
+ className: "absolute left-0 h-1.5 rounded-pill bg-primary",
1836
+ style: { width: `${state.getThumbPercent(0) * 100}%` }
1837
+ }),
1838
+ /* @__PURE__ */ jsx(SliderThumb, { className: "top-1/2 size-5 rounded-pill border-2 border-primary bg-background data-[focus-visible]:outline-2 data-[focus-visible]:outline-offset-2 data-[focus-visible]:outline-ring" })
1839
+ ] })
1840
+ }), editable ? /* @__PURE__ */ jsx(NumberField, {
1841
+ className: "w-28 shrink-0",
1842
+ "aria-label": `${name}, exact value`,
1843
+ value,
1844
+ onChange: (v) => v !== null && onChange(v),
1845
+ min,
1846
+ max,
1847
+ step,
1848
+ unit,
1849
+ locale,
1850
+ places: digits,
1851
+ required: true,
1852
+ disabled
1853
+ }) : !hideValue && /* @__PURE__ */ jsx(SliderOutput, {
1854
+ "aria-hidden": true,
1855
+ className: "w-16 shrink-0 text-right type-body-sm tabular-nums text-foreground",
1856
+ children: text
1857
+ })]
1858
+ })
1859
+ });
1860
+ }
1861
+ /** Choose a colour with the platform picker, see and type its hex value, pick from the app's suggestions, and get a note when it would be hard to see on `against`. A half-typed value is never passed on. */
1862
+ function ColorField({ value, onChange, suggestions, against, againstLabel = "the background", minContrast = 3, disabled, id: idProp, className, "aria-label": ariaLabel }) {
1863
+ const { field, report } = useFieldControl();
1864
+ const [draft, setDraft] = React.useState(null);
1865
+ const [own, setOwn] = React.useState(false);
1866
+ const text = draft ?? value ?? "";
1867
+ const ratio = value && against ? contrastRatio(value, against) : null;
1868
+ const commitText = (t) => {
1869
+ if (t.trim() === "") {
1870
+ setOwn(false);
1871
+ report(void 0);
1872
+ if (value !== null) onChange(null);
1873
+ return true;
1874
+ }
1875
+ const hex = parseHex(t);
1876
+ if (!hex) {
1877
+ setOwn(true);
1878
+ report("Enter a colour like #1a7f64.");
1879
+ return false;
1880
+ }
1881
+ setOwn(false);
1882
+ report(void 0);
1883
+ if (hex !== value) onChange(hex);
1884
+ return true;
1885
+ };
1886
+ const pick = (hex) => {
1887
+ setDraft(null);
1888
+ setOwn(false);
1889
+ report(void 0);
1890
+ if (hex !== value) onChange(hex);
1891
+ };
1892
+ const invalid = field?.invalid || own;
1893
+ return /* @__PURE__ */ jsxs("div", {
1894
+ className: cn("grid min-w-0 gap-2", className),
1895
+ children: [
1896
+ /* @__PURE__ */ jsxs("div", {
1897
+ className: cn(box, "gap-2 px-2"),
1898
+ "aria-invalid": invalid || void 0,
1899
+ children: [/* @__PURE__ */ jsxs("span", {
1900
+ className: "relative size-6 shrink-0 overflow-hidden rounded-control border border-input",
1901
+ children: [/* @__PURE__ */ jsx("span", {
1902
+ "aria-hidden": true,
1903
+ className: cn("absolute inset-0", value === null && "bg-muted"),
1904
+ style: value ? { background: value } : void 0
1905
+ }), /* @__PURE__ */ jsx("input", {
1906
+ type: "color",
1907
+ "aria-label": ariaLabel ? `${ariaLabel}, picker` : "Colour picker",
1908
+ value: value ?? "#000000",
1909
+ disabled,
1910
+ onChange: (e) => pick(e.target.value),
1911
+ className: "absolute inset-0 size-full cursor-pointer opacity-0 disabled:cursor-not-allowed"
1912
+ })]
1913
+ }), /* @__PURE__ */ jsx("input", {
1914
+ id: idProp ?? field?.id,
1915
+ type: "text",
1916
+ autoComplete: "off",
1917
+ spellCheck: false,
1918
+ disabled,
1919
+ "aria-label": ariaLabel,
1920
+ "aria-describedby": field?.describedBy,
1921
+ "aria-invalid": invalid || void 0,
1922
+ placeholder: format.missing,
1923
+ value: text,
1924
+ onChange: (e) => setDraft(e.target.value),
1925
+ onBlur: () => {
1926
+ if (draft !== null && commitText(draft)) setDraft(null);
1927
+ },
1928
+ onKeyDown: (e) => {
1929
+ if (e.key === "Enter" && draft !== null && commitText(draft)) setDraft(null);
1930
+ if (e.key === "Escape" && draft !== null) {
1931
+ setDraft(null);
1932
+ setOwn(false);
1933
+ report(void 0);
1934
+ }
1935
+ },
1936
+ className: "min-w-0 flex-1 bg-transparent tabular-nums outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed"
1937
+ })]
1938
+ }),
1939
+ suggestions && suggestions.length > 0 && /* @__PURE__ */ jsx("div", {
1940
+ role: "group",
1941
+ "aria-label": "Suggested colours",
1942
+ className: "flex flex-wrap gap-1.5",
1943
+ children: suggestions.map((hex) => /* @__PURE__ */ jsx("button", {
1944
+ type: "button",
1945
+ disabled,
1946
+ "aria-label": hex,
1947
+ "aria-pressed": value === hex,
1948
+ onClick: () => pick(hex),
1949
+ className: "size-6 cursor-pointer rounded-control border border-input transition-state aria-pressed:outline-2 aria-pressed:outline-offset-2 aria-pressed:outline-foreground disabled:cursor-not-allowed disabled:opacity-50",
1950
+ style: { background: hex }
1951
+ }, hex))
1952
+ }),
1953
+ ratio !== null && ratio < minContrast && /* @__PURE__ */ jsxs("p", {
1954
+ className: "type-body-sm text-warning",
1955
+ children: [
1956
+ "Hard to see on ",
1957
+ againstLabel,
1958
+ ": contrast ",
1959
+ format.decimal(ratio),
1960
+ ":1, aim for ",
1961
+ format.decimal(minContrast),
1962
+ ":1 or more."
1963
+ ]
1964
+ })
1965
+ ]
1966
+ });
1967
+ }
1661
1968
  // src/components/alert.tsx
1662
1969
  var tones = {
1663
1970
  info: {
@@ -3849,6 +4156,567 @@ function useJob({ isRunning, every = 2e3, invalidates = [], describeError, ...op
3849
4156
  poll
3850
4157
  };
3851
4158
  }
3852
- export { ActiveList, Alert, AlertDialog, AppShell, AsyncButton, Breadcrumb, BulkBar, Button, COLOR_SCHEME_STORAGE_KEY, Checkbox, ChoiceGroup, ColorSchemeChoice, CommandLegend, CommandMenu, CompositionBar, ContextMenu, DataTable, DataView, Dialog, Disclosure, Done, EmptyState, Field, HttpError, IconButton, InfiniteList, Input, JobStatus, KeysLegend, LineChart, LinkCard, LinkCardList, LoadError, LoadMore, MISSING, Menu, Meter, Metric, MetricStrip, NBSP, NativeSelect, PageHeader, Popover, RowAction, SelectableGrid, SideNav, Skeleton, StaleNotice, Status, StrataQueryProvider, StrataToastRegion, Switch, TableSkeleton, Tabs, Textarea, ThemeProvider, Thumbnail, ThumbnailSkeleton, Toggle, ToggleGroup, Toolbar, ToolbarSeparator, Tooltip, UndoBar, VirtualList, buttonVariants, cn, colorSchemeScript, commandShortcut, createFormat, createQueryClient, crossedThreshold, describeActionError, describeHttpError, detectPlatform, fetchJson, fieldErrorsOf, format, formatChord, isReservedChord, isTypingTarget, labelOf, matchesChord, parseChord, parseSort, readErrorBody, serialiseSort, staleOf, toDate, toLoad, toast, toastTimeout, typeSteps, useActiveItem, useAsyncAction, useClientWork, useColorScheme, useCommands, useExpanded, useHotkeys, useInView, useInfiniteLoad, useJob, useLoad, useSelection, useSystemColorScheme, useUndoable };
4159
+ // src/lib/viewport.ts
4160
+ /** The picture fitted in its frame, not panned. */
4161
+ var FIT = {
4162
+ scale: 1,
4163
+ x: 0,
4164
+ y: 0
4165
+ };
4166
+ /** The least zoom: the whole picture fitted. */
4167
+ var MIN_SCALE = 1;
4168
+ /** The most zoom, 32 times the fit. */
4169
+ var MAX_SCALE = 32;
4170
+ /** Keeps the picture from being dragged out of its frame: at most half the extra width or height past each edge. */
4171
+ function clampViewport(v, frame) {
4172
+ const scale = Math.min(32, Math.max(1, v.scale));
4173
+ if (scale === 1) return FIT;
4174
+ const mx = (scale - 1) * frame.width / 2;
4175
+ const my = (scale - 1) * frame.height / 2;
4176
+ return {
4177
+ scale,
4178
+ x: Math.min(mx, Math.max(-mx, v.x)),
4179
+ y: Math.min(my, Math.max(-my, v.y))
4180
+ };
4181
+ }
4182
+ /** A new scale with the point under `at` (pixels from the frame's centre) staying under it. */
4183
+ function zoomAt(v, nextScale, at, frame) {
4184
+ const scale = Math.min(32, Math.max(1, nextScale));
4185
+ const k = scale / v.scale;
4186
+ return clampViewport({
4187
+ scale,
4188
+ x: at.x - k * (at.x - v.x),
4189
+ y: at.y - k * (at.y - v.y)
4190
+ }, frame);
4191
+ }
4192
+ /** The scale at which the picture's own pixels are drawn one to one: its natural width over the width it is fitted at. */
4193
+ function actualPixelsScale(naturalWidth, fittedWidth) {
4194
+ if (!naturalWidth || !fittedWidth) return 1;
4195
+ return Math.min(32, Math.max(1, naturalWidth / fittedWidth));
4196
+ }
4197
+ /** The CSS transform for a viewport. */
4198
+ function viewportTransform(v) {
4199
+ return `translate(${v.x}px, ${v.y}px) scale(${v.scale})`;
4200
+ }
4201
+ // src/components/fullscreen-frame.tsx
4202
+ /** Whether this browser can show an element full screen. False in jsdom and on iPhone, where the modal fallback is used. */
4203
+ function canFullscreen() {
4204
+ return typeof document !== "undefined" && document.fullscreenEnabled === true && typeof Element.prototype.requestFullscreen === "function";
4205
+ }
4206
+ /** Shows its children full screen while `active`: the browser's Fullscreen API where there is one, a window-filling modal where there is not. */
4207
+ function FullscreenFrame({ label, active, onActiveChange, children, className }) {
4208
+ const ref = React.useRef(null);
4209
+ const [native, setNative] = React.useState(false);
4210
+ const [refused, setRefused] = React.useState(false);
4211
+ const change = React.useRef(onActiveChange);
4212
+ change.current = onActiveChange;
4213
+ React.useEffect(() => {
4214
+ const el = ref.current;
4215
+ if (!active) {
4216
+ setNative(false);
4217
+ setRefused(false);
4218
+ if (document.fullscreenElement && document.fullscreenElement === el) document.exitFullscreen();
4219
+ return;
4220
+ }
4221
+ if (!el || !canFullscreen()) return;
4222
+ let cancelled = false;
4223
+ el.requestFullscreen().then(() => !cancelled && setNative(true), () => !cancelled && setRefused(true));
4224
+ const onChange = () => {
4225
+ if (document.fullscreenElement !== el && !cancelled) change.current(false);
4226
+ };
4227
+ document.addEventListener("fullscreenchange", onChange);
4228
+ return () => {
4229
+ cancelled = true;
4230
+ document.removeEventListener("fullscreenchange", onChange);
4231
+ };
4232
+ }, [active]);
4233
+ const modal = active && !native && (!canFullscreen() || refused);
4234
+ const inner = /* @__PURE__ */ jsx("div", {
4235
+ ref,
4236
+ className: cn(active && "flex h-full min-h-0 flex-col bg-background p-4", className),
4237
+ children
4238
+ });
4239
+ if (!modal) return inner;
4240
+ return /* @__PURE__ */ jsx(ModalOverlay, {
4241
+ isOpen: true,
4242
+ onOpenChange: (o) => !o && onActiveChange(false),
4243
+ isDismissable: true,
4244
+ className: "layer-overlay fixed inset-0 bg-foreground/40",
4245
+ children: /* @__PURE__ */ jsx(Modal, {
4246
+ className: cn("h-full w-full outline-none", overlaySurface, "rounded-none"),
4247
+ children: /* @__PURE__ */ jsx(Dialog$1, {
4248
+ "aria-label": label,
4249
+ className: "h-full outline-none",
4250
+ children: inner
4251
+ })
4252
+ })
4253
+ });
4254
+ }
4255
+ // src/components/viewport.tsx
4256
+ /** Zoom and pan held in React state, to give to every `ViewportFrame` that should move together. */
4257
+ function useViewport(initial = FIT) {
4258
+ const [viewport, setViewport] = React.useState(initial);
4259
+ return {
4260
+ viewport,
4261
+ setViewport
4262
+ };
4263
+ }
4264
+ var PAN_STEP = 48;
4265
+ /** A clipped frame whose children zoom and pan together; the viewport is the caller's, so frames can share one. */
4266
+ var ViewportFrame = React.forwardRef(function ViewportFrame({ label, aspect, viewport, setViewport, children, cover, overlay, className }, forwardedRef) {
4267
+ const ref = React.useRef(null);
4268
+ React.useImperativeHandle(forwardedRef, () => ref.current);
4269
+ const latest = React.useRef(viewport);
4270
+ latest.current = viewport;
4271
+ const drag = React.useRef(null);
4272
+ const size = () => {
4273
+ const r = ref.current.getBoundingClientRect();
4274
+ return {
4275
+ width: r.width,
4276
+ height: r.height
4277
+ };
4278
+ };
4279
+ const fromCentre = (clientX, clientY) => {
4280
+ const r = ref.current.getBoundingClientRect();
4281
+ return {
4282
+ x: clientX - r.left - r.width / 2,
4283
+ y: clientY - r.top - r.height / 2
4284
+ };
4285
+ };
4286
+ const setViewportRef = React.useRef(setViewport);
4287
+ setViewportRef.current = setViewport;
4288
+ React.useEffect(() => {
4289
+ const el = ref.current;
4290
+ if (!el) return;
4291
+ const onWheel = (e) => {
4292
+ if (!e.ctrlKey && !e.metaKey) return;
4293
+ e.preventDefault();
4294
+ const r = el.getBoundingClientRect();
4295
+ const at = {
4296
+ x: e.clientX - r.left - r.width / 2,
4297
+ y: e.clientY - r.top - r.height / 2
4298
+ };
4299
+ setViewportRef.current(zoomAt(latest.current, latest.current.scale * Math.exp(-e.deltaY / 200), at, {
4300
+ width: r.width,
4301
+ height: r.height
4302
+ }));
4303
+ };
4304
+ el.addEventListener("wheel", onWheel, { passive: false });
4305
+ return () => el.removeEventListener("wheel", onWheel);
4306
+ }, []);
4307
+ const onKeyDown = (e) => {
4308
+ const v = latest.current;
4309
+ const centre = {
4310
+ x: 0,
4311
+ y: 0
4312
+ };
4313
+ if (e.key === "+" || e.key === "=") setViewport(zoomAt(v, v.scale * 1.5, centre, size()));
4314
+ else if (e.key === "-") setViewport(zoomAt(v, v.scale / 1.5, centre, size()));
4315
+ else if (e.key === "0") setViewport(FIT);
4316
+ else if (v.scale > 1 && e.key.startsWith("Arrow")) {
4317
+ const dx = e.key === "ArrowRight" ? -48 : e.key === "ArrowLeft" ? PAN_STEP : 0;
4318
+ const dy = e.key === "ArrowDown" ? -48 : e.key === "ArrowUp" ? PAN_STEP : 0;
4319
+ setViewport(clampViewport({
4320
+ ...v,
4321
+ x: v.x + dx,
4322
+ y: v.y + dy
4323
+ }, size()));
4324
+ } else return;
4325
+ e.preventDefault();
4326
+ e.stopPropagation();
4327
+ };
4328
+ const zoomed = viewport.scale > 1;
4329
+ return /* @__PURE__ */ jsxs("div", {
4330
+ ref,
4331
+ role: "group",
4332
+ "aria-label": label,
4333
+ "aria-roledescription": "zoomable picture",
4334
+ tabIndex: 0,
4335
+ onKeyDown,
4336
+ onPointerDown: (e) => {
4337
+ if (!zoomed || e.button !== 0) return;
4338
+ drag.current = {
4339
+ x: e.clientX,
4340
+ y: e.clientY
4341
+ };
4342
+ e.currentTarget.setPointerCapture?.(e.pointerId);
4343
+ },
4344
+ onPointerMove: (e) => {
4345
+ if (!drag.current) return;
4346
+ const dx = e.clientX - drag.current.x;
4347
+ const dy = e.clientY - drag.current.y;
4348
+ drag.current = {
4349
+ x: e.clientX,
4350
+ y: e.clientY
4351
+ };
4352
+ setViewport(clampViewport({
4353
+ ...latest.current,
4354
+ x: latest.current.x + dx,
4355
+ y: latest.current.y + dy
4356
+ }, size()));
4357
+ },
4358
+ onPointerUp: () => drag.current = null,
4359
+ onPointerCancel: () => drag.current = null,
4360
+ onDoubleClick: (e) => {
4361
+ const v = latest.current;
4362
+ setViewport(v.scale > 1 ? FIT : zoomAt(v, 2, fromCentre(e.clientX, e.clientY), size()));
4363
+ },
4364
+ style: {
4365
+ aspectRatio: String(aspect),
4366
+ touchAction: zoomed ? "none" : "pan-y"
4367
+ },
4368
+ className: cn("relative w-full overflow-hidden rounded-surface border border-border bg-muted select-none", zoomed ? "cursor-grab active:cursor-grabbing" : "cursor-zoom-in", "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring", className),
4369
+ children: [
4370
+ /* @__PURE__ */ jsx("div", {
4371
+ className: "absolute inset-0",
4372
+ style: {
4373
+ transform: viewportTransform(viewport),
4374
+ transformOrigin: "center"
4375
+ },
4376
+ children
4377
+ }),
4378
+ cover && /* @__PURE__ */ jsx("div", {
4379
+ className: "absolute inset-0",
4380
+ style: { clipPath: `inset(0 ${100 - cover.shown}% 0 0)` },
4381
+ children: /* @__PURE__ */ jsx("div", {
4382
+ className: "absolute inset-0",
4383
+ style: {
4384
+ transform: viewportTransform(viewport),
4385
+ transformOrigin: "center"
4386
+ },
4387
+ children: cover.content
4388
+ })
4389
+ }),
4390
+ overlay != null && /* @__PURE__ */ jsx("div", {
4391
+ className: "pointer-events-none absolute inset-0",
4392
+ onPointerDown: (e) => e.stopPropagation(),
4393
+ onKeyDown: (e) => e.stopPropagation(),
4394
+ onDoubleClick: (e) => e.stopPropagation(),
4395
+ children: overlay
4396
+ })
4397
+ ]
4398
+ });
4399
+ });
4400
+ // src/components/compare.tsx
4401
+ var MODE_LABEL = {
4402
+ side: "Side by side",
4403
+ swipe: "Swipe",
4404
+ fade: "Fade",
4405
+ blink: "Blink"
4406
+ };
4407
+ var ALL_MODES = [
4408
+ "side",
4409
+ "swipe",
4410
+ "fade",
4411
+ "blink"
4412
+ ];
4413
+ /** A word for a picture's name on the page: its label, with its note unless blind. */
4414
+ function Caption({ c, blind }) {
4415
+ return /* @__PURE__ */ jsxs("div", {
4416
+ className: "flex items-baseline gap-2",
4417
+ children: [/* @__PURE__ */ jsx("span", {
4418
+ className: "type-label",
4419
+ children: c.label
4420
+ }), !blind && c.note && /* @__PURE__ */ jsx("span", {
4421
+ className: "type-caption text-muted-foreground",
4422
+ children: c.note
4423
+ })]
4424
+ });
4425
+ }
4426
+ /** One picture inside a frame: a skeleton while it loads, the product's words if it fails. */
4427
+ function Picture({ c, blind, missing, onNatural, imgRef }) {
4428
+ const [state, setState] = React.useState("loading");
4429
+ React.useEffect(() => setState("loading"), [c.media.src]);
4430
+ const alt = blind ? c.label : c.media.alt;
4431
+ if (state === "failed") return /* @__PURE__ */ jsxs("div", {
4432
+ role: "img",
4433
+ "aria-label": `${alt}: ${missing}`,
4434
+ className: "absolute inset-0 grid place-content-center justify-items-center gap-2 text-muted-foreground",
4435
+ children: [/* @__PURE__ */ jsx(ImageOff, {
4436
+ "aria-hidden": true,
4437
+ className: "size-6"
4438
+ }), /* @__PURE__ */ jsx("span", {
4439
+ className: "type-body-sm",
4440
+ children: missing
4441
+ })]
4442
+ });
4443
+ return /* @__PURE__ */ jsxs(Fragment, { children: [state === "loading" && /* @__PURE__ */ jsx(Skeleton, {
4444
+ "aria-hidden": true,
4445
+ className: "absolute inset-0 size-full rounded-none"
4446
+ }), /* @__PURE__ */ jsx("img", {
4447
+ ref: imgRef,
4448
+ src: c.media.src,
4449
+ alt,
4450
+ draggable: false,
4451
+ onLoad: (e) => {
4452
+ setState("ready");
4453
+ onNatural?.(e.currentTarget.naturalWidth, e.currentTarget.naturalHeight);
4454
+ },
4455
+ onError: () => setState("failed"),
4456
+ className: "absolute inset-0 size-full object-contain"
4457
+ })] });
4458
+ }
4459
+ /** Two pictures to judge, side by side or laid over each other, zoomed and panned together, and full screen. */
4460
+ function Compare({ label, a, b, blind, missing, mode, onModeChange, modes = ALL_MODES, keys = true, className }) {
4461
+ const { viewport, setViewport } = useViewport();
4462
+ const [full, setFull] = React.useState(false);
4463
+ const pairKey = `${a.media.src}|${b.media.src}`;
4464
+ const [learned, setLearned] = React.useState(null);
4465
+ const natural = a.media.size ?? (learned?.key === pairKey ? learned.size : null);
4466
+ const [divider, setDivider] = React.useState(50);
4467
+ const [blend, setBlend] = React.useState(50);
4468
+ const [showing, setShowing] = React.useState("a");
4469
+ const frameRef = React.useRef(null);
4470
+ const imgRef = React.useRef(null);
4471
+ const aspect = natural ? natural[0] / natural[1] : 3 / 2;
4472
+ const frameSize = () => {
4473
+ const r = frameRef.current?.getBoundingClientRect();
4474
+ return {
4475
+ width: r?.width ?? 0,
4476
+ height: r?.height ?? 0
4477
+ };
4478
+ };
4479
+ const zoomBy = (k) => setViewport(zoomAt(viewport, viewport.scale * k, {
4480
+ x: 0,
4481
+ y: 0
4482
+ }, frameSize()));
4483
+ const actual = () => {
4484
+ const f = frameSize();
4485
+ const fitted = natural ? Math.min(f.width, f.height * (natural[0] / natural[1])) : f.width;
4486
+ setViewport(zoomAt(viewport, actualPixelsScale(natural?.[0] ?? 0, fitted), {
4487
+ x: 0,
4488
+ y: 0
4489
+ }, f));
4490
+ };
4491
+ const flip = () => setShowing((s) => s === "a" ? "b" : "a");
4492
+ const offered = modes.includes(mode) ? modes : [mode, ...modes];
4493
+ const shortcuts = [
4494
+ ...offered.map((m, i) => ({
4495
+ keys: [String(i + 1)],
4496
+ label: MODE_LABEL[m],
4497
+ run: () => onModeChange(m)
4498
+ })),
4499
+ {
4500
+ keys: ["x"],
4501
+ label: "Flip A and B",
4502
+ run: flip,
4503
+ disabled: mode !== "blink"
4504
+ },
4505
+ {
4506
+ keys: ["+", "="],
4507
+ label: "Zoom in",
4508
+ run: () => zoomBy(1.5),
4509
+ repeat: true
4510
+ },
4511
+ {
4512
+ keys: ["-"],
4513
+ label: "Zoom out",
4514
+ run: () => zoomBy(1 / 1.5),
4515
+ repeat: true
4516
+ },
4517
+ {
4518
+ keys: ["0"],
4519
+ label: "Fit",
4520
+ run: () => setViewport(FIT)
4521
+ },
4522
+ {
4523
+ keys: ["f"],
4524
+ label: full ? "Leave full screen" : "Full screen",
4525
+ run: () => setFull((f) => !f)
4526
+ }
4527
+ ];
4528
+ useHotkeys(shortcuts, { enabled: keys });
4529
+ const pane = (c, ref, onNatural) => /* @__PURE__ */ jsx(Picture, {
4530
+ c,
4531
+ blind,
4532
+ missing,
4533
+ imgRef: ref,
4534
+ onNatural
4535
+ });
4536
+ const learn = (w, h) => setLearned({
4537
+ key: pairKey,
4538
+ size: [w, h]
4539
+ });
4540
+ const frameProps = {
4541
+ viewport,
4542
+ setViewport,
4543
+ aspect
4544
+ };
4545
+ let body;
4546
+ if (mode === "side") body = /* @__PURE__ */ jsx("div", {
4547
+ className: "grid gap-3 sm:grid-cols-2",
4548
+ children: [a, b].map((c, i) => /* @__PURE__ */ jsxs("figure", {
4549
+ className: "grid gap-1.5",
4550
+ children: [/* @__PURE__ */ jsx(Caption, {
4551
+ c,
4552
+ blind
4553
+ }), /* @__PURE__ */ jsx(ViewportFrame, {
4554
+ ...frameProps,
4555
+ label: blind ? `Picture ${c.label}` : `${c.label}`,
4556
+ ref: i === 0 ? frameRef : void 0,
4557
+ children: pane(c, i === 0 ? imgRef : void 0, i === 0 ? learn : void 0)
4558
+ })]
4559
+ }, i))
4560
+ });
4561
+ else {
4562
+ const top = mode === "blink" ? showing === "a" ? a : b : a;
4563
+ const bottom = b;
4564
+ body = /* @__PURE__ */ jsxs("figure", {
4565
+ className: "grid gap-1.5",
4566
+ children: [/* @__PURE__ */ jsxs("div", {
4567
+ className: "flex justify-between gap-4",
4568
+ children: [mode === "blink" ? /* @__PURE__ */ jsx(Caption, {
4569
+ c: top,
4570
+ blind
4571
+ }) : /* @__PURE__ */ jsx(Caption, {
4572
+ c: a,
4573
+ blind
4574
+ }), mode !== "blink" && /* @__PURE__ */ jsx(Caption, {
4575
+ c: b,
4576
+ blind
4577
+ })]
4578
+ }), /* @__PURE__ */ jsxs(ViewportFrame, {
4579
+ ...frameProps,
4580
+ ref: frameRef,
4581
+ cover: mode === "swipe" ? {
4582
+ content: pane(a, imgRef, learn),
4583
+ shown: divider
4584
+ } : void 0,
4585
+ label: mode === "blink" ? `Picture ${top.label}` : `${a.label} over ${b.label}`,
4586
+ overlay: mode === "swipe" && /* @__PURE__ */ jsx(Slider$1, {
4587
+ "aria-label": `Divider between ${a.label} and ${b.label}`,
4588
+ value: divider,
4589
+ onChange: (v) => setDivider(v),
4590
+ minValue: 0,
4591
+ maxValue: 100,
4592
+ step: 1,
4593
+ className: "absolute inset-0",
4594
+ children: /* @__PURE__ */ jsx(SliderTrack, {
4595
+ className: "pointer-events-none h-full w-full",
4596
+ children: /* @__PURE__ */ jsxs(SliderThumb, {
4597
+ className: "pointer-events-auto grid h-full w-8 cursor-ew-resize place-items-center data-[focus-visible]:outline-2 data-[focus-visible]:outline-ring",
4598
+ style: { top: "50%" },
4599
+ children: [/* @__PURE__ */ jsx("span", {
4600
+ "aria-hidden": true,
4601
+ className: "col-start-1 row-start-1 h-full w-1 border-x border-background bg-foreground"
4602
+ }), /* @__PURE__ */ jsx("span", {
4603
+ "aria-hidden": true,
4604
+ className: "col-start-1 row-start-1 size-6 rounded-pill border-2 border-foreground bg-background"
4605
+ })]
4606
+ })
4607
+ })
4608
+ }),
4609
+ children: [pane(bottom), mode !== "swipe" && /* @__PURE__ */ jsx("div", {
4610
+ className: "absolute inset-0",
4611
+ style: mode === "fade" ? { opacity: 1 - blend / 100 } : void 0,
4612
+ children: pane(top, imgRef, learn)
4613
+ })]
4614
+ })]
4615
+ });
4616
+ }
4617
+ return /* @__PURE__ */ jsx(FullscreenFrame, {
4618
+ label,
4619
+ active: full,
4620
+ onActiveChange: setFull,
4621
+ className,
4622
+ children: /* @__PURE__ */ jsxs("section", {
4623
+ "aria-label": label,
4624
+ className: cn("grid gap-3", full && "min-h-0 flex-1 content-start overflow-auto"),
4625
+ children: [
4626
+ /* @__PURE__ */ jsxs(Toolbar, {
4627
+ label: "Comparison view",
4628
+ className: "items-center gap-2",
4629
+ children: [
4630
+ /* @__PURE__ */ jsx(ToggleGroup, {
4631
+ label: "View",
4632
+ options: offered.map((m) => ({
4633
+ value: m,
4634
+ label: MODE_LABEL[m]
4635
+ })),
4636
+ value: mode,
4637
+ onChange: (v) => onModeChange(v),
4638
+ size: "sm"
4639
+ }),
4640
+ /* @__PURE__ */ jsx(ToolbarSeparator, {}),
4641
+ /* @__PURE__ */ jsx(IconButton, {
4642
+ icon: ZoomOut,
4643
+ label: "Zoom out",
4644
+ onPress: () => zoomBy(1 / 1.5),
4645
+ disabled: viewport.scale <= 1
4646
+ }),
4647
+ /* @__PURE__ */ jsx(IconButton, {
4648
+ icon: ZoomIn,
4649
+ label: "Zoom in",
4650
+ onPress: () => zoomBy(1.5)
4651
+ }),
4652
+ /* @__PURE__ */ jsx(IconButton, {
4653
+ icon: Scan,
4654
+ label: "Fit",
4655
+ onPress: () => setViewport(FIT),
4656
+ disabled: viewport.scale === 1
4657
+ }),
4658
+ /* @__PURE__ */ jsx(Button, {
4659
+ size: "sm",
4660
+ variant: "ghost",
4661
+ onPress: actual,
4662
+ disabled: !natural,
4663
+ children: "1:1"
4664
+ }),
4665
+ /* @__PURE__ */ jsx("span", {
4666
+ "aria-live": "off",
4667
+ className: "type-caption tabular-nums text-muted-foreground",
4668
+ children: viewport.scale === 1 ? "Fit" : `${viewport.scale.toFixed(1)}×`
4669
+ }),
4670
+ /* @__PURE__ */ jsx(ToolbarSeparator, {}),
4671
+ /* @__PURE__ */ jsx(IconButton, {
4672
+ icon: full ? Minimize : Maximize,
4673
+ label: full ? "Leave full screen" : "Full screen",
4674
+ onPress: () => setFull(!full)
4675
+ })
4676
+ ]
4677
+ }),
4678
+ mode === "fade" && /* @__PURE__ */ jsxs("div", {
4679
+ className: "flex items-center gap-3",
4680
+ children: [
4681
+ /* @__PURE__ */ jsx("span", {
4682
+ className: "type-caption text-muted-foreground",
4683
+ children: a.label
4684
+ }),
4685
+ /* @__PURE__ */ jsx(Slider, {
4686
+ "aria-label": `Blend from ${a.label} to ${b.label}`,
4687
+ value: blend,
4688
+ onChange: setBlend,
4689
+ min: 0,
4690
+ max: 100,
4691
+ unit: "%",
4692
+ hideValue: true,
4693
+ className: "flex-1"
4694
+ }),
4695
+ /* @__PURE__ */ jsx("span", {
4696
+ className: "type-caption text-muted-foreground",
4697
+ children: b.label
4698
+ })
4699
+ ]
4700
+ }),
4701
+ mode === "blink" && /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(ToggleGroup, {
4702
+ label: "Showing",
4703
+ options: [{
4704
+ value: "a",
4705
+ label: a.label
4706
+ }, {
4707
+ value: "b",
4708
+ label: b.label
4709
+ }],
4710
+ value: showing,
4711
+ onChange: (v) => setShowing(v),
4712
+ size: "sm"
4713
+ }) }),
4714
+ body,
4715
+ keys && /* @__PURE__ */ jsx(KeysLegend, { shortcuts })
4716
+ ]
4717
+ })
4718
+ });
4719
+ }
4720
+ export { ActiveList, Alert, AlertDialog, AppShell, AsyncButton, Breadcrumb, BulkBar, Button, COLOR_SCHEME_STORAGE_KEY, Checkbox, ChoiceGroup, ColorField, ColorSchemeChoice, CommandLegend, CommandMenu, Compare, CompositionBar, ContextMenu, DataTable, DataView, Dialog, Disclosure, Done, EmptyState, FIT, Field, FullscreenFrame, HttpError, IconButton, InfiniteList, Input, JobStatus, KeysLegend, LineChart, LinkCard, LinkCardList, LoadError, LoadMore, MAX_SCALE, MIN_SCALE, MISSING, Menu, Meter, Metric, MetricStrip, NBSP, NativeSelect, NumberField, PageHeader, Popover, RowAction, SelectableGrid, SideNav, Skeleton, Slider, StaleNotice, Status, StrataQueryProvider, StrataToastRegion, Switch, TableSkeleton, Tabs, Textarea, ThemeProvider, Thumbnail, ThumbnailSkeleton, Toggle, ToggleGroup, Toolbar, ToolbarSeparator, Tooltip, UndoBar, ViewportFrame, VirtualList, actualPixelsScale, buttonVariants, canFullscreen, clampViewport, cn, colorSchemeScript, commandShortcut, createFormat, createQueryClient, crossedThreshold, describeActionError, describeHttpError, detectPlatform, fetchJson, fieldErrorsOf, format, formatChord, isReservedChord, isTypingTarget, labelOf, matchesChord, parseChord, parseSort, readErrorBody, serialiseSort, staleOf, toDate, toLoad, toast, toastTimeout, typeSteps, useActiveItem, useAsyncAction, useClientWork, useColorScheme, useCommands, useExpanded, useHotkeys, useInView, useInfiniteLoad, useJob, useLoad, useSelection, useSystemColorScheme, useUndoable, useViewport, zoomAt };
3853
4721
 
3854
4722
  //# sourceMappingURL=index.js.map