@mrpelz/uikit-ish 7.13.1 → 7.26.2
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/components/accordion/style.css +1 -1
- package/dist/components/action-sheet/main.d.ts.map +1 -1
- package/dist/components/action-sheet/main.js +9 -6
- package/dist/components/action-sheet/main.js.map +1 -1
- package/dist/components/action-sheet/props.d.ts +2 -1
- package/dist/components/action-sheet/props.d.ts.map +1 -1
- package/dist/components/alert/main.d.ts.map +1 -1
- package/dist/components/alert/main.js +12 -4
- package/dist/components/alert/main.js.map +1 -1
- package/dist/components/alert/props.d.ts +2 -1
- package/dist/components/alert/props.d.ts.map +1 -1
- package/dist/components/alert/style.css +27 -5
- package/dist/components/button/main.js +1 -1
- package/dist/components/button/main.js.map +1 -1
- package/dist/components/button/props.d.ts +3 -2
- package/dist/components/button/props.d.ts.map +1 -1
- package/dist/components/button/style.css +6 -17
- package/dist/components/card/main.d.ts +53 -0
- package/dist/components/card/main.d.ts.map +1 -0
- package/dist/components/card/main.js +286 -0
- package/dist/components/card/main.js.map +1 -0
- package/dist/components/card/props.d.ts +83 -0
- package/dist/components/card/props.d.ts.map +1 -0
- package/dist/components/card/props.js +2 -0
- package/dist/components/card/props.js.map +1 -0
- package/dist/components/card/style.css +900 -0
- package/dist/components/collapsible/style.css +1 -1
- package/dist/components/color-picker/main.d.ts.map +1 -1
- package/dist/components/color-picker/main.js +20 -33
- package/dist/components/color-picker/main.js.map +1 -1
- package/dist/components/color-picker/style.css +4 -23
- package/dist/components/contextual-menu/main.d.ts +6 -2
- package/dist/components/contextual-menu/main.d.ts.map +1 -1
- package/dist/components/contextual-menu/main.js +67 -43
- package/dist/components/contextual-menu/main.js.map +1 -1
- package/dist/components/contextual-menu/props.d.ts +2 -1
- package/dist/components/contextual-menu/props.d.ts.map +1 -1
- package/dist/components/contextual-menu/style.css +64 -2
- package/dist/components/control-cards/main.d.ts +49 -0
- package/dist/components/control-cards/main.d.ts.map +1 -0
- package/dist/components/control-cards/main.js +835 -0
- package/dist/components/control-cards/main.js.map +1 -0
- package/dist/components/control-cards/props.d.ts +409 -0
- package/dist/components/control-cards/props.d.ts.map +1 -0
- package/dist/components/control-cards/props.js +2 -0
- package/dist/components/control-cards/props.js.map +1 -0
- package/dist/components/control-cards/style.css +492 -0
- package/dist/components/date-picker/main.d.ts.map +1 -1
- package/dist/components/date-picker/main.js +3 -2
- package/dist/components/date-picker/main.js.map +1 -1
- package/dist/components/date-picker/style.css +1 -1
- package/dist/components/drawer/style.css +6 -0
- package/dist/components/edit-menu/main.d.ts +2 -1
- package/dist/components/edit-menu/main.d.ts.map +1 -1
- package/dist/components/edit-menu/main.js +8 -4
- package/dist/components/edit-menu/main.js.map +1 -1
- package/dist/components/glass/backdrop.d.ts +75 -0
- package/dist/components/glass/backdrop.d.ts.map +1 -0
- package/dist/components/glass/backdrop.js +608 -0
- package/dist/components/glass/backdrop.js.map +1 -0
- package/dist/components/glass/gradient.d.ts +17 -0
- package/dist/components/glass/gradient.d.ts.map +1 -0
- package/dist/components/glass/gradient.js +233 -0
- package/dist/components/glass/gradient.js.map +1 -0
- package/dist/components/glass/main.d.ts +60 -6
- package/dist/components/glass/main.d.ts.map +1 -1
- package/dist/components/glass/main.js +100 -122
- package/dist/components/glass/main.js.map +1 -1
- package/dist/components/glass/marker.d.ts +66 -0
- package/dist/components/glass/marker.d.ts.map +1 -0
- package/dist/components/glass/marker.js +268 -0
- package/dist/components/glass/marker.js.map +1 -0
- package/dist/components/glass/props.d.ts +33 -7
- package/dist/components/glass/props.d.ts.map +1 -1
- package/dist/components/glass/style.css +139 -75
- package/dist/components/grid/dashes.d.ts +15 -0
- package/dist/components/grid/dashes.d.ts.map +1 -0
- package/dist/components/grid/dashes.js +37 -0
- package/dist/components/grid/dashes.js.map +1 -0
- package/dist/components/grid/edit.d.ts +11 -0
- package/dist/components/grid/edit.d.ts.map +1 -0
- package/dist/components/grid/edit.js +1066 -0
- package/dist/components/grid/edit.js.map +1 -0
- package/dist/components/grid/handle.d.ts +123 -0
- package/dist/components/grid/handle.d.ts.map +1 -0
- package/dist/components/grid/handle.js +194 -0
- package/dist/components/grid/handle.js.map +1 -0
- package/dist/components/grid/icons.d.ts +40 -0
- package/dist/components/grid/icons.d.ts.map +1 -0
- package/dist/components/grid/icons.js +140 -0
- package/dist/components/grid/icons.js.map +1 -0
- package/dist/components/grid/layout.d.ts +111 -0
- package/dist/components/grid/layout.d.ts.map +1 -0
- package/dist/components/grid/layout.js +1257 -0
- package/dist/components/grid/layout.js.map +1 -0
- package/dist/components/grid/main.d.ts +63 -0
- package/dist/components/grid/main.d.ts.map +1 -0
- package/dist/components/grid/main.js +1894 -0
- package/dist/components/grid/main.js.map +1 -0
- package/dist/components/grid/overlay.d.ts +24 -0
- package/dist/components/grid/overlay.d.ts.map +1 -0
- package/dist/components/grid/overlay.js +312 -0
- package/dist/components/grid/overlay.js.map +1 -0
- package/dist/components/grid/plan.d.ts +78 -0
- package/dist/components/grid/plan.d.ts.map +1 -0
- package/dist/components/grid/plan.js +249 -0
- package/dist/components/grid/plan.js.map +1 -0
- package/dist/components/grid/props.d.ts +509 -0
- package/dist/components/grid/props.d.ts.map +1 -0
- package/dist/components/grid/props.js +2 -0
- package/dist/components/grid/props.js.map +1 -0
- package/dist/components/grid/reshape.d.ts +60 -0
- package/dist/components/grid/reshape.d.ts.map +1 -0
- package/dist/components/grid/reshape.js +214 -0
- package/dist/components/grid/reshape.js.map +1 -0
- package/dist/components/grid/style.css +1293 -0
- package/dist/components/header/main.d.ts.map +1 -1
- package/dist/components/header/main.js +97 -10
- package/dist/components/header/main.js.map +1 -1
- package/dist/components/header/props.d.ts +2 -2
- package/dist/components/header/props.d.ts.map +1 -1
- package/dist/components/header/style.css +28 -43
- package/dist/components/index.d.ts +10 -6
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +5 -1
- package/dist/components/index.js.map +1 -1
- package/dist/components/island/main.d.ts +2 -2
- package/dist/components/island/main.d.ts.map +1 -1
- package/dist/components/island/main.js +193 -41
- package/dist/components/island/main.js.map +1 -1
- package/dist/components/island/props.d.ts +4 -2
- package/dist/components/island/props.d.ts.map +1 -1
- package/dist/components/island/style.css +50 -3
- package/dist/components/layout/main.d.ts +3 -6
- package/dist/components/layout/main.d.ts.map +1 -1
- package/dist/components/layout/main.js +225 -56
- package/dist/components/layout/main.js.map +1 -1
- package/dist/components/layout/props.d.ts +43 -2
- package/dist/components/layout/props.d.ts.map +1 -1
- package/dist/components/layout/style.css +84 -23
- package/dist/components/list/main.d.ts +3 -3
- package/dist/components/list/main.d.ts.map +1 -1
- package/dist/components/list/main.js +90 -19
- package/dist/components/list/main.js.map +1 -1
- package/dist/components/list/props.d.ts +20 -0
- package/dist/components/list/props.d.ts.map +1 -1
- package/dist/components/list/reorder.d.ts +16 -2
- package/dist/components/list/reorder.d.ts.map +1 -1
- package/dist/components/list/reorder.js.map +1 -1
- package/dist/components/list/style.css +60 -3
- package/dist/components/menu/main.d.ts +9 -5
- package/dist/components/menu/main.d.ts.map +1 -1
- package/dist/components/menu/main.js +87 -9
- package/dist/components/menu/main.js.map +1 -1
- package/dist/components/menu/props.d.ts +14 -0
- package/dist/components/menu/props.d.ts.map +1 -1
- package/dist/components/menu/style.css +3 -1
- package/dist/components/page/main.d.ts.map +1 -1
- package/dist/components/page/main.js +6 -3
- package/dist/components/page/main.js.map +1 -1
- package/dist/components/page/props.d.ts +3 -2
- package/dist/components/page/props.d.ts.map +1 -1
- package/dist/components/page/style.css +8 -4
- package/dist/components/page-control/props.d.ts +3 -3
- package/dist/components/popover/main.d.ts +10 -4
- package/dist/components/popover/main.d.ts.map +1 -1
- package/dist/components/popover/main.js +30 -19
- package/dist/components/popover/main.js.map +1 -1
- package/dist/components/popover/props.d.ts +2 -0
- package/dist/components/popover/props.d.ts.map +1 -1
- package/dist/components/popover/style.css +6 -2
- package/dist/components/progress/style.css +2 -2
- package/dist/components/roll/main.d.ts.map +1 -1
- package/dist/components/roll/main.js +5 -3
- package/dist/components/roll/main.js.map +1 -1
- package/dist/components/segmented/main.d.ts.map +1 -1
- package/dist/components/segmented/main.js +4 -4
- package/dist/components/segmented/main.js.map +1 -1
- package/dist/components/segmented/props.d.ts +5 -2
- package/dist/components/segmented/props.d.ts.map +1 -1
- package/dist/components/segmented/style.css +1 -1
- package/dist/components/settings/main.d.ts +13 -0
- package/dist/components/settings/main.d.ts.map +1 -1
- package/dist/components/settings/main.js +16 -0
- package/dist/components/settings/main.js.map +1 -1
- package/dist/components/settings/props.d.ts +1 -0
- package/dist/components/settings/props.d.ts.map +1 -1
- package/dist/components/sheet/main.d.ts.map +1 -1
- package/dist/components/sheet/main.js +156 -45
- package/dist/components/sheet/main.js.map +1 -1
- package/dist/components/sheet/props.d.ts +4 -2
- package/dist/components/sheet/props.d.ts.map +1 -1
- package/dist/components/sheet/snap.d.ts +54 -0
- package/dist/components/sheet/snap.d.ts.map +1 -1
- package/dist/components/sheet/snap.js +253 -23
- package/dist/components/sheet/snap.js.map +1 -1
- package/dist/components/sheet/style.css +126 -20
- package/dist/components/sidebar/main.d.ts +2 -1
- package/dist/components/sidebar/main.d.ts.map +1 -1
- package/dist/components/sidebar/main.js +18 -6
- package/dist/components/sidebar/main.js.map +1 -1
- package/dist/components/sidebar/props.d.ts +2 -1
- package/dist/components/sidebar/props.d.ts.map +1 -1
- package/dist/components/sidebar/style.css +78 -0
- package/dist/components/slider/main.d.ts.map +1 -1
- package/dist/components/slider/main.js +51 -9
- package/dist/components/slider/main.js.map +1 -1
- package/dist/components/slider/props.d.ts +1 -0
- package/dist/components/slider/props.d.ts.map +1 -1
- package/dist/components/slider/style.css +9 -0
- package/dist/components/switch/main.d.ts.map +1 -1
- package/dist/components/switch/main.js +4 -2
- package/dist/components/switch/main.js.map +1 -1
- package/dist/components/symbol/main.d.ts.map +1 -1
- package/dist/components/symbol/main.js +5 -1
- package/dist/components/symbol/main.js.map +1 -1
- package/dist/components/symbol/props.d.ts +10 -0
- package/dist/components/symbol/props.d.ts.map +1 -1
- package/dist/components/symbol/style.css +21 -3
- package/dist/components/tab-bar/main.d.ts.map +1 -1
- package/dist/components/tab-bar/main.js +8 -5
- package/dist/components/tab-bar/main.js.map +1 -1
- package/dist/components/tab-bar/props.d.ts +16 -12
- package/dist/components/tab-bar/props.d.ts.map +1 -1
- package/dist/components/tab-bar/style.css +3 -9
- package/dist/components/text-field/main.d.ts.map +1 -1
- package/dist/components/text-field/main.js +6 -3
- package/dist/components/text-field/main.js.map +1 -1
- package/dist/components/text-field/style.css +1 -1
- package/dist/components/toolbar/main.js +1 -1
- package/dist/components/toolbar/main.js.map +1 -1
- package/dist/components/toolbar/props.d.ts +3 -3
- package/dist/components/toolbar/style.css +1 -1
- package/dist/components/tooltip/main.d.ts +6 -1
- package/dist/components/tooltip/main.d.ts.map +1 -1
- package/dist/components/tooltip/main.js +13 -7
- package/dist/components/tooltip/main.js.map +1 -1
- package/dist/components/utils/main.d.ts +31 -2
- package/dist/components/utils/main.d.ts.map +1 -1
- package/dist/components/utils/main.js +60 -3
- package/dist/components/utils/main.js.map +1 -1
- package/dist/components/viewport/current.d.ts +15 -0
- package/dist/components/viewport/current.d.ts.map +1 -0
- package/dist/components/viewport/current.js +74 -0
- package/dist/components/viewport/current.js.map +1 -0
- package/dist/components/viewport/edge-effect.d.ts.map +1 -1
- package/dist/components/viewport/edge-effect.js +52 -24
- package/dist/components/viewport/edge-effect.js.map +1 -1
- package/dist/components/viewport/edge.d.ts +7 -4
- package/dist/components/viewport/edge.d.ts.map +1 -1
- package/dist/components/viewport/edge.js +21 -7
- package/dist/components/viewport/edge.js.map +1 -1
- package/dist/components/viewport/focus-scroll.d.ts +21 -0
- package/dist/components/viewport/focus-scroll.d.ts.map +1 -0
- package/dist/components/viewport/focus-scroll.js +272 -0
- package/dist/components/viewport/focus-scroll.js.map +1 -0
- package/dist/components/viewport/input.d.ts +39 -1
- package/dist/components/viewport/input.d.ts.map +1 -1
- package/dist/components/viewport/input.js +122 -13
- package/dist/components/viewport/input.js.map +1 -1
- package/dist/components/viewport/main.d.ts +16 -1
- package/dist/components/viewport/main.d.ts.map +1 -1
- package/dist/components/viewport/main.js +243 -49
- package/dist/components/viewport/main.js.map +1 -1
- package/dist/components/viewport/padding.d.ts +1 -1
- package/dist/components/viewport/padding.d.ts.map +1 -1
- package/dist/components/viewport/padding.js +2 -2
- package/dist/components/viewport/props.d.ts +12 -2
- package/dist/components/viewport/props.d.ts.map +1 -1
- package/dist/components/viewport/status-bar.d.ts +9 -0
- package/dist/components/viewport/status-bar.d.ts.map +1 -0
- package/dist/components/viewport/status-bar.js +131 -0
- package/dist/components/viewport/status-bar.js.map +1 -0
- package/dist/components/viewport/style.css +62 -5
- package/dist/styles.css +123 -198
- package/package.json +5 -4
|
@@ -0,0 +1,1894 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import './style.css';
|
|
3
|
+
import { useRender } from '@base-ui/react/use-render';
|
|
4
|
+
import { createContext, forwardRef, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
5
|
+
import { createPortal } from 'react-dom';
|
|
6
|
+
import { cn, reducedMotionOf, useForwardedRef, withStyle, } from '../utils/main.js';
|
|
7
|
+
import { dashCountOf, dashRadius } from './dashes.js';
|
|
8
|
+
import { slop, touchHold, useEditing } from './edit.js';
|
|
9
|
+
import { cellOf, createReorderHandle, effectiveSize, handlesOf, isFull, measureOf, reorder, sizesUpTo, sortSizes, spanOf, useCreateReorderHandle, } from './handle.js';
|
|
10
|
+
import { gridIcons } from './icons.js';
|
|
11
|
+
import { breakOutlineOf, fullOutlineOf, layoutOf, mergedBy, metricsOf, reflowDuration, reflowEasing, unitOf, unitRangeOf, useReflow, } from './layout.js';
|
|
12
|
+
import { useOverlay } from './overlay.js';
|
|
13
|
+
import { landingOf } from './plan.js';
|
|
14
|
+
import { GridItemLayoutProvider, ShapeSnapshot, useReshape, } from './reshape.js';
|
|
15
|
+
export { GridReorderHandle } from './handle.js';
|
|
16
|
+
export { gridIcons } from './icons.js';
|
|
17
|
+
// (The reflow's timing: what moves with it, e.g. what's in a tile as it
|
|
18
|
+
// changes its box, see reshape.ts.)
|
|
19
|
+
export { reflowDuration, reflowEasing } from './layout.js';
|
|
20
|
+
export { startWithFrame, useGridItemLayout } from './reshape.js';
|
|
21
|
+
/** A boolean data attribute's value: present, or not. */
|
|
22
|
+
const flagOf = (is) => (is ? '' : undefined);
|
|
23
|
+
const GridContext = createContext({
|
|
24
|
+
columns: 3,
|
|
25
|
+
dense: false,
|
|
26
|
+
editing: false,
|
|
27
|
+
gap: 12,
|
|
28
|
+
maxTileRows: 3,
|
|
29
|
+
minColumns: 3,
|
|
30
|
+
unit: 110,
|
|
31
|
+
});
|
|
32
|
+
const GridItemContext = createContext(undefined);
|
|
33
|
+
/** An item's area's state, public (see `useGridArea`); none, none. */
|
|
34
|
+
const GridAreaStateContext = createContext(undefined);
|
|
35
|
+
const GridAreaHostContext = createContext(undefined);
|
|
36
|
+
/** Whether what's in it is an area's (see `Area`): its controls act on it. */
|
|
37
|
+
const GridAreaScopeContext = createContext(false);
|
|
38
|
+
/** The range its cells drop among (none: they're no target). */
|
|
39
|
+
const GridRangeContext = createContext(undefined);
|
|
40
|
+
/** The grid (or section) its cells are in, for a `DropGroup`. */
|
|
41
|
+
const GridContainerContext = createContext({
|
|
42
|
+
current: null,
|
|
43
|
+
});
|
|
44
|
+
/**
|
|
45
|
+
* A range of cells (see `GridRange`), its handles' while it has any; its
|
|
46
|
+
* cells registering with it (see `useCellEntry`).
|
|
47
|
+
*/
|
|
48
|
+
const useRange = (part, reorderHandle, dropName, container) => {
|
|
49
|
+
const latest = useRef({ dropName, reorderHandle });
|
|
50
|
+
useLayoutEffect(() => {
|
|
51
|
+
latest.current = { dropName, reorderHandle };
|
|
52
|
+
});
|
|
53
|
+
const [range] = useState(() => ({
|
|
54
|
+
container: () => container.current,
|
|
55
|
+
handles: () => handlesOf(latest.current.reorderHandle),
|
|
56
|
+
members: new Set(),
|
|
57
|
+
name: () => latest.current.dropName,
|
|
58
|
+
part,
|
|
59
|
+
}));
|
|
60
|
+
useLayoutEffect(() => {
|
|
61
|
+
const handles = handlesOf(reorderHandle);
|
|
62
|
+
for (const handle of handles)
|
|
63
|
+
handle.ranges.add(range);
|
|
64
|
+
return () => {
|
|
65
|
+
for (const handle of handles)
|
|
66
|
+
handle.ranges.delete(range);
|
|
67
|
+
};
|
|
68
|
+
}, [range, reorderHandle]);
|
|
69
|
+
return reorderHandle === undefined ? undefined : range;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* A cell (an item, spacer, break) in reordering: its handles, its name, its
|
|
73
|
+
* range; movable while editing, with a handle.
|
|
74
|
+
*/
|
|
75
|
+
const useCellEntry = (ref, reorderHandle, dropName,
|
|
76
|
+
// (Among its range's cells (what's dropped among); a `Spawn` only with
|
|
77
|
+
// a handle.)
|
|
78
|
+
isMember = true) => {
|
|
79
|
+
const { editing } = useContext(GridContext);
|
|
80
|
+
const range = useContext(GridRangeContext);
|
|
81
|
+
const latest = useRef({ dropName, reorderHandle });
|
|
82
|
+
useLayoutEffect(() => {
|
|
83
|
+
latest.current = { dropName, reorderHandle };
|
|
84
|
+
});
|
|
85
|
+
useLayoutEffect(() => {
|
|
86
|
+
const { current: element } = ref;
|
|
87
|
+
if (!element)
|
|
88
|
+
return undefined;
|
|
89
|
+
const handles = handlesOf(reorderHandle);
|
|
90
|
+
cellOf.set(element, {
|
|
91
|
+
handles: () => handlesOf(latest.current.reorderHandle),
|
|
92
|
+
name: () => latest.current.dropName,
|
|
93
|
+
range,
|
|
94
|
+
});
|
|
95
|
+
if (isMember)
|
|
96
|
+
range?.members.add(element);
|
|
97
|
+
for (const handle of handles)
|
|
98
|
+
handle.cells.add(element);
|
|
99
|
+
return () => {
|
|
100
|
+
cellOf.delete(element);
|
|
101
|
+
range?.members.delete(element);
|
|
102
|
+
for (const handle of handles)
|
|
103
|
+
handle.cells.delete(element);
|
|
104
|
+
};
|
|
105
|
+
}, [isMember, range, ref, reorderHandle]);
|
|
106
|
+
const isMovable = editing && handlesOf(reorderHandle).length > 0;
|
|
107
|
+
return {
|
|
108
|
+
'data-grid-movable': isMovable ? '' : undefined,
|
|
109
|
+
'data-grid-name': dropName,
|
|
110
|
+
// (Movable: focusable, moved by keyboard.)
|
|
111
|
+
tabIndex: isMovable ? 0 : undefined,
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The item it's in: its effective size, as requested, supported, whether
|
|
116
|
+
* its grid's editing, and asking for another size. Outside a grid:
|
|
117
|
+
* `undefined`.
|
|
118
|
+
*/
|
|
119
|
+
export const useGridItem = () => useContext(GridItemContext);
|
|
120
|
+
/**
|
|
121
|
+
* The area of the item it's in (see `Grid.Area`): its effective size, as
|
|
122
|
+
* requested, supported, its tile's placement in it, and asking for
|
|
123
|
+
* another size or placement. Without an area, or outside a grid:
|
|
124
|
+
* `undefined`.
|
|
125
|
+
*/
|
|
126
|
+
export const useGridArea = () => useContext(GridAreaStateContext);
|
|
127
|
+
/**
|
|
128
|
+
* An item's (or add tile's) area, as registered by its `Grid.Area` (see
|
|
129
|
+
* `Area`): its spec, kept until it changes (by value), and the context
|
|
130
|
+
* its `Grid.Area` registers through.
|
|
131
|
+
*/
|
|
132
|
+
const useAreaHost = () => {
|
|
133
|
+
const [spec, setSpec] = useState(undefined);
|
|
134
|
+
const [slot, setSlot] = useState(null);
|
|
135
|
+
const host = useMemo(() => ({ register: setSpec, slot }), [slot]);
|
|
136
|
+
return { host, setSlot, spec };
|
|
137
|
+
};
|
|
138
|
+
/** A grid: its items, in order, placed on square units (see the top). */
|
|
139
|
+
const Root = forwardRef(({ children, className, dense = false, dropName, editing = false, gap = 12, maxTileRows = 3, minColumns = 3, reorderHandle, style, unitMax = 130, unitMin = 100, unitSize = 110, ...props }, forwardedRef) => {
|
|
140
|
+
const ref = useForwardedRef(forwardedRef);
|
|
141
|
+
const dragged = useRef(null);
|
|
142
|
+
const [layout, setLayout] = useState({
|
|
143
|
+
columns: minColumns,
|
|
144
|
+
isMeasured: false,
|
|
145
|
+
unit: unitSize,
|
|
146
|
+
});
|
|
147
|
+
useLayoutEffect(() => {
|
|
148
|
+
const { current: root } = ref;
|
|
149
|
+
if (!root)
|
|
150
|
+
return undefined;
|
|
151
|
+
let current;
|
|
152
|
+
const apply = (next) => {
|
|
153
|
+
if (current?.columns === next.columns && current.unit === next.unit) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
current = next;
|
|
157
|
+
// (On it at once, before it's painted: not a frame of the old
|
|
158
|
+
// columns at the new width; what's in it re-rendered after.)
|
|
159
|
+
root.style.setProperty('--uikit-grid-columns', String(next.columns));
|
|
160
|
+
root.style.setProperty('--uikit-grid-unit', `${String(next.unit)}px`);
|
|
161
|
+
setLayout({ ...next, isMeasured: true });
|
|
162
|
+
};
|
|
163
|
+
// (Editing: its columns narrower, beside its end-of-line column (see
|
|
164
|
+
// the CSS), as wide as its room at its end as it is (opening or
|
|
165
|
+
// closing), less a gap: their width, that column's, that room, for
|
|
166
|
+
// what's placed and drawn by them.)
|
|
167
|
+
const padded = root.querySelector(':scope > [data-grid-part="section"]') ?? root;
|
|
168
|
+
const narrow = (width) => {
|
|
169
|
+
if (!current)
|
|
170
|
+
return;
|
|
171
|
+
const strip = Number.parseFloat(getComputedStyle(padded).paddingInlineEnd) || 0;
|
|
172
|
+
const end = Math.max(0, strip - gap);
|
|
173
|
+
const column = (width - strip - (current.columns - 1) * gap) / current.columns;
|
|
174
|
+
root.style.setProperty('--uikit-grid-column', `${String(column)}px`);
|
|
175
|
+
root.style.setProperty('--uikit-grid-end', `${String(end)}px`);
|
|
176
|
+
root.style.setProperty('--uikit-grid-strip', `${String(strip)}px`);
|
|
177
|
+
};
|
|
178
|
+
// (The columns kept while their unit's in range; else changed at
|
|
179
|
+
// once: see the top.)
|
|
180
|
+
const measure = () => {
|
|
181
|
+
const width = root.clientWidth;
|
|
182
|
+
const next = layoutOf(width, gap, unitMin, unitSize, unitMax, minColumns);
|
|
183
|
+
if (!current) {
|
|
184
|
+
apply(next);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const { columns } = current;
|
|
188
|
+
const unit = unitOf(width, gap, columns);
|
|
189
|
+
// (At its fewest columns, its units shrinking below `unitMin`: kept
|
|
190
|
+
// all the same.)
|
|
191
|
+
const [low, high] = unitRangeOf(unitMin, unitSize, unitMax);
|
|
192
|
+
const isKept = columns >= minColumns &&
|
|
193
|
+
unit <= high &&
|
|
194
|
+
(unit >= low || columns === minColumns);
|
|
195
|
+
apply(isKept ? { columns, unit } : next);
|
|
196
|
+
};
|
|
197
|
+
const remeasure = () => {
|
|
198
|
+
measure();
|
|
199
|
+
narrow(root.clientWidth);
|
|
200
|
+
};
|
|
201
|
+
remeasure();
|
|
202
|
+
const resizeObserver = new ResizeObserver(remeasure);
|
|
203
|
+
resizeObserver.observe(root);
|
|
204
|
+
// (Its room at its end opening or closing (see the CSS): each frame
|
|
205
|
+
// while it does, before it's laid out.)
|
|
206
|
+
let frame = 0;
|
|
207
|
+
const follow = () => {
|
|
208
|
+
remeasure();
|
|
209
|
+
frame = requestAnimationFrame(follow);
|
|
210
|
+
};
|
|
211
|
+
const onRun = (event) => {
|
|
212
|
+
if (event.target !== padded ||
|
|
213
|
+
!event.propertyName.startsWith('padding')) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
cancelAnimationFrame(frame);
|
|
217
|
+
frame = requestAnimationFrame(follow);
|
|
218
|
+
};
|
|
219
|
+
const onEnd = (event) => {
|
|
220
|
+
if (event.target !== padded)
|
|
221
|
+
return;
|
|
222
|
+
cancelAnimationFrame(frame);
|
|
223
|
+
remeasure();
|
|
224
|
+
};
|
|
225
|
+
padded.addEventListener('transitionrun', onRun);
|
|
226
|
+
padded.addEventListener('transitionend', onEnd);
|
|
227
|
+
padded.addEventListener('transitioncancel', onEnd);
|
|
228
|
+
return () => {
|
|
229
|
+
resizeObserver.disconnect();
|
|
230
|
+
cancelAnimationFrame(frame);
|
|
231
|
+
padded.removeEventListener('transitionrun', onRun);
|
|
232
|
+
padded.removeEventListener('transitionend', onEnd);
|
|
233
|
+
padded.removeEventListener('transitioncancel', onEnd);
|
|
234
|
+
};
|
|
235
|
+
}, [editing, gap, minColumns, ref, unitMax, unitMin, unitSize]);
|
|
236
|
+
useReflow(ref, dragged, editing);
|
|
237
|
+
useEditing(ref, dragged, editing);
|
|
238
|
+
const range = useRange('Root', reorderHandle, dropName, ref);
|
|
239
|
+
const value = useMemo(() => ({
|
|
240
|
+
columns: layout.columns,
|
|
241
|
+
dense,
|
|
242
|
+
editing,
|
|
243
|
+
gap,
|
|
244
|
+
maxTileRows,
|
|
245
|
+
minColumns,
|
|
246
|
+
unit: layout.unit,
|
|
247
|
+
}), [dense, editing, gap, layout, maxTileRows, minColumns]);
|
|
248
|
+
return (_jsx(GridContext.Provider, { value: value, children: _jsx(GridContainerContext.Provider, { value: ref, children: _jsx(GridRangeContext.Provider, { value: range, children: _jsx("div", { ref: ref, "data-dense": dense && !editing ? '' : undefined, "data-editing": editing ? '' : undefined, "data-grid-drop": range ? '' : undefined, "data-grid-measured": layout.isMeasured ? '' : undefined, "data-grid-part": "root", style: withStyle({
|
|
249
|
+
'--uikit-grid-columns': String(layout.columns),
|
|
250
|
+
'--uikit-grid-gap': `${String(gap)}px`,
|
|
251
|
+
'--uikit-grid-max-rows': String(maxTileRows),
|
|
252
|
+
'--uikit-grid-unit': `${String(layout.unit)}px`,
|
|
253
|
+
}, style), ...props, className: cn('uikit-grid-root', className), children: children }) }) }) }));
|
|
254
|
+
});
|
|
255
|
+
/**
|
|
256
|
+
* A titled run of items, placed on its own (its header across it); with a
|
|
257
|
+
* `reorderHandle`, its cells' gaps a target (see `GridReorderHandle`).
|
|
258
|
+
*/
|
|
259
|
+
const Section = forwardRef(({ children, className, dense, dropName, reorderHandle, ...props }, forwardedRef) => {
|
|
260
|
+
const ref = useForwardedRef(forwardedRef);
|
|
261
|
+
const grid = useContext(GridContext);
|
|
262
|
+
const isDense = (dense ?? grid.dense) && !grid.editing;
|
|
263
|
+
const range = useRange('Section', reorderHandle, dropName, ref);
|
|
264
|
+
return (_jsx(GridContainerContext.Provider, { value: ref, children: _jsx(GridRangeContext.Provider, { value: range, children: _jsx("section", { ref: ref, "data-dense": isDense ? '' : undefined, "data-grid-drop": range ? '' : undefined, "data-grid-part": "section", ...props, className: cn('uikit-grid-section', className), children: children }) }) }));
|
|
265
|
+
});
|
|
266
|
+
/**
|
|
267
|
+
* A part of a section (no element): its cells' gaps a target for its
|
|
268
|
+
* handles (see `GridReorderHandle`), e.g. a section's pinned cells and the
|
|
269
|
+
* rest.
|
|
270
|
+
*/
|
|
271
|
+
const DropGroup = ({ children, dropName, reorderHandle, }) => {
|
|
272
|
+
const container = useContext(GridContainerContext);
|
|
273
|
+
const range = useRange('DropGroup', reorderHandle, dropName, container);
|
|
274
|
+
return (_jsx(GridRangeContext.Provider, { value: range, children: children }));
|
|
275
|
+
};
|
|
276
|
+
/** A section's header: its title, across its columns. */
|
|
277
|
+
const Header = forwardRef(({ className, ...props }, ref) => (_jsx("div", { ref: ref, "data-grid-part": "header", ...props, className: cn('uikit-grid-header', className) })));
|
|
278
|
+
/** A placement change for what has none (a break, see `Break`). */
|
|
279
|
+
const noPlacement = () => undefined;
|
|
280
|
+
/** A size change taking nothing in (see `GridSizeChangeDetails`). */
|
|
281
|
+
const noneMerged = { merged: [] };
|
|
282
|
+
/**
|
|
283
|
+
* A cell's supported sizes, in order: those given no taller than the
|
|
284
|
+
* grid's `maxTileRows` (none of them: all given); none given, every size
|
|
285
|
+
* up to the grid's columns and rows.
|
|
286
|
+
*/
|
|
287
|
+
const supportedOf = (given, columns, maxTileRows) => {
|
|
288
|
+
if (given === undefined)
|
|
289
|
+
return sizesUpTo(columns, maxTileRows);
|
|
290
|
+
const held = given.filter((size) => spanOf(size, columns)[1] <= maxTileRows);
|
|
291
|
+
return sortSizes(held.length > 0 ? held : given, columns);
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* Where a tile of a size sits in its area (see `GridPlacement`), in units
|
|
295
|
+
* from the area's top leading corner (halves, truly centered).
|
|
296
|
+
*/
|
|
297
|
+
const placementOffset = (placement, [areaColumns, areaRows], [columns, rows]) => {
|
|
298
|
+
const across = Math.max(0, areaColumns - columns);
|
|
299
|
+
const down = Math.max(0, areaRows - rows);
|
|
300
|
+
return {
|
|
301
|
+
'bottom-leading': [0, down],
|
|
302
|
+
'bottom-trailing': [across, down],
|
|
303
|
+
center: [across / 2, down / 2],
|
|
304
|
+
'top-leading': [0, 0],
|
|
305
|
+
'top-trailing': [across, 0],
|
|
306
|
+
}[placement];
|
|
307
|
+
};
|
|
308
|
+
/**
|
|
309
|
+
* Which way a tile in its area's corner is inset from it, editing (see the
|
|
310
|
+
* CSS, `--uikit-grid-corner-inset`): off its corner's controls, in from
|
|
311
|
+
* each side it's on (towards the inline end, down: positive), only along
|
|
312
|
+
* an axis with room; truly centered, not at all.
|
|
313
|
+
*/
|
|
314
|
+
const insetOf = (placement, [areaColumns, areaRows], [columns, rows]) => {
|
|
315
|
+
if (placement === 'center')
|
|
316
|
+
return [0, 0];
|
|
317
|
+
const across = areaColumns > columns ? 1 : 0;
|
|
318
|
+
const down = areaRows > rows ? 1 : 0;
|
|
319
|
+
return [
|
|
320
|
+
placement.endsWith('leading') ? across : -across,
|
|
321
|
+
placement.startsWith('top') ? down : -down,
|
|
322
|
+
];
|
|
323
|
+
};
|
|
324
|
+
/** A cell's corner inset (px, see `insetOf`). */
|
|
325
|
+
const cornerInsetOf = (cell) => Number.parseFloat(getComputedStyle(cell).getPropertyValue('--uikit-grid-corner-inset')) || 24;
|
|
326
|
+
/**
|
|
327
|
+
* How much of its end-of-line column (and a gap) a tile in a full-width
|
|
328
|
+
* area moves on by, editing (its area running through it, see the CSS):
|
|
329
|
+
* truly centered with a column's room across, half (clear of the column's
|
|
330
|
+
* cut-away part, see layout.ts); elsewhere none (a trailing one inset off
|
|
331
|
+
* its last column, where its area's shape is cut; a full width's own
|
|
332
|
+
* tile kept to its columns too).
|
|
333
|
+
*/
|
|
334
|
+
const stripShareOf = (placement, { across, isFullArea, isFullBody, }) => {
|
|
335
|
+
if (!isFullArea || isFullBody)
|
|
336
|
+
return 0;
|
|
337
|
+
return placement === 'center' && across >= 1 ? 0.5 : 0;
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* An inset's offset (px, see `insetOf`; from its place on the units),
|
|
341
|
+
* with its share of the end-of-line column (see `stripShareOf`).
|
|
342
|
+
*/
|
|
343
|
+
const insetPxOf = (cell, [ix, iy], share) => {
|
|
344
|
+
const metrics = metricsOf(cell);
|
|
345
|
+
const strip = metrics.end > 0 ? metrics.end + metrics.gap : 0;
|
|
346
|
+
const inset = cornerInsetOf(cell);
|
|
347
|
+
return [ix * inset + share * strip, iy * inset];
|
|
348
|
+
};
|
|
349
|
+
/** Whether a cell's area is a full width's, with room in it. */
|
|
350
|
+
const isFullAreaOf = (cell) => Object.hasOwn(cell.dataset, 'gridFull') &&
|
|
351
|
+
Object.hasOwn(cell.dataset, 'gridRoom');
|
|
352
|
+
/** No area (see `useCell`): none asked for, its tile centered. */
|
|
353
|
+
const noArea = {
|
|
354
|
+
movable: false,
|
|
355
|
+
placement: 'center',
|
|
356
|
+
requestPlacement: noPlacement,
|
|
357
|
+
requestSize: noPlacement,
|
|
358
|
+
requested: undefined,
|
|
359
|
+
supported: undefined,
|
|
360
|
+
};
|
|
361
|
+
/** Supported sizes told about (see `useCell`), once each. */
|
|
362
|
+
const toldSizes = new Set();
|
|
363
|
+
/**
|
|
364
|
+
* An item's (or spacer's, spawn's) sizes: requested (its `size`, else its
|
|
365
|
+
* own), effective (see the top); its area (`area`, else its own; none:
|
|
366
|
+
* its size), effective, its size fitting in it, centered (on whole units,
|
|
367
|
+
* the leading side winning the rounding); asked to change; for
|
|
368
|
+
* `useGridItem`.
|
|
369
|
+
*/
|
|
370
|
+
/** Never open (a spacer, a break; an item's own, see `Item`). */
|
|
371
|
+
const noOpen = () => undefined;
|
|
372
|
+
/**
|
|
373
|
+
* A cell's sizes, effective (see `useCell`), as requested: its area first,
|
|
374
|
+
* fitted to the columns; then its size, fitted to the area; the area at
|
|
375
|
+
* least as big as what it shows. No sizes given (`isFreeSize`,
|
|
376
|
+
* `isFreeArea`): any supported, fitted as it is, not snapped. Its own, and
|
|
377
|
+
* what another would be (see `GridItemState.sizeOf`).
|
|
378
|
+
*/
|
|
379
|
+
const fitOf = (requested, requestedArea, { columns, isFreeArea, isFreeSize, maxTileRows, sorted, sortedAreas, }) => {
|
|
380
|
+
const free = effectiveSize(requested, sorted, columns, {
|
|
381
|
+
isOpen: isFreeSize,
|
|
382
|
+
rows: maxTileRows,
|
|
383
|
+
});
|
|
384
|
+
const areaFit = requestedArea === undefined
|
|
385
|
+
? undefined
|
|
386
|
+
: effectiveSize(requestedArea, sortedAreas, columns, {
|
|
387
|
+
isOpen: isFreeArea,
|
|
388
|
+
rows: maxTileRows,
|
|
389
|
+
});
|
|
390
|
+
const area = areaFit?.size;
|
|
391
|
+
const [areaColumns, areaRows] = area ? spanOf(area, columns) : [0, 0];
|
|
392
|
+
// (A full width in an area that isn't one: as wide as the area.)
|
|
393
|
+
const sizeGrid = area && !isFull(area) ? areaColumns : columns;
|
|
394
|
+
const { isFitted, size } = area
|
|
395
|
+
? effectiveSize(free.size, sorted, areaColumns, {
|
|
396
|
+
gridColumns: sizeGrid,
|
|
397
|
+
isOpen: isFreeSize,
|
|
398
|
+
rows: areaRows,
|
|
399
|
+
})
|
|
400
|
+
: free;
|
|
401
|
+
const [sizeColumns, sizeRows] = spanOf(size, sizeGrid);
|
|
402
|
+
const layoutColumns = Math.max(areaColumns, sizeColumns);
|
|
403
|
+
const layoutRows = Math.max(areaRows, sizeRows);
|
|
404
|
+
const isFullWidth = isFull(area ?? size);
|
|
405
|
+
const layout = area
|
|
406
|
+
? `${isFullWidth ? '*' : String(layoutColumns)}x${String(layoutRows)}`
|
|
407
|
+
: size;
|
|
408
|
+
return {
|
|
409
|
+
isAreaFitted: areaFit?.isFitted ?? false,
|
|
410
|
+
isFitted: isFitted || free.isFitted,
|
|
411
|
+
isFullWidth,
|
|
412
|
+
layout,
|
|
413
|
+
layoutColumns,
|
|
414
|
+
layoutRows,
|
|
415
|
+
size,
|
|
416
|
+
sizeColumns,
|
|
417
|
+
sizeRows,
|
|
418
|
+
};
|
|
419
|
+
};
|
|
420
|
+
const useCell = ({ area: spec, defaultSize = '1x1', onSizeChange, size: controlledSize, supportedSizes, }) => {
|
|
421
|
+
const { columns, editing, maxTileRows, minColumns } = useContext(GridContext);
|
|
422
|
+
const [ownSize, setOwnSize] = useState(defaultSize);
|
|
423
|
+
const requested = controlledSize ?? ownSize;
|
|
424
|
+
// (Its area's, from its `Grid.Area`; none: none, its tile centered.)
|
|
425
|
+
const { movable, placement, requestPlacement, requested: requestedArea, supported: supportedAreas, } = spec ?? noArea;
|
|
426
|
+
const supportedKey = supportedSizes?.join(',');
|
|
427
|
+
const sorted = useMemo(() => supportedOf(supportedSizes, columns, maxTileRows),
|
|
428
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- by content
|
|
429
|
+
[columns, maxTileRows, supportedKey]);
|
|
430
|
+
const areasKey = supportedAreas?.join(',');
|
|
431
|
+
const sortedAreas = useMemo(() => supportedOf(supportedAreas, columns, maxTileRows),
|
|
432
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- by content
|
|
433
|
+
[areasKey, columns, maxTileRows]);
|
|
434
|
+
// (None within the grid's fewest columns and most rows: a layout that
|
|
435
|
+
// can break, told to the developer.)
|
|
436
|
+
useEffect(() => {
|
|
437
|
+
if (supportedKey === undefined)
|
|
438
|
+
return;
|
|
439
|
+
const key = `${supportedKey}/${String(minColumns)}/${String(maxTileRows)}`;
|
|
440
|
+
const fits = (supportedSizes ?? []).some((each) => {
|
|
441
|
+
const [c, r] = spanOf(each, minColumns);
|
|
442
|
+
return c <= minColumns && r <= maxTileRows;
|
|
443
|
+
});
|
|
444
|
+
if (fits || toldSizes.has(key))
|
|
445
|
+
return;
|
|
446
|
+
toldSizes.add(key);
|
|
447
|
+
// eslint-disable-next-line no-console -- a misuse, told to the developer
|
|
448
|
+
console.warn(`Grid.Item: none of its supportedSizes (${supportedKey}) fits ${String(minColumns)} columns and ${String(maxTileRows)} rows (the grid's minColumns and maxTileRows); it can't fit every grid.`);
|
|
449
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- by content
|
|
450
|
+
}, [maxTileRows, minColumns, supportedKey]);
|
|
451
|
+
// (Its sizes, effective (see `fitOf`); and what others would be.)
|
|
452
|
+
const isFreeArea = supportedAreas === undefined;
|
|
453
|
+
const isFreeSize = supportedSizes === undefined;
|
|
454
|
+
const fitting = useMemo(() => ({
|
|
455
|
+
columns,
|
|
456
|
+
isFreeArea,
|
|
457
|
+
isFreeSize,
|
|
458
|
+
maxTileRows,
|
|
459
|
+
sorted,
|
|
460
|
+
sortedAreas,
|
|
461
|
+
}), [columns, isFreeArea, isFreeSize, maxTileRows, sorted, sortedAreas]);
|
|
462
|
+
const { isFitted, isFullWidth, layout, layoutColumns, layoutRows, size, sizeColumns, sizeRows, } = fitOf(requested, requestedArea, fitting);
|
|
463
|
+
const sizeOf = useCallback((next) => {
|
|
464
|
+
const fit = fitOf(next, requestedArea, fitting);
|
|
465
|
+
return { isFitted: fit.isFitted, size: fit.size };
|
|
466
|
+
}, [fitting, requestedArea]);
|
|
467
|
+
const areaSizeOf = useCallback((next) => {
|
|
468
|
+
const fit = fitOf(requested, next, fitting);
|
|
469
|
+
return { isFitted: fit.isAreaFitted, size: fit.layout };
|
|
470
|
+
}, [fitting, requested]);
|
|
471
|
+
const [x, y] = placementOffset(placement, [layoutColumns, layoutRows], [sizeColumns, sizeRows]);
|
|
472
|
+
const [insetX, insetY] = insetOf(placement, [layoutColumns, layoutRows], [sizeColumns, sizeRows]);
|
|
473
|
+
const stripShare = stripShareOf(placement, {
|
|
474
|
+
across: layoutColumns - sizeColumns,
|
|
475
|
+
isFullArea: isFullWidth && layout !== size,
|
|
476
|
+
isFullBody: isFull(size),
|
|
477
|
+
});
|
|
478
|
+
const requestSize = useCallback((next, details) => {
|
|
479
|
+
setOwnSize(next);
|
|
480
|
+
onSizeChange?.(next, details ?? noneMerged);
|
|
481
|
+
}, [onSizeChange]);
|
|
482
|
+
const requestArea = spec?.requestSize ?? requestSize;
|
|
483
|
+
const hasArea = spec !== undefined;
|
|
484
|
+
const state = useMemo(() => ({
|
|
485
|
+
area: layout,
|
|
486
|
+
editing,
|
|
487
|
+
hasArea,
|
|
488
|
+
isFitted,
|
|
489
|
+
isFreeArea,
|
|
490
|
+
isFreeSize,
|
|
491
|
+
movable,
|
|
492
|
+
// (Closed: an item's own, see `Item`.)
|
|
493
|
+
open: false,
|
|
494
|
+
openSize: size,
|
|
495
|
+
placement,
|
|
496
|
+
requestArea,
|
|
497
|
+
requested,
|
|
498
|
+
requestPlacement,
|
|
499
|
+
requestSize,
|
|
500
|
+
setOpen: noOpen,
|
|
501
|
+
size,
|
|
502
|
+
sizeOf,
|
|
503
|
+
supportedAreas: sortedAreas,
|
|
504
|
+
supportedSizes: sorted,
|
|
505
|
+
}), [
|
|
506
|
+
editing,
|
|
507
|
+
hasArea,
|
|
508
|
+
isFitted,
|
|
509
|
+
isFreeArea,
|
|
510
|
+
isFreeSize,
|
|
511
|
+
layout,
|
|
512
|
+
movable,
|
|
513
|
+
placement,
|
|
514
|
+
requestArea,
|
|
515
|
+
requestPlacement,
|
|
516
|
+
requestSize,
|
|
517
|
+
requested,
|
|
518
|
+
size,
|
|
519
|
+
sizeOf,
|
|
520
|
+
sorted,
|
|
521
|
+
sortedAreas,
|
|
522
|
+
]);
|
|
523
|
+
// (Its area's, public, see `useGridArea`.)
|
|
524
|
+
const areaState = useMemo(() => spec && {
|
|
525
|
+
movable,
|
|
526
|
+
placement,
|
|
527
|
+
requested: requestedArea ?? size,
|
|
528
|
+
requestPlacement,
|
|
529
|
+
requestSize: requestArea,
|
|
530
|
+
size: layout,
|
|
531
|
+
sizeOf: areaSizeOf,
|
|
532
|
+
supportedSizes: sortedAreas,
|
|
533
|
+
}, [
|
|
534
|
+
areaSizeOf,
|
|
535
|
+
layout,
|
|
536
|
+
movable,
|
|
537
|
+
placement,
|
|
538
|
+
requestArea,
|
|
539
|
+
requestPlacement,
|
|
540
|
+
requestedArea,
|
|
541
|
+
size,
|
|
542
|
+
sortedAreas,
|
|
543
|
+
spec,
|
|
544
|
+
]);
|
|
545
|
+
return {
|
|
546
|
+
areaState,
|
|
547
|
+
// (A full width: from the first column to the last, see the CSS.)
|
|
548
|
+
isFull: isFullWidth,
|
|
549
|
+
// (Its tile in its area: a sub-grid of the area's units, see the CSS;
|
|
550
|
+
// where it is in units from its top leading corner (truly centered:
|
|
551
|
+
// halves, too); on the cell, for its hatching too.)
|
|
552
|
+
bodyStyle: {
|
|
553
|
+
'--uikit-grid-body-column': String(Math.floor(x) + 1),
|
|
554
|
+
'--uikit-grid-body-columns': String(sizeColumns),
|
|
555
|
+
'--uikit-grid-body-row': String(Math.floor(y) + 1),
|
|
556
|
+
'--uikit-grid-body-rows': String(sizeRows),
|
|
557
|
+
'--uikit-grid-body-x': String(x),
|
|
558
|
+
'--uikit-grid-body-y': String(y),
|
|
559
|
+
'--uikit-grid-inset-x': String(insetX),
|
|
560
|
+
// (Trailing in a full-width area: off its end-of-line column's end.)
|
|
561
|
+
'--uikit-grid-inset-strip': String(stripShare),
|
|
562
|
+
'--uikit-grid-inset-y': String(insetY),
|
|
563
|
+
},
|
|
564
|
+
// (Hatched while editing: room around its tile.)
|
|
565
|
+
hasRoom: layout !== size,
|
|
566
|
+
spanStyle: {
|
|
567
|
+
'--uikit-grid-item-columns': String(layoutColumns),
|
|
568
|
+
'--uikit-grid-item-rows': String(layoutRows),
|
|
569
|
+
},
|
|
570
|
+
state,
|
|
571
|
+
};
|
|
572
|
+
};
|
|
573
|
+
const resizeIcons = {
|
|
574
|
+
both: gridIcons.resizeBoth,
|
|
575
|
+
fixed: gridIcons.resize,
|
|
576
|
+
free: gridIcons.resize,
|
|
577
|
+
grow: gridIcons.resizeGrow,
|
|
578
|
+
shrink: gridIcons.resizeShrink,
|
|
579
|
+
};
|
|
580
|
+
/**
|
|
581
|
+
* A corner icon (see `CornerButton`): its outlines, filled, at its size
|
|
582
|
+
* (its view box in its units, by the button's font size); each with a
|
|
583
|
+
* stroke under it, its backing (shown where there's no fill under it,
|
|
584
|
+
* see the CSS).
|
|
585
|
+
*/
|
|
586
|
+
const CornerIcon = ({ icon }) => {
|
|
587
|
+
const [width = 0, height = 0] = icon.viewBox.split(' ').slice(2).map(Number);
|
|
588
|
+
const units = icon.unitsPerEm ?? 20;
|
|
589
|
+
const opacity = Math.max(...icon.svgPathData.map(({ fillOpacity = 1 }) => fillOpacity));
|
|
590
|
+
return (_jsx("svg", { "aria-hidden": true, className: "uikit-grid-corner-icon", "data-symbol": icon.sourceName, height: `${String(height / units)}em`, viewBox: icon.viewBox, width: `${String(width / units)}em`, children: icon.svgPathData.map(({ d, fillOpacity = 1 }, index) => (_jsx("path", { d: d, fillOpacity: fillOpacity === opacity ? undefined : fillOpacity / opacity }, index))) }));
|
|
591
|
+
};
|
|
592
|
+
/**
|
|
593
|
+
* A button in its cell's corner (see the CSS): a 44px target there, its
|
|
594
|
+
* icon 16.5px in from both of the corner's edges (`gridIcons`,
|
|
595
|
+
* or the app's). Hidden (`visible` false): faded out by its
|
|
596
|
+
* color, inert. The grid's own controls are ones (shown while editing); a
|
|
597
|
+
* card's (`Card.CornerButton`) shown while it isn't.
|
|
598
|
+
*/
|
|
599
|
+
const CornerButton = forwardRef(({ className, corner = 'bottom-trailing', icon, mirrored = false, render, visible = true, ...props }, ref) => useRender({
|
|
600
|
+
defaultTagName: 'button',
|
|
601
|
+
props: {
|
|
602
|
+
className: cn('uikit-grid-corner-button', className),
|
|
603
|
+
'data-grid-corner': corner,
|
|
604
|
+
'data-grid-hidden': visible ? undefined : '',
|
|
605
|
+
'data-grid-mirrored': mirrored ? '' : undefined,
|
|
606
|
+
inert: !visible,
|
|
607
|
+
type: 'button',
|
|
608
|
+
...props,
|
|
609
|
+
children: _jsx(CornerIcon, { icon: icon }),
|
|
610
|
+
},
|
|
611
|
+
ref,
|
|
612
|
+
render,
|
|
613
|
+
}));
|
|
614
|
+
/** A movable tile's mark (see `moveSignOf`: its sign on it). */
|
|
615
|
+
const movableMark = { 'data-grid-movable': '' };
|
|
616
|
+
/**
|
|
617
|
+
* A movable cell's sign, editing (see the CSS): by its entry's mark; on
|
|
618
|
+
* its cell (an area's corner, not its tile's: the area is what's moved).
|
|
619
|
+
*/
|
|
620
|
+
const moveSignOf = (entry) => entry['data-grid-movable'] === undefined ? null : (_jsx("span", { "aria-hidden": true, className: "uikit-grid-move-sign", "data-grid-corner": "top-trailing", "data-grid-mirrored": "", children: _jsx(CornerIcon, { icon: gridIcons.move }) }));
|
|
621
|
+
/** A tile's dashed outline (see dashes.ts), by its size. */
|
|
622
|
+
const dashesFor = (size, columns) => {
|
|
623
|
+
const [c, r] = spanOf(size, columns);
|
|
624
|
+
return (_jsx("svg", { "aria-hidden": true, className: "uikit-grid-dashes", children: _jsx("rect", { height: "100%", pathLength: dashCountOf(c, r), rx: dashRadius, width: "100%" }) }));
|
|
625
|
+
};
|
|
626
|
+
/**
|
|
627
|
+
* Where something's added (an item, by the app): a cell of its size (or
|
|
628
|
+
* area, see `Grid.Area`), its tile dashed, a button filling its tile, a
|
|
629
|
+
* sign in its middle (see the CSS; `render`: e.g. a menu's trigger), what
|
|
630
|
+
* it adds and where (e.g. before it, or in its place) up to the app; in
|
|
631
|
+
* and out of editing. Its controls (`children`, e.g. a `ResizeHandle`)
|
|
632
|
+
* beside the button, not in it. Placed as an item is; not moved, unless
|
|
633
|
+
* it has a `reorderHandle` (e.g. placeholders to be filled in).
|
|
634
|
+
*/
|
|
635
|
+
const Spawn = forwardRef(({ children, defaultSize, dropName, onSizeChange, render, reorderHandle, size: controlledSize, ...props }, ref) => {
|
|
636
|
+
const cell = useRef(null);
|
|
637
|
+
const { columns } = useContext(GridContext);
|
|
638
|
+
const entry = useCellEntry(cell, reorderHandle, dropName, handlesOf(reorderHandle).length > 0);
|
|
639
|
+
const { host, setSlot, spec } = useAreaHost();
|
|
640
|
+
const { areaState, bodyStyle, hasRoom, isFull: isFullWidth, spanStyle, state, } = useCell({
|
|
641
|
+
area: spec,
|
|
642
|
+
defaultSize,
|
|
643
|
+
onSizeChange,
|
|
644
|
+
size: controlledSize,
|
|
645
|
+
});
|
|
646
|
+
const button = useRender({
|
|
647
|
+
defaultTagName: 'button',
|
|
648
|
+
props: {
|
|
649
|
+
'aria-label': 'Add',
|
|
650
|
+
// (Pressed while editing too (an edit control); dragged from, as
|
|
651
|
+
// its cell (moved, if it's movable).)
|
|
652
|
+
'data-grid-drag-through': '',
|
|
653
|
+
'data-grid-edit-control': '',
|
|
654
|
+
type: 'button',
|
|
655
|
+
...props,
|
|
656
|
+
className: cn('uikit-grid-spawn-button', props.className),
|
|
657
|
+
},
|
|
658
|
+
ref,
|
|
659
|
+
render,
|
|
660
|
+
});
|
|
661
|
+
return (_jsx(GridItemContext.Provider, { value: state, children: _jsx(GridAreaStateContext.Provider, { value: areaState, children: _jsx(GridAreaHostContext.Provider, { value: host, children: _jsxs("div", { ref: cell, className: "uikit-grid-item uikit-grid-spawn", "data-grid-full": flagOf(isFullWidth), "data-grid-part": "item", "data-grid-placement": state.placement, "data-grid-room": flagOf(hasRoom), "data-size": state.area, style: { ...spanStyle, ...bodyStyle }, ...entry, children: [_jsxs("div", { className: "uikit-grid-item-body", "data-grid-part": "body", "data-size": state.size, children: [dashesFor(state.size, columns), button, children] }), _jsx("div", { ref: setSlot, className: "uikit-grid-item-slot" }), moveSignOf(entry)] }) }) }) }));
|
|
662
|
+
});
|
|
663
|
+
/**
|
|
664
|
+
* Room around its item's (or add tile's) tile: the room it takes in the
|
|
665
|
+
* layout, at least its tile's size (`size`, its own, see `Grid.Item`), its
|
|
666
|
+
* tile placed in it (`placement`), moved in it (`movable`), the rest
|
|
667
|
+
* hatched while editing; its controls (`children`: a `ResizeHandle`
|
|
668
|
+
* resizing it, a `RemoveButton`) in its corners. Rendering no element of
|
|
669
|
+
* its own: registering with its item before it's painted, its controls in
|
|
670
|
+
* the item's cell. Its presence is its item's having one.
|
|
671
|
+
*/
|
|
672
|
+
const Area = ({ children, defaultPlacement = 'center', defaultSize, movable = false, onPlacementChange, onSizeChange, placement: controlledPlacement, size: controlledSize, supportedSizes, }) => {
|
|
673
|
+
const host = useContext(GridAreaHostContext);
|
|
674
|
+
const [ownSize, setOwnSize] = useState(defaultSize);
|
|
675
|
+
const [ownPlacement, setOwnPlacement] = useState(defaultPlacement);
|
|
676
|
+
const requested = controlledSize ?? ownSize;
|
|
677
|
+
const placement = controlledPlacement ?? ownPlacement;
|
|
678
|
+
const latest = useRef({ onPlacementChange, onSizeChange });
|
|
679
|
+
useLayoutEffect(() => {
|
|
680
|
+
latest.current = { onPlacementChange, onSizeChange };
|
|
681
|
+
});
|
|
682
|
+
const requestSize = useCallback((next, details) => {
|
|
683
|
+
setOwnSize(next);
|
|
684
|
+
latest.current.onSizeChange?.(next, details ?? noneMerged);
|
|
685
|
+
}, []);
|
|
686
|
+
const requestPlacement = useCallback((next) => {
|
|
687
|
+
setOwnPlacement(next);
|
|
688
|
+
latest.current.onPlacementChange?.(next);
|
|
689
|
+
}, []);
|
|
690
|
+
const supportedKey = supportedSizes?.join(',');
|
|
691
|
+
// (Registered as it changes, before it's painted; gone, gone.)
|
|
692
|
+
useLayoutEffect(() => {
|
|
693
|
+
host?.register({
|
|
694
|
+
movable,
|
|
695
|
+
placement,
|
|
696
|
+
requested,
|
|
697
|
+
requestPlacement,
|
|
698
|
+
requestSize,
|
|
699
|
+
supported: supportedSizes,
|
|
700
|
+
});
|
|
701
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- by content
|
|
702
|
+
}, [host?.register, movable, placement, requested, supportedKey]);
|
|
703
|
+
const register = host?.register;
|
|
704
|
+
useLayoutEffect(() => () => register?.(undefined), [register]);
|
|
705
|
+
return host?.slot
|
|
706
|
+
? createPortal(_jsx(GridAreaScopeContext.Provider, { value: true, children: children }), host.slot)
|
|
707
|
+
: null;
|
|
708
|
+
};
|
|
709
|
+
/** The places a tile takes in its area, in order (see `usePlacing`). */
|
|
710
|
+
const placements = [
|
|
711
|
+
'top-leading',
|
|
712
|
+
'top-trailing',
|
|
713
|
+
'center',
|
|
714
|
+
'bottom-leading',
|
|
715
|
+
'bottom-trailing',
|
|
716
|
+
];
|
|
717
|
+
/**
|
|
718
|
+
* Its tile's places in its area (px from the cell's inline start and top,
|
|
719
|
+
* see `placementOffset`), and the one nearest a place (`at`).
|
|
720
|
+
*/
|
|
721
|
+
const nearestPlacement = (cell, at) => {
|
|
722
|
+
const metrics = metricsOf(cell);
|
|
723
|
+
const styles = getComputedStyle(cell);
|
|
724
|
+
const value = (name) => Number.parseFloat(styles.getPropertyValue(name)) || 1;
|
|
725
|
+
const area = [
|
|
726
|
+
value('--uikit-grid-item-columns'),
|
|
727
|
+
value('--uikit-grid-item-rows'),
|
|
728
|
+
];
|
|
729
|
+
const size = [
|
|
730
|
+
value('--uikit-grid-body-columns'),
|
|
731
|
+
value('--uikit-grid-body-rows'),
|
|
732
|
+
];
|
|
733
|
+
const fulls = {
|
|
734
|
+
across: area[0] - size[0],
|
|
735
|
+
isFullArea: isFullAreaOf(cell),
|
|
736
|
+
isFullBody: cell
|
|
737
|
+
.querySelector(':scope > [data-grid-part="body"]')
|
|
738
|
+
?.dataset.size?.startsWith('*') === true,
|
|
739
|
+
};
|
|
740
|
+
let best;
|
|
741
|
+
for (const placement of placements) {
|
|
742
|
+
const [x, y] = placementOffset(placement, area, size);
|
|
743
|
+
// (Inset off its corner there, as it'd be, see `insetOf`.)
|
|
744
|
+
const inset = insetOf(placement, area, size);
|
|
745
|
+
const [ex, ey] = insetPxOf(cell, inset, stripShareOf(placement, fulls));
|
|
746
|
+
const px = [
|
|
747
|
+
x * (metrics.column + metrics.gap) + ex,
|
|
748
|
+
y * (metrics.unit + metrics.gap) + ey,
|
|
749
|
+
];
|
|
750
|
+
const distance = Math.hypot(px[0] - at[0], px[1] - at[1]);
|
|
751
|
+
if (!best || distance < best.distance)
|
|
752
|
+
best = { distance, placement, px };
|
|
753
|
+
}
|
|
754
|
+
return best;
|
|
755
|
+
};
|
|
756
|
+
/**
|
|
757
|
+
* A movable tile in its area (see `movable`), editing: held (a touch a
|
|
758
|
+
* moment first), dragged, following the pointer, the place it'd land
|
|
759
|
+
* outlined on its cell (the nearest; see the CSS); let go, it glides there
|
|
760
|
+
* from where it was let go (the reflow's glide not for it). Its hatched
|
|
761
|
+
* room isn't it: held there, the item moves (see edit.ts).
|
|
762
|
+
*/
|
|
763
|
+
const usePlacing = (cellRef, bodyRef, isOn, { placement, request, }) => {
|
|
764
|
+
const latest = useRef({ placement, request });
|
|
765
|
+
useLayoutEffect(() => {
|
|
766
|
+
latest.current = { placement, request };
|
|
767
|
+
});
|
|
768
|
+
// (Let go somewhere else: where it was then, to glide from.)
|
|
769
|
+
const letGo = useRef(undefined);
|
|
770
|
+
useLayoutEffect(() => {
|
|
771
|
+
const cell = cellRef.current;
|
|
772
|
+
const body = bodyRef.current;
|
|
773
|
+
if (!isOn || !cell || !body)
|
|
774
|
+
return undefined;
|
|
775
|
+
let pending;
|
|
776
|
+
let drag;
|
|
777
|
+
const reduce = reducedMotionOf(cell);
|
|
778
|
+
const isRtl = () => getComputedStyle(cell).direction === 'rtl';
|
|
779
|
+
// (Where it is in its cell: px from the inline start and top.)
|
|
780
|
+
const originOf = () => {
|
|
781
|
+
const box = cell.getBoundingClientRect();
|
|
782
|
+
const own = body.getBoundingClientRect();
|
|
783
|
+
return [
|
|
784
|
+
isRtl() ? box.right - own.right : own.left - box.left,
|
|
785
|
+
own.top - box.top,
|
|
786
|
+
];
|
|
787
|
+
};
|
|
788
|
+
const stop = () => {
|
|
789
|
+
if (pending)
|
|
790
|
+
clearTimeout(pending.timer);
|
|
791
|
+
pending = undefined;
|
|
792
|
+
};
|
|
793
|
+
const finish = (isCancelled) => {
|
|
794
|
+
if (!drag)
|
|
795
|
+
return;
|
|
796
|
+
const { next } = drag;
|
|
797
|
+
drag = undefined;
|
|
798
|
+
delete cell.dataset.gridPlacing;
|
|
799
|
+
cell.style.removeProperty('--uikit-grid-landing-x');
|
|
800
|
+
cell.style.removeProperty('--uikit-grid-landing-y');
|
|
801
|
+
const at = body.getBoundingClientRect();
|
|
802
|
+
body.style.removeProperty('translate');
|
|
803
|
+
if (!isCancelled && next !== latest.current.placement) {
|
|
804
|
+
letGo.current = at;
|
|
805
|
+
body.dataset.gridPlaced = '';
|
|
806
|
+
latest.current.request(next);
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
// (Back where it was.)
|
|
810
|
+
const home = body.getBoundingClientRect();
|
|
811
|
+
if (reduce.matches)
|
|
812
|
+
return;
|
|
813
|
+
body.animate([
|
|
814
|
+
{
|
|
815
|
+
translate: `${String(at.left - home.left)}px ${String(at.top - home.top)}px`,
|
|
816
|
+
},
|
|
817
|
+
{ translate: '0 0' },
|
|
818
|
+
], { duration: reflowDuration, easing: reflowEasing });
|
|
819
|
+
};
|
|
820
|
+
const onDown = (event) => {
|
|
821
|
+
if (!event.isPrimary ||
|
|
822
|
+
event.button !== 0 ||
|
|
823
|
+
event.target?.closest('[data-grid-edit-control]')) {
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
const isTouch = event.pointerType === 'touch';
|
|
827
|
+
stop();
|
|
828
|
+
pending = {
|
|
829
|
+
id: event.pointerId,
|
|
830
|
+
ready: !isTouch,
|
|
831
|
+
start: [event.clientX, event.clientY],
|
|
832
|
+
timer: isTouch
|
|
833
|
+
? window.setTimeout(() => {
|
|
834
|
+
if (pending)
|
|
835
|
+
pending.ready = true;
|
|
836
|
+
}, touchHold)
|
|
837
|
+
: 0,
|
|
838
|
+
};
|
|
839
|
+
};
|
|
840
|
+
const onMove = (event) => {
|
|
841
|
+
const at = [event.clientX, event.clientY];
|
|
842
|
+
if (!drag) {
|
|
843
|
+
if (pending?.id !== event.pointerId ||
|
|
844
|
+
Math.hypot(at[0] - pending.start[0], at[1] - pending.start[1]) < slop) {
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
// (A touch moving before its hold: a scroll.)
|
|
848
|
+
if (!pending.ready) {
|
|
849
|
+
stop();
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
drag = {
|
|
853
|
+
id: pending.id,
|
|
854
|
+
next: latest.current.placement,
|
|
855
|
+
origin: originOf(),
|
|
856
|
+
start: pending.start,
|
|
857
|
+
};
|
|
858
|
+
stop();
|
|
859
|
+
cell.dataset.gridPlacing = '';
|
|
860
|
+
}
|
|
861
|
+
if (drag.id !== event.pointerId)
|
|
862
|
+
return;
|
|
863
|
+
const dx = at[0] - drag.start[0];
|
|
864
|
+
const dy = at[1] - drag.start[1];
|
|
865
|
+
body.style.setProperty('translate', `${String(dx)}px ${String(dy)}px`);
|
|
866
|
+
const nearest = nearestPlacement(cell, [
|
|
867
|
+
drag.origin[0] + (isRtl() ? -dx : dx),
|
|
868
|
+
drag.origin[1] + dy,
|
|
869
|
+
]);
|
|
870
|
+
if (!nearest)
|
|
871
|
+
return;
|
|
872
|
+
drag.next = nearest.placement;
|
|
873
|
+
cell.style.setProperty('--uikit-grid-landing-x', `${String(nearest.px[0])}px`);
|
|
874
|
+
cell.style.setProperty('--uikit-grid-landing-y', `${String(nearest.px[1])}px`);
|
|
875
|
+
};
|
|
876
|
+
const onUp = (event) => {
|
|
877
|
+
if (pending?.id === event.pointerId)
|
|
878
|
+
stop();
|
|
879
|
+
if (drag?.id === event.pointerId)
|
|
880
|
+
finish(event.type === 'pointercancel');
|
|
881
|
+
};
|
|
882
|
+
// (Dragging, or held long enough to: the page not scrolled by it.)
|
|
883
|
+
const onTouchMove = (event) => {
|
|
884
|
+
if (drag || pending?.ready)
|
|
885
|
+
event.preventDefault();
|
|
886
|
+
};
|
|
887
|
+
body.addEventListener('pointerdown', onDown);
|
|
888
|
+
body.addEventListener('touchmove', onTouchMove, { passive: false });
|
|
889
|
+
window.addEventListener('pointermove', onMove);
|
|
890
|
+
window.addEventListener('pointerup', onUp);
|
|
891
|
+
window.addEventListener('pointercancel', onUp);
|
|
892
|
+
return () => {
|
|
893
|
+
stop();
|
|
894
|
+
finish(true);
|
|
895
|
+
body.removeEventListener('pointerdown', onDown);
|
|
896
|
+
body.removeEventListener('touchmove', onTouchMove);
|
|
897
|
+
window.removeEventListener('pointermove', onMove);
|
|
898
|
+
window.removeEventListener('pointerup', onUp);
|
|
899
|
+
window.removeEventListener('pointercancel', onUp);
|
|
900
|
+
};
|
|
901
|
+
}, [bodyRef, cellRef, isOn]);
|
|
902
|
+
// (Placed: from where it was let go, gliding there.)
|
|
903
|
+
useLayoutEffect(() => {
|
|
904
|
+
const body = bodyRef.current;
|
|
905
|
+
const from = letGo.current;
|
|
906
|
+
letGo.current = undefined;
|
|
907
|
+
if (!body || !from)
|
|
908
|
+
return;
|
|
909
|
+
const to = body.getBoundingClientRect();
|
|
910
|
+
if (!reducedMotionOf(body).matches) {
|
|
911
|
+
body.animate([
|
|
912
|
+
{
|
|
913
|
+
translate: `${String(from.left - to.left)}px ${String(from.top - to.top)}px`,
|
|
914
|
+
},
|
|
915
|
+
{ translate: '0 0' },
|
|
916
|
+
], { duration: reflowDuration, easing: reflowEasing });
|
|
917
|
+
}
|
|
918
|
+
requestAnimationFrame(() => {
|
|
919
|
+
delete body.dataset.gridPlaced;
|
|
920
|
+
});
|
|
921
|
+
}, [bodyRef, placement]);
|
|
922
|
+
// (By keys: the arrow keys stepping through its places, in order.)
|
|
923
|
+
return useCallback((event) => {
|
|
924
|
+
if (!isOn || event.altKey || event.target !== event.currentTarget)
|
|
925
|
+
return;
|
|
926
|
+
const by = { ArrowDown: 1, ArrowLeft: -1, ArrowRight: 1, ArrowUp: -1 }[event.key];
|
|
927
|
+
if (by === undefined)
|
|
928
|
+
return;
|
|
929
|
+
event.preventDefault();
|
|
930
|
+
event.stopPropagation();
|
|
931
|
+
const at = placements.indexOf(latest.current.placement);
|
|
932
|
+
const next = placements[Math.min(placements.length - 1, Math.max(0, at + by))];
|
|
933
|
+
if (next && next !== latest.current.placement) {
|
|
934
|
+
latest.current.request(next);
|
|
935
|
+
}
|
|
936
|
+
}, [isOn]);
|
|
937
|
+
};
|
|
938
|
+
/**
|
|
939
|
+
* An item: a cell spanning its area (its effective size without one, see
|
|
940
|
+
* the top), its tile (its body) in it, what's in it filling that (e.g. a
|
|
941
|
+
* `Card`, a `ResizeHandle`), its sizes read with `useGridItem`; room
|
|
942
|
+
* around its tile with a `Grid.Area` in it.
|
|
943
|
+
*/
|
|
944
|
+
const Item = forwardRef(({ children, className, defaultOpen = false, defaultSize, dropName, onOpenChange, onSizeChange, open: controlledOpen, openLabel, openSize = '4x3', reorderHandle, size: controlledSize, style, supportedSizes, ...props }, forwardedRef) => {
|
|
945
|
+
const ref = useForwardedRef(forwardedRef);
|
|
946
|
+
const entry = useCellEntry(ref, reorderHandle, dropName);
|
|
947
|
+
const { host, setSlot, spec } = useAreaHost();
|
|
948
|
+
const { areaState, bodyStyle, hasRoom, isFull: isFullWidth, spanStyle, state: cellState, } = useCell({
|
|
949
|
+
area: spec,
|
|
950
|
+
defaultSize,
|
|
951
|
+
onSizeChange,
|
|
952
|
+
size: controlledSize,
|
|
953
|
+
supportedSizes,
|
|
954
|
+
});
|
|
955
|
+
const body = useRef(null);
|
|
956
|
+
// (Open above the page (see overlay.tsx): never while editing, which
|
|
957
|
+
// closes it.)
|
|
958
|
+
const { columns, gap, unit } = useContext(GridContext);
|
|
959
|
+
const [ownOpen, setOwnOpen] = useState(defaultOpen);
|
|
960
|
+
// (Editing started: closed (its own state too, as it renders).)
|
|
961
|
+
const [wasEditing, setWasEditing] = useState(cellState.editing);
|
|
962
|
+
if (wasEditing !== cellState.editing) {
|
|
963
|
+
setWasEditing(cellState.editing);
|
|
964
|
+
if (cellState.editing)
|
|
965
|
+
setOwnOpen(false);
|
|
966
|
+
}
|
|
967
|
+
const isOpen = (controlledOpen ?? ownOpen) && !cellState.editing;
|
|
968
|
+
const latestOpen = useRef(onOpenChange);
|
|
969
|
+
const wasOpen = useRef(isOpen);
|
|
970
|
+
useLayoutEffect(() => {
|
|
971
|
+
latestOpen.current = onOpenChange;
|
|
972
|
+
// (Closed by editing: told, as any other way.)
|
|
973
|
+
if (wasOpen.current && !isOpen && cellState.editing) {
|
|
974
|
+
onOpenChange?.(false, { reason: 'edit' });
|
|
975
|
+
}
|
|
976
|
+
wasOpen.current = isOpen;
|
|
977
|
+
});
|
|
978
|
+
const changeOpen = useCallback((next, reason) => {
|
|
979
|
+
setOwnOpen(next);
|
|
980
|
+
latestOpen.current?.(next, { reason });
|
|
981
|
+
}, []);
|
|
982
|
+
const standIn = useRef(null);
|
|
983
|
+
const [openColumns, openRows] = spanOf(openSize, columns);
|
|
984
|
+
const { backdrop, phase, settle, underlay } = useOverlay({
|
|
985
|
+
body,
|
|
986
|
+
isOpen,
|
|
987
|
+
onClose: (reason) => changeOpen(false, reason),
|
|
988
|
+
size: [
|
|
989
|
+
openColumns * unit + (openColumns - 1) * gap,
|
|
990
|
+
openRows * unit + (openRows - 1) * gap,
|
|
991
|
+
],
|
|
992
|
+
standIn,
|
|
993
|
+
});
|
|
994
|
+
// (Its tile getting to a new box, what's in it told: after its overlay
|
|
995
|
+
// placed it, see reshape.ts.)
|
|
996
|
+
const [onShapeChange, layoutRegistry] = useReshape({
|
|
997
|
+
body,
|
|
998
|
+
cell: ref,
|
|
999
|
+
onSettled: settle,
|
|
1000
|
+
});
|
|
1001
|
+
const state = useMemo(() => ({
|
|
1002
|
+
...cellState,
|
|
1003
|
+
open: isOpen,
|
|
1004
|
+
openSize,
|
|
1005
|
+
setOpen: (next) => changeOpen(next, 'none'),
|
|
1006
|
+
}), [cellState, changeOpen, isOpen, openSize]);
|
|
1007
|
+
// (Filling its area: nowhere to go in it; held, its area moves (the
|
|
1008
|
+
// item, its cell), its crosshairs the area's, see the CSS.)
|
|
1009
|
+
const isPlaceable = cellState.editing && cellState.movable && hasRoom;
|
|
1010
|
+
// (Focused: moved in its area by keys; open, as a dialog.)
|
|
1011
|
+
let bodyTabIndex;
|
|
1012
|
+
if (isPlaceable)
|
|
1013
|
+
bodyTabIndex = 0;
|
|
1014
|
+
else if (isOpen)
|
|
1015
|
+
bodyTabIndex = -1;
|
|
1016
|
+
const onBodyKeyDown = usePlacing(ref, body, isPlaceable, {
|
|
1017
|
+
placement: cellState.placement,
|
|
1018
|
+
request: cellState.requestPlacement,
|
|
1019
|
+
});
|
|
1020
|
+
return (_jsx(ShapeSnapshot, { shape: {
|
|
1021
|
+
area: cellState.area,
|
|
1022
|
+
editing: cellState.editing,
|
|
1023
|
+
open: isOpen,
|
|
1024
|
+
placement: cellState.placement,
|
|
1025
|
+
size: cellState.size,
|
|
1026
|
+
}, onChange: onShapeChange, children: _jsx(GridItemLayoutProvider, { value: layoutRegistry, children: _jsx(GridItemContext.Provider, { value: state, children: _jsx(GridAreaStateContext.Provider, { value: areaState, children: _jsx(GridAreaHostContext.Provider, { value: host, children: _jsxs("div", { ref: ref, "data-grid-full": flagOf(isFullWidth), "data-grid-part": "item", "data-grid-placement": cellState.placement, "data-grid-room": flagOf(hasRoom), "data-size": cellState.area, style: withStyle({ ...spanStyle, ...bodyStyle }, style), ...entry, ...props, className: cn('uikit-grid-item', className), children: [hasRoom ? (_jsx("span", { "aria-hidden": "true", className: "uikit-grid-room-edge", "data-grid-part": "room-edge" })) : null, _jsxs("div", { ref: body, "aria-label": isOpen ? openLabel : undefined, "aria-modal": isOpen ? true : undefined, className: "uikit-grid-item-body", "data-grid-movable-body": flagOf(isPlaceable), "data-grid-open": phase, "data-grid-part": "body", "data-size": cellState.size, role: isOpen ? 'dialog' : undefined, tabIndex: bodyTabIndex, onKeyDown: onBodyKeyDown, children: [children, isPlaceable ? moveSignOf(movableMark) : null] }), phase ? (_jsx("span", { ref: standIn, "aria-hidden": "true", className: "uikit-grid-stand-in", "data-grid-part": "stand-in", "data-size": cellState.size })) : null, backdrop, underlay, _jsx("div", { ref: setSlot, className: "uikit-grid-item-slot" }), moveSignOf(entry)] }) }) }) }) }) }));
|
|
1027
|
+
});
|
|
1028
|
+
/**
|
|
1029
|
+
* A deliberate hole, as big as an item of its size (any: fitted to the
|
|
1030
|
+
* columns); editing, outlined, what's in it shown (e.g. a control removing
|
|
1031
|
+
* it, a `ResizeHandle`), moved as items are (named).
|
|
1032
|
+
*/
|
|
1033
|
+
const Spacer = forwardRef(({ children, className, defaultSize, dropName, onSizeChange, reorderHandle, size: controlledSize, style, ...props }, forwardedRef) => {
|
|
1034
|
+
const ref = useForwardedRef(forwardedRef);
|
|
1035
|
+
const { columns } = useContext(GridContext);
|
|
1036
|
+
const entry = useCellEntry(ref, reorderHandle, dropName);
|
|
1037
|
+
const { isFull: isFullWidth, spanStyle, state, } = useCell({
|
|
1038
|
+
defaultSize,
|
|
1039
|
+
onSizeChange,
|
|
1040
|
+
size: controlledSize,
|
|
1041
|
+
});
|
|
1042
|
+
return (_jsx(GridItemContext.Provider, { value: state, children: _jsxs("div", { ref: ref, "aria-hidden": state.editing ? undefined : true, "data-grid-full": isFullWidth ? '' : undefined, "data-grid-part": "spacer", "data-size": state.size, style: withStyle(spanStyle, style), ...entry, ...props, className: cn('uikit-grid-spacer', className), children: [dashesFor(state.size, columns), children, moveSignOf(entry)] }) }));
|
|
1043
|
+
});
|
|
1044
|
+
/**
|
|
1045
|
+
* The supported size closest to a span (ties: the smaller), on a grid of
|
|
1046
|
+
* `gridColumns` (a full width's).
|
|
1047
|
+
*/
|
|
1048
|
+
const closestSize = (supported, columns, rows, gridColumns) => {
|
|
1049
|
+
let best;
|
|
1050
|
+
let distance = Number.POSITIVE_INFINITY;
|
|
1051
|
+
for (const size of supported) {
|
|
1052
|
+
const [c, r] = spanOf(size, gridColumns);
|
|
1053
|
+
const next = Math.abs(c - columns) + Math.abs(r - rows);
|
|
1054
|
+
if (!(next < distance)) {
|
|
1055
|
+
continue;
|
|
1056
|
+
}
|
|
1057
|
+
best = size;
|
|
1058
|
+
distance = next;
|
|
1059
|
+
}
|
|
1060
|
+
return best;
|
|
1061
|
+
};
|
|
1062
|
+
/** How far past halfway a resize drag goes before a span changes (units). */
|
|
1063
|
+
const resizeHysteresis = 0.2;
|
|
1064
|
+
/**
|
|
1065
|
+
* The most columns a resize reaches (see `Reach`): into the end-of-line
|
|
1066
|
+
* column for a full width, from a row's start; past its row's end, if a
|
|
1067
|
+
* column wider is supported; else to its row's end. Within an area (no
|
|
1068
|
+
* reach): its.
|
|
1069
|
+
*/
|
|
1070
|
+
const limitOf = (reach, most, columns) => {
|
|
1071
|
+
if (!reach)
|
|
1072
|
+
return most;
|
|
1073
|
+
if (reach.canFull)
|
|
1074
|
+
return columns + 1;
|
|
1075
|
+
return reach.canWrap ? most : Math.min(most, reach.fit);
|
|
1076
|
+
};
|
|
1077
|
+
/** How long a press may last to be a tap, not held (ms). */
|
|
1078
|
+
const tapTime = 350;
|
|
1079
|
+
const wrapOf = (cell, { metrics, own }, [, rows], [, dy]) => {
|
|
1080
|
+
if (dy === 0 || metrics.end <= 0)
|
|
1081
|
+
return undefined;
|
|
1082
|
+
const x = (metrics.columns - own.column) * (metrics.column + metrics.gap);
|
|
1083
|
+
if (dy > 0) {
|
|
1084
|
+
return {
|
|
1085
|
+
box: [x, 0, metrics.end, cell.offsetHeight],
|
|
1086
|
+
direction: 'next',
|
|
1087
|
+
from: [0, 0, cell.offsetWidth, cell.offsetHeight],
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
const height = rows * (metrics.unit + metrics.gap) - metrics.gap;
|
|
1091
|
+
return {
|
|
1092
|
+
box: [x, dy, metrics.end, height],
|
|
1093
|
+
direction: 'previous',
|
|
1094
|
+
// (The second as tall as the tile is now, before the resize: what it
|
|
1095
|
+
// leaves, however its new size grows or shrinks.)
|
|
1096
|
+
echo: [0, 0, metrics.end, cell.offsetHeight],
|
|
1097
|
+
};
|
|
1098
|
+
};
|
|
1099
|
+
/** Cells' row-change outlines (see `showWrap`). */
|
|
1100
|
+
const wraps = new WeakMap();
|
|
1101
|
+
/**
|
|
1102
|
+
* A resize's row change shown (see `Wrap`), or not: elements of their own
|
|
1103
|
+
* in the cell, beside its resize outline; animated there from the tile
|
|
1104
|
+
* (growing), faded in (shrinking: also at the start of its own row), see
|
|
1105
|
+
* the CSS.
|
|
1106
|
+
*/
|
|
1107
|
+
const showWrap = (cell, wrap) => {
|
|
1108
|
+
const elements = wraps.get(cell) ?? [];
|
|
1109
|
+
const boxes = wrap ? [wrap.box, ...(wrap.echo ? [wrap.echo] : [])] : [];
|
|
1110
|
+
while (elements.length > boxes.length)
|
|
1111
|
+
elements.pop()?.remove();
|
|
1112
|
+
for (const [index, box] of boxes.entries()) {
|
|
1113
|
+
let element = elements[index];
|
|
1114
|
+
const isNew = !element || element.dataset.direction !== wrap?.direction;
|
|
1115
|
+
if (!element) {
|
|
1116
|
+
element = document.createElement('div');
|
|
1117
|
+
element.className = 'uikit-grid-resize-wrap';
|
|
1118
|
+
element.dataset.gridPart = 'resize-wrap';
|
|
1119
|
+
element.setAttribute('aria-hidden', 'true');
|
|
1120
|
+
cell.append(element);
|
|
1121
|
+
elements.push(element);
|
|
1122
|
+
}
|
|
1123
|
+
const target = element;
|
|
1124
|
+
const place = ([x, y, width, height]) => {
|
|
1125
|
+
target.style.setProperty('inset-inline-start', `${String(x)}px`);
|
|
1126
|
+
target.style.setProperty('inset-block-start', `${String(y)}px`);
|
|
1127
|
+
target.style.setProperty('width', `${String(width)}px`);
|
|
1128
|
+
target.style.setProperty('height', `${String(height)}px`);
|
|
1129
|
+
};
|
|
1130
|
+
target.dataset.direction = wrap?.direction;
|
|
1131
|
+
if (isNew) {
|
|
1132
|
+
delete target.dataset.shown;
|
|
1133
|
+
place((index === 0 && wrap?.from) || box);
|
|
1134
|
+
// (Laid out where it starts, then on: its transition, see the CSS.)
|
|
1135
|
+
target.getBoundingClientRect();
|
|
1136
|
+
}
|
|
1137
|
+
place(box);
|
|
1138
|
+
target.dataset.shown = '';
|
|
1139
|
+
}
|
|
1140
|
+
if (elements.length > 0)
|
|
1141
|
+
wraps.set(cell, elements);
|
|
1142
|
+
else
|
|
1143
|
+
wraps.delete(cell);
|
|
1144
|
+
};
|
|
1145
|
+
/**
|
|
1146
|
+
* A cell's size to be while its handle's held (see `ResizeHandle`):
|
|
1147
|
+
* outlined over it (see the CSS), nothing moving yet; none, none.
|
|
1148
|
+
*/
|
|
1149
|
+
const showResize = (cell, size, gridColumns = 6, offset) => {
|
|
1150
|
+
if (!size) {
|
|
1151
|
+
showWrap(cell, undefined);
|
|
1152
|
+
delete cell.dataset.gridResizing;
|
|
1153
|
+
delete cell.dataset.gridResizeFull;
|
|
1154
|
+
delete cell.dataset.gridResizeShaped;
|
|
1155
|
+
cell.style.removeProperty('--uikit-grid-resize-columns');
|
|
1156
|
+
cell.style.removeProperty('--uikit-grid-resize-rows');
|
|
1157
|
+
cell.style.removeProperty('--uikit-grid-resize-x');
|
|
1158
|
+
cell.style.removeProperty('--uikit-grid-resize-y');
|
|
1159
|
+
for (const name of ['height', 'shape', 'start', 'top', 'width']) {
|
|
1160
|
+
cell.style.removeProperty(`--uikit-grid-resize-${name}`);
|
|
1161
|
+
}
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
const [columns, rows] = spanOf(size, gridColumns);
|
|
1165
|
+
cell.dataset.gridResizing = '';
|
|
1166
|
+
cell.toggleAttribute('data-grid-resize-full', isFull(size));
|
|
1167
|
+
// (A break's: its shape as it'd be, through the end-of-line column.)
|
|
1168
|
+
if (cell.dataset.gridPart === 'break') {
|
|
1169
|
+
const outline = breakOutlineOf(cell, rows);
|
|
1170
|
+
cell.style.setProperty('--uikit-grid-resize-height', `${String(outline.height)}px`);
|
|
1171
|
+
cell.style.setProperty('--uikit-grid-resize-start', `${String(outline.start)}px`);
|
|
1172
|
+
cell.style.setProperty('--uikit-grid-resize-shape', outline.mask);
|
|
1173
|
+
cell.style.setProperty('--uikit-grid-resize-top', `${String(outline.top)}px`);
|
|
1174
|
+
cell.style.setProperty('--uikit-grid-resize-width', `${String(outline.width)}px`);
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1177
|
+
// (A full width's, still one: its shape as it'd be (see layout.ts).)
|
|
1178
|
+
const full = cell.dataset.gridPart === 'item' &&
|
|
1179
|
+
Object.hasOwn(cell.dataset, 'gridFull') &&
|
|
1180
|
+
isFull(size)
|
|
1181
|
+
? fullOutlineOf(cell, rows)
|
|
1182
|
+
: undefined;
|
|
1183
|
+
cell.toggleAttribute('data-grid-resize-shaped', full !== undefined);
|
|
1184
|
+
if (full) {
|
|
1185
|
+
cell.style.setProperty('--uikit-grid-resize-height', `${String(full.height)}px`);
|
|
1186
|
+
cell.style.setProperty('--uikit-grid-resize-shape', full.mask);
|
|
1187
|
+
cell.style.setProperty('--uikit-grid-resize-width', `${String(full.width)}px`);
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
cell.style.setProperty('--uikit-grid-resize-columns', String(columns));
|
|
1191
|
+
cell.style.setProperty('--uikit-grid-resize-rows', String(rows));
|
|
1192
|
+
if (offset) {
|
|
1193
|
+
cell.style.setProperty('--uikit-grid-resize-x', `${String(offset[0])}px`);
|
|
1194
|
+
cell.style.setProperty('--uikit-grid-resize-y', `${String(offset[1])}px`);
|
|
1195
|
+
}
|
|
1196
|
+
else {
|
|
1197
|
+
cell.style.removeProperty('--uikit-grid-resize-x');
|
|
1198
|
+
cell.style.removeProperty('--uikit-grid-resize-y');
|
|
1199
|
+
}
|
|
1200
|
+
};
|
|
1201
|
+
/** Whether a container's laid out right to left. */
|
|
1202
|
+
const isRtlIn = (container) => getComputedStyle(container).direction === 'rtl';
|
|
1203
|
+
/**
|
|
1204
|
+
* Where a cell is in its container, in units: columns from its inline
|
|
1205
|
+
* start, rows from its first (below its header).
|
|
1206
|
+
*/
|
|
1207
|
+
const placeOf = (element, metrics) => {
|
|
1208
|
+
const container = element.parentElement;
|
|
1209
|
+
const across = metrics.column + metrics.gap;
|
|
1210
|
+
const pitch = metrics.unit + metrics.gap;
|
|
1211
|
+
const header = container?.querySelector(':scope > [data-grid-part="header"]');
|
|
1212
|
+
const origin = header
|
|
1213
|
+
? header.offsetTop + header.offsetHeight + metrics.gap
|
|
1214
|
+
: 0;
|
|
1215
|
+
const start = container && isRtlIn(container)
|
|
1216
|
+
? container.clientWidth - element.offsetLeft - element.offsetWidth
|
|
1217
|
+
: element.offsetLeft;
|
|
1218
|
+
return {
|
|
1219
|
+
column: Math.round(start / across),
|
|
1220
|
+
columns: Math.max(1, Math.round((element.offsetWidth + metrics.gap) / across)),
|
|
1221
|
+
row: Math.round((element.offsetTop - origin) / pitch),
|
|
1222
|
+
rows: Math.max(1, Math.round((element.offsetHeight + metrics.gap) / pitch)),
|
|
1223
|
+
};
|
|
1224
|
+
};
|
|
1225
|
+
/**
|
|
1226
|
+
* What's placed before a cell in its container (in units, in order), and
|
|
1227
|
+
* where it'd go on from (after the last of them; none: the start).
|
|
1228
|
+
*/
|
|
1229
|
+
const placedBefore = (cell, metrics) => {
|
|
1230
|
+
const placed = [];
|
|
1231
|
+
let cursor = { column: 0, row: 0 };
|
|
1232
|
+
for (const child of cell.parentElement?.children ?? []) {
|
|
1233
|
+
if (child === cell)
|
|
1234
|
+
break;
|
|
1235
|
+
if (!(child instanceof HTMLElement) ||
|
|
1236
|
+
child.offsetParent === null ||
|
|
1237
|
+
Object.hasOwn(child.dataset, 'gridBreakNone') ||
|
|
1238
|
+
!['break', 'break-fill', 'item', 'spacer'].includes(child.dataset.gridPart ?? '')) {
|
|
1239
|
+
continue;
|
|
1240
|
+
}
|
|
1241
|
+
const unit = placeOf(child, metrics);
|
|
1242
|
+
placed.push(unit);
|
|
1243
|
+
if (child.dataset.gridPart !== 'break-fill') {
|
|
1244
|
+
cursor = { column: unit.column + unit.columns, row: unit.row };
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
return { cursor, placed };
|
|
1248
|
+
};
|
|
1249
|
+
/** A pointer's place along a row (its container's, from its start; px). */
|
|
1250
|
+
const alongRow = (x, container) => {
|
|
1251
|
+
const box = container.getBoundingClientRect();
|
|
1252
|
+
return isRtlIn(container) ? box.right - x : x - box.left;
|
|
1253
|
+
};
|
|
1254
|
+
/**
|
|
1255
|
+
* A place along a row (see `alongRow`), in columns: past its columns, its
|
|
1256
|
+
* end-of-line column counted as one, then as many more.
|
|
1257
|
+
*/
|
|
1258
|
+
const columnAt = (at, metrics) => {
|
|
1259
|
+
const across = metrics.column + metrics.gap;
|
|
1260
|
+
const edge = metrics.columns * across;
|
|
1261
|
+
const strip = metrics.end > 0 ? metrics.end + metrics.gap : 0;
|
|
1262
|
+
if (at <= edge || strip === 0)
|
|
1263
|
+
return at / across;
|
|
1264
|
+
return at <= edge + strip
|
|
1265
|
+
? metrics.columns + (at - edge) / strip
|
|
1266
|
+
: metrics.columns + 1 + (at - edge - strip) / across;
|
|
1267
|
+
};
|
|
1268
|
+
const reachOf = (cell, size, sizes, x) => {
|
|
1269
|
+
const container = cell.parentElement;
|
|
1270
|
+
if (!container)
|
|
1271
|
+
return undefined;
|
|
1272
|
+
const metrics = metricsOf(cell);
|
|
1273
|
+
const own = placeOf(cell, metrics);
|
|
1274
|
+
const fit = metrics.columns - own.column;
|
|
1275
|
+
const rows = spanOf(size, metrics.columns)[1];
|
|
1276
|
+
const { cursor, placed } = placedBefore(cell, metrics);
|
|
1277
|
+
const across = metrics.column + metrics.gap;
|
|
1278
|
+
const strip = metrics.end > 0 ? metrics.end + metrics.gap : 0;
|
|
1279
|
+
return {
|
|
1280
|
+
before: placed,
|
|
1281
|
+
// (Starting its row: into the end-of-line column, a full width, if
|
|
1282
|
+
// supported. Elsewhere: a column wider, into the next row, if that's.)
|
|
1283
|
+
canFull: own.column === 0 && sizes.some((each) => isFull(each)),
|
|
1284
|
+
canWrap: own.column > 0 &&
|
|
1285
|
+
fit < metrics.columns &&
|
|
1286
|
+
sizes.some((each) => {
|
|
1287
|
+
const [c, r] = spanOf(each, metrics.columns);
|
|
1288
|
+
return c === fit + 1 && r === rows;
|
|
1289
|
+
}),
|
|
1290
|
+
container,
|
|
1291
|
+
cursor,
|
|
1292
|
+
fit,
|
|
1293
|
+
metrics,
|
|
1294
|
+
own,
|
|
1295
|
+
start: {
|
|
1296
|
+
corner: isFull(size)
|
|
1297
|
+
? metrics.columns * across + strip
|
|
1298
|
+
: (own.column + own.columns) * across,
|
|
1299
|
+
pointer: alongRow(x, container),
|
|
1300
|
+
},
|
|
1301
|
+
};
|
|
1302
|
+
};
|
|
1303
|
+
/**
|
|
1304
|
+
* A size as dragged (see `draggedSpan`): a full width's, a column past the
|
|
1305
|
+
* grid's (its end-of-line column), with a reach (see `Reach`).
|
|
1306
|
+
*/
|
|
1307
|
+
const spanDragged = (size, reach, columns, canFull = false) => {
|
|
1308
|
+
const [c, r] = spanOf(size, columns);
|
|
1309
|
+
return [(reach || canFull) && isFull(size) ? columns + 1 : c, r];
|
|
1310
|
+
};
|
|
1311
|
+
/**
|
|
1312
|
+
* Where a span grown past its row's end lands, from where it is (px; see
|
|
1313
|
+
* the CSS), as placed after what's before it (none: as it is).
|
|
1314
|
+
*/
|
|
1315
|
+
const landingOffset = ({ before, cursor, metrics, own }, [columns, rows]) => {
|
|
1316
|
+
const at = landingOf(before, cursor, { columns, rows }, metrics.columns);
|
|
1317
|
+
return at && (at.column !== own.column || at.row !== own.row)
|
|
1318
|
+
? [
|
|
1319
|
+
(at.column - own.column) * (metrics.column + metrics.gap),
|
|
1320
|
+
(at.row - own.row) * (metrics.unit + metrics.gap),
|
|
1321
|
+
]
|
|
1322
|
+
: undefined;
|
|
1323
|
+
};
|
|
1324
|
+
const boundsOf = (body) => {
|
|
1325
|
+
const cell = body.parentElement;
|
|
1326
|
+
const container = cell?.parentElement;
|
|
1327
|
+
if (!cell || !container)
|
|
1328
|
+
return undefined;
|
|
1329
|
+
const metrics = metricsOf(cell);
|
|
1330
|
+
const box = cell.getBoundingClientRect();
|
|
1331
|
+
const trailing = alongRow(isRtlIn(container) ? box.left : box.right, container);
|
|
1332
|
+
return {
|
|
1333
|
+
bottom: box.bottom,
|
|
1334
|
+
container,
|
|
1335
|
+
edge: Math.min(trailing, metrics.columns * (metrics.column + metrics.gap) - metrics.gap),
|
|
1336
|
+
};
|
|
1337
|
+
};
|
|
1338
|
+
/**
|
|
1339
|
+
* Whether a pointer's past its area's edges (see `Bounds`): its trailing
|
|
1340
|
+
* one (a full width's: into its end-of-line column), or its bottom.
|
|
1341
|
+
*/
|
|
1342
|
+
const isPastBounds = ({ bottom, container, edge }, at) => alongRow(at[0], container) > edge || at[1] > bottom;
|
|
1343
|
+
/**
|
|
1344
|
+
* A span dragged in its area (see `draggedSpan`): by how far along its row
|
|
1345
|
+
* (its end-of-line column one column, see `columnAt`), down by rows.
|
|
1346
|
+
*/
|
|
1347
|
+
const boundedSpan = ({ container }, from, start, at, metrics) => {
|
|
1348
|
+
const columns = from[0] +
|
|
1349
|
+
columnAt(alongRow(at[0], container), metrics) -
|
|
1350
|
+
columnAt(alongRow(start[0], container), metrics);
|
|
1351
|
+
const rows = from[1] + (at[1] - start[1]) / (metrics.unit + metrics.gap);
|
|
1352
|
+
return [columns, rows];
|
|
1353
|
+
};
|
|
1354
|
+
/**
|
|
1355
|
+
* A resize's span as dragged (see `ResizeHandle`): by how far, wherever on
|
|
1356
|
+
* its cell it is; along its row, its end-of-line column one column (see
|
|
1357
|
+
* `columnAt`); within `limit` (columns up to the grid's, past its row's
|
|
1358
|
+
* end only if it may; rows up to the grid's `maxTileRows`; within an area,
|
|
1359
|
+
* its), changing only
|
|
1360
|
+
* once it's dragged a little past half a unit.
|
|
1361
|
+
*/
|
|
1362
|
+
const draggedSpan = ({ bounds, from, reach, span, start, }, at, metrics, limit) => {
|
|
1363
|
+
let raw = [
|
|
1364
|
+
from[0] +
|
|
1365
|
+
(reach
|
|
1366
|
+
? columnAt(reach.start.corner +
|
|
1367
|
+
alongRow(at[0], reach.container) -
|
|
1368
|
+
reach.start.pointer, metrics) -
|
|
1369
|
+
reach.own.column -
|
|
1370
|
+
from[0]
|
|
1371
|
+
: (at[0] - start[0]) / (metrics.column + metrics.gap)),
|
|
1372
|
+
from[1] + (at[1] - start[1]) / (metrics.unit + metrics.gap),
|
|
1373
|
+
];
|
|
1374
|
+
if (bounds)
|
|
1375
|
+
raw = boundedSpan(bounds, from, start, at, metrics);
|
|
1376
|
+
return [0, 1].map((axis) => {
|
|
1377
|
+
const was = span[axis] ?? 1;
|
|
1378
|
+
const wanted = raw[axis] ?? was;
|
|
1379
|
+
return Math.abs(wanted - was) < 0.5 + resizeHysteresis
|
|
1380
|
+
? was
|
|
1381
|
+
: Math.min(limit[axis] ?? 3, Math.max(1, Math.round(wanted)));
|
|
1382
|
+
});
|
|
1383
|
+
};
|
|
1384
|
+
/**
|
|
1385
|
+
* The size a resize is dragged to (see `ResizeHandle`): the supported one
|
|
1386
|
+
* closest to its span (into the end-of-line column: a full width; else a
|
|
1387
|
+
* fixed one, even as wide as the grid); past its area's edge, at least the
|
|
1388
|
+
* next one up from where it was (see `isPastBounds`), dragged further, as
|
|
1389
|
+
* far as it's dragged.
|
|
1390
|
+
*/
|
|
1391
|
+
const draggedSize = (sizes, resized, span, { canFull = false, columns, isPast, reach, }) => {
|
|
1392
|
+
// (Past its row's end, or into a full-width area's end-of-line column
|
|
1393
|
+
// (`canFull`): a full width; else as wide as the columns at most.)
|
|
1394
|
+
const isFullSpan = (reach !== undefined || canFull) && span[0] > columns;
|
|
1395
|
+
const pool = reach || canFull
|
|
1396
|
+
? sizes.filter((each) => isFull(each) === isFullSpan)
|
|
1397
|
+
: sizes;
|
|
1398
|
+
const closest = closestSize(pool.length > 0 ? pool : [resized], isFullSpan ? columns : span[0], span[1], columns);
|
|
1399
|
+
const was = sizes.indexOf(resized);
|
|
1400
|
+
return isPast && closest && was !== -1 && sizes.indexOf(closest) <= was
|
|
1401
|
+
? (sizes[was + 1] ?? closest)
|
|
1402
|
+
: closest;
|
|
1403
|
+
};
|
|
1404
|
+
/**
|
|
1405
|
+
* Its grid's root marked while a cell's resized by a drag (see the CSS;
|
|
1406
|
+
* e.g. a card's content unveiled, see Card).
|
|
1407
|
+
*/
|
|
1408
|
+
const markResizing = (cell, isResizing) => {
|
|
1409
|
+
const root = cell?.closest('[data-grid-part="root"]');
|
|
1410
|
+
if (isResizing)
|
|
1411
|
+
root?.setAttribute('data-grid-drag', 'resize');
|
|
1412
|
+
else
|
|
1413
|
+
root?.removeAttribute('data-grid-drag');
|
|
1414
|
+
};
|
|
1415
|
+
/**
|
|
1416
|
+
* Whether a tile resized in its area is dragged past it (see
|
|
1417
|
+
* `isPastBounds`): in a full-width area that may be a full width
|
|
1418
|
+
* (`canFull`), only past its bottom (its end-of-line column a full width).
|
|
1419
|
+
*/
|
|
1420
|
+
const pastOf = ({ bounds, canFull }, at) => {
|
|
1421
|
+
if (!bounds)
|
|
1422
|
+
return false;
|
|
1423
|
+
return canFull ? at[1] > bounds.bottom : isPastBounds(bounds, at);
|
|
1424
|
+
};
|
|
1425
|
+
/** Cells to be taken in by a resize (see `mergedBy`), marked (see the CSS). */
|
|
1426
|
+
const markMerged = (cells, isMerging) => {
|
|
1427
|
+
for (const cell of cells) {
|
|
1428
|
+
cell.toggleAttribute('data-grid-merging', isMerging);
|
|
1429
|
+
}
|
|
1430
|
+
};
|
|
1431
|
+
/**
|
|
1432
|
+
* A tile resized in its area (see `ResizeHandle`): where it'd sit at a
|
|
1433
|
+
* size, by its placement, from where it is (px; none: not in an area, or
|
|
1434
|
+
* no move).
|
|
1435
|
+
*/
|
|
1436
|
+
const placedOffset = (body, [columns, rows], isFullBody) => {
|
|
1437
|
+
const cell = body.parentElement;
|
|
1438
|
+
if (body.dataset.gridPart !== 'body' || !cell)
|
|
1439
|
+
return undefined;
|
|
1440
|
+
const styles = getComputedStyle(cell);
|
|
1441
|
+
const value = (name) => Number.parseFloat(styles.getPropertyValue(name)) || 0;
|
|
1442
|
+
const placement = (cell.dataset.gridPlacement ?? 'center');
|
|
1443
|
+
const area = [
|
|
1444
|
+
value('--uikit-grid-item-columns'),
|
|
1445
|
+
value('--uikit-grid-item-rows'),
|
|
1446
|
+
];
|
|
1447
|
+
const [x, y] = placementOffset(placement, area, [columns, rows]);
|
|
1448
|
+
// (Its inset off its corner at that size, see `insetOf`.)
|
|
1449
|
+
const inset = insetOf(placement, area, [columns, rows]);
|
|
1450
|
+
const to = insetPxOf(cell, inset, stripShareOf(placement, {
|
|
1451
|
+
across: area[0] - columns,
|
|
1452
|
+
isFullArea: isFullAreaOf(cell),
|
|
1453
|
+
isFullBody,
|
|
1454
|
+
}));
|
|
1455
|
+
const from = insetPxOf(cell, [value('--uikit-grid-inset-x'), value('--uikit-grid-inset-y')], value('--uikit-grid-inset-strip'));
|
|
1456
|
+
const dx = x - value('--uikit-grid-body-x');
|
|
1457
|
+
const dy = y - value('--uikit-grid-body-y');
|
|
1458
|
+
const ex = to[0] - from[0];
|
|
1459
|
+
const ey = to[1] - from[1];
|
|
1460
|
+
if (dx === 0 && dy === 0 && ex === 0 && ey === 0)
|
|
1461
|
+
return undefined;
|
|
1462
|
+
const metrics = metricsOf(cell);
|
|
1463
|
+
return [
|
|
1464
|
+
dx * (metrics.column + metrics.gap) + ex,
|
|
1465
|
+
dy * (metrics.unit + metrics.gap) + ey,
|
|
1466
|
+
];
|
|
1467
|
+
};
|
|
1468
|
+
/**
|
|
1469
|
+
* A resize's outline at a size (see the CSS), past its row's end where it
|
|
1470
|
+
* lands, nothing taken in then; else what it takes in marked.
|
|
1471
|
+
*/
|
|
1472
|
+
const outlineResize = (current, next, { columns, isTaking }) => {
|
|
1473
|
+
const { cell, reach } = current;
|
|
1474
|
+
if (!cell)
|
|
1475
|
+
return;
|
|
1476
|
+
const grown = spanOf(next, columns);
|
|
1477
|
+
// (Where it'll land: elsewhere (past its row's end, or back up into the
|
|
1478
|
+
// row before), outlined there, nothing taken in; a tile in its area,
|
|
1479
|
+
// where its placement puts it at that size.)
|
|
1480
|
+
const offset = reach
|
|
1481
|
+
? landingOffset(reach, grown)
|
|
1482
|
+
: placedOffset(cell, grown, isFull(next));
|
|
1483
|
+
showResize(cell, next, columns, offset);
|
|
1484
|
+
showWrap(cell, reach && offset ? wrapOf(cell, reach, grown, offset) : undefined);
|
|
1485
|
+
markMerged(current.merged, false);
|
|
1486
|
+
current.merged =
|
|
1487
|
+
isTaking && !offset ? mergedBy(cell, grown[0], grown[1]) : [];
|
|
1488
|
+
markMerged(current.merged, true);
|
|
1489
|
+
};
|
|
1490
|
+
/**
|
|
1491
|
+
* A press's own mouse events (after it's released: a touch's, then a
|
|
1492
|
+
* click) swallowed, wherever they land (a touch's aimed at what's nearest,
|
|
1493
|
+
* e.g. a neighboring spawn opening its menu on its mouse down); only the
|
|
1494
|
+
* next of each, soon after.
|
|
1495
|
+
*/
|
|
1496
|
+
const swallow = (event) => {
|
|
1497
|
+
event.preventDefault();
|
|
1498
|
+
event.stopImmediatePropagation();
|
|
1499
|
+
};
|
|
1500
|
+
const swallowClick = (pointerType) => {
|
|
1501
|
+
// (A mouse's own mouse down came before it was released.)
|
|
1502
|
+
const swallowed = pointerType === 'mouse'
|
|
1503
|
+
? ['mouseup', 'click']
|
|
1504
|
+
: ['mousedown', 'mouseup', 'click'];
|
|
1505
|
+
for (const type of swallowed) {
|
|
1506
|
+
window.addEventListener(type, swallow, { capture: true, once: true });
|
|
1507
|
+
}
|
|
1508
|
+
window.setTimeout(() => {
|
|
1509
|
+
for (const type of swallowed) {
|
|
1510
|
+
window.removeEventListener(type, swallow, { capture: true });
|
|
1511
|
+
}
|
|
1512
|
+
}, 400);
|
|
1513
|
+
};
|
|
1514
|
+
/**
|
|
1515
|
+
* A tile filling its area resized (see `resizeTarget`): to a size, and
|
|
1516
|
+
* its area grown along if the size's past it (the smallest supported area
|
|
1517
|
+
* it fits).
|
|
1518
|
+
*/
|
|
1519
|
+
const growingArea = (item, columns, [areaColumns, areaRows]) => (next, details) => {
|
|
1520
|
+
item.requestSize(next, details);
|
|
1521
|
+
const [c, r] = spanOf(next, columns);
|
|
1522
|
+
if (c <= areaColumns && r <= areaRows)
|
|
1523
|
+
return;
|
|
1524
|
+
const grown = item.supportedAreas.find((each) => {
|
|
1525
|
+
const [ac, ar] = spanOf(each, columns);
|
|
1526
|
+
return ac >= c && ar >= r;
|
|
1527
|
+
});
|
|
1528
|
+
if (grown)
|
|
1529
|
+
item.requestArea(grown, details);
|
|
1530
|
+
};
|
|
1531
|
+
/**
|
|
1532
|
+
* What a resize handle resizes (see `ResizeHandle`): its item's area, or
|
|
1533
|
+
* its size; within an area of its own, bounded by it (outlined on its
|
|
1534
|
+
* tile, changing no layout). Its sizes to go to: those within its bounds
|
|
1535
|
+
* (the columns, the grid's `maxTileRows`; an area's). A tile filling its
|
|
1536
|
+
* area (its handle the one shown there, see the CSS): the tile resized
|
|
1537
|
+
* first, grown past the area with it (the smallest supported area its
|
|
1538
|
+
* size fits), shrunk alone (room opening around it, the area's handle
|
|
1539
|
+
* shown again).
|
|
1540
|
+
*/
|
|
1541
|
+
const resizeTarget = (item, isAreaHandle, { columns, maxTileRows }) => {
|
|
1542
|
+
if (!item) {
|
|
1543
|
+
return {
|
|
1544
|
+
isBounded: false,
|
|
1545
|
+
isFree: false,
|
|
1546
|
+
most: [columns, maxTileRows],
|
|
1547
|
+
request: undefined,
|
|
1548
|
+
resized: undefined,
|
|
1549
|
+
sizes: [],
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1552
|
+
const isBounded = !isAreaHandle && item.hasArea;
|
|
1553
|
+
const [areaColumns, areaRows] = spanOf(item.area, columns);
|
|
1554
|
+
const [sizeColumns, sizeRows] = spanOf(item.size, columns);
|
|
1555
|
+
const isFilling = isBounded && sizeColumns === areaColumns && sizeRows === areaRows;
|
|
1556
|
+
const most = isBounded && !isFilling ? [areaColumns, areaRows] : [columns, maxTileRows];
|
|
1557
|
+
const sizes = (isAreaHandle ? item.supportedAreas : item.supportedSizes).filter((size) => {
|
|
1558
|
+
const [c, r] = spanOf(size, columns);
|
|
1559
|
+
return c <= most[0] && r <= most[1];
|
|
1560
|
+
});
|
|
1561
|
+
let request = isAreaHandle ? item.requestArea : item.requestSize;
|
|
1562
|
+
if (isFilling)
|
|
1563
|
+
request = growingArea(item, columns, [areaColumns, areaRows]);
|
|
1564
|
+
return {
|
|
1565
|
+
isBounded,
|
|
1566
|
+
isFree: isAreaHandle ? item.isFreeArea : item.isFreeSize,
|
|
1567
|
+
most,
|
|
1568
|
+
request,
|
|
1569
|
+
resized: isAreaHandle ? item.area : item.size,
|
|
1570
|
+
sizes,
|
|
1571
|
+
};
|
|
1572
|
+
};
|
|
1573
|
+
/**
|
|
1574
|
+
* Resizing its item (or spacer, break), editing (an edit control): its
|
|
1575
|
+
* size (within its area, if it has one: nothing reflowing, nothing taken
|
|
1576
|
+
* in; dragged out of the area, its next size up at least), or in a
|
|
1577
|
+
* `Grid.Area` the area (in the area's corner); pressed by a key or tapped, its
|
|
1578
|
+
* next supported size fitting the columns (after the largest, the
|
|
1579
|
+
* smallest; a key's, unless its `onClick` prevents it), held without
|
|
1580
|
+
* moving, nothing; dragged, grown or shrunk by as many units as it's
|
|
1581
|
+
* dragged (the supported size closest, fitted to the columns), a span
|
|
1582
|
+
* changing once it's dragged a little past halfway. Outlined while
|
|
1583
|
+
* held (its size as it is first), the grid reflowing once it's released
|
|
1584
|
+
* (cancelled: not at all); grown over spacers and breaks, taking them in
|
|
1585
|
+
* (see `mergedBy`; marked while held; the app's to remove, see
|
|
1586
|
+
* `GridSizeChangeDetails`). E.g. a card's corner (`render`).
|
|
1587
|
+
*/
|
|
1588
|
+
const ResizeHandle = forwardRef(({ className, onClick, render, ...props }, ref) => {
|
|
1589
|
+
const { columns, maxTileRows } = useContext(GridContext);
|
|
1590
|
+
const item = useContext(GridItemContext);
|
|
1591
|
+
// (In a `Grid.Area`: the area's, in its cell (see `Area`).)
|
|
1592
|
+
const isAreaHandle = useContext(GridAreaScopeContext);
|
|
1593
|
+
const { isBounded, isFree, most, request, resized, sizes } = resizeTarget(item, isAreaHandle, { columns, maxTileRows });
|
|
1594
|
+
const drag = useRef(undefined);
|
|
1595
|
+
// (A tile resized in its area by a drag: where its outline showed it,
|
|
1596
|
+
// at once, not gliding there (see layout.ts); once it's laid out.)
|
|
1597
|
+
const placed = useRef(undefined);
|
|
1598
|
+
useLayoutEffect(() => {
|
|
1599
|
+
const body = placed.current;
|
|
1600
|
+
placed.current = undefined;
|
|
1601
|
+
if (!body)
|
|
1602
|
+
return;
|
|
1603
|
+
requestAnimationFrame(() => {
|
|
1604
|
+
delete body.dataset.gridPlaced;
|
|
1605
|
+
});
|
|
1606
|
+
}, [resized]);
|
|
1607
|
+
const sizeIndex = resized ? sizes.indexOf(resized) : -1;
|
|
1608
|
+
// (The next size, round to the first.)
|
|
1609
|
+
const following = resized && (sizes[(sizeIndex + 1) % sizes.length] ?? resized);
|
|
1610
|
+
// (Only larger: grows; only smaller: shrinks; by their measures, a full
|
|
1611
|
+
// width larger than as wide as the columns, see `measureOf`.)
|
|
1612
|
+
const own = resized ? measureOf(resized, columns) : 0;
|
|
1613
|
+
const canGrow = sizes.some((each) => measureOf(each, columns) > own);
|
|
1614
|
+
const canShrink = sizes.some((each) => measureOf(each, columns) < own);
|
|
1615
|
+
// (No list given, either way to go: free, the arc alone; at an end of
|
|
1616
|
+
// what it can be (e.g. a full width, its most rows), one way only.)
|
|
1617
|
+
let direction = isFree ? 'free' : 'both';
|
|
1618
|
+
if (!canGrow && !canShrink)
|
|
1619
|
+
direction = 'fixed';
|
|
1620
|
+
else if (!canShrink)
|
|
1621
|
+
direction = 'grow';
|
|
1622
|
+
else if (!canGrow)
|
|
1623
|
+
direction = 'shrink';
|
|
1624
|
+
const onPointerDown = (event) => {
|
|
1625
|
+
if (!item?.editing || !resized || !event.isPrimary)
|
|
1626
|
+
return;
|
|
1627
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
1628
|
+
// (A resize before not taken: gliding again.)
|
|
1629
|
+
if (placed.current)
|
|
1630
|
+
delete placed.current.dataset.gridPlaced;
|
|
1631
|
+
placed.current = undefined;
|
|
1632
|
+
const cell = event.currentTarget.closest(isBounded
|
|
1633
|
+
? '[data-grid-part="body"]'
|
|
1634
|
+
: '[data-grid-part="item"], [data-grid-part="spacer"], [data-grid-part="break"]');
|
|
1635
|
+
const reach = cell && !isBounded && cell.dataset.gridPart !== 'break'
|
|
1636
|
+
? reachOf(cell, resized, sizes, event.clientX)
|
|
1637
|
+
: undefined;
|
|
1638
|
+
// (A tile in a full-width area that may be a full width: its area's
|
|
1639
|
+
// end-of-line column one column more, a full width; see
|
|
1640
|
+
// `onPointerMove`.)
|
|
1641
|
+
const canFull = isBounded &&
|
|
1642
|
+
Object.hasOwn(cell?.parentElement?.dataset ?? {}, 'gridFull') &&
|
|
1643
|
+
sizes.some((each) => isFull(each));
|
|
1644
|
+
drag.current = {
|
|
1645
|
+
bounds: cell && isBounded ? boundsOf(cell) : undefined,
|
|
1646
|
+
canFull,
|
|
1647
|
+
cell,
|
|
1648
|
+
from: spanDragged(resized, reach, columns, canFull),
|
|
1649
|
+
id: event.pointerId,
|
|
1650
|
+
isDragging: false,
|
|
1651
|
+
merged: [],
|
|
1652
|
+
reach,
|
|
1653
|
+
span: spanDragged(resized, reach, columns, canFull),
|
|
1654
|
+
start: [event.clientX, event.clientY],
|
|
1655
|
+
time: event.timeStamp,
|
|
1656
|
+
};
|
|
1657
|
+
// (Outlined from the touch on: its size, as it is.)
|
|
1658
|
+
if (drag.current.cell) {
|
|
1659
|
+
showResize(drag.current.cell, resized, columns);
|
|
1660
|
+
}
|
|
1661
|
+
};
|
|
1662
|
+
const onPointerMove = (event) => {
|
|
1663
|
+
const { current } = drag;
|
|
1664
|
+
if (!current || current.id !== event.pointerId || !item || !resized) {
|
|
1665
|
+
return;
|
|
1666
|
+
}
|
|
1667
|
+
const at = [event.clientX, event.clientY];
|
|
1668
|
+
if (!current.isDragging &&
|
|
1669
|
+
Math.hypot(at[0] - current.start[0], at[1] - current.start[1]) < slop) {
|
|
1670
|
+
return;
|
|
1671
|
+
}
|
|
1672
|
+
// (Its root too, once it's dragged (not at the touch's start): what's
|
|
1673
|
+
// in the grid may show itself while one's resized, see Card.)
|
|
1674
|
+
if (!current.isDragging)
|
|
1675
|
+
markResizing(current.cell, true);
|
|
1676
|
+
current.isDragging = true;
|
|
1677
|
+
const { reach } = current;
|
|
1678
|
+
// (Its area's end-of-line column a full width (see `onPointerDown`);
|
|
1679
|
+
// past its bottom only, its next size up.)
|
|
1680
|
+
const { canFull } = current;
|
|
1681
|
+
const span = draggedSpan(current, at, reach?.metrics ?? metricsOf(event.currentTarget), [
|
|
1682
|
+
canFull ? columns + 1 : limitOf(reach, most[0] ?? columns, columns),
|
|
1683
|
+
most[1] ?? maxTileRows,
|
|
1684
|
+
]);
|
|
1685
|
+
const isPast = pastOf(current, at);
|
|
1686
|
+
if (span[0] === current.span[0] &&
|
|
1687
|
+
span[1] === current.span[1] &&
|
|
1688
|
+
isPast === current.isPast) {
|
|
1689
|
+
return;
|
|
1690
|
+
}
|
|
1691
|
+
current.span = span;
|
|
1692
|
+
current.isPast = isPast;
|
|
1693
|
+
const next = draggedSize(sizes, resized, span, {
|
|
1694
|
+
canFull,
|
|
1695
|
+
columns,
|
|
1696
|
+
isPast,
|
|
1697
|
+
reach,
|
|
1698
|
+
});
|
|
1699
|
+
current.next = next;
|
|
1700
|
+
if (next) {
|
|
1701
|
+
outlineResize(current, next, {
|
|
1702
|
+
columns,
|
|
1703
|
+
isTaking: next !== resized && !isBounded,
|
|
1704
|
+
});
|
|
1705
|
+
}
|
|
1706
|
+
};
|
|
1707
|
+
// (Released: its size to be, requested; cancelled, none.)
|
|
1708
|
+
const onPointerEnd = (event) => {
|
|
1709
|
+
const { current } = drag;
|
|
1710
|
+
if (current?.id !== event.pointerId)
|
|
1711
|
+
return;
|
|
1712
|
+
drag.current = undefined;
|
|
1713
|
+
// (Its own mouse events too, dragged or not: a pointer's press alone
|
|
1714
|
+
// changes nothing, see `onClick`.)
|
|
1715
|
+
swallowClick(event.pointerType);
|
|
1716
|
+
if (current.cell)
|
|
1717
|
+
showResize(current.cell, undefined);
|
|
1718
|
+
if (current.isDragging)
|
|
1719
|
+
markResizing(current.cell, false);
|
|
1720
|
+
markMerged(current.merged, false);
|
|
1721
|
+
// (Tapped, not dragged: the next size, as by a key; also where
|
|
1722
|
+
// there's no room to drag it. Held, not dragged: nothing.)
|
|
1723
|
+
if (event.type === 'pointerup' && !current.isDragging) {
|
|
1724
|
+
if (following && event.timeStamp - current.time < tapTime) {
|
|
1725
|
+
request?.(following);
|
|
1726
|
+
}
|
|
1727
|
+
return;
|
|
1728
|
+
}
|
|
1729
|
+
if (event.type === 'pointerup' &&
|
|
1730
|
+
current.next &&
|
|
1731
|
+
current.next !== resized) {
|
|
1732
|
+
if (isBounded && current.cell) {
|
|
1733
|
+
current.cell.dataset.gridPlaced = '';
|
|
1734
|
+
placed.current = current.cell;
|
|
1735
|
+
}
|
|
1736
|
+
request?.(current.next, {
|
|
1737
|
+
merged: current.merged
|
|
1738
|
+
.map(({ dataset }) => dataset.gridName)
|
|
1739
|
+
.filter((name) => name !== undefined),
|
|
1740
|
+
});
|
|
1741
|
+
}
|
|
1742
|
+
};
|
|
1743
|
+
// (Only with another size to be; shown while editing.)
|
|
1744
|
+
if (sizes.length < 2)
|
|
1745
|
+
return null;
|
|
1746
|
+
return (_jsx(CornerButton, { ref: ref, mirrored: true, "aria-label": "Resize", className: cn('uikit-grid-resize-handle', className), corner: "bottom-trailing", "data-direction": direction, "data-grid-area-handle": isAreaHandle ? '' : undefined, "data-grid-edit-control": "", "data-grid-part": "resize-handle", icon: resizeIcons[direction], render: render, visible: item?.editing ?? false, ...props, onLostPointerCapture: onPointerEnd, onPointerCancel: onPointerEnd, onPointerDown: onPointerDown, onPointerMove: onPointerMove, onPointerUp: onPointerEnd, onClick: (event) => {
|
|
1747
|
+
onClick?.(event);
|
|
1748
|
+
// (Pressed by a key: the next size fitting the columns. By a
|
|
1749
|
+
// pointer: see `onPointerEnd`.)
|
|
1750
|
+
if (event.defaultPrevented || !following || event.detail !== 0) {
|
|
1751
|
+
return;
|
|
1752
|
+
}
|
|
1753
|
+
request?.(following);
|
|
1754
|
+
} }));
|
|
1755
|
+
});
|
|
1756
|
+
/**
|
|
1757
|
+
* Removing its item (or spacer, break), editing (an edit control): an x
|
|
1758
|
+
* in its top leading corner, drawn as a `ResizeHandle` is, a 44px target;
|
|
1759
|
+
* what it removes is up to its `onClick` (the app's data: in a
|
|
1760
|
+
* `Grid.Area`, the area, in the area's corner). Dragged from (held first,
|
|
1761
|
+
* by touch), its cell's moved instead, not removed. E.g. a card's corner
|
|
1762
|
+
* (`render`).
|
|
1763
|
+
*/
|
|
1764
|
+
const RemoveButton = forwardRef(({ className, render, ...props }, ref) => {
|
|
1765
|
+
const item = useContext(GridItemContext);
|
|
1766
|
+
const isArea = useContext(GridAreaScopeContext);
|
|
1767
|
+
return (_jsx(CornerButton, { ref: ref, "aria-label": "Remove", className: cn('uikit-grid-remove-button', className), corner: "top-leading", "data-grid-area-button": isArea ? '' : undefined, "data-grid-drag-through": "", "data-grid-edit-control": "", "data-grid-part": "remove-button", icon: gridIcons.remove, render: render, visible: item?.editing ?? false, ...props }));
|
|
1768
|
+
});
|
|
1769
|
+
/**
|
|
1770
|
+
* The area a tile gets added (see `AreaButton`): a column wider, as tall;
|
|
1771
|
+
* a full width's (or one as wide as the columns), a row taller; the
|
|
1772
|
+
* nearest larger supported area holding it if that's not supported (the
|
|
1773
|
+
* smallest); none, none.
|
|
1774
|
+
*/
|
|
1775
|
+
const addedAreaOf = (size, supported, { columns, maxTileRows }) => {
|
|
1776
|
+
const [c, r] = spanOf(size, columns);
|
|
1777
|
+
const isWide = isFull(size) || c >= columns;
|
|
1778
|
+
if (isWide && r >= maxTileRows)
|
|
1779
|
+
return undefined;
|
|
1780
|
+
const wanted = isWide
|
|
1781
|
+
? `${isFull(size) ? '*' : String(c)}x${String(r + 1)}`
|
|
1782
|
+
: `${String(c + 1)}x${String(r)}`;
|
|
1783
|
+
if (supported.includes(wanted))
|
|
1784
|
+
return wanted;
|
|
1785
|
+
return supported.find((each) => {
|
|
1786
|
+
const [ac, ar] = spanOf(each, columns);
|
|
1787
|
+
return (ac >= c &&
|
|
1788
|
+
ar >= r &&
|
|
1789
|
+
ac <= columns &&
|
|
1790
|
+
ar <= maxTileRows &&
|
|
1791
|
+
(ac > c || ar > r) &&
|
|
1792
|
+
(!isFull(size) || isFull(each)));
|
|
1793
|
+
});
|
|
1794
|
+
};
|
|
1795
|
+
/**
|
|
1796
|
+
* Adding an area to its item, editing (an edit control): three diagonal
|
|
1797
|
+
* strokes (the hatching's) in its tile's bottom leading corner, drawn as
|
|
1798
|
+
* a `RemoveButton` is; only without an area (a `Grid.Area`), and with one
|
|
1799
|
+
* to add (see `addedAreaOf`, of its `supportedSizes`): pressed, its
|
|
1800
|
+
* `onAdd` given that size (the app's to render a `Grid.Area` of it; unless
|
|
1801
|
+
* its `onClick` prevents it).
|
|
1802
|
+
*/
|
|
1803
|
+
const AreaButton = forwardRef(({ className, onAdd, onClick, render, supportedSizes, ...props }, ref) => {
|
|
1804
|
+
const { columns, maxTileRows } = useContext(GridContext);
|
|
1805
|
+
const item = useContext(GridItemContext);
|
|
1806
|
+
const supported = useMemo(() => supportedOf(supportedSizes, columns, maxTileRows), [columns, maxTileRows, supportedSizes]);
|
|
1807
|
+
const added = item && !item.hasArea
|
|
1808
|
+
? addedAreaOf(item.size, supported, { columns, maxTileRows })
|
|
1809
|
+
: undefined;
|
|
1810
|
+
// (Only with an area to add; shown while editing.)
|
|
1811
|
+
if (!added)
|
|
1812
|
+
return null;
|
|
1813
|
+
return (_jsx(CornerButton, { ref: ref, mirrored: true, "aria-label": "Add area", className: cn('uikit-grid-area-button', className), corner: "bottom-leading", "data-grid-drag-through": "", "data-grid-edit-control": "", "data-grid-part": "area-button", icon: gridIcons.addArea, render: render, visible: item?.editing ?? false, ...props, onClick: (event) => {
|
|
1814
|
+
onClick?.(event);
|
|
1815
|
+
if (event.defaultPrevented)
|
|
1816
|
+
return;
|
|
1817
|
+
onAdd?.(added);
|
|
1818
|
+
} }));
|
|
1819
|
+
});
|
|
1820
|
+
// A break's sizes (see `Break`): a unit wide, its rows tall (up to the
|
|
1821
|
+
// grid's `maxTileRows`).
|
|
1822
|
+
const breakSizesOf = (rows) => Array.from({ length: Math.max(1, rows) }, (_, index) => `1x${String(index + 1)}`);
|
|
1823
|
+
/**
|
|
1824
|
+
* The next item starts a new row (see the top); of more rows, the rows
|
|
1825
|
+
* below it kept empty too, the next item starting below them (see
|
|
1826
|
+
* layout.ts). Editing: a cell from where it is to its row's end (a whole
|
|
1827
|
+
* row after a full one), as tall as its rows, outlined as a spacer, what's
|
|
1828
|
+
* in it shown (e.g. a control removing it, a `ResizeHandle`: its rows),
|
|
1829
|
+
* moved as items are (named).
|
|
1830
|
+
*/
|
|
1831
|
+
const Break = forwardRef(({ children, className, defaultRows = 1, dropName, onRowsChange, reorderHandle, rows: controlledRows, style, ...props }, forwardedRef) => {
|
|
1832
|
+
const ref = useForwardedRef(forwardedRef);
|
|
1833
|
+
const { editing, maxTileRows } = useContext(GridContext);
|
|
1834
|
+
const entry = useCellEntry(ref, reorderHandle, dropName);
|
|
1835
|
+
const [ownRows, setOwnRows] = useState(defaultRows);
|
|
1836
|
+
const rows = controlledRows ?? ownRows;
|
|
1837
|
+
// (Its rows as a size, for a `ResizeHandle`: a unit wide.)
|
|
1838
|
+
const requestSize = useCallback((size, details) => {
|
|
1839
|
+
const next = spanOf(size)[1];
|
|
1840
|
+
setOwnRows(next);
|
|
1841
|
+
onRowsChange?.(next, details ?? noneMerged);
|
|
1842
|
+
}, [onRowsChange]);
|
|
1843
|
+
const state = useMemo(() => {
|
|
1844
|
+
const size = `1x${rows}`;
|
|
1845
|
+
const breakSizes = breakSizesOf(maxTileRows);
|
|
1846
|
+
// (Its rows as a size; no area of its own.)
|
|
1847
|
+
return {
|
|
1848
|
+
area: size,
|
|
1849
|
+
editing,
|
|
1850
|
+
hasArea: false,
|
|
1851
|
+
isFitted: false,
|
|
1852
|
+
// (Any of its rows: free.)
|
|
1853
|
+
isFreeArea: true,
|
|
1854
|
+
isFreeSize: true,
|
|
1855
|
+
movable: false,
|
|
1856
|
+
open: false,
|
|
1857
|
+
openSize: size,
|
|
1858
|
+
placement: 'center',
|
|
1859
|
+
requestArea: requestSize,
|
|
1860
|
+
requested: size,
|
|
1861
|
+
requestPlacement: noPlacement,
|
|
1862
|
+
requestSize,
|
|
1863
|
+
setOpen: noOpen,
|
|
1864
|
+
size,
|
|
1865
|
+
// (Its rows, as asked: a unit wide, never fitted.)
|
|
1866
|
+
sizeOf: (next) => ({
|
|
1867
|
+
isFitted: false,
|
|
1868
|
+
size: `1x${String(spanOf(next)[1])}`,
|
|
1869
|
+
}),
|
|
1870
|
+
supportedAreas: breakSizes,
|
|
1871
|
+
supportedSizes: breakSizes,
|
|
1872
|
+
};
|
|
1873
|
+
}, [editing, maxTileRows, requestSize, rows]);
|
|
1874
|
+
return (_jsx(GridItemContext.Provider, { value: state, children: _jsxs("div", { ref: ref, "aria-hidden": editing ? undefined : true, "data-grid-part": "break", "data-grid-rows": rows, style: withStyle({ '--uikit-grid-break-rows': String(rows) }, style), ...entry, ...props, className: cn('uikit-grid-break', className), children: [children, _jsx("span", { "aria-hidden": true, className: "uikit-grid-break-sign" }), moveSignOf(entry)] }) }));
|
|
1875
|
+
});
|
|
1876
|
+
export const Grid = {
|
|
1877
|
+
Root,
|
|
1878
|
+
Section,
|
|
1879
|
+
DropGroup,
|
|
1880
|
+
Header,
|
|
1881
|
+
Item,
|
|
1882
|
+
Spacer,
|
|
1883
|
+
Spawn,
|
|
1884
|
+
Break,
|
|
1885
|
+
CornerButton,
|
|
1886
|
+
ResizeHandle,
|
|
1887
|
+
RemoveButton,
|
|
1888
|
+
Area,
|
|
1889
|
+
AreaButton,
|
|
1890
|
+
createReorderHandle,
|
|
1891
|
+
reorder,
|
|
1892
|
+
useCreateReorderHandle,
|
|
1893
|
+
};
|
|
1894
|
+
//# sourceMappingURL=main.js.map
|