-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathmenu.svelte
More file actions
176 lines (156 loc) · 4.08 KB
/
menu.svelte
File metadata and controls
176 lines (156 loc) · 4.08 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
<script lang="ts" module>
import { cva } from 'class-variance-authority';
const sharedMenuStyles = [
'surface-primary',
'min-w-fit',
'list-none',
'overflow-auto',
'border',
'border-subtle',
'text-primary',
'shadow',
'w-full',
];
const menuStyles = cva(
[
...sharedMenuStyles,
'absolute',
'z-20',
'mt-1',
'transition-all',
'duration-100',
'ease-out',
],
{
variants: {
position: {
left: 'left-0 origin-top-left',
right: 'right-0 origin-top-right',
'top-left': 'left-0 origin-top-left',
'top-right': 'right-0 origin-top-right',
},
state: {
open: 'visible scale-100 opacity-100',
closed: 'invisible scale-95 opacity-0',
},
},
},
);
</script>
<script lang="ts">
import type { HTMLAttributes } from 'svelte/elements';
import { getContext } from 'svelte';
import { type ClassNameValue, twMerge as merge } from 'tailwind-merge';
import Portal from '$lib/holocene/portal/portal.svelte';
import type { PortalPosition } from '$lib/holocene/portal/types';
import { getFocusableElements } from '$lib/utilities/focus-trap';
import { MENU_CONTEXT, type MenuContext } from './menu-container.svelte';
export interface Props
extends Omit<HTMLAttributes<HTMLUListElement>, 'class'> {
id: string;
keepOpen?: boolean;
position?: 'left' | 'right' | 'top-left' | 'top-right';
menuElement?: HTMLUListElement | null;
maxHeight?: string;
class?: ClassNameValue;
usePortal?: boolean;
scrollContainer?: string;
flipOnCollision?: boolean;
hideWhenAnchorHidden?: boolean;
}
let {
class: className = '',
id,
keepOpen = false,
position = 'left',
menuElement = $bindable(null),
maxHeight = 'max-h-[20rem]',
usePortal = false,
scrollContainer,
flipOnCollision = undefined,
hideWhenAnchorHidden = undefined,
children,
...rest
}: Props = $props();
let height = $state(0);
let anchorElement = $state<HTMLElement | null>(null);
const {
keepOpen: keepOpenCtx,
menuElement: menuElementCtx,
open,
} = getContext<MenuContext>(MENU_CONTEXT);
$effect(() => {
$keepOpenCtx = keepOpen;
});
$effect(() => {
$menuElementCtx = menuElement;
});
$effect(() => {
if (usePortal && id) {
anchorElement = document.querySelector(
`[aria-controls="${id}"]`,
) as HTMLElement | null;
}
});
const portalPosition: PortalPosition | undefined = $derived.by(() => {
if (!usePortal) return undefined;
if (position.includes('top')) return position;
return position === 'left' ? 'bottom-left' : 'bottom-right';
});
const menuItems = $derived(
menuElement ? getFocusableElements(menuElement) : [],
);
const lastMenuItem = $derived(menuItems[menuItems.length - 1]);
const handleFocusOut = (e: FocusEvent) => {
if (!$keepOpenCtx && e.target === lastMenuItem) $open = false;
};
const handleClick = (e: MouseEvent) => {
e.stopPropagation();
};
const styles = $derived(
menuStyles({ position, state: $open ? 'open' : 'closed' }),
);
</script>
{#if usePortal && anchorElement}
<Portal
anchor={anchorElement}
open={$open}
position={portalPosition}
{flipOnCollision}
{hideWhenAnchorHidden}
{scrollContainer}
>
<ul
role="menu"
class={merge(sharedMenuStyles, maxHeight, className)}
aria-labelledby={id}
tabindex={-1}
{id}
bind:this={menuElement}
bind:clientHeight={height}
onfocusout={handleFocusOut}
onclick={handleClick}
{...rest}
>
{@render children?.()}
</ul>
</Portal>
{:else}
<ul
role="menu"
class={merge(styles, maxHeight, className)}
aria-labelledby={id}
tabindex={-1}
style={position === 'top-right' || position === 'top-left'
? `top: -${height + 16}px;`
: ''}
{id}
bind:this={menuElement}
bind:clientHeight={height}
onfocusout={handleFocusOut}
onclick={handleClick}
{...rest}
>
{@render children?.()}
</ul>
{/if}