UNPKG

@equinor/fusion-react-styles

Version:
96 lines 3.9 kB
import type { Jss } from 'jss'; import type { StyleRules } from '../types'; /** * Map of class names generated by JSS * Keys are the style rule names, values are the generated CSS class names */ export interface ClassNameMap { [key: string]: string; } /** * Result of getting or creating a stylesheet */ export interface SheetResult { /** Map of class names (rule key -> CSS class name) */ classes: ClassNameMap; /** Instance ID for dynamic sheet cleanup (only present if dynamic styles exist) */ instanceId?: string; /** Cache key used for this sheet */ cacheKey: string; } /** * Simple sheet manager for caching and reusing JSS stylesheets * * This manager implements a caching strategy to avoid recreating stylesheets * for the same styles/theme combination. It separates static and dynamic styles * for optimal performance. * * Key features: * - Reference counting for cleanup * - Separation of static and dynamic styles * - Caching based on name + theme combination * - Automatic cleanup when sheets are no longer referenced * - Hot-reload support: detects style changes and updates existing sheets */ declare class SheetManager { /** Cache of stylesheets keyed by name-theme combination */ private sheets; /** Counter for generating unique instance IDs for dynamic sheets */ private instanceCounter; /** Map of style hashes to cache keys for hot-reload detection (dev only) */ private stylesHashToKey; /** * Gets or creates a JSS stylesheet for the given styles * * This method implements a caching strategy: * 1. Creates a cache key from cacheKey + theme (or name + theme if cacheKey not provided) * 2. If cached, increments reference count and returns cached classes * 3. If not cached, creates static sheet and extracts dynamic styles * 4. For dynamic styles, creates a linked sheet that updates with props * 5. Merges static and dynamic class names * * @param styles - Style rules object * @param theme - Theme object * @param name - Name prefix for the JSS stylesheet (used by JSS) * @param jss - JSS instance * @param generateClassName - Class name generator function * @param props - Props for dynamic styles (optional) * @param cacheKey - Optional cache key (if not provided, uses name) * @param isFirstRender - Whether this is the first render of the component (increments refs) * @returns Result containing class names, instance ID (if dynamic), and cache key */ getOrCreateSheet(styles: StyleRules, theme: unknown, name: string, jss: Jss, generateClassName: (rule: { key: string; }, sheet?: unknown) => string, props?: Record<string, unknown>, isFirstRender?: boolean): SheetResult; /** * Removes a dynamic sheet instance without decrementing ref count * Used when component props change and a new dynamic sheet is created * * @param key - Cache key for the stylesheet * @param instanceId - Instance ID for the dynamic sheet to remove */ removeDynamicSheet(key: string, instanceId: string): void; /** * Removes a reference to a sheet and cleans up if no longer needed * * @param key - Cache key for the stylesheet * @param instanceId - Optional instance ID for dynamic sheet cleanup */ removeSheet(key: string, instanceId?: string): void; /** * Clears all sheets * * This method detaches all sheets and clears the cache. * Useful for testing or full application teardown. * * @internal This is primarily used for testing and cleanup scenarios */ clear(): void; } /** * Default sheet manager instance */ export declare const defaultSheetManager: SheetManager; export { SheetManager }; export type { SheetManager as SheetManagerType }; //# sourceMappingURL=sheet-manager.d.ts.map