2026-01-18 12:57:56 +03:00
|
|
|
<!--
|
|
|
|
|
Component: FontApplicator
|
|
|
|
|
Loads fonts from fontshare with link tag
|
2026-01-18 15:55:07 +03:00
|
|
|
- Loads font only if it's not already applied
|
|
|
|
|
- Uses IntersectionObserver to detect when font is visible
|
|
|
|
|
- Adds smooth transition when font is loaded
|
2026-01-18 12:57:56 +03:00
|
|
|
-->
|
|
|
|
|
<script lang="ts">
|
|
|
|
|
import { cn } from '$shared/shadcn/utils/shadcn-utils';
|
|
|
|
|
import type { Snippet } from 'svelte';
|
2026-01-18 15:55:07 +03:00
|
|
|
import { appliedFontsManager } from '../../model';
|
2026-01-18 12:57:56 +03:00
|
|
|
|
|
|
|
|
interface Props {
|
2026-01-18 15:55:07 +03:00
|
|
|
/**
|
|
|
|
|
* Font name to set
|
|
|
|
|
*/
|
2026-01-18 12:57:56 +03:00
|
|
|
name: string;
|
2026-01-18 15:55:07 +03:00
|
|
|
/**
|
|
|
|
|
* Font id to load
|
|
|
|
|
*/
|
2026-01-18 12:57:56 +03:00
|
|
|
id: string;
|
2026-01-18 15:55:07 +03:00
|
|
|
/**
|
|
|
|
|
* Additional classes
|
|
|
|
|
*/
|
2026-01-18 12:57:56 +03:00
|
|
|
className?: string;
|
2026-01-18 15:55:07 +03:00
|
|
|
/**
|
|
|
|
|
* Children
|
|
|
|
|
*/
|
2026-01-18 12:57:56 +03:00
|
|
|
children?: Snippet;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 14:47:31 +03:00
|
|
|
let { name, id, className, children }: Props = $props();
|
2026-01-18 12:57:56 +03:00
|
|
|
let element: Element;
|
|
|
|
|
|
|
|
|
|
$effect(() => {
|
|
|
|
|
const observer = new IntersectionObserver(entries => {
|
|
|
|
|
if (entries[0].isIntersecting) {
|
|
|
|
|
appliedFontsManager.touch([id]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
observer.observe(element);
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const isLoading = $derived(appliedFontsManager.getFontStatus(id) === 'loading');
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
bind:this={element}
|
|
|
|
|
style:font-family={name}
|
|
|
|
|
class={cn(
|
|
|
|
|
'transition-all duration-200 ease-out',
|
|
|
|
|
isLoading ? 'opacity-0 translate-y-1' : 'opacity-100 translate-y-0',
|
|
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{@render children?.()}
|
|
|
|
|
</div>
|