-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathaction-menu.ts
More file actions
105 lines (92 loc) · 2.71 KB
/
action-menu.ts
File metadata and controls
105 lines (92 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {css, html, LitElement} from 'lit';
import {OverlayMixin, withDropdownConfig} from '@lion/ui/overlays.js';
import {uuid} from '@lion/ui/core.js';
import type CraftActionItem from '@src/components/action-item/action-item';
/**
* @slot - Items to be rendered in the menu.
* @slot invoker - Element that triggers the menu.
* @slot backdrop - Element that covers the screen when the menu is open.
* @slot content - Content to be rendered inside the menu.
*/
export default class CraftActionMenu extends OverlayMixin(LitElement) {
static override styles = css`
::slotted([slot='content']) {
font-size: var(--c-text-base);
font-weight: 400;
display: grid;
gap: var(--c-spacing-xs);
border: 1px solid var(--c-color-neutral-border-quiet);
border-radius: var(--c-radius-md);
background-color: var(--c-surface-overlay);
box-shadow: var(--c-shadow-sm);
padding: var(--c-spacing-sm);
min-width: calc(180rem / 16);
max-width: calc(320rem / 16);
}
::slotted(hr) {
margin: 0;
}
`;
private uid: string;
get actionItems(): NodeListOf<CraftActionItem> {
return (
this._overlayContentNode?.querySelectorAll('craft-action-item') ??
([] as unknown as NodeListOf<CraftActionItem>)
);
}
// @ts-ignore
_defineOverlayConfig() {
return {
...withDropdownConfig(),
};
}
/**
* @TODO Figure out how to best manage passing an elementToFocusAfterHide to the Lion modal
* @private
*/
private __addEventListeners() {
this.actionItems.forEach((item) => {
item.addEventListener('click', (e) => {
e.target?.dispatchEvent(new Event('close-overlay', {bubbles: true}));
});
});
}
private __setupInvoker() {
const invoker = this._overlayInvokerNode;
if (invoker) {
invoker.setAttribute('id', `invoker-${this.uid}`);
invoker.setAttribute('aria-controls', `content-${this.uid}`);
}
}
private __setupContent() {
const content = this._overlayContentNode;
if (content) {
content.setAttribute('id', `content-${this.uid}`);
content.setAttribute('role', 'none');
}
}
override _setupOverlayCtrl() {
super._setupOverlayCtrl();
this.__setupInvoker();
this.__setupContent();
}
override firstUpdated() {
this.uid = uuid();
this.__addEventListeners();
}
protected override render(): unknown {
return html`
<slot name="invoker"></slot>
<slot name="backdrop"></slot>
<slot name="content"></slot>
`;
}
}
if (!customElements.get('craft-action-menu')) {
customElements.define('craft-action-menu', CraftActionMenu);
}
declare global {
interface HTMLElementTagNameMap {
'craft-action-menu': CraftActionMenu;
}
}