UNPKG

@osaedasia/oresume

Version:

A user-friendly library for generating complete Single Page Applications (SPAs)

117 lines (116 loc) 4.62 kB
import { Theme } from "./Theme"; import { Observable } from "../observer/Observable"; import { BaseTheme } from "../../domain/definitions/types/theme/BaseTheme"; import { IThemeService } from "../../domain/definitions/interfaces/IThemeService"; /** * Configuration for theme service settings. * @template ThemeName String literal type for theme names. * @template PropName String literal type for theme property names. */ interface ThemeServiceConfig<ThemeName extends string, PropName extends string> { /** * Base theme configuration serving as the default theme. */ defaultTheme: BaseTheme<ThemeName>; /** * Collection of available themes with their specific properties. */ themes: Theme<PropName>[]; } /** * Service managing application themes and system preference synchronization. * Implements singleton pattern and uses observables for dynamic theme tracking. * @template ThemeName String literal type for theme names. * @template PropName String literal type for theme property names. * @implements {IThemeService<ThemeName>} */ export declare class ThemeService<ThemeName extends string = never, PropName extends string = string> implements IThemeService<ThemeName> { /** * Singleton instance of the ThemeService. */ private static _instance; /** @inheritDoc */ readonly currentTheme: Observable<BaseTheme<ThemeName>>; /** * Storage key for theme service data persistence. */ private readonly _storeKey; /** * Media query list for monitoring system theme preferences. */ private readonly _mediaQueryTheme; /** * Collection of theme configurations mapped to their base themes. */ private readonly _themes; /** * Default theme configuration for the application. */ private readonly _defaultTheme; /** * Proxy object mapping property names to their CSS variable representations. * Each property is transformed into a CSS variable string format: var(--propertyName). */ private readonly _propertyVariablesProxy; /** * Creates a new ThemeService instance with the specified configuration. * @param {ThemeServiceConfig<ThemeName, PropName>} config Theme service configuration settings. */ private constructor(); /** @inheritDoc */ get var(): { [K in PropName]: `var(--${K})`; }; /** * Retrieves the singleton instance of ThemeService. * @param {ThemeServiceConfig<ThemeName, PropName>} config Configuration for creating a new instance if none exists. * @returns {ThemeService<ThemeName, PropName>} The singleton ThemeService instance. */ static getInstance<ThemeName extends string = never, PropName extends string = string>(config: ThemeServiceConfig<ThemeName, PropName>): ThemeService<ThemeName, PropName>; /** * Initializes the theme collection with provided themes. * @param {Theme<PropName>[]} themes Array of themes to register. */ private _initializeThemes; /** * Sets up media query listener for system theme changes. */ private _initializeMediaQuery; /** * Registers a new theme in the theme collection. * @param {Theme<PropName>} theme Theme to register. * @throws {DuplicateThemeError} If theme with the same name already exists. */ private _registerTheme; /** * Initializes and configures theme observable with system preferences. * @returns {Observable<BaseTheme<ThemeName>>} Configured theme observable. */ private _initializeThemeObservable; /** * Determines the system theme based on media query. * @param {MediaQueryListEvent | MediaQueryList} query Media query information. * @returns {'light' | 'dark'} Resolved system theme. */ private _resolveSystemTheme; /** * Validates a theme against registered themes. * @param {BaseTheme<ThemeName>} theme - Theme to validate. * @returns {BaseTheme<ThemeName>} Validated theme or default theme. */ private _validateTheme; /** * Resolves the active theme based on current state. * @param {BaseTheme<ThemeName>} state Current theme state. * @returns {[BaseTheme<ThemeName>, Theme<PropName>]} Resolved theme tuple. * @throws {ThemeNotFoundError} If resolved theme doesn't exist. */ private _resolveCurrentTheme; /** * Applies theme to document root element. * @param {BaseTheme<ThemeName>} name Name of theme to apply. * @throws {ThemeNotFoundError} If theme doesn't exist. */ private _applyTheme; } export {};