|
| 1 | +/* |
| 2 | + * Copyright (c) Mike Lischke. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + */ |
| 5 | + |
| 6 | +import "./Menu.css"; |
| 7 | + |
| 8 | +import { ComponentChild } from "preact"; |
| 9 | + |
| 10 | +import { Button } from "../Button.js"; |
| 11 | +import { Icon } from "../Icon.js"; |
| 12 | +import { UIComponent, type ICommonUIProperties } from "../UIComponent.js"; |
| 13 | +import { getNewId } from "../../../../core/utils.js"; |
| 14 | +import { Codicon } from "../Codicon.js"; |
| 15 | +import { type IMenuItem, MenuItem } from "./MenuItem.js"; |
| 16 | + |
| 17 | +interface IMenuProperties extends ICommonUIProperties { |
| 18 | + /** The menu items (including separators). */ |
| 19 | + items: IMenuItem[]; |
| 20 | + |
| 21 | + /** Shown as the trigger button caption when used standalone. */ |
| 22 | + caption?: string; |
| 23 | + |
| 24 | + /** Shown as the trigger button icon when used standalone. */ |
| 25 | + icon?: Codicon; |
| 26 | + |
| 27 | + /** Called when an item is clicked. The item id is passed. */ |
| 28 | + onItemClick?: (id: string) => void; |
| 29 | +} |
| 30 | + |
| 31 | +interface IMenuState { |
| 32 | + open: boolean; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * A vertical popup menu. Uses the native popover API for positioning. |
| 37 | + * |
| 38 | + * Can be used standalone (renders a trigger button) or embedded |
| 39 | + * in a MenuBar (the MenuBar controls open/close via ref). |
| 40 | + */ |
| 41 | +export class Menu extends UIComponent<IMenuProperties, IMenuState> { |
| 42 | + private popoverId = `menu-popover-${getNewId()}`; |
| 43 | + private anchorName = `--menu-anchor-${getNewId()}`; |
| 44 | + |
| 45 | + public constructor(props: IMenuProperties) { |
| 46 | + super(props); |
| 47 | + |
| 48 | + this.state = { |
| 49 | + open: false, |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + public render(): ComponentChild { |
| 54 | + const { id, caption, icon, items, style } = this.props; |
| 55 | + const triggerShown = (caption ?? icon) !== undefined; |
| 56 | + const className = this.generateFinalClassName(["menuHost"]); |
| 57 | + |
| 58 | + return ( |
| 59 | + <div id={id} className={className} style={style}> |
| 60 | + {triggerShown && ( |
| 61 | + <div style={{ anchorName: this.anchorName }}> |
| 62 | + <Button |
| 63 | + className="du-btn-ghost" |
| 64 | + popoverTarget={this.popoverId} |
| 65 | + imageOnly={!caption && icon !== undefined} |
| 66 | + onClick={(e) => { |
| 67 | + e.stopPropagation(); |
| 68 | + }} |
| 69 | + > |
| 70 | + {icon && <Icon src={icon} />} |
| 71 | + {caption} |
| 72 | + </Button> |
| 73 | + </div> |
| 74 | + )} |
| 75 | + |
| 76 | + <div |
| 77 | + className="menu du-dropdown du-menu rounded-box bg-base-100 shadow-sm" |
| 78 | + id={this.popoverId} |
| 79 | + popover="auto" |
| 80 | + style={{ positionAnchor: this.anchorName }} |
| 81 | + onKeyDown={this.handleKeyDown} |
| 82 | + onToggle={this.handlePopoverToggle} |
| 83 | + > |
| 84 | + {items.map((item) => { |
| 85 | + return ( |
| 86 | + <div |
| 87 | + key={item.id} |
| 88 | + onClick={() => { |
| 89 | + if (!item.disabled && item.label !== "-") { |
| 90 | + this.props.onItemClick?.(item.id); |
| 91 | + this.close(); |
| 92 | + } |
| 93 | + }} |
| 94 | + > |
| 95 | + <MenuItem item={item} /> |
| 96 | + </div> |
| 97 | + ); |
| 98 | + })} |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + public open(): void { |
| 105 | + this.setState({ open: true }); |
| 106 | + } |
| 107 | + |
| 108 | + public close(): void { |
| 109 | + const popover = document.getElementById(this.popoverId); |
| 110 | + |
| 111 | + if (popover && typeof (popover as HTMLElement & { hidePopover?: () => void; }).hidePopover === "function") { |
| 112 | + (popover as HTMLElement & { hidePopover: () => void; }).hidePopover(); |
| 113 | + } |
| 114 | + |
| 115 | + this.setState({ open: false }); |
| 116 | + } |
| 117 | + |
| 118 | + private handleKeyDown = (e: KeyboardEvent): void => { |
| 119 | + if (e.key === "Escape") { |
| 120 | + this.close(); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + private handlePopoverToggle = (e: Event): void => { |
| 125 | + const popover = e.target as HTMLElement; |
| 126 | + |
| 127 | + this.setState({ open: popover.matches(":popover-open") }); |
| 128 | + }; |
| 129 | +} |
0 commit comments