2024-02-18 03:34:27 +00:00
|
|
|
<script lang="ts">
|
2024-09-05 09:24:24 -04:00
|
|
|
import { t } from 'svelte-i18n';
|
2024-02-18 03:34:27 +00:00
|
|
|
import Button from './button.svelte';
|
2025-04-30 00:19:38 -04:00
|
|
|
import { getTabbable } from '$lib/utils/focus-util';
|
2024-02-18 03:34:27 +00:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
interface Props {
|
|
|
|
|
/**
|
|
|
|
|
* Target for the skip link to move focus to.
|
|
|
|
|
*/
|
|
|
|
|
target?: string;
|
2025-04-01 22:12:04 -04:00
|
|
|
/**
|
|
|
|
|
* Text for the skip link button.
|
|
|
|
|
*/
|
2024-11-14 08:43:25 -06:00
|
|
|
text?: string;
|
2025-04-01 22:12:04 -04:00
|
|
|
/**
|
|
|
|
|
* Breakpoint at which the skip link is visible. Defaults to always being visible.
|
|
|
|
|
*/
|
|
|
|
|
breakpoint?: 'sm' | 'md' | 'lg' | 'xl' | '2xl';
|
2024-11-14 08:43:25 -06:00
|
|
|
}
|
2024-02-18 03:34:27 +00:00
|
|
|
|
2025-04-01 22:12:04 -04:00
|
|
|
let { target = 'main', text = $t('skip_to_content'), breakpoint }: Props = $props();
|
2024-11-14 08:43:25 -06:00
|
|
|
|
|
|
|
|
let isFocused = $state(false);
|
2024-02-18 03:34:27 +00:00
|
|
|
|
|
|
|
|
const moveFocus = () => {
|
|
|
|
|
const targetEl = document.querySelector<HTMLElement>(target);
|
2025-04-30 00:19:38 -04:00
|
|
|
if (targetEl) {
|
|
|
|
|
const element = getTabbable(targetEl)[0];
|
|
|
|
|
if (element) {
|
|
|
|
|
element.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-18 03:34:27 +00:00
|
|
|
};
|
2025-04-01 22:12:04 -04:00
|
|
|
|
|
|
|
|
const getBreakpoint = () => {
|
|
|
|
|
if (!breakpoint) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
switch (breakpoint) {
|
|
|
|
|
case 'sm': {
|
|
|
|
|
return 'hidden sm:block';
|
|
|
|
|
}
|
|
|
|
|
case 'md': {
|
|
|
|
|
return 'hidden md:block';
|
|
|
|
|
}
|
|
|
|
|
case 'lg': {
|
|
|
|
|
return 'hidden lg:block';
|
|
|
|
|
}
|
|
|
|
|
case 'xl': {
|
|
|
|
|
return 'hidden xl:block';
|
|
|
|
|
}
|
|
|
|
|
case '2xl': {
|
|
|
|
|
return 'hidden 2xl:block';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-02-18 03:34:27 +00:00
|
|
|
</script>
|
|
|
|
|
|
2025-04-28 09:53:53 -04:00
|
|
|
<div class="absolute z-50 top-2 start-2 transition-transform {isFocused ? 'translate-y-0' : '-translate-y-10 sr-only'}">
|
2024-02-18 03:34:27 +00:00
|
|
|
<Button
|
2025-03-03 14:24:26 +00:00
|
|
|
size="sm"
|
2024-07-29 16:36:10 +02:00
|
|
|
rounded="none"
|
2024-11-14 08:43:25 -06:00
|
|
|
onclick={moveFocus}
|
2025-04-01 22:12:04 -04:00
|
|
|
class={getBreakpoint()}
|
2024-11-14 08:43:25 -06:00
|
|
|
onfocus={() => (isFocused = true)}
|
|
|
|
|
onblur={() => (isFocused = false)}
|
2024-02-18 03:34:27 +00:00
|
|
|
>
|
2024-09-05 09:24:24 -04:00
|
|
|
{text}
|
2024-02-18 03:34:27 +00:00
|
|
|
</Button>
|
|
|
|
|
</div>
|