strata-react 0.1.12 → 0.1.14
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/README.md +3 -3
- package/dist/components/thumbnail.d.ts +31 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +106 -33
- package/dist/index.js.map +1 -1
- package/dist/lib/in-view.d.ts +21 -0
- package/dist/testing/index.d.ts +76 -4
- package/dist/testing.js +37 -9
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ it against the built files:
|
|
|
36
36
|
| Import | Needs | Why |
|
|
37
37
|
|---|---|---|
|
|
38
38
|
| `strata-react` | `react`, `react-dom`, `react-router`, `@tanstack/react-query`, `tailwindcss` | The stack is fixed: `DataTable` and `Tabs` keep sort and tab in the URL, `AppShell` links with `NavLink`, every remote read is `useLoad`. The barrel imports both libraries, so an app that uses only a `Button` still installs them (both are needed for anything past a static page) |
|
|
39
|
-
| `strata-react/testing` | the above, plus `@testing-library/react` | `renderWithStrata`
|
|
39
|
+
| `strata-react/testing` | the above, plus `@testing-library/react` | `renderWithStrata` and `renderRoutes` wrap Testing Library's `render` |
|
|
40
40
|
| `strata-react/visual`, `strata-visual` | `playwright` (exact version), `msw`, `vite` | The screenshot runner; see [Visual regression for apps](#visual-regression-for-apps) |
|
|
41
41
|
|
|
42
42
|
The last three are optional peers: an app that never imports those entries never needs them.
|
|
@@ -119,7 +119,7 @@ function KilnRoom() {
|
|
|
119
119
|
| Actions | `Button`, `IconButton`, `RowAction`, `AsyncButton`, `useAsyncAction` | Default is `outline`; one filled button per view; `IconButton` requires `label`; `RowAction` is named for its row ("Withdraw Tom Ellery"); `AsyncButton` keeps one width through idle, pending, done and error, and announces `error` with its retry label; `useAsyncAction` returns `{ phase, run, error }` with a stable `run`, and describes an `HttpError` in a sentence (`describeActionError`, or the product's own `describeError`) |
|
|
120
120
|
| Form | `Field`, `Input`, `Textarea`, `NativeSelect`, `ChoiceGroup`, `Checkbox` | `Field` wires label, hint, error, `aria-describedby`, `aria-invalid`; `Checkbox` requires `label` and is one target with it, and has a mixed state |
|
|
121
121
|
| Status and figures | `Status`, `Metric`, `MetricStrip`, `Meter`, `crossedThreshold`, `CompositionBar` | A status is always a word; figures have no icons or sparklines; `Meter` is a `meter` named for what it measures, with its reading in words ("72% used"), threshold notches, and a `Status` word once a threshold is crossed (each threshold must name its state); `CompositionBar` is a stacked bar over a table naming every part, its amount and share, and names the remainder (`total` needs `restLabel`) |
|
|
122
|
-
| Data | `DataView`, `Load<T>`, `EmptyState`, `Skeleton`, `DataTable`, `TableSkeleton`, `VirtualList`, `InfiniteList`, `LineChart` | `DataView` requires skeleton and empty; tables and lists virtualise from 100 rows; `InfiniteList` loads the next page on approach, with a skeleton for it, a Try again row when it fails and a Show more button for the keyboard; charts label values directly |
|
|
122
|
+
| Data | `DataView`, `Load<T>`, `EmptyState`, `Skeleton`, `Thumbnail`, `ThumbnailSkeleton`, `DataTable`, `TableSkeleton`, `VirtualList`, `InfiniteList`, `LineChart` | `DataView` requires skeleton and empty; `Thumbnail` is a remote image in a box of fixed `aspect`, with a skeleton while it loads, the required `missing` words when it fails or `src` is null, required `alt`, and a request only once it nears the screen (`eager` for the first few); tables and lists virtualise from 100 rows; `InfiniteList` loads the next page on approach, with a skeleton for it, a Try again row when it fails and a Show more button for the keyboard; charts label values directly |
|
|
123
123
|
| Selection | `useSelection`, `BulkBar`, `DataTable`'s `selection` (`RowSelection<T>`) | Opt-in checkbox column with "Select all shown" that respects `isSelectable`; the selection drops rows that are gone; `BulkBar` announces the count politely |
|
|
124
124
|
| Data loading | `StrataQueryProvider`, `createQueryClient`, `useLoad`, `useInfiniteLoad`, `toLoad`, `fetchJson`, `HttpError` | TanStack Query in, `Load<T>` out; `useInfiniteLoad` works out the cursor itself (after the last item held), merges pages by key and can `dismiss` an item at once |
|
|
125
125
|
| Page and navigation | `AppShell`, `SideNav`, `Breadcrumb`, `Tabs`, `PageHeader`, `LinkCard`, `LinkCardList` | React Router; tab and sort state in the URL; a `LinkCard` is one link named by its title, described by its text and `Status` marks, with the whole card as the target (`to` a route, `href` elsewhere, marked with an arrow) |
|
|
@@ -127,7 +127,7 @@ function KilnRoom() {
|
|
|
127
127
|
| Theme | `ThemeProvider`, `useColorScheme`, `useSystemColorScheme`, `ColorSchemeChoice`, `colorSchemeScript` | System, light or dark; sets `.dark`, persists in localStorage when it can; `ColorSchemeChoice` is a radio list for a settings page, or `compact` icon segments named "Appearance" for an app with no settings page |
|
|
128
128
|
| Feedback | `UndoBar`, `useUndoable`, `Done` | Undo before confirm; the hook owns which rows are out of view (`isRemoved`), a failed commit brings the row back with Try again in the same bar, and `flush()` commits before a dependent action; `Done` is the one celebration, and it must say what happens next |
|
|
129
129
|
| Helpers | `cn`, `format`, `createFormat`, `NBSP` | `cn` knows `type-*`; all numbers and dates through `Intl`; `format.integer(n)`, `.decimal(n)`, `.percent(n)` are callable and keep Intl's methods; `format.bytes(n, unit?)` writes sizes in decimal units (`byteUnit` shares one unit across figures); `format.date(at)` ("8 Nov 2026") and `format.dateTime(at)` ("8 Nov 2026, 06:05"), like `clock`, `day` and `shortDay`, take a Date, ISO string or epoch ms; `createFormat(locale, { timeZone })` fixes the zone; units and counts join with a non-breaking space |
|
|
130
|
-
| Testing | `strata-react/testing`: `renderWithStrata`, `createTestQueryClient`, `installLayoutShims`, `installFakeIntersectionObserver` | Router + query client around Testing Library's `render`; a fixed box and ResizeObserver so measuring components render in jsdom; an IntersectionObserver the test fires with `scrollToEnd()` or `intersect(el)`. Role names keep the non-breaking space from `format.count`, so match them with the same call or a `\s` regex |
|
|
130
|
+
| Testing | `strata-react/testing`: `renderWithStrata`, `renderRoutes`, `createTestQueryClient`, `installLayoutShims`, `installFakeIntersectionObserver` | Router + query client around Testing Library's `render`; `renderRoutes(routes, { route, basename })` renders the app's own `RouteObject[]` in a memory data router, so layout routes, redirects, loaders and error elements run, and returns the `router` (where a redirect landed is `router.state.location`) and an act-wrapped `navigate`; a fixed box and ResizeObserver so measuring components render in jsdom; an IntersectionObserver the test fires with `scrollToEnd()` or `intersect(el)`. Role names keep the non-breaking space from `format.count`, so match them with the same call or a `\s` regex |
|
|
131
131
|
| Visual regression | `strata-react/visual` and the `strata-visual` command | Screenshots an app's Storybook and its routes in each MSW state, light and dark, against baselines in the app's repo |
|
|
132
132
|
|
|
133
133
|
Specs for the components not built yet (dialogs, sheets, menus, toasts, command menu, prose)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
/** A width-over-height ratio: a number (`1.5`) or a fraction (`"4/3"`). */
|
|
3
|
+
export type AspectRatio = number | `${number}/${number}`;
|
|
4
|
+
/** The thumbnail's props. */
|
|
5
|
+
export type ThumbnailProps = {
|
|
6
|
+
/** The image URL. `null` when the record has no photo: the `missing` words show without a request. */
|
|
7
|
+
src: string | null;
|
|
8
|
+
/** What the photo shows ("Celadon bowl, front"). `""` only when the text beside it already says it. */
|
|
9
|
+
alt: string;
|
|
10
|
+
/** What to show when there is no photo or it cannot be loaded, in the product's words: "Photo not found". */
|
|
11
|
+
missing: string;
|
|
12
|
+
/** Width over height. The box has this shape before the image loads. Default `1`. */
|
|
13
|
+
aspect?: AspectRatio;
|
|
14
|
+
/** `cover` crops to fill the box (default); `contain` fits the whole image in it. */
|
|
15
|
+
fit?: "cover" | "contain";
|
|
16
|
+
srcSet?: string;
|
|
17
|
+
sizes?: string;
|
|
18
|
+
/** Load at once rather than when near the screen: for the first few above the fold. */
|
|
19
|
+
eager?: boolean;
|
|
20
|
+
/** How far outside the scroll region the request starts (IntersectionObserver `rootMargin`). */
|
|
21
|
+
rootMargin?: string;
|
|
22
|
+
/** Layout at the call site: a width (`w-24`, `w-full`). The height follows from `aspect`. */
|
|
23
|
+
className?: string;
|
|
24
|
+
};
|
|
25
|
+
/** A box of fixed aspect ratio for a remote image: a skeleton until it loads, the `missing` words if it cannot, and a request only when it nears the screen. `alt` and `missing` are required. */
|
|
26
|
+
export declare function Thumbnail({ src, alt, missing, aspect, fit, srcSet, sizes, eager, rootMargin, className, }: ThumbnailProps): React.JSX.Element;
|
|
27
|
+
/** The loading shape of a `Thumbnail`, for a `DataView` skeleton: the same box and aspect ratio, so nothing moves when the list arrives. */
|
|
28
|
+
export declare function ThumbnailSkeleton({ aspect, className }: {
|
|
29
|
+
aspect?: AspectRatio;
|
|
30
|
+
className?: string;
|
|
31
|
+
}): React.JSX.Element;
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ export { Meter, crossedThreshold, type MeterProps, type MeterThreshold } from '.
|
|
|
18
18
|
export { CompositionBar, type CompositionBarProps, type CompositionPart } from './components/composition';
|
|
19
19
|
export { LinkCard, LinkCardList, type LinkCardProps } from './components/link-card';
|
|
20
20
|
export { DataView, EmptyState, Skeleton, type Load } from './components/data-view';
|
|
21
|
+
export { Thumbnail, ThumbnailSkeleton, type ThumbnailProps, type AspectRatio } from './components/thumbnail';
|
|
21
22
|
export { DataTable, TableSkeleton, parseSort, serialiseSort, type Column, type Sort, type DataTableProps, type RowSelection, } from './components/data-table';
|
|
22
23
|
export { BulkBar, useSelection } from './components/selection';
|
|
23
24
|
export { VirtualList } from './components/virtual-list';
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import * as React from "react";
|
|
|
3
3
|
import { Slot } from "@radix-ui/react-slot";
|
|
4
4
|
import { cva } from "class-variance-authority";
|
|
5
5
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
-
import { ArrowDown, ArrowUp, ArrowUpDown, ArrowUpRight, Check, CircleAlert, LoaderCircle, RotateCcw, X } from "lucide-react";
|
|
6
|
+
import { ArrowDown, ArrowUp, ArrowUpDown, ArrowUpRight, Check, CircleAlert, ImageOff, LoaderCircle, RotateCcw, X } from "lucide-react";
|
|
7
7
|
import { Link, NavLink, useSearchParams } from "react-router";
|
|
8
8
|
import { flexRender, getCoreRowModel, getSortedRowModel, useReactTable } from "@tanstack/react-table";
|
|
9
9
|
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
@@ -667,6 +667,106 @@ function Skeleton({ className }) {
|
|
|
667
667
|
className: cn("rounded-control bg-muted motion-safe:animate-pulse", className)
|
|
668
668
|
});
|
|
669
669
|
}
|
|
670
|
+
// src/lib/in-view.ts
|
|
671
|
+
/**
|
|
672
|
+
* Whether an element is within `rootMargin` of its scroll region (the nearest
|
|
673
|
+
* `[data-strata-scroll]` ancestor, else the viewport). Returns a callback ref, so it follows the
|
|
674
|
+
* element as it mounts and unmounts.
|
|
675
|
+
*/
|
|
676
|
+
function useInView({ active = true, rootMargin = "0px", once = false, unsupported = false } = {}) {
|
|
677
|
+
const [inView, setInView] = React.useState(false);
|
|
678
|
+
const [el, setEl] = React.useState(null);
|
|
679
|
+
const seen = once && inView;
|
|
680
|
+
React.useEffect(() => {
|
|
681
|
+
if (seen) return;
|
|
682
|
+
if (!active || !el) {
|
|
683
|
+
setInView(false);
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
687
|
+
setInView(unsupported);
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
const root = el.parentElement?.closest("[data-strata-scroll]") ?? null;
|
|
691
|
+
const observer = new IntersectionObserver((entries) => setInView(entries[entries.length - 1].isIntersecting), {
|
|
692
|
+
root,
|
|
693
|
+
rootMargin
|
|
694
|
+
});
|
|
695
|
+
observer.observe(el);
|
|
696
|
+
return () => observer.disconnect();
|
|
697
|
+
}, [
|
|
698
|
+
active,
|
|
699
|
+
el,
|
|
700
|
+
rootMargin,
|
|
701
|
+
seen,
|
|
702
|
+
unsupported
|
|
703
|
+
]);
|
|
704
|
+
return {
|
|
705
|
+
ref: setEl,
|
|
706
|
+
inView
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
// src/components/thumbnail.tsx
|
|
710
|
+
var ratio = (aspect) => typeof aspect === "number" ? String(aspect) : aspect.replace("/", " / ");
|
|
711
|
+
/** A box of fixed aspect ratio for a remote image: a skeleton until it loads, the `missing` words if it cannot, and a request only when it nears the screen. `alt` and `missing` are required. */
|
|
712
|
+
function Thumbnail({ src, alt, missing, aspect = 1, fit = "cover", srcSet, sizes, eager = false, rootMargin = "200px", className }) {
|
|
713
|
+
const request = src ? `${src} ${srcSet ?? ""}` : null;
|
|
714
|
+
const [result, setResult] = React.useState(null);
|
|
715
|
+
const phase = !request ? "missing" : result?.request === request ? result.ok ? "loaded" : "missing" : "loading";
|
|
716
|
+
const near = useInView({
|
|
717
|
+
active: !eager && !!src,
|
|
718
|
+
rootMargin,
|
|
719
|
+
once: true,
|
|
720
|
+
unsupported: true
|
|
721
|
+
});
|
|
722
|
+
const requested = eager || near.inView;
|
|
723
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
724
|
+
ref: near.ref,
|
|
725
|
+
className: cn("relative overflow-hidden rounded-control bg-muted", className),
|
|
726
|
+
style: { aspectRatio: ratio(aspect) },
|
|
727
|
+
"aria-busy": phase === "loading" || void 0,
|
|
728
|
+
children: [
|
|
729
|
+
phase === "loading" && /* @__PURE__ */ jsx(Skeleton, { className: "absolute inset-0" }),
|
|
730
|
+
src && request && phase !== "missing" && requested && /* @__PURE__ */ jsx("img", {
|
|
731
|
+
src,
|
|
732
|
+
srcSet,
|
|
733
|
+
sizes,
|
|
734
|
+
alt,
|
|
735
|
+
decoding: "async",
|
|
736
|
+
onLoad: () => setResult({
|
|
737
|
+
request,
|
|
738
|
+
ok: true
|
|
739
|
+
}),
|
|
740
|
+
onError: () => setResult({
|
|
741
|
+
request,
|
|
742
|
+
ok: false
|
|
743
|
+
}),
|
|
744
|
+
className: cn("absolute inset-0 size-full", fit === "cover" ? "object-cover" : "object-contain", phase === "loading" && "opacity-0")
|
|
745
|
+
}, request),
|
|
746
|
+
phase === "missing" && /* @__PURE__ */ jsxs("div", {
|
|
747
|
+
role: "img",
|
|
748
|
+
"aria-label": alt ? `${alt}: ${missing}` : missing,
|
|
749
|
+
className: "absolute inset-0 grid content-center justify-items-center gap-0.5 p-1 text-center text-muted-foreground",
|
|
750
|
+
children: [/* @__PURE__ */ jsx(ImageOff, {
|
|
751
|
+
"aria-hidden": true,
|
|
752
|
+
className: "size-4 shrink-0"
|
|
753
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
754
|
+
className: "type-caption text-balance",
|
|
755
|
+
children: missing
|
|
756
|
+
})]
|
|
757
|
+
})
|
|
758
|
+
]
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
/** The loading shape of a `Thumbnail`, for a `DataView` skeleton: the same box and aspect ratio, so nothing moves when the list arrives. */
|
|
762
|
+
function ThumbnailSkeleton({ aspect = 1, className }) {
|
|
763
|
+
return /* @__PURE__ */ jsx("div", {
|
|
764
|
+
"aria-hidden": true,
|
|
765
|
+
className: cn("relative overflow-hidden rounded-control bg-muted", className),
|
|
766
|
+
style: { aspectRatio: ratio(aspect) },
|
|
767
|
+
children: /* @__PURE__ */ jsx(Skeleton, { className: "absolute inset-0" })
|
|
768
|
+
});
|
|
769
|
+
}
|
|
670
770
|
// src/components/data-table.tsx
|
|
671
771
|
function useLocalSort(initial) {
|
|
672
772
|
const [sort, setSort] = React.useState(initial);
|
|
@@ -1003,7 +1103,10 @@ function InfiniteList({ state, label, children, skeleton, empty, moreLabel, more
|
|
|
1003
1103
|
const { more, itemKey } = state;
|
|
1004
1104
|
const held = state.status === "ready" ? state.data.length : 0;
|
|
1005
1105
|
const box = React.useRef(null);
|
|
1006
|
-
const near =
|
|
1106
|
+
const near = useInView({
|
|
1107
|
+
active: state.status === "ready" && more.hasMore,
|
|
1108
|
+
rootMargin
|
|
1109
|
+
});
|
|
1007
1110
|
const { hasMore, loadingMore, error, loadMore } = more;
|
|
1008
1111
|
React.useEffect(() => {
|
|
1009
1112
|
if (state.status !== "ready" || !hasMore || loadingMore || error) return;
|
|
@@ -1108,36 +1211,6 @@ function InfiniteList({ state, label, children, skeleton, empty, moreLabel, more
|
|
|
1108
1211
|
})
|
|
1109
1212
|
});
|
|
1110
1213
|
}
|
|
1111
|
-
/**
|
|
1112
|
-
* Watches a sentinel element and reports whether it is within `rootMargin` of its scroll region
|
|
1113
|
-
* (the nearest `[data-strata-scroll]` ancestor, else the viewport). A callback ref, so it follows
|
|
1114
|
-
* the sentinel as it mounts and unmounts.
|
|
1115
|
-
*/
|
|
1116
|
-
function useNearEnd(active, rootMargin) {
|
|
1117
|
-
const [inView, setInView] = React.useState(false);
|
|
1118
|
-
const [el, setEl] = React.useState(null);
|
|
1119
|
-
React.useEffect(() => {
|
|
1120
|
-
if (!active || !el || typeof IntersectionObserver === "undefined") {
|
|
1121
|
-
setInView(false);
|
|
1122
|
-
return;
|
|
1123
|
-
}
|
|
1124
|
-
const root = el.parentElement?.closest("[data-strata-scroll]") ?? null;
|
|
1125
|
-
const observer = new IntersectionObserver((entries) => setInView(entries[entries.length - 1].isIntersecting), {
|
|
1126
|
-
root,
|
|
1127
|
-
rootMargin
|
|
1128
|
-
});
|
|
1129
|
-
observer.observe(el);
|
|
1130
|
-
return () => observer.disconnect();
|
|
1131
|
-
}, [
|
|
1132
|
-
active,
|
|
1133
|
-
el,
|
|
1134
|
-
rootMargin
|
|
1135
|
-
]);
|
|
1136
|
-
return {
|
|
1137
|
-
ref: setEl,
|
|
1138
|
-
inView
|
|
1139
|
-
};
|
|
1140
|
-
}
|
|
1141
1214
|
// src/components/line-chart.tsx
|
|
1142
1215
|
function useWidth() {
|
|
1143
1216
|
const ref = React.useRef(null);
|
|
@@ -2120,6 +2193,6 @@ function nextAfter(pages, items, itemKey, hidden) {
|
|
|
2120
2193
|
if (held.length === 0 || pages.length > 1 && !lastPageAddedAny) return last;
|
|
2121
2194
|
return held[held.length - 1];
|
|
2122
2195
|
}
|
|
2123
|
-
export { ActiveList, AppShell, AsyncButton, Breadcrumb, BulkBar, Button, COLOR_SCHEME_STORAGE_KEY, Checkbox, ChoiceGroup, ColorSchemeChoice, CompositionBar, DataTable, DataView, Done, EmptyState, Field, HttpError, IconButton, InfiniteList, Input, KeysLegend, LineChart, LinkCard, LinkCardList, Meter, Metric, MetricStrip, NBSP, NativeSelect, PageHeader, RowAction, SideNav, Skeleton, Status, StrataQueryProvider, TableSkeleton, Tabs, Textarea, ThemeProvider, UndoBar, VirtualList, buttonVariants, cn, colorSchemeScript, createFormat, createQueryClient, crossedThreshold, describeActionError, describeHttpError, fetchJson, format, isTypingTarget, parseSort, serialiseSort, toDate, toLoad, typeSteps, useActiveItem, useAsyncAction, useColorScheme, useHotkeys, useInfiniteLoad, useLoad, useSelection, useSystemColorScheme, useUndoable };
|
|
2196
|
+
export { ActiveList, AppShell, AsyncButton, Breadcrumb, BulkBar, Button, COLOR_SCHEME_STORAGE_KEY, Checkbox, ChoiceGroup, ColorSchemeChoice, CompositionBar, DataTable, DataView, Done, EmptyState, Field, HttpError, IconButton, InfiniteList, Input, KeysLegend, LineChart, LinkCard, LinkCardList, Meter, Metric, MetricStrip, NBSP, NativeSelect, PageHeader, RowAction, SideNav, Skeleton, Status, StrataQueryProvider, TableSkeleton, Tabs, Textarea, ThemeProvider, Thumbnail, ThumbnailSkeleton, UndoBar, VirtualList, buttonVariants, cn, colorSchemeScript, createFormat, createQueryClient, crossedThreshold, describeActionError, describeHttpError, fetchJson, format, isTypingTarget, parseSort, serialiseSort, toDate, toLoad, typeSteps, useActiveItem, useAsyncAction, useColorScheme, useHotkeys, useInfiniteLoad, useLoad, useSelection, useSystemColorScheme, useUndoable };
|
|
2124
2197
|
|
|
2125
2198
|
//# sourceMappingURL=index.js.map
|