UNPKG

lit-css-vars

Version:

For easily creating and sharing typed CSS vars for the lit.dev ecosystem.

57 lines (56 loc) 2.03 kB
import { ensureErrorAndPrependMessage, stringify } from '@augment-vir/common'; /** * Class for storing CSS property registrations. An instance is automatically constructed at * {@link cssPropertyRegistry}. * * @category Internal */ export class CssPropertyRegistry { static cssPropertyDefinitionSupported = !!( // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition (globalThis.CSS && globalThis.CSS.registerProperty)); registry = new Map(); constructor() { /* node:coverage ignore next 4 */ const originalRegisterProperty = CssPropertyRegistry.cssPropertyDefinitionSupported ? globalThis.CSS.registerProperty.bind(globalThis.CSS) : undefined; if (originalRegisterProperty) { globalThis.CSS.registerProperty = (definition) => { cssPropertyRegistry.registry.set(definition.name, definition); return originalRegisterProperty(definition); }; } } /** * Detects if the name can be registered. Also checks if `globalThis.CSS.registerProperty` is * supported at all. */ canRegisterCssProperty(name) { return CssPropertyRegistry.cssPropertyDefinitionSupported && !this.registry.has(name); } /** * Register a property only if registration is supported and if the CSS var has not already been * registered. * * @returns `true` if the definition was registered, `false` otherwise. */ registerProperty(definition) { if (!this.canRegisterCssProperty(definition.name)) { return false; } try { globalThis.CSS.registerProperty(definition); return true; } catch (error) { throw ensureErrorAndPrependMessage(error, `Failed to define CSS var: ${stringify(definition, 4)}\n\n`); } } } /** * An instance of {@link CssPropertyRegistry}. * * @category Internal */ export const cssPropertyRegistry = new CssPropertyRegistry();