@datalayer/primer-addons 1.0.39 → 1.0.40
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/lib/components/box/boxCss.js +17 -1
- package/lib/components/box/boxTokens.d.ts +2 -1
- package/lib/components/box/boxTokens.js +2 -1
- package/lib/components/card/Card.d.ts +119 -5
- package/lib/components/card/Card.js +60 -46
- package/lib/components/card/Card.stories.d.ts +6 -0
- package/lib/components/card/Card.stories.js +26 -0
- package/lib/components/entity-avatar/EntityAvatar.d.ts +73 -0
- package/lib/components/entity-avatar/EntityAvatar.js +63 -0
- package/lib/components/entity-avatar/index.d.ts +1 -0
- package/lib/components/entity-avatar/index.js +5 -0
- package/lib/components/hover-card/HoverCard.d.ts +63 -0
- package/lib/components/hover-card/HoverCard.js +55 -0
- package/lib/components/hover-card/HoverCardAnchor.d.ts +33 -0
- package/lib/components/hover-card/HoverCardAnchor.js +26 -0
- package/lib/components/hover-card/HoverCardDetails.d.ts +36 -0
- package/lib/components/hover-card/HoverCardDetails.js +65 -0
- package/lib/components/hover-card/HoverCardLayer.d.ts +34 -0
- package/lib/components/hover-card/HoverCardLayer.js +57 -0
- package/lib/components/hover-card/index.d.ts +12 -0
- package/lib/components/hover-card/index.js +16 -0
- package/lib/components/hover-card/useHoverCard.d.ts +57 -0
- package/lib/components/hover-card/useHoverCard.js +200 -0
- package/lib/components/hover-card/usePrefersReducedMotion.d.ts +10 -0
- package/lib/components/hover-card/usePrefersReducedMotion.js +28 -0
- package/lib/components/index.d.ts +2 -0
- package/lib/components/index.js +2 -0
- package/lib/theme/css/createThemeCSSVars.d.ts +18 -1
- package/lib/theme/css/createThemeCSSVars.js +17 -1
- package/lib/theme/css/primerComponentsCss.d.ts +47 -0
- package/lib/theme/css/primerComponentsCss.js +72 -0
- package/lib/theme/index.d.ts +1 -0
- package/lib/theme/index.js +1 -0
- package/lib/theme/themes/loopThemeStyles.d.ts +4 -31
- package/lib/theme/themes/loopThemeStyles.js +17 -44
- package/package.json +1 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { IconButton, Text } from '@primer/react';
|
|
3
|
+
import { ChevronDownIcon } from '@primer/octicons-react';
|
|
4
|
+
import { Box } from '../box/Box';
|
|
5
|
+
import { Card } from '../card/Card';
|
|
6
|
+
import { HOVER_CARD_MOTION_MS } from './useHoverCard';
|
|
7
|
+
import { usePrefersReducedMotion } from './usePrefersReducedMotion';
|
|
8
|
+
/**
|
|
9
|
+
* How much bigger than its anchor the hover card is.
|
|
10
|
+
*
|
|
11
|
+
* Enough that the growth is visible and the artwork gets room; not so much
|
|
12
|
+
* that it stops reading as the same object enlarged.
|
|
13
|
+
*/
|
|
14
|
+
export const HOVER_CARD_SCALE = 1.45;
|
|
15
|
+
export function HoverCard({ anchorWidth, scale = HOVER_CARD_SCALE, closing = false, picture, onOpen, openLabel, actions, onExpand, expandLabel = 'More', title, meta, description, children, }) {
|
|
16
|
+
const reduced = usePrefersReducedMotion();
|
|
17
|
+
const width = Math.round(anchorWidth * scale);
|
|
18
|
+
const pictureHeight = Math.round(width * 0.5);
|
|
19
|
+
const from = (anchorWidth / width).toFixed(3);
|
|
20
|
+
return (_jsxs(Card, { border: true, shadow: "extraLarge", bg: "canvas.overlay", width: width, maxWidth: "92vw", overflow: "hidden", textAlign: "left",
|
|
21
|
+
/*
|
|
22
|
+
* Grown, not faded in. The card starts at the size of the one it
|
|
23
|
+
* covers and settles at its own, which is what makes it read as the
|
|
24
|
+
* same object rather than a second one arriving on top.
|
|
25
|
+
*/
|
|
26
|
+
transformOrigin: "center", animation: reduced
|
|
27
|
+
? undefined
|
|
28
|
+
: `${closing ? 'dlaHoverCardShrink' : 'dlaHoverCardGrow'} ${HOVER_CARD_MOTION_MS}ms ease-out both`, sx: {
|
|
29
|
+
'@keyframes dlaHoverCardGrow': {
|
|
30
|
+
from: { transform: `scale(${from})`, opacity: 0 },
|
|
31
|
+
to: { transform: 'scale(1)', opacity: 1 },
|
|
32
|
+
},
|
|
33
|
+
// The entrance backwards, at the same speed, ending on the card it
|
|
34
|
+
// grew from rather than simply disappearing.
|
|
35
|
+
'@keyframes dlaHoverCardShrink': {
|
|
36
|
+
from: { transform: 'scale(1)', opacity: 1 },
|
|
37
|
+
to: { transform: `scale(${from})`, opacity: 0 },
|
|
38
|
+
},
|
|
39
|
+
}, children: [picture ? (onOpen ? (_jsx(Box, { all: "unset", as: "button", type: "button", onClick: onOpen, "aria-label": openLabel, display: "block", width: "100%", cursor: "pointer", focusVisible: { outline: '2px solid', outlineColor: 'accent.emphasis', outlineOffset: -2 }, children: picture(pictureHeight) })) : (picture(pictureHeight))) : null, _jsxs(Box, { p: 3, display: "grid", gap: 2, children: [actions || onExpand ? (_jsxs(Box, { display: "flex", alignItems: "center", gap: 2, children: [actions, _jsx(Box, { flexGrow: 1 }), onExpand ? (_jsx(IconButton, { icon: ChevronDownIcon, "aria-label": expandLabel, size: "small", variant: "invisible", sx: {
|
|
40
|
+
borderRadius: '50%',
|
|
41
|
+
border: '1px solid',
|
|
42
|
+
borderColor: 'border.default',
|
|
43
|
+
color: 'fg.muted',
|
|
44
|
+
'&:hover': { borderColor: 'fg.default', color: 'fg.default' },
|
|
45
|
+
}, onClick: onExpand })) : null] })) : null, _jsx(Text, { sx: { fontWeight: 600, fontSize: 2, lineHeight: 1.25 },
|
|
46
|
+
// The two-line clamp as a plain style: Primer's sx type has no
|
|
47
|
+
// vendor-prefixed properties, so in sx it fails the type check.
|
|
48
|
+
style: {
|
|
49
|
+
overflow: 'hidden',
|
|
50
|
+
display: '-webkit-box',
|
|
51
|
+
WebkitLineClamp: 2,
|
|
52
|
+
WebkitBoxOrient: 'vertical',
|
|
53
|
+
}, children: title }), meta, description ? (_jsx(Text, { sx: { fontSize: 0, color: 'fg.muted', lineHeight: 1.5 }, children: description })) : null, children] })] }));
|
|
54
|
+
}
|
|
55
|
+
export default HoverCard;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One item of a grid or a list, as something a card can grow out of.
|
|
3
|
+
*
|
|
4
|
+
* All it does is report the pointer and, with it, where the item currently
|
|
5
|
+
* is on screen — measured at the moment of the hover rather than kept in
|
|
6
|
+
* state, because the page scrolls and a rectangle read at render time is
|
|
7
|
+
* wrong by the time it is used.
|
|
8
|
+
*
|
|
9
|
+
* `mouseover`/`mouseout` reach here as React's `onMouseEnter`/`onMouseLeave`,
|
|
10
|
+
* which are synthesised from them and so do not fire again as the pointer
|
|
11
|
+
* moves between the item's own children — the distinction that makes a card
|
|
12
|
+
* flicker if it is got wrong.
|
|
13
|
+
*
|
|
14
|
+
* @module components/hover-card/HoverCardAnchor
|
|
15
|
+
*/
|
|
16
|
+
import type { JSX, ReactNode } from 'react';
|
|
17
|
+
import { type BoxProps } from '../box/Box';
|
|
18
|
+
import type { HoverCardRect, HoverCardState } from './useHoverCard';
|
|
19
|
+
export type HoverCardAnchorProps = BoxProps & {
|
|
20
|
+
/** The machine this item reports to. */
|
|
21
|
+
state: HoverCardState<HoverCardRect>;
|
|
22
|
+
/** What identifies this item to it. */
|
|
23
|
+
itemKey: string | number;
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* A grid cell by default, which is the shape that makes the wrapper
|
|
28
|
+
* invisible: a lone child of a grid stretches to fill it, so wrapping a
|
|
29
|
+
* card changes nothing about how tall it is or where it sits. Any `Box`
|
|
30
|
+
* prop changes how it sits in its parent.
|
|
31
|
+
*/
|
|
32
|
+
export declare function HoverCardAnchor({ state, itemKey, children, ...rest }: HoverCardAnchorProps): JSX.Element;
|
|
33
|
+
export default HoverCardAnchor;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useRef } from 'react';
|
|
3
|
+
import { Box } from '../box/Box';
|
|
4
|
+
/**
|
|
5
|
+
* A grid cell by default, which is the shape that makes the wrapper
|
|
6
|
+
* invisible: a lone child of a grid stretches to fill it, so wrapping a
|
|
7
|
+
* card changes nothing about how tall it is or where it sits. Any `Box`
|
|
8
|
+
* prop changes how it sits in its parent.
|
|
9
|
+
*/
|
|
10
|
+
export function HoverCardAnchor({ state, itemKey, children, ...rest }) {
|
|
11
|
+
const host = useRef(null);
|
|
12
|
+
return (_jsx(Box, { ref: host, onMouseEnter: () => {
|
|
13
|
+
const element = host.current;
|
|
14
|
+
if (!element) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const rect = element.getBoundingClientRect();
|
|
18
|
+
state.open(itemKey, {
|
|
19
|
+
left: rect.left,
|
|
20
|
+
top: rect.top,
|
|
21
|
+
width: rect.width,
|
|
22
|
+
height: rect.height,
|
|
23
|
+
});
|
|
24
|
+
}, onMouseLeave: state.scheduleClose, display: "grid", minWidth: 0, ...rest, children: children }));
|
|
25
|
+
}
|
|
26
|
+
export default HoverCardAnchor;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The large card: everything about one thing, over the page.
|
|
3
|
+
*
|
|
4
|
+
* Not a route: the reader asked for more about a card on a shelf, not to go
|
|
5
|
+
* somewhere. Closing it — the close button, a press on the veil, Escape —
|
|
6
|
+
* leaves them where they were, which is the difference between browsing and
|
|
7
|
+
* navigating.
|
|
8
|
+
*
|
|
9
|
+
* Its own element rather than Primer's `Dialog`, because a picture runs to
|
|
10
|
+
* the edges and under the close button, which a dialog with a header cannot
|
|
11
|
+
* do. Rendered into `document.body` through a portal: a `transform` on any
|
|
12
|
+
* ancestor (an entrance animation's `both` fill-mode is enough) makes
|
|
13
|
+
* `position: fixed` fix to that ancestor rather than to the viewport.
|
|
14
|
+
*
|
|
15
|
+
* It closes itself before it tells anybody: the exit plays, and only then
|
|
16
|
+
* does `onClose` run — unmounting on the click would be a disappearance, not
|
|
17
|
+
* a close. Drawn by the theme: a `Card` with the theme's corner (square to
|
|
18
|
+
* the screen on a narrow one, where it is the page), shadow and ground, on
|
|
19
|
+
* Primer's overlay backdrop.
|
|
20
|
+
*
|
|
21
|
+
* @module components/hover-card/HoverCardDetails
|
|
22
|
+
*/
|
|
23
|
+
import type { JSX, ReactNode } from 'react';
|
|
24
|
+
export type HoverCardDetailsProps = {
|
|
25
|
+
/** What the dialog is called, for assistive technology. */
|
|
26
|
+
label: string;
|
|
27
|
+
/** Called once the exit has played. */
|
|
28
|
+
onClose: () => void;
|
|
29
|
+
/** Its width, in pixels; it never exceeds the screen's. 880 by default. */
|
|
30
|
+
width?: number;
|
|
31
|
+
/** What the close button is called. */
|
|
32
|
+
closeLabel?: string;
|
|
33
|
+
children: ReactNode;
|
|
34
|
+
};
|
|
35
|
+
export declare function HoverCardDetails({ label, onClose, width, closeLabel, children, }: HoverCardDetailsProps): JSX.Element | null;
|
|
36
|
+
export default HoverCardDetails;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
|
+
import { IconButton } from '@primer/react';
|
|
5
|
+
import { XIcon } from '@primer/octicons-react';
|
|
6
|
+
import { Box } from '../box/Box';
|
|
7
|
+
import { Card } from '../card/Card';
|
|
8
|
+
import { HOVER_CARD_MOTION_MS } from './useHoverCard';
|
|
9
|
+
import { usePrefersReducedMotion } from './usePrefersReducedMotion';
|
|
10
|
+
export function HoverCardDetails({ label, onClose, width = 880, closeLabel = 'Close', children, }) {
|
|
11
|
+
const reduced = usePrefersReducedMotion();
|
|
12
|
+
const [leaving, setLeaving] = useState(false);
|
|
13
|
+
const dismiss = () => {
|
|
14
|
+
if (leaving) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (reduced) {
|
|
18
|
+
onClose();
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
setLeaving(true);
|
|
22
|
+
window.setTimeout(onClose, HOVER_CARD_MOTION_MS);
|
|
23
|
+
};
|
|
24
|
+
// Escape closes it, as it would any overlay.
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const onKey = (event) => {
|
|
27
|
+
if (event.key === 'Escape') {
|
|
28
|
+
dismiss();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
document.addEventListener('keydown', onKey);
|
|
32
|
+
return () => document.removeEventListener('keydown', onKey);
|
|
33
|
+
});
|
|
34
|
+
if (typeof document === 'undefined') {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return createPortal(_jsx(Box, { role: "presentation", onClick: dismiss, position: "fixed", inset: 0, zIndex: 1000, display: "flex", alignItems: "flex-start", justifyContent: "center", overflowY: "auto", p: [0, 3, 5], background: "var(--overlay-backdrop-bgColor)", animation: reduced
|
|
38
|
+
? undefined
|
|
39
|
+
: `${leaving ? 'dlaHoverCardVeilOut' : 'dlaHoverCardVeilIn'} ${HOVER_CARD_MOTION_MS}ms ease-out both`, sx: {
|
|
40
|
+
'@keyframes dlaHoverCardVeilIn': { from: { opacity: 0 }, to: { opacity: 1 } },
|
|
41
|
+
'@keyframes dlaHoverCardVeilOut': { from: { opacity: 1 }, to: { opacity: 0 } },
|
|
42
|
+
}, children: _jsxs(Card, { role: "dialog", "aria-modal": "true", "aria-label": label, onClick: (event) => event.stopPropagation(), shadow: "extraLarge", width: width, maxWidth: "100%", borderRadius: [0, 'card'], overflow: "hidden", position: "relative", display: "block",
|
|
43
|
+
// Rises into place and sinks back out, at the hover card's speed: the
|
|
44
|
+
// third size arrives the way the second one did.
|
|
45
|
+
animation: reduced
|
|
46
|
+
? undefined
|
|
47
|
+
: `${leaving ? 'dlaHoverCardPanelOut' : 'dlaHoverCardPanelIn'} ${HOVER_CARD_MOTION_MS}ms ease-out both`, sx: {
|
|
48
|
+
'@keyframes dlaHoverCardPanelIn': {
|
|
49
|
+
from: { transform: 'translateY(12px) scale(0.97)', opacity: 0 },
|
|
50
|
+
to: { transform: 'translateY(0) scale(1)', opacity: 1 },
|
|
51
|
+
},
|
|
52
|
+
'@keyframes dlaHoverCardPanelOut': {
|
|
53
|
+
from: { transform: 'translateY(0) scale(1)', opacity: 1 },
|
|
54
|
+
to: { transform: 'translateY(12px) scale(0.97)', opacity: 0 },
|
|
55
|
+
},
|
|
56
|
+
}, children: [_jsx(IconButton, { icon: XIcon, "aria-label": closeLabel, onClick: dismiss, sx: {
|
|
57
|
+
position: 'absolute',
|
|
58
|
+
top: 3,
|
|
59
|
+
right: 3,
|
|
60
|
+
zIndex: 2,
|
|
61
|
+
borderRadius: '50%',
|
|
62
|
+
bg: 'canvas.default',
|
|
63
|
+
} }), children] }) }), document.body);
|
|
64
|
+
}
|
|
65
|
+
export default HoverCardDetails;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a hover card goes when it is bigger than the thing it grew from.
|
|
3
|
+
*
|
|
4
|
+
* Two problems, both of which the obvious answer — an absolutely positioned
|
|
5
|
+
* element inside the card — gets wrong.
|
|
6
|
+
*
|
|
7
|
+
* The first is escape. A results grid clips, a marquee clips, a section
|
|
8
|
+
* animates; and `position: fixed` is fixed to the *viewport* only until some
|
|
9
|
+
* ancestor has a `transform`, at which point it fixes to that ancestor
|
|
10
|
+
* instead. An entrance animation with `both` fill-mode is enough to do it,
|
|
11
|
+
* for ever after it has played. A portal to `document.body` is immune to all
|
|
12
|
+
* of it, so that is where the card is put.
|
|
13
|
+
*
|
|
14
|
+
* The second is the edges. The card is wider and taller than the thing it
|
|
15
|
+
* covers, so one grown from the first column reaches past the left edge of
|
|
16
|
+
* the window and one at the bottom of the page reaches past the fold. It is
|
|
17
|
+
* measured once it is up and nudged back inside, which is why the position is
|
|
18
|
+
* state rather than a style computed on the way out: the layout has to exist
|
|
19
|
+
* before it can be corrected.
|
|
20
|
+
*
|
|
21
|
+
* @module components/hover-card/HoverCardLayer
|
|
22
|
+
*/
|
|
23
|
+
import type { JSX, ReactNode } from 'react';
|
|
24
|
+
import type { HoverCardRect } from './useHoverCard';
|
|
25
|
+
export type HoverCardLayerProps = {
|
|
26
|
+
anchor: HoverCardRect;
|
|
27
|
+
children: ReactNode;
|
|
28
|
+
/** Kept high enough to clear page furniture; under the details dialog. */
|
|
29
|
+
zIndex?: number;
|
|
30
|
+
onMouseEnter?: () => void;
|
|
31
|
+
onMouseLeave?: () => void;
|
|
32
|
+
};
|
|
33
|
+
export declare function HoverCardLayer({ anchor, children, zIndex, onMouseEnter, onMouseLeave }: HoverCardLayerProps): JSX.Element | null;
|
|
34
|
+
export default HoverCardLayer;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useLayoutEffect, useRef, useState } from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
|
+
/** How close to the window's edge the card may come. */
|
|
5
|
+
const MARGIN = 12;
|
|
6
|
+
export function HoverCardLayer({ anchor, children, zIndex = 900, onMouseEnter, onMouseLeave }) {
|
|
7
|
+
const box = useRef(null);
|
|
8
|
+
/** Centred on the anchor to start with, then corrected against the window. */
|
|
9
|
+
const [at, setAt] = useState(() => ({
|
|
10
|
+
left: anchor.left + anchor.width / 2,
|
|
11
|
+
top: anchor.top + anchor.height / 2
|
|
12
|
+
}));
|
|
13
|
+
useLayoutEffect(() => {
|
|
14
|
+
const element = box.current;
|
|
15
|
+
const centre = {
|
|
16
|
+
left: anchor.left + anchor.width / 2,
|
|
17
|
+
top: anchor.top + anchor.height / 2
|
|
18
|
+
};
|
|
19
|
+
if (!element) {
|
|
20
|
+
setAt(centre);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
/*
|
|
24
|
+
* The card's own size, not the anchor's.
|
|
25
|
+
*
|
|
26
|
+
* Read from the layout box rather than from the caller: the card decides
|
|
27
|
+
* how much bigger than its anchor it is, and a layer that had to be told
|
|
28
|
+
* would be one more thing to keep in step. `getBoundingClientRect` would
|
|
29
|
+
* report the *animated* size — the entrance starts at the anchor's scale
|
|
30
|
+
* — so the untransformed offsets are what is measured.
|
|
31
|
+
*/
|
|
32
|
+
const half = { x: element.offsetWidth / 2, y: element.offsetHeight / 2 };
|
|
33
|
+
const clamp = (value, half_, limit) => {
|
|
34
|
+
// A card too big for the window at all: centre it, rather than pinning
|
|
35
|
+
// it to one edge and letting the other run off.
|
|
36
|
+
if (half_ * 2 + MARGIN * 2 > limit) {
|
|
37
|
+
return limit / 2;
|
|
38
|
+
}
|
|
39
|
+
return Math.min(Math.max(value, half_ + MARGIN), limit - half_ - MARGIN);
|
|
40
|
+
};
|
|
41
|
+
setAt({
|
|
42
|
+
left: clamp(centre.left, half.x, window.innerWidth),
|
|
43
|
+
top: clamp(centre.top, half.y, window.innerHeight)
|
|
44
|
+
});
|
|
45
|
+
}, [anchor.left, anchor.top, anchor.width, anchor.height]);
|
|
46
|
+
if (typeof document === 'undefined') {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
return createPortal(_jsx("div", { ref: box, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, style: {
|
|
50
|
+
position: 'fixed',
|
|
51
|
+
left: at.left,
|
|
52
|
+
top: at.top,
|
|
53
|
+
transform: 'translate(-50%, -50%)',
|
|
54
|
+
zIndex
|
|
55
|
+
}, children: children }), document.body);
|
|
56
|
+
}
|
|
57
|
+
export default HoverCardLayer;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hover card, in parts that compose: `useHoverCard` is the timing,
|
|
3
|
+
* `HoverCardAnchor` the pair of pointer handlers a grid item needs,
|
|
4
|
+
* `HoverCardLayer` the placement, `HoverCard` the card grown out of the
|
|
5
|
+
* small one and `HoverCardDetails` the large one its chevron opens.
|
|
6
|
+
*/
|
|
7
|
+
export * from './useHoverCard';
|
|
8
|
+
export * from './usePrefersReducedMotion';
|
|
9
|
+
export * from './HoverCardLayer';
|
|
10
|
+
export * from './HoverCardAnchor';
|
|
11
|
+
export * from './HoverCard';
|
|
12
|
+
export * from './HoverCardDetails';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021-2026 Datalayer, Inc.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The hover card, in parts that compose: `useHoverCard` is the timing,
|
|
7
|
+
* `HoverCardAnchor` the pair of pointer handlers a grid item needs,
|
|
8
|
+
* `HoverCardLayer` the placement, `HoverCard` the card grown out of the
|
|
9
|
+
* small one and `HoverCardDetails` the large one its chevron opens.
|
|
10
|
+
*/
|
|
11
|
+
export * from './useHoverCard';
|
|
12
|
+
export * from './usePrefersReducedMotion';
|
|
13
|
+
export * from './HoverCardLayer';
|
|
14
|
+
export * from './HoverCardAnchor';
|
|
15
|
+
export * from './HoverCard';
|
|
16
|
+
export * from './HoverCardDetails';
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** The thing a hover card grew out of, in viewport coordinates. */
|
|
2
|
+
export type HoverCardRect = {
|
|
3
|
+
left: number;
|
|
4
|
+
top: number;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* How long the pointer must rest on something before its card opens.
|
|
10
|
+
*
|
|
11
|
+
* This is the wait that separates *reading a page* from *choosing something
|
|
12
|
+
* on it*: a card that opens while the eye is still travelling is an
|
|
13
|
+
* interruption, since the reader did not ask for it and has to wait for it
|
|
14
|
+
* to leave again. Set by feel, from the top of the range down — long enough
|
|
15
|
+
* that a pointer crossing the page is not chased by anything, short enough
|
|
16
|
+
* that stopping on something does not feel like waiting for it.
|
|
17
|
+
*/
|
|
18
|
+
export declare const HOVER_CARD_OPEN_DELAY_MS = 320;
|
|
19
|
+
/** How long a card waits, after the pointer leaves, before it starts out. */
|
|
20
|
+
export declare const HOVER_CARD_CLOSE_DELAY_MS = 320;
|
|
21
|
+
/**
|
|
22
|
+
* How long a card takes to arrive, and to leave.
|
|
23
|
+
*
|
|
24
|
+
* The same number both ways: a card that leaves faster than it came reads as
|
|
25
|
+
* being dismissed rather than closed. Whatever draws the card has to animate
|
|
26
|
+
* for exactly this long, because the card is kept mounted for exactly this
|
|
27
|
+
* long after it is told it is leaving.
|
|
28
|
+
*/
|
|
29
|
+
export declare const HOVER_CARD_MOTION_MS = 320;
|
|
30
|
+
export type HoverCardOptions = {
|
|
31
|
+
/**
|
|
32
|
+
* Whether the gesture is offered at all.
|
|
33
|
+
*
|
|
34
|
+
* A caller with nothing to preview passes `false` rather than skipping the
|
|
35
|
+
* hook, which it cannot do.
|
|
36
|
+
*/
|
|
37
|
+
enabled?: boolean;
|
|
38
|
+
openDelayMs?: number;
|
|
39
|
+
closeDelayMs?: number;
|
|
40
|
+
motionMs?: number;
|
|
41
|
+
};
|
|
42
|
+
export type HoverCardState<Placement> = {
|
|
43
|
+
/** Where to put the open card, or `null` when there is none. */
|
|
44
|
+
active: Placement | null;
|
|
45
|
+
/** Which item it is open for. */
|
|
46
|
+
activeKey: string | number | null;
|
|
47
|
+
/** Whether it is playing its exit, and so is still on screen. */
|
|
48
|
+
closing: boolean;
|
|
49
|
+
/** The pointer reached this item. */
|
|
50
|
+
open: (key: string | number, at: Placement) => void;
|
|
51
|
+
/** The pointer left it, or left the card. */
|
|
52
|
+
scheduleClose: () => void;
|
|
53
|
+
/** The pointer came back before the card was gone. */
|
|
54
|
+
cancelClose: () => void;
|
|
55
|
+
};
|
|
56
|
+
export declare function useHoverCard<Placement = HoverCardRect>({ enabled, openDelayMs, closeDelayMs, motionMs }?: HoverCardOptions): HoverCardState<Placement>;
|
|
57
|
+
export default useHoverCard;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021-2026 Datalayer, Inc.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Hovering a card to see more of it, as a state machine.
|
|
7
|
+
*
|
|
8
|
+
* The gesture is the one a shelf of films uses: rest the pointer on
|
|
9
|
+
* something and it grows into a larger card; move off and the card plays its
|
|
10
|
+
* entrance backwards. Simple to describe and full of edges, all of which are
|
|
11
|
+
* about *time* rather than about what is drawn — which is why they live here,
|
|
12
|
+
* in a hook that draws nothing, instead of being written again beside every
|
|
13
|
+
* grid that wants the gesture.
|
|
14
|
+
*
|
|
15
|
+
* The edges, and what each one is for:
|
|
16
|
+
*
|
|
17
|
+
* - **Opening waits.** Dragging the pointer across a row would otherwise
|
|
18
|
+
* open a card over every item on the way, and the reader is chased by
|
|
19
|
+
* something they never asked for. The wait is the difference between
|
|
20
|
+
* passing over an item and stopping on it.
|
|
21
|
+
* - **Closing waits too, then plays.** First a grace, because the pointer has
|
|
22
|
+
* to cross the gap into the card and a card that vanished in that gap could
|
|
23
|
+
* never be reached. Then the exit, during which the card is still mounted
|
|
24
|
+
* and knows it is leaving, so it can shrink rather than blink out.
|
|
25
|
+
* - **Leaving is not interruptible.** A card on its way out keeps the stage
|
|
26
|
+
* until it is gone: two movements at once — one shrinking, one growing —
|
|
27
|
+
* read as a glitch rather than as a change of subject. An item reached
|
|
28
|
+
* mid-exit is queued, and the exit hands over to it.
|
|
29
|
+
* - **Moving between items plays the exit.** Sliding the open card across to
|
|
30
|
+
* the new one instead skips the gesture entirely: it teleports, at full
|
|
31
|
+
* size, and the two cards never read as two. The grace is dropped there,
|
|
32
|
+
* though — a pointer that has landed on another item is plainly not
|
|
33
|
+
* heading for the card.
|
|
34
|
+
* - **Escape closes it.** A card opened by resting on something can otherwise
|
|
35
|
+
* only be closed by moving off, and a reader who wants back what it covers
|
|
36
|
+
* reaches for the key every overlay answers. It also calls off a card that
|
|
37
|
+
* has not appeared yet: "no" is the same answer either side of the wait.
|
|
38
|
+
*
|
|
39
|
+
* The hook knows nothing about where the card goes. Callers hand it whatever
|
|
40
|
+
* they need to place one — a coordinate inside a scrolling row, a rectangle
|
|
41
|
+
* in the viewport — and get it back in `active`.
|
|
42
|
+
*
|
|
43
|
+
* @module components/hover-card/useHoverCard
|
|
44
|
+
*/
|
|
45
|
+
import { useEffect, useRef, useState } from 'react';
|
|
46
|
+
/**
|
|
47
|
+
* How long the pointer must rest on something before its card opens.
|
|
48
|
+
*
|
|
49
|
+
* This is the wait that separates *reading a page* from *choosing something
|
|
50
|
+
* on it*: a card that opens while the eye is still travelling is an
|
|
51
|
+
* interruption, since the reader did not ask for it and has to wait for it
|
|
52
|
+
* to leave again. Set by feel, from the top of the range down — long enough
|
|
53
|
+
* that a pointer crossing the page is not chased by anything, short enough
|
|
54
|
+
* that stopping on something does not feel like waiting for it.
|
|
55
|
+
*/
|
|
56
|
+
export const HOVER_CARD_OPEN_DELAY_MS = 320;
|
|
57
|
+
/** How long a card waits, after the pointer leaves, before it starts out. */
|
|
58
|
+
export const HOVER_CARD_CLOSE_DELAY_MS = 320;
|
|
59
|
+
/**
|
|
60
|
+
* How long a card takes to arrive, and to leave.
|
|
61
|
+
*
|
|
62
|
+
* The same number both ways: a card that leaves faster than it came reads as
|
|
63
|
+
* being dismissed rather than closed. Whatever draws the card has to animate
|
|
64
|
+
* for exactly this long, because the card is kept mounted for exactly this
|
|
65
|
+
* long after it is told it is leaving.
|
|
66
|
+
*/
|
|
67
|
+
export const HOVER_CARD_MOTION_MS = 320;
|
|
68
|
+
export function useHoverCard({ enabled = true, openDelayMs = HOVER_CARD_OPEN_DELAY_MS, closeDelayMs = HOVER_CARD_CLOSE_DELAY_MS, motionMs = HOVER_CARD_MOTION_MS } = {}) {
|
|
69
|
+
const [opened, setOpened] = useState(null);
|
|
70
|
+
const [closing, setClosing] = useState(false);
|
|
71
|
+
const openTimer = useRef(null);
|
|
72
|
+
const closeTimer = useRef(null);
|
|
73
|
+
const exitTimer = useRef(null);
|
|
74
|
+
/**
|
|
75
|
+
* An item the pointer reached while a card was still leaving.
|
|
76
|
+
*
|
|
77
|
+
* A ref rather than state: nothing renders from it, and it is written and
|
|
78
|
+
* read inside timers that must see the latest value rather than the one
|
|
79
|
+
* captured when they were set.
|
|
80
|
+
*/
|
|
81
|
+
const queued = useRef(null);
|
|
82
|
+
const clear = (timer) => {
|
|
83
|
+
if (timer.current !== null) {
|
|
84
|
+
window.clearTimeout(timer.current);
|
|
85
|
+
timer.current = null;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const cancelClose = () => {
|
|
89
|
+
queued.current = null;
|
|
90
|
+
clear(closeTimer);
|
|
91
|
+
clear(exitTimer);
|
|
92
|
+
setClosing(false);
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* The exit: the entrance played backwards.
|
|
96
|
+
*
|
|
97
|
+
* The card stays mounted through it, marked `closing` so it knows to
|
|
98
|
+
* shrink rather than to grow, and only then is it taken off the screen —
|
|
99
|
+
* or handed over to whichever item the pointer found in the meantime.
|
|
100
|
+
*/
|
|
101
|
+
const beginExit = () => {
|
|
102
|
+
clear(closeTimer);
|
|
103
|
+
clear(exitTimer);
|
|
104
|
+
setClosing(true);
|
|
105
|
+
exitTimer.current = window.setTimeout(() => {
|
|
106
|
+
const next = queued.current;
|
|
107
|
+
queued.current = null;
|
|
108
|
+
setClosing(false);
|
|
109
|
+
// Straight into the next card if the pointer found one while this was
|
|
110
|
+
// leaving: the reader has already waited out the exit, and making them
|
|
111
|
+
// wait the hover intent again on top of it would read as being ignored.
|
|
112
|
+
setOpened(next ?? null);
|
|
113
|
+
}, motionMs);
|
|
114
|
+
};
|
|
115
|
+
const scheduleClose = () => {
|
|
116
|
+
/*
|
|
117
|
+
* The wait to open is called off, first of all.
|
|
118
|
+
*
|
|
119
|
+
* This is what makes the hover intent mean anything. Without it, a
|
|
120
|
+
* pointer that crossed an item and left again still had a timer running:
|
|
121
|
+
* it fired after the pointer had gone, opened a card over nothing, and
|
|
122
|
+
* the close that had been scheduled behind it took the card away again.
|
|
123
|
+
* Passing over a row of cards flashed one open at every item — the exact
|
|
124
|
+
* behaviour the wait exists to prevent, arriving a fifth of a second
|
|
125
|
+
* late.
|
|
126
|
+
*/
|
|
127
|
+
clear(openTimer);
|
|
128
|
+
/*
|
|
129
|
+
* A card already on its way out is left to finish.
|
|
130
|
+
*
|
|
131
|
+
* Without this, the pointer leaving whatever it moved to next would
|
|
132
|
+
* clear the exit timer and start the whole close again — the card would
|
|
133
|
+
* shrink, stop, and shrink once more. Leaving is not interruptible; only
|
|
134
|
+
* coming back cancels it, and that goes through `cancelClose`.
|
|
135
|
+
*/
|
|
136
|
+
queued.current = null;
|
|
137
|
+
if (closing) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
clear(closeTimer);
|
|
141
|
+
clear(exitTimer);
|
|
142
|
+
closeTimer.current = window.setTimeout(beginExit, closeDelayMs);
|
|
143
|
+
};
|
|
144
|
+
const open = (key, at) => {
|
|
145
|
+
if (!enabled) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const next = { key, at };
|
|
149
|
+
if (closing) {
|
|
150
|
+
queued.current = next;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (opened !== null) {
|
|
154
|
+
if (opened.key === key) {
|
|
155
|
+
// Back on the thing it is already open for: nothing to do but stay.
|
|
156
|
+
cancelClose();
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
queued.current = next;
|
|
160
|
+
beginExit();
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
cancelClose();
|
|
164
|
+
clear(openTimer);
|
|
165
|
+
openTimer.current = window.setTimeout(() => setOpened(next), openDelayMs);
|
|
166
|
+
};
|
|
167
|
+
useEffect(() => () => {
|
|
168
|
+
clear(openTimer);
|
|
169
|
+
clear(closeTimer);
|
|
170
|
+
clear(exitTimer);
|
|
171
|
+
}, []);
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
if (!enabled) {
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
const onKey = (event) => {
|
|
177
|
+
if (event.key !== 'Escape') {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
clear(openTimer);
|
|
181
|
+
if (opened !== null && !closing) {
|
|
182
|
+
beginExit();
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
document.addEventListener('keydown', onKey);
|
|
186
|
+
return () => document.removeEventListener('keydown', onKey);
|
|
187
|
+
// `beginExit` is rebuilt every render; what decides whether it should run
|
|
188
|
+
// is the state below, and the listener is re-attached when that changes.
|
|
189
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
190
|
+
}, [enabled, opened, closing]);
|
|
191
|
+
return {
|
|
192
|
+
active: opened?.at ?? null,
|
|
193
|
+
activeKey: opened?.key ?? null,
|
|
194
|
+
closing,
|
|
195
|
+
open,
|
|
196
|
+
scheduleClose,
|
|
197
|
+
cancelClose
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
export default useHoverCard;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the reader has asked for less movement.
|
|
3
|
+
*
|
|
4
|
+
* A hover card grows, and growing is the part somebody with vestibular
|
|
5
|
+
* trouble asked not to see. Everything else about it — that it appears,
|
|
6
|
+
* what it says — is unchanged; only the animation is dropped, and an exit
|
|
7
|
+
* that waited for an animation no longer waits.
|
|
8
|
+
*/
|
|
9
|
+
export declare function usePrefersReducedMotion(): boolean;
|
|
10
|
+
export default usePrefersReducedMotion;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021-2026 Datalayer, Inc.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
import { useEffect, useState } from 'react';
|
|
6
|
+
/**
|
|
7
|
+
* Whether the reader has asked for less movement.
|
|
8
|
+
*
|
|
9
|
+
* A hover card grows, and growing is the part somebody with vestibular
|
|
10
|
+
* trouble asked not to see. Everything else about it — that it appears,
|
|
11
|
+
* what it says — is unchanged; only the animation is dropped, and an exit
|
|
12
|
+
* that waited for an animation no longer waits.
|
|
13
|
+
*/
|
|
14
|
+
export function usePrefersReducedMotion() {
|
|
15
|
+
const [reduced, setReduced] = useState(false);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
const query = window.matchMedia?.('(prefers-reduced-motion: reduce)');
|
|
18
|
+
if (!query) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
setReduced(query.matches);
|
|
22
|
+
const listen = (event) => setReduced(event.matches);
|
|
23
|
+
query.addEventListener?.('change', listen);
|
|
24
|
+
return () => query.removeEventListener?.('change', listen);
|
|
25
|
+
}, []);
|
|
26
|
+
return reduced;
|
|
27
|
+
}
|
|
28
|
+
export default usePrefersReducedMotion;
|
|
@@ -9,6 +9,8 @@ export * from "./appearance/ThemeCircle";
|
|
|
9
9
|
export * from "./box/Box";
|
|
10
10
|
export * from "./calendar-picker";
|
|
11
11
|
export * from "./card/Card";
|
|
12
|
+
export * from "./hover-card";
|
|
13
|
+
export * from "./entity-avatar";
|
|
12
14
|
export * from "./sheet/Sheet";
|
|
13
15
|
export * from "./color";
|
|
14
16
|
export * from "./color-picker";
|
package/lib/components/index.js
CHANGED
|
@@ -13,6 +13,8 @@ export * from "./appearance/ThemeCircle";
|
|
|
13
13
|
export * from "./box/Box";
|
|
14
14
|
export * from "./calendar-picker";
|
|
15
15
|
export * from "./card/Card";
|
|
16
|
+
export * from "./hover-card";
|
|
17
|
+
export * from "./entity-avatar";
|
|
16
18
|
export * from "./sheet/Sheet";
|
|
17
19
|
export * from "./color";
|
|
18
20
|
export * from "./color-picker";
|