2026-01-18 12:57:56 +03:00
|
|
|
<!--
|
|
|
|
|
Component: FontApplicator
|
2026-04-20 22:20:44 +03:00
|
|
|
Applies a font to its children once the font file is loaded.
|
|
|
|
|
Shows the skeleton snippet while loading; falls back to system font if no skeleton is provided.
|
2026-01-18 12:57:56 +03:00
|
|
|
-->
|
|
|
|
|
<script lang="ts">
|
2026-04-17 13:37:44 +03:00
|
|
|
import clsx from 'clsx';
|
2026-01-18 12:57:56 +03:00
|
|
|
import type { Snippet } from 'svelte';
|
2026-02-10 18:09:13 +03:00
|
|
|
import {
|
2026-04-16 09:05:34 +03:00
|
|
|
DEFAULT_FONT_WEIGHT,
|
2026-02-10 18:09:13 +03:00
|
|
|
type UnifiedFont,
|
|
|
|
|
appliedFontsManager,
|
|
|
|
|
} from '../../model';
|
2026-01-18 12:57:56 +03:00
|
|
|
|
|
|
|
|
interface Props {
|
2026-01-18 15:55:07 +03:00
|
|
|
/**
|
2026-03-02 22:18:21 +03:00
|
|
|
* Font to apply
|
2026-01-18 15:55:07 +03:00
|
|
|
*/
|
2026-02-10 18:09:13 +03:00
|
|
|
font: UnifiedFont;
|
2026-01-20 14:21:07 +03:00
|
|
|
/**
|
|
|
|
|
* Font weight
|
2026-03-02 22:18:21 +03:00
|
|
|
* @default 400
|
2026-01-20 14:21:07 +03:00
|
|
|
*/
|
|
|
|
|
weight?: number;
|
2026-01-18 15:55:07 +03:00
|
|
|
/**
|
2026-03-02 22:18:21 +03:00
|
|
|
* CSS classes
|
2026-01-18 15:55:07 +03:00
|
|
|
*/
|
2026-01-18 12:57:56 +03:00
|
|
|
className?: string;
|
2026-01-18 15:55:07 +03:00
|
|
|
/**
|
2026-03-02 22:18:21 +03:00
|
|
|
* Content snippet
|
2026-01-18 15:55:07 +03:00
|
|
|
*/
|
2026-01-18 12:57:56 +03:00
|
|
|
children?: Snippet;
|
2026-04-20 22:20:44 +03:00
|
|
|
/**
|
|
|
|
|
* Shown while the font file is loading.
|
|
|
|
|
* When omitted, children render in system font until ready.
|
|
|
|
|
*/
|
|
|
|
|
skeleton?: Snippet;
|
2026-01-18 12:57:56 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 11:14:22 +03:00
|
|
|
let {
|
|
|
|
|
font,
|
2026-04-16 09:05:34 +03:00
|
|
|
weight = DEFAULT_FONT_WEIGHT,
|
2026-02-12 11:14:22 +03:00
|
|
|
className,
|
|
|
|
|
children,
|
2026-04-20 22:20:44 +03:00
|
|
|
skeleton,
|
2026-02-12 11:14:22 +03:00
|
|
|
}: Props = $props();
|
2026-01-18 12:57:56 +03:00
|
|
|
|
2026-02-12 11:14:22 +03:00
|
|
|
const status = $derived(
|
|
|
|
|
appliedFontsManager.getFontStatus(
|
|
|
|
|
font.id,
|
|
|
|
|
weight,
|
2026-03-02 22:18:21 +03:00
|
|
|
font.features?.isVariable,
|
2026-02-12 11:14:22 +03:00
|
|
|
),
|
|
|
|
|
);
|
2026-02-05 11:44:16 +03:00
|
|
|
|
2026-02-12 11:21:04 +03:00
|
|
|
const shouldReveal = $derived(status === 'loaded' || status === 'error');
|
2026-01-18 12:57:56 +03:00
|
|
|
</script>
|
|
|
|
|
|
2026-04-20 22:20:44 +03:00
|
|
|
{#if !shouldReveal && skeleton}
|
|
|
|
|
{@render skeleton()}
|
|
|
|
|
{:else}
|
|
|
|
|
<div
|
|
|
|
|
style:font-family={shouldReveal ? `'${font.name}'` : 'system-ui, -apple-system, sans-serif'}
|
|
|
|
|
class={clsx(className)}
|
|
|
|
|
>
|
|
|
|
|
{@render children?.()}
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|