Feat: Add workspace context actions
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
import { For, Show, createMemo, type JSX } from "solid-js";
|
||||
import { Portal } from "solid-js/web";
|
||||
import { X } from "../../../lib/icons";
|
||||
import {
|
||||
getWorkspaceContextMenuEyebrow,
|
||||
getWorkspaceContextMenuSections,
|
||||
type WorkspaceContextMenuAction,
|
||||
type WorkspaceContextMenuSection,
|
||||
type WorkspaceContextMenuTarget,
|
||||
} from "../data/shell.data";
|
||||
import styles from "./WorkspaceMobileActionSheet.module.scss";
|
||||
|
||||
type WorkspaceMobileActionSheetProps = {
|
||||
target: WorkspaceContextMenuTarget | null;
|
||||
onClose: VoidFunction;
|
||||
onSelect: (action: WorkspaceContextMenuAction, target: WorkspaceContextMenuTarget) => void;
|
||||
};
|
||||
|
||||
type FlattenedActionSection = {
|
||||
id: string;
|
||||
label?: string;
|
||||
items: readonly WorkspaceContextMenuAction[];
|
||||
};
|
||||
|
||||
const flattenMobileSections = (
|
||||
sections: readonly WorkspaceContextMenuSection[],
|
||||
): readonly FlattenedActionSection[] => {
|
||||
// Mobile uses a flat action-sheet model, so desktop flyout groups become
|
||||
// standalone labeled sections instead of nested menus.
|
||||
return sections.flatMap((section) => {
|
||||
const directActions = section.items.filter((action) => !action.children?.length);
|
||||
const nestedSections = section.items
|
||||
.filter((action) => action.children?.length)
|
||||
.map((action) => ({
|
||||
id: `${section.id}-${action.id}`,
|
||||
label: action.label,
|
||||
items: action.children ?? [],
|
||||
}));
|
||||
|
||||
const flattenedSections: FlattenedActionSection[] = [];
|
||||
|
||||
if (directActions.length > 0) {
|
||||
flattenedSections.push({
|
||||
id: section.id,
|
||||
label: section.label,
|
||||
items: directActions,
|
||||
});
|
||||
}
|
||||
|
||||
return [...flattenedSections, ...nestedSections];
|
||||
});
|
||||
};
|
||||
|
||||
export const WorkspaceMobileActionSheet = (props: WorkspaceMobileActionSheetProps): JSX.Element => {
|
||||
const sheetState = createMemo(() => {
|
||||
if (!props.target) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
target: props.target,
|
||||
sections: flattenMobileSections(getWorkspaceContextMenuSections(props.target)),
|
||||
};
|
||||
});
|
||||
|
||||
const handleActionSelect = (action: WorkspaceContextMenuAction, target: WorkspaceContextMenuTarget): void => {
|
||||
props.onSelect(action, target);
|
||||
props.onClose();
|
||||
};
|
||||
|
||||
return (
|
||||
<Show when={sheetState()}>
|
||||
{(sheetState): JSX.Element => {
|
||||
const target = sheetState().target;
|
||||
const sections = sheetState().sections;
|
||||
|
||||
return (
|
||||
<Portal>
|
||||
<div class={styles.layer}>
|
||||
<button class={styles.backdrop} type="button" aria-label="Close action sheet" onClick={props.onClose} />
|
||||
|
||||
<section class={styles.sheet} aria-label={`${target.label} actions`}>
|
||||
<div class={styles.handle} aria-hidden="true" />
|
||||
|
||||
<header class={styles.header}>
|
||||
<div class={styles.headerCopy}>
|
||||
<span class={styles.eyebrow}>{getWorkspaceContextMenuEyebrow(target)}</span>
|
||||
<strong class={styles.title}>{target.label}</strong>
|
||||
</div>
|
||||
|
||||
<button class={styles.closeButton} type="button" aria-label="Close action sheet" onClick={props.onClose}>
|
||||
<X size={18} strokeWidth={2} />
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<div class={styles.sectionList}>
|
||||
<For each={sections}>
|
||||
{(section): JSX.Element => (
|
||||
<section class={styles.section}>
|
||||
<Show when={section.label}>
|
||||
<span class={styles.sectionLabel}>{section.label}</span>
|
||||
</Show>
|
||||
|
||||
<div class={styles.actionList}>
|
||||
<For each={section.items}>
|
||||
{(action): JSX.Element => (
|
||||
<button
|
||||
type="button"
|
||||
classList={{
|
||||
[styles.action]: true,
|
||||
[styles.actionDanger]: action.tone === "danger",
|
||||
}}
|
||||
onClick={() => handleActionSelect(action, target)}
|
||||
>
|
||||
<span class={styles.actionLabel}>{action.label}</span>
|
||||
</button>
|
||||
)}
|
||||
</For>
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
</For>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</Portal>
|
||||
);
|
||||
}}
|
||||
</Show>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user