@design-edito/tools 0.5.1 → 0.5.3
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/agnostic/arrays/index.d.ts +1 -1
- package/agnostic/arrays/index.js +1 -1
- package/agnostic/colors/index.d.ts +1 -1
- package/agnostic/colors/index.js +1 -1
- package/agnostic/css/index.d.ts +1 -1
- package/agnostic/css/index.js +1 -1
- package/agnostic/html/deep-select/index.js +5 -3
- package/agnostic/html/hyper-json/smart-tags/coalesced/index.d.ts +9 -9
- package/agnostic/html/hyper-json/smart-tags/coalesced/index.js +9 -9
- package/agnostic/html/hyper-json/smart-tags/isolated/index.d.ts +1 -1
- package/agnostic/html/hyper-json/smart-tags/isolated/index.js +1 -1
- package/agnostic/html/index.d.ts +3 -3
- package/agnostic/html/index.js +3 -3
- package/agnostic/index.d.ts +3 -3
- package/agnostic/index.js +3 -3
- package/agnostic/misc/index.d.ts +1 -1
- package/agnostic/misc/index.js +1 -1
- package/agnostic/misc/logs/index.d.ts +1 -1
- package/agnostic/misc/logs/index.js +1 -1
- package/agnostic/numbers/index.d.ts +3 -3
- package/agnostic/numbers/index.js +3 -3
- package/agnostic/objects/index.d.ts +3 -3
- package/agnostic/objects/index.js +3 -3
- package/agnostic/random/index.d.ts +1 -1
- package/agnostic/random/index.js +1 -1
- package/agnostic/strings/index.d.ts +3 -3
- package/agnostic/strings/index.js +3 -3
- package/agnostic/time/dates/format-date/index.d.ts +4 -2
- package/agnostic/time/dates/format-date/index.js +43 -94
- package/agnostic/time/dates/format-date/index.test.js +154 -0
- package/agnostic/time/dates/format-date/parts.d.ts +28 -0
- package/agnostic/time/dates/format-date/parts.js +136 -0
- package/agnostic/time/dates/format-date/types.d.ts +84 -0
- package/agnostic/time/dates/format-date/types.js +1 -0
- package/agnostic/time/duration/format-duration/index.d.ts +42 -0
- package/agnostic/time/duration/format-duration/index.js +94 -0
- package/agnostic/time/duration/format-duration/index.test.d.ts +1 -0
- package/agnostic/time/duration/format-duration/index.test.js +148 -0
- package/agnostic/time/duration/format-duration/parts.d.ts +32 -0
- package/agnostic/time/duration/format-duration/parts.js +107 -0
- package/agnostic/time/duration/format-duration/types.d.ts +93 -0
- package/agnostic/time/duration/format-duration/types.js +1 -0
- package/agnostic/time/duration/index.d.ts +6 -1
- package/agnostic/time/duration/index.js +2 -2
- package/agnostic/time/duration/index.test.d.ts +1 -0
- package/agnostic/time/duration/index.test.js +168 -0
- package/components/BeforeAfter/index.controlled.d.ts +25 -25
- package/components/BeforeAfter/index.controlled.js +47 -53
- package/components/BeforeAfter/index.d.ts +20 -23
- package/components/BeforeAfter/index.js +48 -40
- package/components/Button/index.js +1 -1
- package/components/Clippable/index.d.ts +17 -26
- package/components/Clippable/index.js +21 -23
- package/components/Disclaimer/index.d.ts +29 -22
- package/components/Disclaimer/index.js +29 -24
- package/components/Drawer/index.d.ts +45 -25
- package/components/Drawer/index.js +61 -52
- package/components/EventListener/index.d.ts +4 -5
- package/components/Gallery/index.d.ts +14 -16
- package/components/Gallery/index.js +25 -30
- package/components/Iframe/index.js +2 -2
- package/components/Image/index.d.ts +5 -12
- package/components/Image/index.js +11 -32
- package/components/Input/index.d.ts +23 -19
- package/components/Input/index.js +30 -20
- package/components/JsonEditor/index.d.ts +177 -9
- package/components/JsonEditor/index.js +133 -75
- package/components/JsonEditor/types.d.ts +11 -0
- package/components/JsonEditor/types.js +1 -0
- package/components/JsonEditor/utils.d.ts +8 -0
- package/components/JsonEditor/utils.js +23 -0
- package/components/ListLoader/index.controlled.d.ts +78 -0
- package/components/ListLoader/index.controlled.js +99 -0
- package/components/ListLoader/index.d.ts +69 -0
- package/components/ListLoader/index.js +146 -0
- package/components/ListLoader/utils.d.ts +8 -0
- package/components/ListLoader/utils.js +10 -0
- package/components/Overlayer/index.js +4 -4
- package/components/Paginator/index.d.ts +7 -12
- package/components/Paginator/index.js +7 -11
- package/components/ResizeObserver/index.d.ts +14 -3
- package/components/ResizeObserver/index.js +49 -24
- package/components/ResizeObserver/styles.module.css +0 -0
- package/components/Scrllgngn/index.d.ts +17 -13
- package/components/Scrllgngn/index.js +25 -84
- package/components/Scrllgngn/utils.d.ts +52 -0
- package/components/Scrllgngn/utils.js +102 -0
- package/components/ScrollListener/index.d.ts +43 -28
- package/components/ScrollListener/index.js +59 -79
- package/components/ScrollListener/utils.d.ts +37 -16
- package/components/ScrollListener/utils.js +141 -28
- package/components/Select/index.d.ts +24 -20
- package/components/Select/index.js +30 -20
- package/components/Sequencer/index.controlled.d.ts +16 -26
- package/components/Sequencer/index.controlled.js +15 -12
- package/components/Sequencer/index.d.ts +25 -30
- package/components/Sequencer/index.js +20 -34
- package/components/Subtitles/index.d.ts +29 -5
- package/components/Subtitles/index.js +90 -12
- package/components/Textarea/index.d.ts +26 -20
- package/components/Textarea/index.js +40 -21
- package/components/Theatre/index.d.ts +35 -39
- package/components/Theatre/index.js +46 -58
- package/components/UIModule/index.d.ts +25 -29
- package/components/UIModule/index.js +81 -110
- package/components/Video/index.controlled.d.ts +51 -51
- package/components/Video/index.controlled.js +75 -72
- package/components/Video/index.d.ts +12 -14
- package/components/Video/index.js +41 -86
- package/components/Video/utils.d.ts +35 -3
- package/components/Video/utils.js +37 -14
- package/components/index.d.ts +5 -4
- package/components/index.js +5 -4
- package/components/public-classnames.d.ts +1 -0
- package/components/public-classnames.js +1 -0
- package/components/utils/index.d.ts +22 -0
- package/components/utils/index.js +31 -0
- package/node/@google-cloud/storage/directory/index.d.ts +2 -2
- package/node/@google-cloud/storage/directory/index.js +2 -2
- package/node/@google-cloud/storage/file/index.d.ts +2 -2
- package/node/@google-cloud/storage/file/index.js +2 -2
- package/node/@google-cloud/storage/index.d.ts +1 -1
- package/node/@google-cloud/storage/index.js +1 -1
- package/node/cloud-storage/operations/index.d.ts +2 -2
- package/node/cloud-storage/operations/index.js +2 -2
- package/node/ftps/directory/index.d.ts +1 -1
- package/node/ftps/directory/index.js +1 -1
- package/node/ftps/file/index.d.ts +2 -2
- package/node/ftps/file/index.js +2 -2
- package/node/images/index.d.ts +1 -1
- package/node/images/index.js +1 -1
- package/node/images/transform/operations/index.d.ts +3 -3
- package/node/images/transform/operations/index.js +3 -3
- package/node/index.d.ts +2 -2
- package/node/index.js +2 -2
- package/node/process/index.d.ts +1 -1
- package/node/process/index.js +1 -1
- package/node/sftp/directory/index.d.ts +1 -1
- package/node/sftp/directory/index.js +1 -1
- package/node/sftp/file/index.d.ts +1 -1
- package/node/sftp/file/index.js +1 -1
- package/package.json +27 -5
- package/TODO.md +0 -269
- package/components/BeforeAfter/utils.d.ts +0 -4
- package/components/BeforeAfter/utils.js +0 -9
- package/components/Input/index.controlled.d.ts +0 -42
- package/components/Input/index.controlled.js +0 -54
- package/components/Select/index.controlled.d.ts +0 -43
- package/components/Select/index.controlled.js +0 -54
- package/components/Subtitles/types.d.ts +0 -24
- package/components/Subtitles/utils.d.ts +0 -32
- package/components/Subtitles/utils.js +0 -107
- package/components/Textarea/index.controlled.d.ts +0 -50
- package/components/Textarea/index.controlled.js +0 -75
- /package/{components/Subtitles/types.js → agnostic/time/dates/format-date/index.test.d.ts} +0 -0
- /package/components/{ResizeObserver/style.module.css → ListLoader/styles.module.css} +0 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { clss } from '../../agnostic/css/clss/index.js';
|
|
2
|
+
import { isNonNullObject } from '../../agnostic/objects/is-object/index.js';
|
|
3
|
+
import { jsonEditor as publicClassName } from '../public-classnames.js';
|
|
4
|
+
import cssModule from './styles.module.css';
|
|
5
|
+
/**
|
|
6
|
+
* Shared by every editor in the tree — they all emit class names under the same
|
|
7
|
+
* public prefix, so a single instance is what keeps them consistent.
|
|
8
|
+
*/
|
|
9
|
+
export const c = clss(publicClassName, { cssModule });
|
|
10
|
+
/** Which {@link ValueType} a value belongs to. */
|
|
11
|
+
export function getValueType(value) {
|
|
12
|
+
if (value === null)
|
|
13
|
+
return 'null';
|
|
14
|
+
if (Array.isArray(value))
|
|
15
|
+
return 'array';
|
|
16
|
+
if (isNonNullObject(value))
|
|
17
|
+
return 'record';
|
|
18
|
+
if (typeof value === 'string')
|
|
19
|
+
return 'string';
|
|
20
|
+
if (typeof value === 'number')
|
|
21
|
+
return 'number';
|
|
22
|
+
return 'boolean';
|
|
23
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { WithClassName } from '../utils/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Props for the {@link ControlledListLoader} component.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The item type carried by each page.
|
|
7
|
+
*
|
|
8
|
+
* @property pages - Page positions the consumer wants rendered. Entries outside
|
|
9
|
+
* `firstPagePos`–`lastPagePos` are ignored, as are entries absent from `itemsPages`.
|
|
10
|
+
* @property firstPagePos - Lowest page position that exists. No load button is
|
|
11
|
+
* ever rendered below it.
|
|
12
|
+
* @property lastPagePos - Highest page position that exists. No load button is
|
|
13
|
+
* ever rendered above it.
|
|
14
|
+
* @property itemsPages - Already-loaded items, keyed by page position. The
|
|
15
|
+
* component holds no data of its own and renders only what this map contains.
|
|
16
|
+
* @property filter - Decides whether an item is rendered. Applied before deduplication.
|
|
17
|
+
* @property display - Renders a single item.
|
|
18
|
+
* @property getIdentifier - Item identity, used as React key and to deduplicate.
|
|
19
|
+
* When two rendered items share an identifier, only the later one in page order
|
|
20
|
+
* survives. Returning `undefined` opts an item out of deduplication, and falls
|
|
21
|
+
* back to a positional key.
|
|
22
|
+
* @property loadingPages - Page positions currently being fetched. Exposed on the
|
|
23
|
+
* root element as `data-loading-pages`.
|
|
24
|
+
* @property onLoadPageClicked - Called with the page position a load button targets,
|
|
25
|
+
* either on click or on viewport entry when auto-loading is enabled.
|
|
26
|
+
* @property autoLoadPrevWhenVisible - Wraps the leading load button in an
|
|
27
|
+
* {@link IntersectionObserverComponent} and fires `onLoadPageClicked` when it enters the viewport.
|
|
28
|
+
* @property autoLoadNextWhenVisible - Same, for the trailing load button.
|
|
29
|
+
* @property className - Additional class name(s) applied to the root element.
|
|
30
|
+
*/
|
|
31
|
+
export type Props<T> = WithClassName<{
|
|
32
|
+
pages: number[];
|
|
33
|
+
firstPagePos: number;
|
|
34
|
+
lastPagePos: number;
|
|
35
|
+
itemsPages: Map<number, T[]>;
|
|
36
|
+
filter: (item: T) => boolean;
|
|
37
|
+
display: (item: T) => ReactNode;
|
|
38
|
+
getIdentifier?: (item: T) => string | undefined;
|
|
39
|
+
loadingPages?: number[];
|
|
40
|
+
onLoadPageClicked?: (pagePos: number) => void;
|
|
41
|
+
autoLoadPrevWhenVisible?: boolean;
|
|
42
|
+
autoLoadNextWhenVisible?: boolean;
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Presentational layer of the paginated list. Renders already-loaded pages as a
|
|
46
|
+
* single flat item list, and interleaves load buttons wherever a page is missing.
|
|
47
|
+
*
|
|
48
|
+
* This component holds no state and performs no fetching. Loaded pages are sorted
|
|
49
|
+
* by position and flattened, so items appear in page order then in item order,
|
|
50
|
+
* with no per-page wrapper element.
|
|
51
|
+
*
|
|
52
|
+
* ### Load buttons
|
|
53
|
+
* One button is rendered per missing page position:
|
|
54
|
+
* - before the first loaded page, unless it is already `firstPagePos`,
|
|
55
|
+
* - in place of each page missing between two loaded pages,
|
|
56
|
+
* - after the last loaded page, unless it is already `lastPagePos`.
|
|
57
|
+
*
|
|
58
|
+
* ### CSS elements
|
|
59
|
+
* - `item` — wraps each rendered item. Carries `data-page`.
|
|
60
|
+
* - `load` — a load button. Rendered empty, so its label belongs in CSS.
|
|
61
|
+
* Carries `data-page` and a `prev`, `next` or `gap` modifier.
|
|
62
|
+
* - `load-observer` — the intersection observer wrapping an auto-loading button.
|
|
63
|
+
* Present only when `autoLoadPrevWhenVisible` or `autoLoadNextWhenVisible` is on.
|
|
64
|
+
* Carries a `prev` or `next` modifier.
|
|
65
|
+
*
|
|
66
|
+
* ### Data attributes on the root element
|
|
67
|
+
* - `data-loading-pages` — comma-separated ascending list of the pages being
|
|
68
|
+
* fetched, e.g. `"1,3,6"`. Absent entirely when nothing is loading.
|
|
69
|
+
*
|
|
70
|
+
* @param props - Component properties.
|
|
71
|
+
* @see {@link Props}
|
|
72
|
+
* @returns A flat list of items with load buttons in place of missing pages.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* Deduplication runs after `filter`, so an item hidden by `filter` never evicts
|
|
76
|
+
* an earlier duplicate.
|
|
77
|
+
*/
|
|
78
|
+
export declare const ControlledListLoader: <T>({ className, pages, firstPagePos, lastPagePos, itemsPages, filter, display, getIdentifier, loadingPages, onLoadPageClicked, autoLoadPrevWhenVisible, autoLoadNextWhenVisible }: Props<T>) => ReactNode;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { clss } from '../../agnostic/css/clss/index.js';
|
|
3
|
+
import { IntersectionObserverComponent } from '../IntersectionObserver/index.js';
|
|
4
|
+
import { mergeClassNames } from '../utils/index.js';
|
|
5
|
+
import { listLoader as publicClassName } from '../public-classnames.js';
|
|
6
|
+
import { rangeBetween } from './utils.js';
|
|
7
|
+
import cssModule from './styles.module.css';
|
|
8
|
+
/**
|
|
9
|
+
* Presentational layer of the paginated list. Renders already-loaded pages as a
|
|
10
|
+
* single flat item list, and interleaves load buttons wherever a page is missing.
|
|
11
|
+
*
|
|
12
|
+
* This component holds no state and performs no fetching. Loaded pages are sorted
|
|
13
|
+
* by position and flattened, so items appear in page order then in item order,
|
|
14
|
+
* with no per-page wrapper element.
|
|
15
|
+
*
|
|
16
|
+
* ### Load buttons
|
|
17
|
+
* One button is rendered per missing page position:
|
|
18
|
+
* - before the first loaded page, unless it is already `firstPagePos`,
|
|
19
|
+
* - in place of each page missing between two loaded pages,
|
|
20
|
+
* - after the last loaded page, unless it is already `lastPagePos`.
|
|
21
|
+
*
|
|
22
|
+
* ### CSS elements
|
|
23
|
+
* - `item` — wraps each rendered item. Carries `data-page`.
|
|
24
|
+
* - `load` — a load button. Rendered empty, so its label belongs in CSS.
|
|
25
|
+
* Carries `data-page` and a `prev`, `next` or `gap` modifier.
|
|
26
|
+
* - `load-observer` — the intersection observer wrapping an auto-loading button.
|
|
27
|
+
* Present only when `autoLoadPrevWhenVisible` or `autoLoadNextWhenVisible` is on.
|
|
28
|
+
* Carries a `prev` or `next` modifier.
|
|
29
|
+
*
|
|
30
|
+
* ### Data attributes on the root element
|
|
31
|
+
* - `data-loading-pages` — comma-separated ascending list of the pages being
|
|
32
|
+
* fetched, e.g. `"1,3,6"`. Absent entirely when nothing is loading.
|
|
33
|
+
*
|
|
34
|
+
* @param props - Component properties.
|
|
35
|
+
* @see {@link Props}
|
|
36
|
+
* @returns A flat list of items with load buttons in place of missing pages.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* Deduplication runs after `filter`, so an item hidden by `filter` never evicts
|
|
40
|
+
* an earlier duplicate.
|
|
41
|
+
*/
|
|
42
|
+
export const ControlledListLoader = ({ className, pages, firstPagePos, lastPagePos, itemsPages, filter, display, getIdentifier, loadingPages, onLoadPageClicked, autoLoadPrevWhenVisible, autoLoadNextWhenVisible }) => {
|
|
43
|
+
const inBoundsPages = pages.filter(pagePos => pagePos >= firstPagePos && pagePos <= lastPagePos);
|
|
44
|
+
const filteredItems = Array.from(itemsPages)
|
|
45
|
+
.filter(([pagePos]) => inBoundsPages.includes(pagePos))
|
|
46
|
+
.sort(([pageA], [pageB]) => pageA - pageB)
|
|
47
|
+
.flatMap(([pagePos, items]) => items.map((item, itemPos) => ({ item, pagePos, itemPos })))
|
|
48
|
+
.filter(({ item }) => filter(item));
|
|
49
|
+
const identifiers = filteredItems.map(({ item }) => getIdentifier?.(item));
|
|
50
|
+
const displayedItems = filteredItems.filter((_, pos) => {
|
|
51
|
+
const identifier = identifiers[pos];
|
|
52
|
+
if (identifier === undefined)
|
|
53
|
+
return true;
|
|
54
|
+
return identifiers.lastIndexOf(identifier) === pos;
|
|
55
|
+
});
|
|
56
|
+
const loadedPages = Array.from(itemsPages.keys())
|
|
57
|
+
.filter(pagePos => inBoundsPages.includes(pagePos))
|
|
58
|
+
.sort((a, b) => a - b);
|
|
59
|
+
const firstLoadedPage = loadedPages[0];
|
|
60
|
+
const lastLoadedPage = loadedPages[loadedPages.length - 1];
|
|
61
|
+
const gapPages = loadedPages.flatMap((pagePos, pos) => {
|
|
62
|
+
const prevPage = loadedPages[pos - 1];
|
|
63
|
+
if (prevPage === undefined)
|
|
64
|
+
return [];
|
|
65
|
+
return rangeBetween(prevPage + 1, pagePos - 1);
|
|
66
|
+
});
|
|
67
|
+
const slots = [
|
|
68
|
+
...firstLoadedPage !== undefined && firstLoadedPage > firstPagePos
|
|
69
|
+
? [{ pagePos: firstLoadedPage - 1, kind: 'prev' }]
|
|
70
|
+
: [],
|
|
71
|
+
...loadedPages.map(pagePos => ({ pagePos, kind: 'items' })),
|
|
72
|
+
...gapPages.map(pagePos => ({ pagePos, kind: 'gap' })),
|
|
73
|
+
...lastLoadedPage !== undefined && lastLoadedPage < lastPagePos
|
|
74
|
+
? [{ pagePos: lastLoadedPage + 1, kind: 'next' }]
|
|
75
|
+
: []
|
|
76
|
+
].sort((a, b) => a.pagePos - b.pagePos);
|
|
77
|
+
const sortedLoadingPages = [...loadingPages ?? []]
|
|
78
|
+
.filter(pagePos => inBoundsPages.includes(pagePos))
|
|
79
|
+
.sort((a, b) => a - b);
|
|
80
|
+
const c = clss(publicClassName, { cssModule });
|
|
81
|
+
const rootClss = mergeClassNames(c(), className);
|
|
82
|
+
const renderLoadButton = (pagePos, kind) => {
|
|
83
|
+
const button = _jsx("button", { className: c('load', kind), "data-page": pagePos, onClick: () => onLoadPageClicked?.(pagePos) }, `load/${pagePos}`);
|
|
84
|
+
const autoLoads = (kind === 'prev' && autoLoadPrevWhenVisible === true)
|
|
85
|
+
|| (kind === 'next' && autoLoadNextWhenVisible === true);
|
|
86
|
+
if (!autoLoads)
|
|
87
|
+
return button;
|
|
88
|
+
return _jsx(IntersectionObserverComponent, { className: c('load-observer', kind), onIntersected: ({ ioEntry }) => {
|
|
89
|
+
if (ioEntry?.isIntersecting !== true)
|
|
90
|
+
return;
|
|
91
|
+
onLoadPageClicked?.(pagePos);
|
|
92
|
+
}, children: button }, `load/${pagePos}`);
|
|
93
|
+
};
|
|
94
|
+
return _jsx("div", { className: rootClss, "data-loading-pages": sortedLoadingPages.length === 0 ? undefined : sortedLoadingPages.join(','), children: slots.map(({ pagePos, kind }) => kind === 'items'
|
|
95
|
+
? displayedItems
|
|
96
|
+
.filter(entry => entry.pagePos === pagePos)
|
|
97
|
+
.map(({ item, itemPos }) => _jsx("div", { className: c('item'), "data-page": pagePos, children: display(item) }, getIdentifier?.(item) ?? `${pagePos}/${itemPos}`))
|
|
98
|
+
: renderLoadButton(pagePos, kind)) });
|
|
99
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type Props as ControlledProps } from './index.controlled.js';
|
|
3
|
+
/**
|
|
4
|
+
* Props for the {@link ListLoader} component.
|
|
5
|
+
*
|
|
6
|
+
* Extends {@link ControlledProps} — minus the data it derives itself — with
|
|
7
|
+
* fetching, staleness and page-set management.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The item type carried by each page.
|
|
10
|
+
*
|
|
11
|
+
* @property pages - Page positions to keep loaded. When provided, the component
|
|
12
|
+
* is fully controlled: load buttons only report clicks and never change the set
|
|
13
|
+
* themselves. When omitted, the set is managed internally.
|
|
14
|
+
* @property defaultPage - Page loaded first in uncontrolled mode. Clamped into
|
|
15
|
+
* `firstPagePos`–`lastPagePos`, and defaults to `firstPagePos`. Ignored when
|
|
16
|
+
* `pages` is provided.
|
|
17
|
+
* @property fillGaps - When `true` (the default), any page missing between the
|
|
18
|
+
* lowest and highest requested page is requested automatically, so the loaded
|
|
19
|
+
* range stays contiguous. Ignored when `pages` is provided.
|
|
20
|
+
* @property dropPagesFurtherThan - Maximum distance, in pages, kept around a page
|
|
21
|
+
* loaded through a load button. Pages further away are dropped from state and
|
|
22
|
+
* from the DOM. Applies to load-button activations only, never to retries or
|
|
23
|
+
* stale reloads. Ignored when `pages` is provided.
|
|
24
|
+
* @property fetch - Fetches one page's items. Rejections are reported through
|
|
25
|
+
* `onPageFetchFailed` and retried according to `fetchRetriesNb`.
|
|
26
|
+
* @property staleAfterMs - Delay after which a loaded page is refetched, counted
|
|
27
|
+
* from its own last successful load. When omitted, pages are never refreshed.
|
|
28
|
+
* @property onPageFetched - Called after a page's items have been stored. Not
|
|
29
|
+
* called for a page dropped while its request was in flight.
|
|
30
|
+
* @property onPageFetchFailed - Called on every failed attempt, not only once retries
|
|
31
|
+
* are exhausted. When omitted, failures are logged with `console.warn`.
|
|
32
|
+
* @property fetchRetriesNb - Number of retries after a failed fetch. Defaults to
|
|
33
|
+
* `Infinity`, so a page keeps retrying until it succeeds.
|
|
34
|
+
* @property fetchRetriesDelayMs - Delay between two attempts. Defaults to `1000`.
|
|
35
|
+
*/
|
|
36
|
+
export type Props<T> = Omit<ControlledProps<T>, 'itemsPages' | 'loadingPages' | 'pages'> & {
|
|
37
|
+
pages?: number[];
|
|
38
|
+
defaultPage?: number;
|
|
39
|
+
fillGaps?: boolean;
|
|
40
|
+
dropPagesFurtherThan?: number;
|
|
41
|
+
fetch: (page: number) => Promise<T[]>;
|
|
42
|
+
staleAfterMs?: number;
|
|
43
|
+
onPageFetched?: (pagePos: number, items: T[]) => void;
|
|
44
|
+
onPageFetchFailed?: (pagePos: number, error: Error) => void;
|
|
45
|
+
fetchRetriesNb?: number;
|
|
46
|
+
fetchRetriesDelayMs?: number;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Paginated list abstraction. Fetches the pages it is asked for, keeps them
|
|
50
|
+
* fresh, and drives a {@link ControlledListLoader} with the result.
|
|
51
|
+
*
|
|
52
|
+
* @param props - Component properties.
|
|
53
|
+
* @see {@link Props}
|
|
54
|
+
* @see {@link ControlledListLoader} for the rendered markup and CSS elements.
|
|
55
|
+
* @returns A {@link ControlledListLoader} fed with the loaded pages.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* - In controlled mode (`pages` defined), the page set is entirely driven by the
|
|
59
|
+
* parent. `fillGaps` and `dropPagesFurtherThan` are inert, and load buttons only
|
|
60
|
+
* report through `onLoadPageClicked`.
|
|
61
|
+
* - In uncontrolled mode, internal state is initialized from `defaultPage` and load
|
|
62
|
+
* buttons extend the set themselves. `onLoadPageClicked` still fires, after the
|
|
63
|
+
* internal state has been updated.
|
|
64
|
+
* - A page removed from the effective set is dropped from memory, and a request
|
|
65
|
+
* still in flight for it is discarded on arrival rather than re-inserted.
|
|
66
|
+
* - No page outside `firstPagePos`–`lastPagePos` is ever fetched, whichever mode
|
|
67
|
+
* is in use.
|
|
68
|
+
*/
|
|
69
|
+
export declare const ListLoader: <T>({ className, pages, defaultPage, fillGaps, dropPagesFurtherThan, autoLoadPrevWhenVisible, autoLoadNextWhenVisible, firstPagePos, lastPagePos, fetch, filter, display, getIdentifier, onLoadPageClicked, staleAfterMs, onPageFetched, onPageFetchFailed, fetchRetriesNb, fetchRetriesDelayMs }: Props<T>) => ReactNode;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { toError } from '../../agnostic/misc/cast/index.js';
|
|
4
|
+
import { clamp } from '../../agnostic/numbers/clamp/index.js';
|
|
5
|
+
import { ControlledListLoader } from './index.controlled.js';
|
|
6
|
+
import { rangeBetween } from './utils.js';
|
|
7
|
+
/**
|
|
8
|
+
* Paginated list abstraction. Fetches the pages it is asked for, keeps them
|
|
9
|
+
* fresh, and drives a {@link ControlledListLoader} with the result.
|
|
10
|
+
*
|
|
11
|
+
* @param props - Component properties.
|
|
12
|
+
* @see {@link Props}
|
|
13
|
+
* @see {@link ControlledListLoader} for the rendered markup and CSS elements.
|
|
14
|
+
* @returns A {@link ControlledListLoader} fed with the loaded pages.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* - In controlled mode (`pages` defined), the page set is entirely driven by the
|
|
18
|
+
* parent. `fillGaps` and `dropPagesFurtherThan` are inert, and load buttons only
|
|
19
|
+
* report through `onLoadPageClicked`.
|
|
20
|
+
* - In uncontrolled mode, internal state is initialized from `defaultPage` and load
|
|
21
|
+
* buttons extend the set themselves. `onLoadPageClicked` still fires, after the
|
|
22
|
+
* internal state has been updated.
|
|
23
|
+
* - A page removed from the effective set is dropped from memory, and a request
|
|
24
|
+
* still in flight for it is discarded on arrival rather than re-inserted.
|
|
25
|
+
* - No page outside `firstPagePos`–`lastPagePos` is ever fetched, whichever mode
|
|
26
|
+
* is in use.
|
|
27
|
+
*/
|
|
28
|
+
export const ListLoader = ({ className, pages, defaultPage, fillGaps = true, dropPagesFurtherThan, autoLoadPrevWhenVisible, autoLoadNextWhenVisible, firstPagePos, lastPagePos, fetch, filter, display, getIdentifier, onLoadPageClicked, staleAfterMs, onPageFetched, onPageFetchFailed, fetchRetriesNb = Infinity, fetchRetriesDelayMs = 1000 }) => {
|
|
29
|
+
const [itemsPages, setItemsPages] = useState(new Map());
|
|
30
|
+
const [loadingPages, setLoadingPages] = useState(new Set());
|
|
31
|
+
const [internalPages, setInternalPages] = useState([
|
|
32
|
+
clamp(defaultPage ?? firstPagePos, firstPagePos, lastPagePos)
|
|
33
|
+
]);
|
|
34
|
+
const requestedPages = useRef(new Set());
|
|
35
|
+
// Single source of truth for what should be loaded. Memoized so the effects
|
|
36
|
+
// below keep a stable dependency when the page set has not actually changed.
|
|
37
|
+
const currentPages = useMemo(() => (pages ?? internalPages).filter(page => page >= firstPagePos && page <= lastPagePos), [pages, internalPages, firstPagePos, lastPagePos]);
|
|
38
|
+
const handleLoadClick = (pagePos) => {
|
|
39
|
+
if (pages === undefined) {
|
|
40
|
+
setInternalPages(curr => {
|
|
41
|
+
const next = curr.includes(pagePos) ? curr : [...curr, pagePos];
|
|
42
|
+
if (dropPagesFurtherThan === undefined)
|
|
43
|
+
return next;
|
|
44
|
+
const kept = next.filter(page => Math.abs(page - pagePos) <= dropPagesFurtherThan);
|
|
45
|
+
return kept.length === next.length ? next : kept;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
onLoadPageClicked?.(pagePos);
|
|
49
|
+
};
|
|
50
|
+
const storePage = (page, items) => setItemsPages(curr => {
|
|
51
|
+
const next = new Map(curr);
|
|
52
|
+
next.set(page, { loadedAt: new Date(), items });
|
|
53
|
+
return next;
|
|
54
|
+
});
|
|
55
|
+
const setPageLoading = (page, isLoading) => setLoadingPages(curr => {
|
|
56
|
+
const next = new Set(curr);
|
|
57
|
+
if (isLoading)
|
|
58
|
+
next.add(page);
|
|
59
|
+
else
|
|
60
|
+
next.delete(page);
|
|
61
|
+
return next;
|
|
62
|
+
});
|
|
63
|
+
const reportFetchError = (page, err) => {
|
|
64
|
+
const error = toError(err);
|
|
65
|
+
if (onPageFetchFailed !== undefined)
|
|
66
|
+
return onPageFetchFailed(page, error);
|
|
67
|
+
// eslint-disable-next-line no-console
|
|
68
|
+
console.warn(`ListLoader failed to fetch page ${page}`, error);
|
|
69
|
+
};
|
|
70
|
+
// Guards the whole load path at once: a page dropped meanwhile stops its
|
|
71
|
+
// pending retries and its stale reloads without any further bookkeeping.
|
|
72
|
+
const loadPage = (page, retriesLeft = fetchRetriesNb) => {
|
|
73
|
+
if (!requestedPages.current.has(page))
|
|
74
|
+
return;
|
|
75
|
+
setPageLoading(page, true);
|
|
76
|
+
void fetch(page)
|
|
77
|
+
.then(items => {
|
|
78
|
+
if (!requestedPages.current.has(page))
|
|
79
|
+
return;
|
|
80
|
+
storePage(page, items);
|
|
81
|
+
onPageFetched?.(page, items);
|
|
82
|
+
setPageLoading(page, false);
|
|
83
|
+
})
|
|
84
|
+
.catch((err) => {
|
|
85
|
+
reportFetchError(page, err);
|
|
86
|
+
if (retriesLeft <= 0)
|
|
87
|
+
return setPageLoading(page, false);
|
|
88
|
+
window.setTimeout(() => loadPage(page, retriesLeft - 1), fetchRetriesDelayMs);
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
// Fx. dep. `currentPages` - Drops everything held for pages no longer wanted.
|
|
92
|
+
// The updaters return `curr` untouched when nothing was removed, so an unstable
|
|
93
|
+
// `pages` prop cannot spin the render loop.
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
Array.from(requestedPages.current)
|
|
96
|
+
.filter(page => !currentPages.includes(page))
|
|
97
|
+
.forEach(page => { requestedPages.current.delete(page); });
|
|
98
|
+
setItemsPages(curr => {
|
|
99
|
+
const next = new Map(Array.from(curr).filter(([page]) => currentPages.includes(page)));
|
|
100
|
+
return next.size === curr.size ? curr : next;
|
|
101
|
+
});
|
|
102
|
+
setLoadingPages(curr => {
|
|
103
|
+
const next = new Set(Array.from(curr).filter(page => currentPages.includes(page)));
|
|
104
|
+
return next.size === curr.size ? curr : next;
|
|
105
|
+
});
|
|
106
|
+
}, [currentPages]);
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
if (pages !== undefined)
|
|
109
|
+
return;
|
|
110
|
+
if (!fillGaps)
|
|
111
|
+
return;
|
|
112
|
+
setInternalPages(curr => {
|
|
113
|
+
if (curr.length === 0)
|
|
114
|
+
return curr;
|
|
115
|
+
const missing = rangeBetween(Math.min(...curr), Math.max(...curr))
|
|
116
|
+
.filter(page => !curr.includes(page));
|
|
117
|
+
if (missing.length === 0)
|
|
118
|
+
return curr;
|
|
119
|
+
return [...curr, ...missing];
|
|
120
|
+
});
|
|
121
|
+
}, [currentPages, fillGaps, pages]);
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
currentPages.forEach(page => {
|
|
124
|
+
if (requestedPages.current.has(page))
|
|
125
|
+
return;
|
|
126
|
+
requestedPages.current.add(page);
|
|
127
|
+
loadPage(page);
|
|
128
|
+
});
|
|
129
|
+
}, [currentPages, fetch]);
|
|
130
|
+
// Fx. dep. `itemsPages` - Reschedules one timeout per page on every store, each
|
|
131
|
+
// due from its own `loadedAt`. A refetch updates `loadedAt`, which re-runs this
|
|
132
|
+
// effect and keeps the cycle going.
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (staleAfterMs === undefined)
|
|
135
|
+
return;
|
|
136
|
+
const timeouts = Array.from(itemsPages).map(([page, { loadedAt }]) => {
|
|
137
|
+
const dueInMs = Math.max(0, staleAfterMs - (Date.now() - loadedAt.getTime()));
|
|
138
|
+
return window.setTimeout(() => loadPage(page), dueInMs);
|
|
139
|
+
});
|
|
140
|
+
return () => timeouts.forEach(timeout => window.clearTimeout(timeout));
|
|
141
|
+
}, [itemsPages, staleAfterMs, fetch]);
|
|
142
|
+
const controlledItemsPages = new Map(Array
|
|
143
|
+
.from(itemsPages)
|
|
144
|
+
.map(([page, { items }]) => [page, items]));
|
|
145
|
+
return _jsx(ControlledListLoader, { className: className, pages: currentPages, firstPagePos: firstPagePos, lastPagePos: lastPagePos, itemsPages: controlledItemsPages, filter: filter, display: display, getIdentifier: getIdentifier, loadingPages: Array.from(loadingPages), onLoadPageClicked: handleLoadClick, autoLoadPrevWhenVisible: autoLoadPrevWhenVisible, autoLoadNextWhenVisible: autoLoadNextWhenVisible });
|
|
146
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists every integer from `from` to `to`, both included.
|
|
3
|
+
*
|
|
4
|
+
* @param from - Lower bound.
|
|
5
|
+
* @param to - Upper bound. When lower than `from`, the range is empty.
|
|
6
|
+
* @returns The integers in ascending order.
|
|
7
|
+
*/
|
|
8
|
+
export declare function rangeBetween(from: number, to: number): number[];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists every integer from `from` to `to`, both included.
|
|
3
|
+
*
|
|
4
|
+
* @param from - Lower bound.
|
|
5
|
+
* @param to - Upper bound. When lower than `from`, the range is empty.
|
|
6
|
+
* @returns The integers in ascending order.
|
|
7
|
+
*/
|
|
8
|
+
export function rangeBetween(from, to) {
|
|
9
|
+
return Array.from({ length: Math.max(0, to - from + 1) }, (_, pos) => from + pos);
|
|
10
|
+
}
|
|
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import { clss } from '../../agnostic/css/clss/index.js';
|
|
3
3
|
import { isFalsy } from '../../agnostic/booleans/is-falsy/index.js';
|
|
4
4
|
import { mergeClassNames } from '../utils/index.js';
|
|
5
|
-
import { overlayer as
|
|
5
|
+
import { overlayer as publicClassName } from '../public-classnames.js';
|
|
6
6
|
import cssModule from './styles.module.css';
|
|
7
7
|
/**
|
|
8
8
|
* Absolute positioning overlay component. Renders a base content layer and
|
|
@@ -27,10 +27,10 @@ import cssModule from './styles.module.css';
|
|
|
27
27
|
* @returns A root `<div>` containing the base layer and all positioned overlay elements.
|
|
28
28
|
*/
|
|
29
29
|
export const Overlayer = ({ overlays, children, className }) => {
|
|
30
|
-
const c = clss(
|
|
30
|
+
const c = clss(publicClassName, { cssModule });
|
|
31
31
|
const rootClss = mergeClassNames(c(), className);
|
|
32
32
|
const baseClss = c('base');
|
|
33
|
-
return _jsxs("div", { className: rootClss, children: [_jsx("div", { className: baseClss, children: children }), overlays?.map(({ content, xPercent = 0, yPercent = 0, justify }) => {
|
|
33
|
+
return _jsxs("div", { className: rootClss, children: [_jsx("div", { className: baseClss, children: children }), overlays?.map(({ content, xPercent = 0, yPercent = 0, justify }, overlayPos) => {
|
|
34
34
|
const overlayClss = c('overlay');
|
|
35
35
|
let computedTranslateX;
|
|
36
36
|
if (typeof justify === 'number') {
|
|
@@ -55,6 +55,6 @@ export const Overlayer = ({ overlays, children, className }) => {
|
|
|
55
55
|
};
|
|
56
56
|
if (isFalsy(content))
|
|
57
57
|
return null;
|
|
58
|
-
return _jsx("div", { className: overlayClss, style: overlayCustomProps, children: content });
|
|
58
|
+
return _jsx("div", { className: overlayClss, style: overlayCustomProps, children: content }, overlayPos);
|
|
59
59
|
})] });
|
|
60
60
|
};
|
|
@@ -27,23 +27,18 @@ type DirectionState = 'forwards' | 'backwards' | null;
|
|
|
27
27
|
* the {@link IntersectionObserver} root margin. Determines how far into the viewport
|
|
28
28
|
* a page must be before it is considered `'curr'`. Defaults to `0`.
|
|
29
29
|
*
|
|
30
|
-
* @property
|
|
31
|
-
* @
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* @property stateHandlers.pageChanged - Callback invoked whenever any page's {@link PageState}
|
|
36
|
-
* changes. Receives a flat array of all pages' states, ordered by position.
|
|
37
|
-
*
|
|
30
|
+
* @property onDirectionChanged - Called after the scroll direction changed, with
|
|
31
|
+
* the new {@link DirectionState}. Repeated scrolls in the same direction do not
|
|
32
|
+
* trigger it again.
|
|
33
|
+
* @property onPagesChanged - Called after any page's {@link PageState} changed,
|
|
34
|
+
* with a flat array of every page's state, ordered by position.
|
|
38
35
|
* @property className - Optional additional class name(s) applied to the root element.
|
|
39
36
|
* @property children - Each direct child is treated as an individual page slot.
|
|
40
37
|
*/
|
|
41
38
|
export type Props = PropsWithChildren<WithClassName<{
|
|
42
39
|
thresholdOffsetPercent?: number;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
pageChanged?: (pages: PageState[]) => void;
|
|
46
|
-
};
|
|
40
|
+
onDirectionChanged?: (direction: DirectionState) => void;
|
|
41
|
+
onPagesChanged?: (pages: PageState[]) => void;
|
|
47
42
|
}>>;
|
|
48
43
|
/**
|
|
49
44
|
* A scroll-driven pagination component that tracks which child page is currently
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useEffect, useRef, Children, useState } from 'react';
|
|
3
3
|
import { clss } from '../../agnostic/css/clss/index.js';
|
|
4
|
-
import { mergeClassNames } from '../utils/index.js';
|
|
4
|
+
import { mergeClassNames, useChangeDispatch } from '../utils/index.js';
|
|
5
5
|
import { paginator as publicClassName } from '../public-classnames.js';
|
|
6
6
|
import cssModule from './styles.module.css';
|
|
7
7
|
/**
|
|
@@ -27,21 +27,17 @@ import cssModule from './styles.module.css';
|
|
|
27
27
|
* - `currCount` on each {@link PageState} increments each time a page transitions
|
|
28
28
|
* into the `'curr'` position, making it useful as a re-entry counter.
|
|
29
29
|
*/
|
|
30
|
-
export const Paginator = ({ thresholdOffsetPercent,
|
|
30
|
+
export const Paginator = ({ thresholdOffsetPercent, onDirectionChanged, onPagesChanged, className, children }) => {
|
|
31
31
|
// State, refs, effects
|
|
32
32
|
const [pagesState, setPagesState] = useState(new Map());
|
|
33
33
|
const [directionState, setDirectionState] = useState(null);
|
|
34
34
|
const pagesRef = useRef(null);
|
|
35
35
|
const directionRef = useRef(null);
|
|
36
|
-
// State
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}, [pagesState]);
|
|
42
|
-
useEffect(() => {
|
|
43
|
-
stateHandlers?.directionChanged?.(directionState);
|
|
44
|
-
}, [directionState]);
|
|
36
|
+
// State dispatch
|
|
37
|
+
useChangeDispatch(pagesState, pages => onPagesChanged?.(Array
|
|
38
|
+
.from(pages)
|
|
39
|
+
.map(([, pageState]) => pageState)));
|
|
40
|
+
useChangeDispatch(directionState, onDirectionChanged);
|
|
45
41
|
// Catch scroll direction listening on scroll events
|
|
46
42
|
useEffect(() => {
|
|
47
43
|
let lastCall = 0;
|
|
@@ -16,9 +16,20 @@ export type Props = PropsWithChildren<WithClassName<{
|
|
|
16
16
|
onResized?: (entry: ROEntryWithBoundingRect) => void;
|
|
17
17
|
}>>;
|
|
18
18
|
/**
|
|
19
|
-
* Component that observes its own size changes and exposes the
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* Component that observes its own size changes and exposes the measured content
|
|
20
|
+
* rect on its root element. Both sets stay absent until the first measurement
|
|
21
|
+
* lands.
|
|
22
|
+
*
|
|
23
|
+
* ### Data attributes
|
|
24
|
+
* `data-x`, `data-y`, `data-top`, `data-left`, `data-bottom`, `data-right`,
|
|
25
|
+
* `data-width` and `data-height` — the content rect, as plain numbers.
|
|
26
|
+
*
|
|
27
|
+
* ### CSS custom properties
|
|
28
|
+
* The same eight measurements, each exposed twice: `--lm-resize-observer-width`
|
|
29
|
+
* carries the ready-to-use `px` length, `--lm-resize-observer-width-raw` the
|
|
30
|
+
* plain number for `calc()`. Same pattern for `-x`, `-y`, `-top`, `-left`,
|
|
31
|
+
* `-bottom`, `-right` and `-height`.
|
|
32
|
+
*
|
|
22
33
|
* @param props - Component properties
|
|
23
34
|
* @see {@link Props}
|
|
24
35
|
* @returns A div wrapping `children`, with resize observation applied.
|
|
@@ -3,11 +3,22 @@ import { useRef, useEffect, useState } from 'react';
|
|
|
3
3
|
import { clss } from '../../agnostic/css/clss/index.js';
|
|
4
4
|
import { mergeClassNames } from '../utils/index.js';
|
|
5
5
|
import { resizeObserver as publicClassName } from '../public-classnames.js';
|
|
6
|
-
import cssModule from './
|
|
6
|
+
import cssModule from './styles.module.css';
|
|
7
7
|
/**
|
|
8
|
-
* Component that observes its own size changes and exposes the
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* Component that observes its own size changes and exposes the measured content
|
|
9
|
+
* rect on its root element. Both sets stay absent until the first measurement
|
|
10
|
+
* lands.
|
|
11
|
+
*
|
|
12
|
+
* ### Data attributes
|
|
13
|
+
* `data-x`, `data-y`, `data-top`, `data-left`, `data-bottom`, `data-right`,
|
|
14
|
+
* `data-width` and `data-height` — the content rect, as plain numbers.
|
|
15
|
+
*
|
|
16
|
+
* ### CSS custom properties
|
|
17
|
+
* The same eight measurements, each exposed twice: `--lm-resize-observer-width`
|
|
18
|
+
* carries the ready-to-use `px` length, `--lm-resize-observer-width-raw` the
|
|
19
|
+
* plain number for `calc()`. Same pattern for `-x`, `-y`, `-top`, `-left`,
|
|
20
|
+
* `-bottom`, `-right` and `-height`.
|
|
21
|
+
*
|
|
11
22
|
* @param props - Component properties
|
|
12
23
|
* @see {@link Props}
|
|
13
24
|
* @returns A div wrapping `children`, with resize observation applied.
|
|
@@ -43,27 +54,41 @@ export const ResizeObserverComponent = ({ className, onResized, children }) => {
|
|
|
43
54
|
return () => observerRef.current?.disconnect();
|
|
44
55
|
}, [onResized]);
|
|
45
56
|
// Data attributes, CSS custom props & Rendering
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
...acc,
|
|
62
|
-
[`--${publicClassName}-${key}`]: val.toString(),
|
|
63
|
-
[`--${publicClassName}-${key}-px`]: `${val.toString()}px`
|
|
57
|
+
const contentRect = roEntry?.entry.contentRect;
|
|
58
|
+
let dataAttributes = {};
|
|
59
|
+
let cssCustomProps = {};
|
|
60
|
+
if (contentRect !== undefined) {
|
|
61
|
+
const { x, y, top, left, bottom, right, width, height } = contentRect;
|
|
62
|
+
dataAttributes = {
|
|
63
|
+
'data-x': `${x}`,
|
|
64
|
+
'data-y': `${y}`,
|
|
65
|
+
'data-top': `${top}`,
|
|
66
|
+
'data-left': `${left}`,
|
|
67
|
+
'data-bottom': `${bottom}`,
|
|
68
|
+
'data-right': `${right}`,
|
|
69
|
+
'data-width': `${width}`,
|
|
70
|
+
'data-height': `${height}`
|
|
64
71
|
};
|
|
65
|
-
|
|
72
|
+
cssCustomProps = {
|
|
73
|
+
'--lm-resize-observer-x': `${x}px`,
|
|
74
|
+
'--lm-resize-observer-x-raw': `${x}`,
|
|
75
|
+
'--lm-resize-observer-y': `${y}px`,
|
|
76
|
+
'--lm-resize-observer-y-raw': `${y}`,
|
|
77
|
+
'--lm-resize-observer-top': `${top}px`,
|
|
78
|
+
'--lm-resize-observer-top-raw': `${top}`,
|
|
79
|
+
'--lm-resize-observer-left': `${left}px`,
|
|
80
|
+
'--lm-resize-observer-left-raw': `${left}`,
|
|
81
|
+
'--lm-resize-observer-bottom': `${bottom}px`,
|
|
82
|
+
'--lm-resize-observer-bottom-raw': `${bottom}`,
|
|
83
|
+
'--lm-resize-observer-right': `${right}px`,
|
|
84
|
+
'--lm-resize-observer-right-raw': `${right}`,
|
|
85
|
+
'--lm-resize-observer-width': `${width}px`,
|
|
86
|
+
'--lm-resize-observer-width-raw': `${width}`,
|
|
87
|
+
'--lm-resize-observer-height': `${height}px`,
|
|
88
|
+
'--lm-resize-observer-height-raw': `${height}`
|
|
89
|
+
};
|
|
90
|
+
}
|
|
66
91
|
const c = clss(publicClassName, { cssModule });
|
|
67
|
-
const rootClss = mergeClassNames(c(
|
|
92
|
+
const rootClss = mergeClassNames(c(), className);
|
|
68
93
|
return _jsx("div", { ...dataAttributes, className: rootClss, ref: rootRef, style: { ...cssCustomProps }, children: children });
|
|
69
94
|
};
|