creddy/src/ui/Link.svelte

44 lines
968 B
Svelte
Raw Normal View History

2023-04-24 22:16:25 -07:00
<script>
2023-04-26 13:05:51 -07:00
import { navigate } from '../lib/routing.js';
2023-04-24 22:16:25 -07:00
2023-04-25 08:49:00 -07:00
export let target;
2023-04-24 22:16:25 -07:00
export let hotkey = null;
export let ctrl = false
export let alt = false;
export let shift = false;
let classes = "";
export {classes as class};
2023-04-24 22:16:25 -07:00
function click() {
if (typeof target === 'string') {
2023-04-25 08:49:00 -07:00
navigate(target);
2023-04-24 22:16:25 -07:00
}
else if (typeof target === 'function') {
target();
}
else {
throw(`Link target is not a string or a function: ${target}`)
}
}
function handleHotkey(event) {
if (!hotkey) return;
if (ctrl && !event.ctrlKey) return;
if (alt && !event.altKey) return;
if (shift && !event.shiftKey) return;
2023-04-30 10:52:46 -07:00
if (event.key === hotkey) {
2023-04-25 22:10:14 -07:00
click();
}
2023-04-24 22:16:25 -07:00
}
</script>
<svelte:window on:keydown={handleHotkey} />
2023-04-30 10:52:46 -07:00
<a href="/{target}" on:click|preventDefault="{click}" class={classes}>
2023-04-24 22:16:25 -07:00
<slot></slot>
</a>