From 1559b08a63b92c26266801e39da9513c290d6b74 Mon Sep 17 00:00:00 2001 From: Lea Verou Date: Thu, 8 May 2025 14:14:41 -0400 Subject: [PATCH] Move FA kit-specific stuff to the FA icon library --- src/components/icon/library.default.ts | 32 +++++++++++++++++++++++++- src/utilities/base-path.ts | 30 ------------------------ src/webawesome.ts | 3 ++- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/components/icon/library.default.ts b/src/components/icon/library.default.ts index 3eda30954..679015e2d 100644 --- a/src/components/icon/library.default.ts +++ b/src/components/icon/library.default.ts @@ -1,6 +1,36 @@ -import { getKitCode } from '../../utilities/base-path.js'; import type { IconLibraryCache, UnregisteredIconLibrary } from './library.js'; +let kitCode = ''; + +/** Sets the library's Web Awesome kit code. */ +export function setKitCode(code: string) { + kitCode = code; +} + +/** + * Gets the library's Web Awesome kit code. + * + * The kit code is used to fetch premium assets, so it needs to be set for certain components to work correctly. This + * isn't something we can infer, so the user will need to provide it using the `data-fa-kit-code` attribute. This can + * be on any element, but ideally it should exist on the script that imports Web Awesome. + * + * + * + * Alternatively, you can set the kit code manually using the exported `setKitCode()` function. + * + */ +export function getKitCode() { + if (!kitCode) { + const el = document.querySelector('[data-fa-kit-code]'); + + if (el) { + setKitCode(el.getAttribute('data-fa-kit-code') || ''); + } + } + + return kitCode; +} + function getIconUrl(name: string, family: string, variant: string) { const kitCode = getKitCode(); const isPro = kitCode.length > 0; diff --git a/src/utilities/base-path.ts b/src/utilities/base-path.ts index 8504de903..789b16075 100644 --- a/src/utilities/base-path.ts +++ b/src/utilities/base-path.ts @@ -1,5 +1,4 @@ let basePath = ''; -let kitCode = ''; /** Sets the library's base path to the specified directory or URL. */ export function setBasePath(path: string) { @@ -49,32 +48,3 @@ export function getBasePath(subpath = '') { // Return the base path without a trailing slash. If one exists, append the subpath separated by a slash. return basePath.replace(/\/$/, '') + (subpath ? `/${subpath.replace(/^\//, '')}` : ``); } - -/** Sets the library's Web Awesome kit code. */ -export function setKitCode(code: string) { - kitCode = code; -} - -/** - * Gets the library's Web Awesome kit code. - * - * The kit code is used to fetch premium assets, so it needs to be set for certain components to work correctly. This - * isn't something we can infer, so the user will need to provide it using the `data-fa-kit-code` attribute. This can - * be on any element, but ideally it should exist on the script that imports Web Awesome. - * - * - * - * Alternatively, you can set the kit code manually using the exported `setKitCode()` function. - * - */ -export function getKitCode() { - if (!kitCode) { - const el = document.querySelector('[data-fa-kit-code]'); - - if (el) { - setKitCode(el.getAttribute('data-fa-kit-code') || ''); - } - } - - return kitCode; -} diff --git a/src/webawesome.ts b/src/webawesome.ts index fae58d0bd..563e7b95e 100644 --- a/src/webawesome.ts +++ b/src/webawesome.ts @@ -1,6 +1,7 @@ +export { getKitCode, setKitCode } from './components/icon/library.default.js'; export { registerIconLibrary, unregisterIconLibrary } from './components/icon/registry.js'; export { discover, preventTurboFouce, startLoader, stopLoader } from './utilities/autoloader.js'; -export { getBasePath, getKitCode, setBasePath, setKitCode } from './utilities/base-path.js'; +export { getBasePath, setBasePath } from './utilities/base-path.js'; export { allDefined } from './utilities/defined.js'; export { registerTranslation } from './utilities/localize.js';