2025-06-05 21:29:07 +02:00
|
|
|
<script lang="ts">
|
2026-08-04 13:37:44 +02:00
|
|
|
let { children, onclose }: {
|
2025-10-13 16:05:50 +02:00
|
|
|
children?: import('svelte').Snippet;
|
2026-08-04 13:37:44 +02:00
|
|
|
onclose?: () => void
|
2025-10-13 16:05:50 +02:00
|
|
|
} = $props();
|
|
|
|
|
let dialog: HTMLDialogElement = $state()
|
2025-06-05 21:29:07 +02:00
|
|
|
|
2026-01-30 12:26:36 +01:00
|
|
|
export const open = (origin: DOMRect|MouseEvent) => {
|
2025-06-05 21:29:07 +02:00
|
|
|
// Show the window so we can get the location
|
|
|
|
|
dialog.showModal()
|
|
|
|
|
|
2026-08-04 13:37:44 +02:00
|
|
|
const edge_offset = 10
|
2025-06-05 21:29:07 +02:00
|
|
|
|
|
|
|
|
// Get the egdes of the screen, so the window does not spawn off-screen
|
2025-06-12 11:32:56 +02:00
|
|
|
const dialog_rect = dialog.getBoundingClientRect()
|
2026-08-04 13:37:44 +02:00
|
|
|
const max_left = document.documentElement.clientWidth - dialog_rect.width - edge_offset
|
|
|
|
|
const max_top = document.documentElement.clientHeight - dialog_rect.height - edge_offset
|
2025-06-12 11:32:56 +02:00
|
|
|
|
2026-01-30 12:26:36 +01:00
|
|
|
let min_left: number, min_top: number
|
|
|
|
|
if (origin instanceof DOMRect) {
|
|
|
|
|
// Position the dialog in horizontally in the center of the button and
|
|
|
|
|
// verticially below it
|
|
|
|
|
min_left = Math.max((origin.left + (origin.width/2)) - (dialog_rect.width/2), edge_offset)
|
|
|
|
|
min_top = Math.max(origin.bottom, edge_offset)
|
|
|
|
|
} else if (origin instanceof MouseEvent) {
|
|
|
|
|
// Place the dialog at the bottom right of the mouse pointer, like
|
|
|
|
|
// regular context menus
|
|
|
|
|
min_left = Math.max(origin.clientX, edge_offset)
|
|
|
|
|
min_top = Math.max(origin.clientY, edge_offset)
|
|
|
|
|
}
|
2025-06-05 21:29:07 +02:00
|
|
|
|
|
|
|
|
// Place the window
|
|
|
|
|
dialog.style.left = Math.round(Math.min(min_left, max_left)) + "px"
|
|
|
|
|
dialog.style.top = Math.round(Math.min(min_top, max_top)) + "px"
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-09 15:48:23 +02:00
|
|
|
export const close = () => {
|
|
|
|
|
dialog.close()
|
2026-08-04 13:37:44 +02:00
|
|
|
if (onclose !== undefined) {
|
|
|
|
|
onclose()
|
|
|
|
|
}
|
2025-10-09 15:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-05 21:29:07 +02:00
|
|
|
// Attach a click handler so that the dialog is closed when the user clicks
|
|
|
|
|
// outside the dialog. The way this check works is that there is usually an
|
|
|
|
|
// element inside the dialog with all the content. When the click target is
|
|
|
|
|
// the dialog itself then the click was on the dialog background
|
|
|
|
|
const click = (e: MouseEvent) => {
|
|
|
|
|
if (e.target === dialog) {
|
2026-01-30 12:26:36 +01:00
|
|
|
e.preventDefault()
|
2026-08-04 13:37:44 +02:00
|
|
|
close()
|
2025-06-05 21:29:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
2025-10-13 16:05:50 +02:00
|
|
|
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
|
|
|
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
|
2026-01-30 12:26:36 +01:00
|
|
|
<dialog bind:this={dialog} onclick={click} oncontextmenu={click}>
|
2025-10-13 16:05:50 +02:00
|
|
|
{@render children?.()}
|
2025-06-05 21:29:07 +02:00
|
|
|
</dialog>
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
dialog {
|
|
|
|
|
background-color: var(--card_color);
|
|
|
|
|
color: var(--body_text_color);
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
border: none;
|
|
|
|
|
padding: 4px;
|
|
|
|
|
margin: 0;
|
|
|
|
|
box-shadow: 2px 2px 10px -4px #000000;
|
|
|
|
|
}
|
|
|
|
|
</style>
|