/google-fonts-loader-v2

A Google Fonts library specifically designed for the v2 API and variable fonts, which helps to load them in the browser

Primary LanguageTypeScriptMIT LicenseMIT

Google Fonts Loader v2

npm version npm downloads npm downloads npm downloads

A Google Fonts library specifically designed for the v2 API and variable fonts, which helps to load them in the browser.

🎯 Features

  • Google Fonts v2 API
  • Variable fonts
  • Promise that await for a font to be fully loaded
  • List loaded fonts
  • Unloading fonts
  • Type safe/Typescript support

⚙️ Install

npm install google-fonts-loader-v2
# or
yarn add google-fonts-loader-v2
# or
pnpm add google-fonts-loader-v2
# or
bun add google-fonts-loader-v2

📖 Usage

import {loadAwait, list, unload} from "google-fonts-loader-v2";

console.log("Loading Open Sans…");
await loadAwait({
    family: "Open Sans",
    axisTupleList: [
        [
            {tag: "ital", value: 0},
            {tag: "wght", value: 500},
        ],
        [
            {tag: "ital", value: 1},
            {tag: "wght", value: 500},
        ],
    ],
}, {
    display: "swap",
});
console.log("Open Sans loaded!");

console.log(`${list().length} fonts loaded!`);

console.log("Unloading Open Sans…");
unload({
    family: "Open Sans",
});
console.log("Open Sans unloaded!");

📚 Documentation

load()

load(font: Font, options?: Options): void

Just load the given font in the curent page.

loadAwait()

loadAwait(font: Font, options?: Options): Promise<void>

Load the given font and return a promise that resolve when the font is fully loaded.

list()

list(): Font[]

List the currently loaded fonts.

unload()

unload(font: Font): void

Unload the given font family.

unloadAll()

unloadAll(): void

Unload all the previously loaded fonts.

🐜 Known bugs