@blueprintjs/icons
Version:
Components, fonts, icons, and css files for creating and displaying icons.
119 lines • 4.98 kB
JavaScript
;
/*
* Copyright 2026 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.IconsNext = exports.IconVariant = void 0;
const names_1 = require("./generated/names");
const iconNextNames_1 = require("./iconNextNames");
const iconVariant_1 = require("./iconVariant");
const pathsLoader_1 = require("./pathsLoader");
var iconVariant_2 = require("./iconVariant");
Object.defineProperty(exports, "IconVariant", { enumerable: true, get: function () { return iconVariant_2.IconVariant; } });
/**
* Next-generation icon loader. Simplified from `Icons` — no size branching, single cache,
* with outlined/filled variant support.
*/
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class IconsNext {
/** Set of icon names that have filled variants. */
static filledSet = new Set(names_1.nextFilledIconNames);
/** Cache keyed by "iconName:variant". */
static cache = new Map();
/** Icons for which the missing-filled-variant warning has already been emitted. */
static warnedAboutMissingFilledVariant = new Set();
/** Configurable loader function. */
static loader = pathsLoader_1.defaultNextIconPathsLoader;
/**
* Set global loader options for all subsequent `IconsNext.load()` calls.
*/
static setLoaderOptions(options) {
if (options.loader !== undefined) {
this.loader = options.loader;
}
}
/**
* Load one or more next icons for use in Blueprint components.
*/
static async load(icons, variant = iconVariant_1.IconVariant.OUTLINED) {
const iconNames = Array.isArray(icons) ? icons : [icons];
// Validate the whole request before starting any loads so a malformed runtime value cannot
// leave a partially populated cache.
for (const icon of iconNames) {
if (!this.isValidIconName(icon)) {
throw new Error(`[Blueprint] Unknown next icon '${icon}'`);
}
}
await Promise.all(iconNames.map(icon => this.loadImpl(icon, variant)));
}
/**
* Load all next icons (outlined variant). Optionally also load filled variants.
*/
static async loadAll(options) {
await this.load([...names_1.nextIconNames], iconVariant_1.IconVariant.OUTLINED);
if (options?.includeFilled) {
await this.load([...names_1.nextFilledIconNames], iconVariant_1.IconVariant.FILLED);
}
}
/**
* Get the icon SVG paths. Returns `undefined` if the icon has not been loaded yet.
* If `variant="filled"` is requested but unavailable, falls back to outlined.
*/
static getPaths(icon, variant = iconVariant_1.IconVariant.OUTLINED) {
const resolvedVariant = this.resolveVariant(icon, variant);
return this.cache.get(this.cacheKey(icon, resolvedVariant));
}
/**
* Returns `true` if the given icon has a filled variant available.
*/
static hasFilled(icon) {
return this.filledSet.has(icon);
}
/**
* Returns `true` if the given string is a valid next icon name.
*/
static isValidIconName(name) {
return iconNextNames_1.IconNextNamesSet.has(name);
}
static async loadImpl(icon, variant) {
const resolvedVariant = this.resolveVariant(icon, variant);
const key = this.cacheKey(icon, resolvedVariant);
if (this.cache.has(key)) {
return;
}
const paths = await this.loader(icon, resolvedVariant);
this.cache.set(key, paths);
}
static resolveVariant(icon, variant) {
if (variant !== iconVariant_1.IconVariant.FILLED) {
return iconVariant_1.IconVariant.OUTLINED;
}
if (this.filledSet.has(icon)) {
return iconVariant_1.IconVariant.FILLED;
}
if (process.env.NODE_ENV !== "production" &&
iconNextNames_1.IconNextNamesSet.has(icon) &&
!this.warnedAboutMissingFilledVariant.has(icon)) {
this.warnedAboutMissingFilledVariant.add(icon);
console.warn(`[Blueprint] Icon "${icon}" does not have a filled variant. Falling back to outlined.`);
}
return iconVariant_1.IconVariant.OUTLINED;
}
static cacheKey(icon, variant) {
return `${icon}:${variant}`;
}
}
exports.IconsNext = IconsNext;
//# sourceMappingURL=iconLoaderNext.js.map