@macrostrat/data-sheet 4.5.1 → 4.6.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/CHANGELOG.md +14 -0
- package/dist/data-panel.cjs +9 -4
- package/dist/data-panel.cjs.map +1 -1
- package/dist/data-panel.d.ts +6 -0
- package/dist/data-panel.js +9 -4
- package/dist/data-panel.js.map +1 -1
- package/dist/data-panel.module.sass.cjs +26 -26
- package/dist/data-panel.module.sass.js +26 -26
- package/dist/data-sheet.css +64 -36
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/masonry.cjs +118 -0
- package/dist/masonry.cjs.map +1 -0
- package/dist/masonry.d.ts +17 -0
- package/dist/masonry.js +118 -0
- package/dist/masonry.js.map +1 -0
- package/dist/masonry.module.sass.cjs +14 -0
- package/dist/masonry.module.sass.cjs.map +1 -0
- package/dist/masonry.module.sass.js +13 -0
- package/dist/masonry.module.sass.js.map +1 -0
- package/package.json +3 -3
- package/src/data-panel.module.sass +5 -4
- package/src/data-panel.ts +17 -5
- package/src/index.ts +1 -0
- package/src/masonry.module.sass +29 -0
- package/src/masonry.ts +217 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"masonry.module.sass.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@macrostrat/data-sheet",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"description": "Scalable data sheet with optional editing capabilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"@macrostrat/color-utils": "^1.2.2",
|
|
52
52
|
"@macrostrat/data-components": "^1.5.1",
|
|
53
53
|
"@macrostrat/hyper": "^3.1.1",
|
|
54
|
-
"@macrostrat/scoped-store": "^1.0
|
|
54
|
+
"@macrostrat/scoped-store": "^1.1.0",
|
|
55
55
|
"@macrostrat/ui-components": "^5.2.0",
|
|
56
56
|
"@supabase/postgrest-js": "^1.17.7",
|
|
57
57
|
"@types/underscore": "^1.13.0",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@macrostrat/web-components-bundler": "1.0.1",
|
|
65
|
-
"@storybook/react-vite": "10.5.
|
|
65
|
+
"@storybook/react-vite": "10.5.10"
|
|
66
66
|
},
|
|
67
67
|
"publishConfig": {
|
|
68
68
|
"access": "public"
|
|
@@ -144,10 +144,10 @@
|
|
|
144
144
|
|
|
145
145
|
.data-panel
|
|
146
146
|
&.toolbar-style-bordered
|
|
147
|
-
.data-panel-
|
|
147
|
+
.data-panel-top-area, .data-panel-footer
|
|
148
148
|
background-color: var(--panel-secondary-background-color)
|
|
149
149
|
|
|
150
|
-
.data-panel-
|
|
150
|
+
.data-panel-top-area
|
|
151
151
|
border-bottom: 1px solid var(--panel-rule-color)
|
|
152
152
|
|
|
153
153
|
.data-panel-toolbar-content
|
|
@@ -168,6 +168,7 @@
|
|
|
168
168
|
position: absolute
|
|
169
169
|
top: 0
|
|
170
170
|
left: 0
|
|
171
|
+
right: 0
|
|
171
172
|
|
|
172
173
|
.data-panel-footer, .data-panel-toolbar
|
|
173
174
|
flex: unset
|
|
@@ -195,8 +196,8 @@
|
|
|
195
196
|
padding-right: 6px
|
|
196
197
|
flex-grow: 1
|
|
197
198
|
max-width: fit-content
|
|
198
|
-
|
|
199
|
-
|
|
199
|
+
margin-left: var(--data-panel-toolbar-offset, -6px)
|
|
200
|
+
margin-right: var(--data-panel-toolbar-offset, -6px)
|
|
200
201
|
pointer-events: all
|
|
201
202
|
&>*:empty
|
|
202
203
|
display: none
|
package/src/data-panel.ts
CHANGED
|
@@ -89,6 +89,12 @@ export enum DataPanelToolbarStyle {
|
|
|
89
89
|
export interface ScrollBodyProps {
|
|
90
90
|
/** The rendered item cards (each wrapped for selection styling). */
|
|
91
91
|
children: ReactNode;
|
|
92
|
+
/** Loading skeletons for the page currently being fetched, if any. Kept
|
|
93
|
+
* separate from `children` because they are transient stand-ins, not items:
|
|
94
|
+
* a layout that assigns a lasting position per item (e.g. the masonry body)
|
|
95
|
+
* must not hand one to a placeholder. Render them after the items; a body
|
|
96
|
+
* that ignores them simply shows no loading state. */
|
|
97
|
+
placeholders?: ReactNode;
|
|
92
98
|
}
|
|
93
99
|
|
|
94
100
|
/** Props handed to a consumer's card renderer for one row. */
|
|
@@ -324,10 +330,11 @@ export function DataPanelRenderer<T>({
|
|
|
324
330
|
// reset pre-sizes to a page of `null` slots) or the next scroll page — so the
|
|
325
331
|
// body shows shimmer where rows will land instead of a blank flash or a
|
|
326
332
|
// footer pinging up into the gap. Bounded to a viewport's worth of cards.
|
|
333
|
+
const placeholderCards: ReactNode[] = [];
|
|
327
334
|
if (loadPending) {
|
|
328
335
|
const pending = Math.min(Math.max(data.length - loadedCount, 0), 24);
|
|
329
336
|
for (let i = 0; i < pending; i++) {
|
|
330
|
-
|
|
337
|
+
placeholderCards.push(
|
|
331
338
|
h(
|
|
332
339
|
"div.data-panel-item-container.is-skeleton",
|
|
333
340
|
{ key: `skeleton-${i}`, "aria-hidden": true },
|
|
@@ -336,6 +343,10 @@ export function DataPanelRenderer<T>({
|
|
|
336
343
|
);
|
|
337
344
|
}
|
|
338
345
|
}
|
|
346
|
+
let placeholders: ReactNode = null;
|
|
347
|
+
if (placeholderCards.length > 0) {
|
|
348
|
+
placeholders = placeholderCards;
|
|
349
|
+
}
|
|
339
350
|
|
|
340
351
|
// The default spinner sentinel (below-placement): hidden while paused so the
|
|
341
352
|
// pinned footer's "Load more" takes over.
|
|
@@ -450,7 +461,7 @@ export function DataPanelRenderer<T>({
|
|
|
450
461
|
},
|
|
451
462
|
[
|
|
452
463
|
h("div.data-panel-body-content", [
|
|
453
|
-
emptyState ?? h(ScrollBody, cards),
|
|
464
|
+
emptyState ?? h(ScrollBody, { placeholders }, cards),
|
|
454
465
|
_contentFooter,
|
|
455
466
|
]),
|
|
456
467
|
],
|
|
@@ -468,9 +479,10 @@ function DataPanelStatusBar({ children }) {
|
|
|
468
479
|
return h("div.data-panel-status-bar", [children, h(LoadProgressIndicator)]);
|
|
469
480
|
}
|
|
470
481
|
|
|
471
|
-
/** Default scroll-body layout: a vertical flex list of cards
|
|
472
|
-
|
|
473
|
-
|
|
482
|
+
/** Default scroll-body layout: a vertical flex list of cards, with any loading
|
|
483
|
+
* placeholders appended in the same flow. */
|
|
484
|
+
function DefaultScrollBody({ children, placeholders }: ScrollBodyProps) {
|
|
485
|
+
return h("div.data-panel-list", [children, placeholders]);
|
|
474
486
|
}
|
|
475
487
|
|
|
476
488
|
/** Runs the shared windowed loader from inside the panel (renders nothing).
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Masonry scroll body for the DataPanel — a fixed set of equal-width columns
|
|
2
|
+
// that items are distributed across by measured height (see `masonry.ts`).
|
|
3
|
+
// Not CSS multi-column: real columns, so appending a page never reflows what
|
|
4
|
+
// is already on screen.
|
|
5
|
+
|
|
6
|
+
.data-panel-masonry-body
|
|
7
|
+
display: flex
|
|
8
|
+
flex-direction: column
|
|
9
|
+
flex-grow: 1
|
|
10
|
+
|
|
11
|
+
.data-panel-masonry
|
|
12
|
+
display: flex
|
|
13
|
+
align-items: flex-start
|
|
14
|
+
gap: var(--data-panel-item-gap, 8px)
|
|
15
|
+
|
|
16
|
+
.data-panel-masonry-column
|
|
17
|
+
flex: 1 1 0
|
|
18
|
+
min-width: 0
|
|
19
|
+
display: flex
|
|
20
|
+
flex-direction: column
|
|
21
|
+
gap: var(--data-panel-masonry-row-gap, var(--data-panel-item-gap, 8px))
|
|
22
|
+
|
|
23
|
+
// Loading placeholders sit below the columns, outside the measured layout —
|
|
24
|
+
// they are transient, so they never take a (frozen) column assignment.
|
|
25
|
+
.data-panel-masonry-placeholders
|
|
26
|
+
display: flex
|
|
27
|
+
flex-direction: column
|
|
28
|
+
gap: var(--data-panel-masonry-row-gap, var(--data-panel-item-gap, 8px))
|
|
29
|
+
margin-top: var(--data-panel-masonry-row-gap, var(--data-panel-item-gap, 8px))
|
package/src/masonry.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Masonry scroll body for the `DataPanel` — variable-height cards laid out in
|
|
3
|
+
* equal-width columns, balanced by *measured* height rather than a flat
|
|
4
|
+
* estimate or CSS multi-column.
|
|
5
|
+
*
|
|
6
|
+
* The problem it solves is paging: CSS `columns` re-flows the whole list every
|
|
7
|
+
* time a page is appended, so the cards under the user's cursor move. Here each
|
|
8
|
+
* item is placed in the running-shortest column **once**, keyed by its React
|
|
9
|
+
* key, and then frozen — appending a page never reshuffles what is on screen.
|
|
10
|
+
*
|
|
11
|
+
* Placement is measured: a freshly-appended batch mounts in provisional
|
|
12
|
+
* columns, a `useLayoutEffect` reads each card's real `offsetHeight`, then the
|
|
13
|
+
* batch is greedily assigned from those real heights and re-rendered — all
|
|
14
|
+
* before paint, so only the balanced result is seen.
|
|
15
|
+
*
|
|
16
|
+
* Two things keep the balance honest over many pages:
|
|
17
|
+
*
|
|
18
|
+
* - **Placeholders are not items.** The panel's loading skeletons arrive as
|
|
19
|
+
* `placeholders`, not `children`, and render below the columns. Freezing a
|
|
20
|
+
* column assignment for a skeleton (measured at skeleton height, then
|
|
21
|
+
* replaced by real content) is what makes a masonry drift, one page at a
|
|
22
|
+
* time, into "this whole page went into one column".
|
|
23
|
+
* - **Identity is the React key, not the array index.** A sort, filter or
|
|
24
|
+
* refresh replaces the keys, and assignments for keys that are gone are
|
|
25
|
+
* pruned — so column heights are always recomputed from items that are
|
|
26
|
+
* actually on screen.
|
|
27
|
+
*/
|
|
28
|
+
import h from "./masonry.module.sass";
|
|
29
|
+
import {
|
|
30
|
+
Children,
|
|
31
|
+
cloneElement,
|
|
32
|
+
isValidElement,
|
|
33
|
+
ReactElement,
|
|
34
|
+
ReactNode,
|
|
35
|
+
useEffect,
|
|
36
|
+
useLayoutEffect,
|
|
37
|
+
useReducer,
|
|
38
|
+
useRef,
|
|
39
|
+
useState,
|
|
40
|
+
} from "react";
|
|
41
|
+
import type { ScrollBodyProps } from "./data-panel";
|
|
42
|
+
|
|
43
|
+
/** Height assumed for an item that hasn't been measured yet (it always is, by
|
|
44
|
+
* the time it matters — this only guards a torn-down/hidden container). */
|
|
45
|
+
const FALLBACK_ITEM_HEIGHT = 160;
|
|
46
|
+
|
|
47
|
+
export interface MasonryOptions {
|
|
48
|
+
/** Number of columns (the maximum, when `minColumnWidth` is set). Default 2. */
|
|
49
|
+
columns?: number;
|
|
50
|
+
/** If set, the column count drops below `columns` when the container is too
|
|
51
|
+
* narrow to give every column at least this many pixels. */
|
|
52
|
+
minColumnWidth?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface MasonryState {
|
|
56
|
+
/** Column count the current assignments were computed for. */
|
|
57
|
+
columns: number;
|
|
58
|
+
/** Frozen key → column. */
|
|
59
|
+
assign: Map<string, number>;
|
|
60
|
+
/** Last measured height per key. */
|
|
61
|
+
heights: Map<string, number>;
|
|
62
|
+
/** Mounted element per key, for measurement. */
|
|
63
|
+
els: Map<string, HTMLElement>;
|
|
64
|
+
/** Stable ref callbacks, so an item isn't detached/reattached every render. */
|
|
65
|
+
refs: Map<string, (el: HTMLElement | null) => void>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A `scrollBody` component for `DataPanel` with the default two columns. For
|
|
70
|
+
* anything else — a different column count, or responsive columns — build one
|
|
71
|
+
* with `createMasonryScrollBody`.
|
|
72
|
+
*/
|
|
73
|
+
export function MasonryScrollBody(props: ScrollBodyProps & MasonryOptions) {
|
|
74
|
+
const { children, placeholders, columns = 2, minColumnWidth } = props;
|
|
75
|
+
|
|
76
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
77
|
+
const nCols = useColumnCount(containerRef, columns, minColumnWidth);
|
|
78
|
+
|
|
79
|
+
const stateRef = useRef<MasonryState | null>(null);
|
|
80
|
+
stateRef.current ??= {
|
|
81
|
+
columns: nCols,
|
|
82
|
+
assign: new Map(),
|
|
83
|
+
heights: new Map(),
|
|
84
|
+
els: new Map(),
|
|
85
|
+
refs: new Map(),
|
|
86
|
+
};
|
|
87
|
+
const state = stateRef.current;
|
|
88
|
+
const [, bump] = useReducer((x: number) => x + 1, 0);
|
|
89
|
+
|
|
90
|
+
const items = Children.toArray(children).filter(
|
|
91
|
+
isValidElement,
|
|
92
|
+
) as ReactElement[];
|
|
93
|
+
|
|
94
|
+
// Runs on every commit (no deps): measurement has to see the layout that was
|
|
95
|
+
// just produced, including one caused by a resize or a card's own content
|
|
96
|
+
// settling. It is cheap when nothing is unassigned, and re-renders only when
|
|
97
|
+
// it actually assigns something — so there's no render loop.
|
|
98
|
+
useLayoutEffect(() => {
|
|
99
|
+
// A column-count change invalidates every assignment (a column may no
|
|
100
|
+
// longer exist). Handled here, not in render, so render stays pure.
|
|
101
|
+
if (state.columns !== nCols) {
|
|
102
|
+
state.columns = nCols;
|
|
103
|
+
state.assign.clear();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
for (const [key, el] of state.els) state.heights.set(key, el.offsetHeight);
|
|
107
|
+
|
|
108
|
+
const present = new Set(items.map(keyOf));
|
|
109
|
+
for (const key of [...state.assign.keys()]) {
|
|
110
|
+
if (!present.has(key)) state.assign.delete(key);
|
|
111
|
+
}
|
|
112
|
+
for (const key of [...state.heights.keys()]) {
|
|
113
|
+
if (!present.has(key)) state.heights.delete(key);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const unassigned = items.filter((c) => !state.assign.has(keyOf(c)));
|
|
117
|
+
if (unassigned.length === 0) return;
|
|
118
|
+
|
|
119
|
+
// Column heights from the items already frozen into them…
|
|
120
|
+
const colHeights = new Array(nCols).fill(0);
|
|
121
|
+
for (const [key, col] of state.assign) {
|
|
122
|
+
colHeights[col] += state.heights.get(key) ?? 0;
|
|
123
|
+
}
|
|
124
|
+
// …then greedily place the new batch by its just-measured real heights.
|
|
125
|
+
for (const child of unassigned) {
|
|
126
|
+
const key = keyOf(child);
|
|
127
|
+
let col = 0;
|
|
128
|
+
for (let c = 1; c < nCols; c++) {
|
|
129
|
+
if (colHeights[c] < colHeights[col]) col = c;
|
|
130
|
+
}
|
|
131
|
+
state.assign.set(key, col);
|
|
132
|
+
colHeights[col] += state.heights.get(key) ?? FALLBACK_ITEM_HEIGHT;
|
|
133
|
+
}
|
|
134
|
+
bump(); // move the new batch into its balanced columns, before paint
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Items without an assignment yet render in a provisional column so they
|
|
138
|
+
// mount and can be measured; the layout effect above then places them.
|
|
139
|
+
const stale = state.columns !== nCols;
|
|
140
|
+
const columnItems: ReactNode[][] = Array.from({ length: nCols }, () => []);
|
|
141
|
+
items.forEach((child, i) => {
|
|
142
|
+
const key = keyOf(child);
|
|
143
|
+
let assigned: number | undefined = undefined;
|
|
144
|
+
if (!stale) {
|
|
145
|
+
assigned = state.assign.get(key);
|
|
146
|
+
}
|
|
147
|
+
const col = assigned ?? i % nCols;
|
|
148
|
+
columnItems[col].push(
|
|
149
|
+
cloneElement(child, { ref: refFor(state, key) } as any),
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return h("div.data-panel-masonry-body", [
|
|
154
|
+
h(
|
|
155
|
+
"div.data-panel-masonry",
|
|
156
|
+
{ ref: containerRef },
|
|
157
|
+
columnItems.map((col, c) =>
|
|
158
|
+
h("div.data-panel-masonry-column", { key: c }, col),
|
|
159
|
+
),
|
|
160
|
+
),
|
|
161
|
+
h.if(placeholders != null)(
|
|
162
|
+
"div.data-panel-masonry-placeholders",
|
|
163
|
+
placeholders,
|
|
164
|
+
),
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Build a `scrollBody` masonry component with a fixed or responsive column
|
|
169
|
+
* count: `scrollBody: createMasonryScrollBody({ columns: 3 })`. */
|
|
170
|
+
export function createMasonryScrollBody(options: MasonryOptions = {}) {
|
|
171
|
+
return function MasonryScrollBodyWithOptions(props: ScrollBodyProps) {
|
|
172
|
+
return h(MasonryScrollBody, { ...options, ...props });
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function keyOf(child: ReactElement): string {
|
|
177
|
+
return String(child.key);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** One stable ref callback per key — a fresh closure each render would detach
|
|
181
|
+
* and reattach every card on every render. */
|
|
182
|
+
function refFor(state: MasonryState, key: string) {
|
|
183
|
+
let cb = state.refs.get(key);
|
|
184
|
+
if (cb == null) {
|
|
185
|
+
cb = (el: HTMLElement | null) => {
|
|
186
|
+
if (el == null) {
|
|
187
|
+
state.els.delete(key);
|
|
188
|
+
} else {
|
|
189
|
+
state.els.set(key, el);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
state.refs.set(key, cb);
|
|
193
|
+
}
|
|
194
|
+
return cb;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The effective column count: `columns`, reduced to fit `minColumnWidth`. */
|
|
198
|
+
function useColumnCount(
|
|
199
|
+
ref: { current: HTMLElement | null },
|
|
200
|
+
columns: number,
|
|
201
|
+
minColumnWidth: number | undefined,
|
|
202
|
+
): number {
|
|
203
|
+
const [width, setWidth] = useState<number | null>(null);
|
|
204
|
+
|
|
205
|
+
useEffect(() => {
|
|
206
|
+
const el = ref.current;
|
|
207
|
+
if (minColumnWidth == null || el == null) return;
|
|
208
|
+
const obs = new ResizeObserver(([entry]) => {
|
|
209
|
+
setWidth(entry.contentRect.width);
|
|
210
|
+
});
|
|
211
|
+
obs.observe(el);
|
|
212
|
+
return () => obs.disconnect();
|
|
213
|
+
}, [minColumnWidth]);
|
|
214
|
+
|
|
215
|
+
if (minColumnWidth == null || width == null) return Math.max(1, columns);
|
|
216
|
+
return Math.max(1, Math.min(columns, Math.floor(width / minColumnWidth)));
|
|
217
|
+
}
|