Move FA kit-specific stuff to the FA icon library

This commit is contained in:
Lea Verou
2025-05-08 14:14:41 -04:00
parent 400db9a608
commit 1559b08a63
3 changed files with 33 additions and 32 deletions

View File

@@ -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.
*
* <script src="bundle.js" data-fa-kit-code="abc123"></script>
*
* 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;

View File

@@ -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.
*
* <script src="bundle.js" data-fa-kit-code="abc123"></script>
*
* 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;
}

View File

@@ -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';