mirror of
https://github.com/thiloho/aurora.git
synced 2025-11-22 11:31:36 +01:00
Initial commit
This commit is contained in:
49
src/components/Dropdown.svelte
Normal file
49
src/components/Dropdown.svelte
Normal file
@@ -0,0 +1,49 @@
|
||||
<script lang="ts">
|
||||
export let shortcut: string;
|
||||
|
||||
const possibleShortcuts = ["S", "T"];
|
||||
|
||||
let detailsElement: HTMLDetailsElement;
|
||||
let summaryElement: HTMLElement;
|
||||
|
||||
function openMenu() {
|
||||
detailsElement.setAttribute("open", "");
|
||||
}
|
||||
|
||||
export function closeMenu() {
|
||||
detailsElement.removeAttribute("open");
|
||||
}
|
||||
|
||||
function handleKeydown(event: KeyboardEvent) {
|
||||
if (event.key === "Escape") closeMenu();
|
||||
|
||||
if (event.key === shortcut) {
|
||||
summaryElement.focus();
|
||||
openMenu();
|
||||
} else if (possibleShortcuts.includes(event.key) && event.key !== shortcut) {
|
||||
closeMenu();
|
||||
}
|
||||
}
|
||||
|
||||
function handleClick(event: MouseEvent) {
|
||||
if (!detailsElement.contains(event.target as Node)) {
|
||||
closeMenu();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:window on:keydown={handleKeydown} on:click={handleClick} />
|
||||
|
||||
<details bind:this={detailsElement}>
|
||||
<summary bind:this={summaryElement}>
|
||||
<slot name="icon" />
|
||||
</summary>
|
||||
<slot />
|
||||
</details>
|
||||
|
||||
<style>
|
||||
summary {
|
||||
display: grid;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user