@su-labs/font-size
Version:
A lightweight Angular service for dynamic font size management.
129 lines (125 loc) • 4 kB
TypeScript
import * as _angular_core from '@angular/core';
interface FontSizeConfig {
defaultSize: number;
minSize: number;
maxSize: number;
step: number;
storageKey?: string;
cssVarName?: string;
}
declare class FontSizeService {
/**
* Default font size in pixels.
* Can be overridden via {@link configure}.
*/
defaultSize: _angular_core.WritableSignal<number>;
/**
* Minimum allowed font size in pixels.
* Can be overridden via {@link configure}.
*/
minSize: _angular_core.WritableSignal<number>;
/**
* Maximum allowed font size in pixels.
* Can be overridden via {@link configure}.
*/
maxSize: _angular_core.WritableSignal<number>;
/**
* Default step value (in pixels) for increase/decrease operations.
* Can be overridden via {@link configure} or per call in {@link increase} / {@link decrease}.
*/
step: _angular_core.WritableSignal<number>;
/**
* Key used for persisting the font size in localStorage.
* Defaults to `"app:font-size"`.
*/
private storageKey;
/**
* CSS custom property name where the font size is applied.
* Defaults to `"--base-font-size"`.
*/
private cssVarName;
/**
* Current font size state (reactive).
*/
fontSize: _angular_core.WritableSignal<number>;
/**
* True if current font size equals the default size.
*/
isDefault: _angular_core.Signal<boolean>;
/**
* True if current font size equals the maximum size.
*/
isMax: _angular_core.Signal<boolean>;
/**
* True if current font size equals the minimum size.
*/
isMin: _angular_core.Signal<boolean>;
/**
* Updates the configuration for font size handling.
* Any omitted values will fall back to their defaults.
*
* @param cfg Partial configuration object (min, max, default, step, storage key, CSS variable name).
*/
configure(cfg?: Partial<FontSizeConfig>): void;
/**
* Initializes the font size service.
* Loads the persisted value (if available), clamps it,
* applies it to the DOM, and updates internal state.
* Should typically be called once at application startup.
*/
init(): void;
/**
* Sets the font size to a specific value (clamped within min/max).
*
* @param size Desired font size in pixels.
*/
setFontSize(size: number): void;
/**
* Increases the font size.
*
* @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.
*/
increase(step?: number): void;
/**
* Decreases the font size.
*
* @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.
*/
decrease(step?: number): void;
/**
* Resets the font size to the configured {@link defaultSize}.
* Also removes the persisted value from localStorage.
*/
reset(): void;
/**
* Clamps a given value between the configured {@link minSize} and {@link maxSize}.
*
* @param size Value to clamp.
* @returns Clamped value within the allowed range.
*/
private clamp;
/**
* Reads the font size from localStorage.
* Falls back to {@link defaultSize} if no value is stored or value is invalid.
*
* @returns Stored font size or default.
*/
private readFromStorage;
/**
* Persists the current font size in localStorage.
*
* @param size Font size value to persist.
*/
private writeToStorage;
/**
* Applies the current font size to the DOM
* using the configured CSS custom property name.
*
* @param size Font size value in pixels.
*/
private applyCssVar;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<FontSizeService, never>;
static ɵprov: _angular_core.ɵɵInjectableDeclaration<FontSizeService>;
}
export { FontSizeService };
export type { FontSizeConfig };