@swan-io/lake 13.7.11 → 13.7.13
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/package.json
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import { VerticalPlacement } from "../hooks/useContextualLayer";
|
|
3
|
+
type ContextMenuProps = {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
ariaLabel: string;
|
|
6
|
+
withPill?: boolean;
|
|
7
|
+
verticalPlacement?: VerticalPlacement;
|
|
8
|
+
};
|
|
9
|
+
export declare const ContextMenu: ({ children, ariaLabel, withPill, verticalPlacement, }: ContextMenuProps) => import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
type ContextMenuItemProps = {
|
|
11
|
+
children: string;
|
|
12
|
+
withPill?: boolean;
|
|
13
|
+
onPress: () => void;
|
|
14
|
+
};
|
|
15
|
+
export declare const ContextMenuItem: ({ children, withPill, onPress }: ContextMenuItemProps) => import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useRef } from "react";
|
|
3
|
+
import { StyleSheet, View } from "react-native";
|
|
4
|
+
import { colors, spacings } from "../constants/design";
|
|
5
|
+
import { useDisclosure } from "../hooks/useDisclosure";
|
|
6
|
+
import { noop } from "../utils/function";
|
|
7
|
+
import { LakeButton } from "./LakeButton";
|
|
8
|
+
import { LakeText } from "./LakeText";
|
|
9
|
+
import { Popover } from "./Popover";
|
|
10
|
+
import { Pressable } from "./Pressable";
|
|
11
|
+
import { Space } from "./Space";
|
|
12
|
+
const styles = StyleSheet.create({
|
|
13
|
+
container: {
|
|
14
|
+
minWidth: 120,
|
|
15
|
+
},
|
|
16
|
+
menuItem: {
|
|
17
|
+
flexDirection: "row",
|
|
18
|
+
alignItems: "center",
|
|
19
|
+
paddingHorizontal: spacings[24],
|
|
20
|
+
paddingVertical: spacings[12],
|
|
21
|
+
},
|
|
22
|
+
dot: {
|
|
23
|
+
width: 8,
|
|
24
|
+
height: 8,
|
|
25
|
+
borderRadius: 4,
|
|
26
|
+
backgroundColor: colors.negative[500],
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
const ContextMenuContext = createContext({ close: noop });
|
|
30
|
+
export const ContextMenu = ({ children, ariaLabel, withPill = false, verticalPlacement, }) => {
|
|
31
|
+
const [isOpen, { open, close }] = useDisclosure(false);
|
|
32
|
+
const triggerRef = useRef(null);
|
|
33
|
+
return (_jsxs(ContextMenuContext.Provider, { value: { close }, children: [_jsx(LakeButton, { ref: triggerRef, mode: "tertiary", size: "small", icon: "more-horizontal-filled", color: "gray", onPress: open, ariaLabel: ariaLabel, pill: withPill }), _jsx(Popover, { referenceRef: triggerRef, visible: isOpen, onDismiss: close, verticalPlacement: verticalPlacement, children: _jsx(View, { style: styles.container, children: children }) })] }));
|
|
34
|
+
};
|
|
35
|
+
export const ContextMenuItem = ({ children, withPill = false, onPress }) => {
|
|
36
|
+
const { close } = useContext(ContextMenuContext);
|
|
37
|
+
return (_jsxs(Pressable, { style: styles.menuItem, onPress: () => {
|
|
38
|
+
close();
|
|
39
|
+
onPress();
|
|
40
|
+
}, children: [_jsx(LakeText, { variant: "regular", color: colors.gray[700], children: children }), withPill ? (_jsxs(_Fragment, { children: [_jsx(Space, { width: 8 }), _jsx(View, { style: styles.dot })] })) : null] }));
|
|
41
|
+
};
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { ReactNode, RefObject } from "react";
|
|
2
2
|
import { Text, View } from "react-native";
|
|
3
|
+
import { Placement, VerticalPlacement } from "../hooks/useContextualLayer";
|
|
3
4
|
type Props = {
|
|
4
5
|
children: ReactNode | ((state: {
|
|
5
6
|
mode: "dropdown" | "panel";
|
|
6
7
|
}) => ReactNode);
|
|
7
8
|
id?: string;
|
|
8
9
|
label?: string;
|
|
9
|
-
placement?:
|
|
10
|
+
placement?: Placement;
|
|
11
|
+
verticalPlacement?: VerticalPlacement;
|
|
10
12
|
role?: "listbox" | "combobox" | "dialog";
|
|
11
13
|
describedBy?: string;
|
|
12
14
|
matchReferenceWidth?: boolean;
|
|
@@ -56,12 +56,13 @@ const animation = {
|
|
|
56
56
|
],
|
|
57
57
|
};
|
|
58
58
|
export const VIEWPORT_WIDTH_THRESHOLD = 600;
|
|
59
|
-
export const Popover = memo(({ children, id, label, placement, role = "dialog", describedBy, matchReferenceWidth = false, matchReferenceMinWidth = false, onDismiss = noop, onEscapeKey = onDismiss, referenceRef, returnFocus = true, autoFocus = true, visible, underlay = true, forcedMode, }) => {
|
|
59
|
+
export const Popover = memo(({ children, id, label, placement, verticalPlacement, role = "dialog", describedBy, matchReferenceWidth = false, matchReferenceMinWidth = false, onDismiss = noop, onEscapeKey = onDismiss, referenceRef, returnFocus = true, autoFocus = true, visible, underlay = true, forcedMode, }) => {
|
|
60
60
|
const [rootElement, setRootElement] = useState(null);
|
|
61
61
|
const underlayRef = useRef(null);
|
|
62
62
|
const { desktop } = useResponsive(VIEWPORT_WIDTH_THRESHOLD);
|
|
63
63
|
const { position } = useContextualLayer({
|
|
64
64
|
placement,
|
|
65
|
+
verticalPlacement,
|
|
65
66
|
referenceRef,
|
|
66
67
|
visible,
|
|
67
68
|
matchReferenceWidth,
|
|
@@ -15,9 +15,11 @@ export type ElementPosition = {
|
|
|
15
15
|
width: number;
|
|
16
16
|
height: number;
|
|
17
17
|
};
|
|
18
|
-
type Placement = "left" | "center" | "right";
|
|
18
|
+
export type Placement = "left" | "center" | "right";
|
|
19
|
+
export type VerticalPlacement = "above" | "below";
|
|
19
20
|
type Config = {
|
|
20
21
|
placement?: Placement;
|
|
22
|
+
verticalPlacement?: VerticalPlacement;
|
|
21
23
|
visible: boolean;
|
|
22
24
|
matchReferenceWidth?: boolean;
|
|
23
25
|
matchReferenceMinWidth?: boolean;
|
|
@@ -33,5 +35,5 @@ type ContextualLayerConfig = {
|
|
|
33
35
|
referenceRef: RefObject<View | Text | null>;
|
|
34
36
|
position: Option<ContextualLayerPosition>;
|
|
35
37
|
};
|
|
36
|
-
export declare const useContextualLayer: ({ placement, visible, matchReferenceWidth, matchReferenceMinWidth, referenceRef: externalReferenceRef, }: Config) => ContextualLayerConfig;
|
|
38
|
+
export declare const useContextualLayer: ({ placement, verticalPlacement, visible, matchReferenceWidth, matchReferenceMinWidth, referenceRef: externalReferenceRef, }: Config) => ContextualLayerConfig;
|
|
37
39
|
export {};
|
|
@@ -3,7 +3,7 @@ import { useCallback, useEffect, useRef, useState } from "react";
|
|
|
3
3
|
import { match } from "ts-pattern";
|
|
4
4
|
const MAX_OFFSET_FOR_CENTER_PLACEMENT = 100;
|
|
5
5
|
const HORIZONTAL_SAFETY_MARGIN = 16;
|
|
6
|
-
export const useContextualLayer = ({ placement, visible, matchReferenceWidth = false, matchReferenceMinWidth = false, referenceRef: externalReferenceRef, }) => {
|
|
6
|
+
export const useContextualLayer = ({ placement, verticalPlacement, visible, matchReferenceWidth = false, matchReferenceMinWidth = false, referenceRef: externalReferenceRef, }) => {
|
|
7
7
|
const referenceRef = useRef(null);
|
|
8
8
|
const usedRef = externalReferenceRef !== null && externalReferenceRef !== void 0 ? externalReferenceRef : referenceRef;
|
|
9
9
|
const [position, setPosition] = useState(Option.None());
|
|
@@ -27,7 +27,12 @@ export const useContextualLayer = ({ placement, visible, matchReferenceWidth = f
|
|
|
27
27
|
: availableSpaceBefore > availableSpaceAfter
|
|
28
28
|
? "right"
|
|
29
29
|
: "left");
|
|
30
|
-
const
|
|
30
|
+
const openAbove = verticalPlacement === "above"
|
|
31
|
+
? true
|
|
32
|
+
: verticalPlacement === "below"
|
|
33
|
+
? false
|
|
34
|
+
: availableSpaceAbove > availableSpaceBelow;
|
|
35
|
+
const verticalPosition = openAbove
|
|
31
36
|
? {
|
|
32
37
|
maxHeight: availableSpaceAbove,
|
|
33
38
|
bottom: viewportHeight - rect.top,
|
|
@@ -41,9 +46,7 @@ export const useContextualLayer = ({ placement, visible, matchReferenceWidth = f
|
|
|
41
46
|
.with("right", () => ({ right: window.innerWidth - rect.right }))
|
|
42
47
|
.with("center", () => ({ left: rect.left + width / 2, transform: "translateX(-50%)" }))
|
|
43
48
|
.exhaustive();
|
|
44
|
-
const maxHeight = availableSpaceAbove
|
|
45
|
-
? window.innerHeight - rect.top - height
|
|
46
|
-
: availableSpaceAbove;
|
|
49
|
+
const maxHeight = openAbove ? availableSpaceAbove : window.innerHeight - rect.top - height;
|
|
47
50
|
const rootStyle = {
|
|
48
51
|
position: "absolute",
|
|
49
52
|
top: window.scrollY,
|
|
@@ -55,7 +58,8 @@ export const useContextualLayer = ({ placement, visible, matchReferenceWidth = f
|
|
|
55
58
|
const maxWidth = match(inferedPlacement)
|
|
56
59
|
.with("left", () => viewportWidth - rect.left - HORIZONTAL_SAFETY_MARGIN)
|
|
57
60
|
.with("right", () => rect.right - HORIZONTAL_SAFETY_MARGIN)
|
|
58
|
-
.with("center", () => Math.min(availableSpaceBefore + width / 2, availableSpaceAfter + width / 2) * 2 -
|
|
61
|
+
.with("center", () => Math.min(availableSpaceBefore + width / 2, availableSpaceAfter + width / 2) * 2 -
|
|
62
|
+
HORIZONTAL_SAFETY_MARGIN)
|
|
59
63
|
.exhaustive();
|
|
60
64
|
const style = {
|
|
61
65
|
...verticalPosition,
|
|
@@ -69,10 +73,10 @@ export const useContextualLayer = ({ placement, visible, matchReferenceWidth = f
|
|
|
69
73
|
return Option.Some({
|
|
70
74
|
rootStyle,
|
|
71
75
|
horizontalPosition: inferedPlacement,
|
|
72
|
-
verticalPosition:
|
|
76
|
+
verticalPosition: openAbove ? "top" : "bottom",
|
|
73
77
|
style,
|
|
74
78
|
});
|
|
75
|
-
}, [placement, matchReferenceWidth, matchReferenceMinWidth, usedRef]);
|
|
79
|
+
}, [placement, verticalPlacement, matchReferenceWidth, matchReferenceMinWidth, usedRef]);
|
|
76
80
|
useEffect(() => {
|
|
77
81
|
if (visible) {
|
|
78
82
|
setPosition(getPosition());
|