-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathct-menu-shared.ts
More file actions
300 lines (265 loc) · 8.22 KB
/
Copy pathct-menu-shared.ts
File metadata and controls
300 lines (265 loc) · 8.22 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { CSSResult, css } from "lit";
/** Duration of the menu open/close surface animation (ms). */
export const MENU_SURFACE_ANIMATION_MS = 220;
/** Shared surface styles for portaled menu panels. */
export const menuPanelStyles = css`
:host {
position: fixed;
left: 0;
top: 0;
z-index: var(--z-index-menu, 1000);
display: block;
}
.dd-menu {
background: var(--color-surface, #fff);
border-radius: var(--border-radius, 8px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
outline: 1px solid #99999973;
padding: max(calc(var(--border-radius, 8px) / 2), 8px) 0;
opacity: 0;
transform: scale(0.94);
pointer-events: none;
visibility: hidden;
transition:
opacity 0.18s ease,
transform 0.22s cubic-bezier(0.34, 1.35, 0.64, 1),
visibility 0s 0.22s;
}
.dd-menu.active {
opacity: 1;
transform: scale(1);
pointer-events: auto;
visibility: visible;
transition:
opacity 0.18s ease,
transform 0.28s cubic-bezier(0.34, 1.45, 0.64, 1),
visibility 0s;
}
.dd-menu.closing {
opacity: 0;
transform: scale(0.96);
pointer-events: none;
visibility: hidden;
transition:
opacity 0.16s ease,
transform 0.16s ease,
visibility 0s 0.16s;
}
.dd-menu ::slotted(button) {
min-width: 220px;
color: var(--color-on-surface, #474747);
margin: 0;
padding: 8px 16px;
min-height: 38px;
width: 100%;
background: none;
outline: none;
border: none;
font-size: 1em;
text-align: left;
font-weight: 500;
}
.dd-menu ::slotted(button:last-of-type) {
border: none;
}
.dd-menu ::slotted(*) {
min-width: 220px;
display: block;
opacity: 0;
transition: all 0.25s ease;
transform: translateY(-30%);
cursor: pointer;
align-items: center;
}
.dd-menu ::slotted(span:empty),
.dd-menu ::slotted(hr) {
height: 1px;
background: var(--color-outline, #dadce0);
margin: 4px 2px;
border: 0.5px solid var(--color-outline, #dadce0);
}
.dd-menu ::slotted(h1) {
padding: 8px 16px;
font-size: 0.8em;
color: var(--color-primary);
font-weight: bold;
text-transform: uppercase;
letter-spacing: 0.15em;
font-family: "Google Sans", "Ubuntu", arial, sans-serif;
margin: 0;
}
.dd-menu ::slotted(button:hover) {
background: var(--color-primary-light);
color: var(--color-primary);
transition: all 0.15s ease;
}
.dd-menu ::slotted(button:active) {
background: #d2d2d2;
transition: all 0.15s ease;
}
.dd-menu.active ::slotted(*) {
opacity: 1;
transform: translateY(0);
}
.dd-menu.closing ::slotted(*) {
opacity: 1;
transform: translateY(0);
transition: none;
}
@supports (-webkit-backdrop-filter: none) or (backdrop-filter: none) {
.dd-menu {
background: var(--color-blur-surface, #ffffffbd);
backdrop-filter: saturate(180%) blur(15px);
-webkit-backdrop-filter: saturate(180%) blur(15px);
}
}
`;
export type FloatingMenuOwner = HTMLElement & { opened: boolean; close(): void };
/** Maps a floating panel element to its owning menu/submenu. */
export const floatingMenuOwners = new WeakMap<Element, FloatingMenuOwner>();
let floatingId = 0;
/**
* Creates a shadow-DOM panel intended to be appended to `document.body`.
* Light-DOM children of the returned host are projected into the menu surface.
*/
export function createFloatingMenuPanel(owner: FloatingMenuOwner, styles: CSSResult = menuPanelStyles): HTMLElement {
const host = document.createElement("div");
host.id = `ct-floating-menu-${++floatingId}`;
host.setAttribute("data-ct-floating-menu", owner.localName);
floatingMenuOwners.set(host, owner);
const shadow = host.attachShadow({ mode: "open" });
const sheet = styles.styleSheet;
if (sheet && "adoptedStyleSheets" in shadow) {
shadow.adoptedStyleSheets = [sheet];
} else {
const style = document.createElement("style");
style.textContent = styles.cssText;
shadow.appendChild(style);
}
const menu = document.createElement("div");
menu.className = "dd-menu";
menu.setAttribute("part", "menu");
menu.setAttribute("role", "menu");
menu.innerHTML = `<slot></slot>`;
shadow.appendChild(menu);
return host;
}
export function getFloatingMenuSurface(panel: HTMLElement): HTMLElement | null {
return panel.shadowRoot?.querySelector(".dd-menu") ?? null;
}
/** Adds the open state so the surface fades/scales in with a light bounce. */
export function openFloatingMenuSurface(surface: HTMLElement | null) {
if (!surface) return;
surface.classList.remove("closing");
// Restart transition when reopening mid-close.
void surface.offsetWidth;
surface.classList.add("active");
}
/** Removes the open state and resolves after the close animation finishes. */
export function closeFloatingMenuSurface(surface: HTMLElement | null): Promise<void> {
if (!surface) return Promise.resolve();
if (!surface.classList.contains("active") && !surface.classList.contains("closing")) {
return Promise.resolve();
}
return new Promise(resolve => {
surface.classList.add("closing");
surface.classList.remove("active");
let settled = false;
const finish = () => {
if (settled) return;
settled = true;
surface.removeEventListener("transitionend", onEnd);
resolve();
};
const onEnd = (e: TransitionEvent) => {
if (e.target !== surface) return;
if (e.propertyName !== "opacity" && e.propertyName !== "transform") return;
finish();
};
surface.addEventListener("transitionend", onEnd);
setTimeout(finish, MENU_SURFACE_ANIMATION_MS + 40);
});
}
/** Returns true when the event path hits this owner or one of its floating panels / nested submenus. */
export function isEventInsideMenuTree(path: EventTarget[], owner: FloatingMenuOwner, panel?: HTMLElement | null): boolean {
if (path.includes(owner) || (panel && path.includes(panel))) return true;
for (const node of path) {
if (!(node instanceof Element)) continue;
const panelOwner = floatingMenuOwners.get(node);
if (!panelOwner) continue;
if (panelOwner === owner) return true;
// Nested submenu panels belong to the same tree when the submenu is (or was) under this owner.
if (owner.contains(panelOwner) || panel?.contains(panelOwner)) return true;
// After portaling, submenu hosts live inside a parent panel — walk via stored parent refs.
let current: FloatingMenuOwner | null = panelOwner;
const seen = new Set<FloatingMenuOwner>();
while (current && !seen.has(current)) {
seen.add(current);
if (current === owner) return true;
current = (current as FloatingMenuOwner & { _parentMenuOwner?: FloatingMenuOwner | null })._parentMenuOwner ?? null;
}
}
return false;
}
/**
* Resolves the menu/submenu that owns `start`, walking up through light DOM,
* shadow roots and portaled floating panels (via `floatingMenuOwners`).
*/
export function resolveFloatingMenuOwner(start: Element): FloatingMenuOwner | null {
let node: Node | null = start.parentNode;
while (node) {
if (node instanceof Element) {
if (node.localName === "ct-submenu" || node.localName === "ct-menu") {
return node as FloatingMenuOwner;
}
const owner = floatingMenuOwners.get(node);
if (owner && owner !== start) return owner;
}
node = node instanceof ShadowRoot ? node.host : node.parentNode;
}
return null;
}
export function shouldKeepMenuOpen(path: EventTarget[]): boolean {
return path.some(node => {
if (!(node instanceof HTMLElement)) return false;
if (node.localName === "ct-submenu") return true;
if (node.hasAttribute("keep-open")) return true;
return (node as HTMLElement & { keepOpen?: boolean }).keepOpen === true;
});
}
export function staggerMenuItems(nodes: Element[]) {
nodes.forEach((item, index) => {
const el = item as HTMLElement;
el.style.transitionDelay = `${index * 40}ms`;
setTimeout(
() => {
el.style.transitionDelay = "";
},
index * 40 + 1000
);
});
}
export function setTransformOrigin(el: HTMLElement, placement: string) {
const [side, alignment] = placement.split("-") as [string, string | undefined];
const originX =
side === "left" || side === "right"
? side === "left"
? "right"
: "left"
: alignment === "start"
? "left"
: alignment === "end"
? "right"
: "center";
const originY =
side === "top" || side === "bottom"
? side === "top"
? "bottom"
: "top"
: alignment === "start"
? "top"
: alignment === "end"
? "bottom"
: "center";
el.style.transformOrigin = `${originX} ${originY}`;
}