Initial commit: group-by-host Paseo sidebar plugin
This commit is contained in:
commit
dcfe97a2e6
13 files changed
+6789
No files matched your search
@@ -0,0 +1,394 @@
|
||||
import type { PluginTheme } from "@getpaseo/plugin";
|
||||
import { Icon, Modal } from "@getpaseo/plugin/client/ui";
|
||||
import { type RefObject, useEffect, useRef, useState } from "react";
|
||||
import { Pressable, Text, View } from "react-native";
|
||||
|
||||
export interface ContextMenuItem {
|
||||
readonly id: string;
|
||||
readonly label: string;
|
||||
readonly icon: string;
|
||||
readonly tone?: "default" | "danger";
|
||||
readonly disabled?: boolean;
|
||||
readonly separatorBefore?: boolean;
|
||||
onSelect(): void;
|
||||
}
|
||||
|
||||
export interface ContextMenuRequest {
|
||||
readonly clientX: number;
|
||||
readonly clientY: number;
|
||||
readonly title?: string;
|
||||
readonly items: readonly ContextMenuItem[];
|
||||
}
|
||||
|
||||
interface DomRectLike {
|
||||
left: number;
|
||||
top: number;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
interface DomEventLike {
|
||||
preventDefault(): void;
|
||||
stopPropagation(): void;
|
||||
clientX: number;
|
||||
clientY: number;
|
||||
target?: unknown;
|
||||
key?: string;
|
||||
}
|
||||
|
||||
interface DomNodeLike {
|
||||
addEventListener?(
|
||||
type: string,
|
||||
listener: (event: DomEventLike) => void,
|
||||
capture?: boolean,
|
||||
): void;
|
||||
removeEventListener?(
|
||||
type: string,
|
||||
listener: (event: DomEventLike) => void,
|
||||
capture?: boolean,
|
||||
): void;
|
||||
getBoundingClientRect?(): DomRectLike;
|
||||
contains?(target: unknown): boolean;
|
||||
}
|
||||
|
||||
function asDomNode(value: unknown): DomNodeLike | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
return value as DomNodeLike;
|
||||
}
|
||||
|
||||
function getDocumentNode(): DomNodeLike | null {
|
||||
const globalObj = globalThis as { document?: unknown };
|
||||
return asDomNode(globalObj.document);
|
||||
}
|
||||
|
||||
export function useContextMenuTarget(
|
||||
onOpen: (clientX: number, clientY: number) => void,
|
||||
) {
|
||||
const [target, setTarget] = useState<unknown>(null);
|
||||
const onOpenRef = useRef(onOpen);
|
||||
onOpenRef.current = onOpen;
|
||||
|
||||
useEffect(() => {
|
||||
const node = asDomNode(target);
|
||||
if (!node || typeof node.addEventListener !== "function") {
|
||||
return;
|
||||
}
|
||||
|
||||
const handleContextMenu = (event: DomEventLike) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
onOpenRef.current(event.clientX, event.clientY);
|
||||
};
|
||||
|
||||
node.addEventListener("contextmenu", handleContextMenu);
|
||||
return () => {
|
||||
node.removeEventListener?.("contextmenu", handleContextMenu);
|
||||
};
|
||||
}, [target]);
|
||||
|
||||
return setTarget;
|
||||
}
|
||||
|
||||
function clamp(value: number, min: number, max: number): number {
|
||||
if (max < min) return min;
|
||||
return Math.min(Math.max(value, min), max);
|
||||
}
|
||||
|
||||
interface MenuItemListProps {
|
||||
items: readonly ContextMenuItem[];
|
||||
theme: PluginTheme;
|
||||
compact: boolean;
|
||||
onSelectItem(item: ContextMenuItem): void;
|
||||
}
|
||||
|
||||
function MenuItemList({
|
||||
items,
|
||||
theme,
|
||||
compact,
|
||||
onSelectItem,
|
||||
}: MenuItemListProps) {
|
||||
const [hoveredId, setHoveredId] = useState<string | null>(null);
|
||||
|
||||
return (
|
||||
<View>
|
||||
{items.map((item) => {
|
||||
const isDanger = item.tone === "danger";
|
||||
const isHovered = hoveredId === item.id;
|
||||
const foreground = isDanger
|
||||
? theme.colors.statusDanger
|
||||
: theme.colors.foreground;
|
||||
const iconColor = isDanger
|
||||
? theme.colors.statusDanger
|
||||
: theme.colors.foregroundMuted;
|
||||
|
||||
return (
|
||||
<View key={item.id}>
|
||||
{item.separatorBefore ? (
|
||||
<View
|
||||
style={{
|
||||
height: 1,
|
||||
marginVertical: 4,
|
||||
marginHorizontal: 6,
|
||||
backgroundColor: theme.colors.border,
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
<Pressable
|
||||
accessibilityRole="menuitem"
|
||||
accessibilityLabel={item.label}
|
||||
disabled={item.disabled}
|
||||
onHoverIn={() => setHoveredId(item.id)}
|
||||
onHoverOut={() =>
|
||||
setHoveredId((prev) => (prev === item.id ? null : prev))
|
||||
}
|
||||
onPress={() => onSelectItem(item)}
|
||||
style={({ pressed }) => ({
|
||||
flexDirection: "row",
|
||||
alignItems: "center",
|
||||
gap: compact ? 8 : 10,
|
||||
marginHorizontal: 4,
|
||||
paddingHorizontal: compact ? 8 : 10,
|
||||
paddingVertical: compact ? 6 : 9,
|
||||
borderRadius: 6,
|
||||
backgroundColor:
|
||||
pressed || isHovered ? theme.colors.surface2 : "transparent",
|
||||
opacity: item.disabled ? 0.5 : 1,
|
||||
})}
|
||||
>
|
||||
<Icon
|
||||
name={item.icon}
|
||||
size={compact ? 13 : 15}
|
||||
color={iconColor}
|
||||
/>
|
||||
<Text
|
||||
numberOfLines={1}
|
||||
style={{
|
||||
flex: 1,
|
||||
fontSize: compact ? 12 : 14,
|
||||
fontWeight: "500",
|
||||
color: foreground,
|
||||
}}
|
||||
>
|
||||
{item.label}
|
||||
</Text>
|
||||
</Pressable>
|
||||
</View>
|
||||
);
|
||||
})}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
export interface ContextMenuOverlayProps {
|
||||
request: ContextMenuRequest | null;
|
||||
containerRef: RefObject<unknown>;
|
||||
platform: "ios" | "android" | "web";
|
||||
theme: PluginTheme;
|
||||
onClose(): void;
|
||||
}
|
||||
|
||||
export function ContextMenuOverlay({
|
||||
request,
|
||||
containerRef,
|
||||
platform,
|
||||
theme,
|
||||
onClose,
|
||||
}: ContextMenuOverlayProps) {
|
||||
const backdropRef = useRef<unknown>(null);
|
||||
const menuRef = useRef<unknown>(null);
|
||||
const [menuSize, setMenuSize] = useState<{
|
||||
width: number;
|
||||
height: number;
|
||||
} | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setMenuSize(null);
|
||||
}, [request]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!request || platform !== "web") return;
|
||||
const doc = getDocumentNode();
|
||||
if (!doc || typeof doc.addEventListener !== "function") return;
|
||||
|
||||
const handleKeyDown = (event: DomEventLike) => {
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
|
||||
const handlePointerDown = (event: DomEventLike) => {
|
||||
const menuNode = asDomNode(menuRef.current);
|
||||
if (menuNode?.contains?.(event.target)) {
|
||||
return;
|
||||
}
|
||||
const containerNode = asDomNode(containerRef.current);
|
||||
if (!containerNode?.contains?.(event.target)) {
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
|
||||
doc.addEventListener("keydown", handleKeyDown, true);
|
||||
doc.addEventListener("pointerdown", handlePointerDown, true);
|
||||
return () => {
|
||||
doc.removeEventListener?.("keydown", handleKeyDown, true);
|
||||
doc.removeEventListener?.("pointerdown", handlePointerDown, true);
|
||||
};
|
||||
}, [containerRef, onClose, platform, request]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!request || platform !== "web") return;
|
||||
const backdropNode = asDomNode(backdropRef.current);
|
||||
if (!backdropNode || typeof backdropNode.addEventListener !== "function") {
|
||||
return;
|
||||
}
|
||||
|
||||
const handleContextMenu = (event: DomEventLike) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
onClose();
|
||||
};
|
||||
|
||||
backdropNode.addEventListener("contextmenu", handleContextMenu);
|
||||
return () => {
|
||||
backdropNode.removeEventListener?.("contextmenu", handleContextMenu);
|
||||
};
|
||||
}, [onClose, platform, request]);
|
||||
|
||||
if (!request) return null;
|
||||
|
||||
function handleSelectItem(item: ContextMenuItem) {
|
||||
onClose();
|
||||
item.onSelect();
|
||||
}
|
||||
|
||||
const containerRect =
|
||||
platform === "web"
|
||||
? asDomNode(containerRef.current)?.getBoundingClientRect?.() ?? null
|
||||
: null;
|
||||
|
||||
if (!containerRect) {
|
||||
return (
|
||||
<Modal
|
||||
title={request.title ?? "Actions"}
|
||||
open
|
||||
onOpenChange={(nextOpen) => {
|
||||
if (!nextOpen) onClose();
|
||||
}}
|
||||
>
|
||||
<Modal.Content>
|
||||
<MenuItemList
|
||||
items={request.items}
|
||||
theme={theme}
|
||||
compact={false}
|
||||
onSelectItem={handleSelectItem}
|
||||
/>
|
||||
</Modal.Content>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
const padding = 8;
|
||||
const estimatedWidth = menuSize?.width ?? 192;
|
||||
const estimatedHeight = menuSize?.height ?? request.items.length * 32 + 36;
|
||||
const rawLeft = request.clientX - containerRect.left;
|
||||
const rawTop = request.clientY - containerRect.top;
|
||||
const left = clamp(
|
||||
rawLeft,
|
||||
padding,
|
||||
containerRect.width - estimatedWidth - padding,
|
||||
);
|
||||
const top = clamp(
|
||||
rawTop,
|
||||
padding,
|
||||
containerRect.height - estimatedHeight - padding,
|
||||
);
|
||||
|
||||
return (
|
||||
<View
|
||||
pointerEvents="box-none"
|
||||
style={{
|
||||
position: "absolute",
|
||||
top: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
zIndex: 50,
|
||||
}}
|
||||
>
|
||||
<Pressable
|
||||
ref={backdropRef as never}
|
||||
accessibilityLabel="Dismiss context menu"
|
||||
onPress={onClose}
|
||||
style={{
|
||||
position: "absolute",
|
||||
top: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
}}
|
||||
/>
|
||||
|
||||
<View
|
||||
ref={menuRef as never}
|
||||
accessibilityRole="menu"
|
||||
onLayout={(event) => {
|
||||
const { width, height } = event.nativeEvent.layout;
|
||||
setMenuSize((prev) =>
|
||||
prev && prev.width === width && prev.height === height
|
||||
? prev
|
||||
: { width, height },
|
||||
);
|
||||
}}
|
||||
style={{
|
||||
position: "absolute",
|
||||
left,
|
||||
top,
|
||||
minWidth: 184,
|
||||
maxWidth: Math.max(184, containerRect.width - padding * 2),
|
||||
paddingVertical: 4,
|
||||
borderRadius: 10,
|
||||
borderWidth: 1,
|
||||
borderColor: theme.colors.border,
|
||||
backgroundColor: theme.colors.surface1,
|
||||
shadowColor: "#000000",
|
||||
shadowOpacity: 0.24,
|
||||
shadowRadius: 12,
|
||||
shadowOffset: { width: 0, height: 6 },
|
||||
elevation: 8,
|
||||
}}
|
||||
>
|
||||
{request.title ? (
|
||||
<View
|
||||
style={{
|
||||
paddingHorizontal: 12,
|
||||
paddingTop: 4,
|
||||
paddingBottom: 6,
|
||||
marginBottom: 4,
|
||||
borderBottomWidth: 1,
|
||||
borderBottomColor: theme.colors.border,
|
||||
}}
|
||||
>
|
||||
<Text
|
||||
numberOfLines={1}
|
||||
style={{
|
||||
fontSize: 11,
|
||||
fontWeight: "600",
|
||||
color: theme.colors.foregroundMuted,
|
||||
}}
|
||||
>
|
||||
{request.title}
|
||||
</Text>
|
||||
</View>
|
||||
) : null}
|
||||
|
||||
<MenuItemList
|
||||
items={request.items}
|
||||
theme={theme}
|
||||
compact
|
||||
onSelectItem={handleSelectItem}
|
||||
/>
|
||||
</View>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user