Skip to content

Favicon

Reactive Favicon

๐ŸŽฌ Usage

<script>
    import { favicon } from "@sveu/browser"

    const icon = favicon()
</script>

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

๐Ÿ‘ป Arguments

Name Description Type Required
icon The icon to set string No

๐Ÿ™ˆ Options

Name Description Type default
base_url The base url of the icon string /
rel The rel of the icon string icon

โ†ฉ๏ธ Returns

A watchable store of the favicon.

๐Ÿงช Playground

StackBlitz

Source Code ๐Ÿ‘€

Source Code
import { is_string, watchable } from "@sveu/shared"

import type { FaviconOptions } from "../utils"

/**
 * Reactive favicon.
 *
 * @param icon - The icon to use.
 *
 * @param options - Options to use:
 * - `base_url` - The base url to prepend to the favicon.
 * - `rel` - The rel attribute of the favicon.
 *
 * @returns A watchable store of the favicon.
 */
export function favicon(
    icon?: string | null | undefined,
    options: FaviconOptions = {}
) {
    const { base_url = "/", rel = "icon" } = options

    function apply_icon(icon: string) {
        document?.head
            .querySelectorAll<HTMLLinkElement>(`link[rel*="${rel}"]`)
            .forEach((el) => (el.href = `${base_url}${icon}`))
    }

    const favicon = watchable(icon, (o, n) => {
        if (!!n && is_string(n) && o !== n) apply_icon(n)
    })

    return favicon
}

Last update: 2023-03-09