@su-labs/font-size 22.0.0 → 22.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [22.1.0] - 2026-11-05
9
+
10
+ ### Changed
11
+ - Font size logic moved to the framework-agnostic `@su-labs/core/font-size`; `FontSizeService` is now a thin wrapper with the same API
12
+ - `FontSizeConfig` is now a type alias built from the core types (same fields)
13
+ - Change limits and step with `configure()`. Writing to the `minSize`, `maxSize`, `defaultSize` or `step` signals directly no longer affects clamping or `reset()`
14
+ - Added `@su-labs/core` as a dependency
15
+ - README links to the `@su-labs/core` installation guides for non-Angular projects
16
+
17
+ ### Fixed
18
+ - Safe to import during server-side rendering (no `localStorage` access without a browser)
19
+ - README now documents the actual API
20
+ - Repair table of contents links in README.md
21
+
8
22
  ## [22.0.0] - 2026-11-04
9
23
 
10
24
  ### Changed
package/README.md CHANGED
@@ -13,20 +13,22 @@
13
13
 
14
14
  An accessible Angular service for managing font size preferences. Provides reactive controls for text scaling, user preference persistence, and WCAG-compliant font size management.
15
15
 
16
+ > **Not using Angular?** The same font size controls are available for React, Svelte and vanilla JavaScript in [`@su-labs/core`](https://www.npmjs.com/package/@su-labs/core). See the [installation guides](https://github.com/Greezaaa/settings-utils-lib/blob/main/docs/README.md).
17
+
16
18
  ## Table of Contents
17
19
 
18
- - [Why Use This Library?](#-why-use-this-library)
20
+ - [Why Use This Library?](#why-use-this-library)
19
21
  - [Features](#features)
20
22
  - [Installation](#installation)
21
- - [Quick Start](#-quick-start)
23
+ - [Quick Start](#quick-start)
22
24
  - [Usage](#usage)
23
- - [API Reference](#-api-reference)
25
+ - [API Reference](#api-reference)
24
26
  - [Configuration Options](#configuration-options)
25
27
  - [Accessibility](#accessibility)
26
- - [Browser Compatibility](#-browser-compatibility)
27
- - [Related Libraries](#-related-libraries)
28
- - [Troubleshooting](#-troubleshooting)
29
- - [Support](#-support)
28
+ - [Browser Compatibility](#browser-compatibility)
29
+ - [Related Libraries](#related-libraries)
30
+ - [Troubleshooting](#troubleshooting)
31
+ - [Support](#support)
30
32
  - [Contributing](#contributing)
31
33
  - [License](#license)
32
34
 
@@ -34,22 +36,20 @@ An accessible Angular service for managing font size preferences. Provides react
34
36
 
35
37
  - **Lightweight** - Minimal bundle size
36
38
  - **Reactive** - Built with Angular Signals
37
- - **Accessible** - WCAG 2.1 AA compliant
39
+ - **Accessible** - Helps meet WCAG 2.1 text resizing guidelines
38
40
  - **Persistent** - Remembers user preference
39
41
  - **CSS Variables** - Modern implementation
42
+ - **SSR Safe** - No browser APIs are touched on the server
40
43
  - **Type-Safe** - Full TypeScript support
41
- - **Zero Dependencies** - No bloat
42
44
  - **Well Tested** - Comprehensive test coverage
43
- - **Well Documented** - Clear examples and API docs
44
45
 
45
46
  ## Features
46
47
 
47
- * **Reactive State**: Uses Angular signals for efficient, reactive font size management.
48
- * **Persistence**: Remembers the user's font size preference using `localStorage`.
49
- * **Multiple Presets**: Support for small, normal, large, and extra-large font sizes.
50
- * **CSS Variables**: Applies font sizes using CSS custom properties for maximum flexibility.
51
- * **Accessibility**: Helps users with visual impairments customize text size for better readability.
52
- * **Configurable**: Customizable storage keys, CSS variable names, and size scales.
48
+ * **Reactive State**: The size, limits and `isDefault` / `isMin` / `isMax` flags are Angular signals.
49
+ * **Bounded**: The size always stays between the configured minimum and maximum.
50
+ * **Persistence**: Remembers the user's font size in `localStorage`.
51
+ * **CSS Variable**: Applies the size in pixels to a CSS custom property on `<html>`.
52
+ * **Configurable**: Customizable limits, step, storage key and CSS variable name.
53
53
 
54
54
  ## Installation
55
55
 
@@ -69,189 +69,150 @@ npm install @angular/common@^22 @angular/core@^22
69
69
 
70
70
  ## Quick Start
71
71
 
72
- ```typescript
73
- // 1. Install
74
- npm install @su-labs/font-size
72
+ 1. Initialize the service in `app.config.ts`:
75
73
 
76
- // 2. Initialize in app.config.ts
77
- import { provideAppInitializer, inject } from '@angular/core';
78
- import { SuFontSizeService } from '@su-labs/font-size';
74
+ ```typescript
75
+ import { ApplicationConfig, inject, provideAppInitializer } from '@angular/core';
76
+ import { FontSizeService } from '@su-labs/font-size';
79
77
 
80
- export const appConfig = {
78
+ export const appConfig: ApplicationConfig = {
81
79
  providers: [
82
80
  provideAppInitializer(() => {
83
- inject(SuFontSizeService).init();
81
+ inject(FontSizeService).init();
84
82
  }),
85
83
  ],
86
84
  };
87
-
88
- // 3. Create font size controls
89
- import { Component, inject } from '@angular/core';
90
- import { SuFontSizeService } from '@su-labs/font-size';
91
-
92
- @Component({
93
- template: `
94
- <button (click)="fontService.setSize('small')">A-</button>
95
- <button (click)="fontService.setSize('normal')">A</button>
96
- <button (click)="fontService.setSize('large')">A+</button>
97
- <p>Current: {{ fontService.size() }}</p>
98
- `
99
- })
100
- export class AppComponent {
101
- fontService = inject(SuFontSizeService);
102
- }
103
-
104
- // 4. Use CSS variables in your styles
105
- body {
106
- font-size: var(--su-font-size-base);
107
- }
108
85
  ```
109
86
 
110
- ## Usage
111
-
112
- ### Example 1: Basic Font Size Controls
87
+ 2. Add font size controls:
113
88
 
114
89
  ```typescript
115
90
  import { Component, inject } from '@angular/core';
116
- import { SuFontSizeService, FontSize } from '@su-labs/font-size';
91
+ import { FontSizeService } from '@su-labs/font-size';
117
92
 
118
93
  @Component({
119
94
  selector: 'app-font-controls',
120
- standalone: true,
121
95
  template: `
122
- <div class="font-controls">
123
- <h3>Text Size</h3>
124
- <div class="button-group">
125
- @for (size of fontSizes; track size) {
126
- <button
127
- (click)="setFontSize(size)"
128
- [class.active]="fontService.size() === size"
129
- >
130
- {{ size | titlecase }}
131
- </button>
132
- }
133
- </div>
134
- <p>Current size: {{ fontService.size() }}</p>
135
- </div>
96
+ <button type="button" (click)="fontSize.decrease()" [disabled]="fontSize.isMin()">A-</button>
97
+ <button type="button" (click)="fontSize.reset()" [disabled]="fontSize.isDefault()">A</button>
98
+ <button type="button" (click)="fontSize.increase()" [disabled]="fontSize.isMax()">A+</button>
99
+ <p>Current: {{ fontSize.fontSize() }}px</p>
136
100
  `,
137
- styles: [`
138
- .button-group button.active {
139
- background-color: var(--su-theme-primary);
140
- color: white;
141
- }
142
- `]
143
101
  })
144
102
  export class FontControlsComponent {
145
- fontService = inject(SuFontSizeService);
146
- fontSizes: FontSize[] = ['small', 'normal', 'large', 'x-large'];
103
+ protected readonly fontSize = inject(FontSizeService);
104
+ }
105
+ ```
147
106
 
148
- setFontSize(size: FontSize) {
149
- this.fontService.setSize(size);
150
- }
107
+ 3. Use the CSS variable in your styles:
108
+
109
+ ```css
110
+ html {
111
+ font-size: var(--base-font-size);
151
112
  }
152
113
  ```
153
114
 
154
- ### Example 2: Custom Configuration
115
+ ## Usage
116
+
117
+ ### Custom Configuration
118
+
119
+ Call `configure()` before `init()`:
155
120
 
156
121
  ```typescript
157
- import { provideAppInitializer, inject } from '@angular/core';
158
- import { SuFontSizeService, SuFontSizeConfig } from '@su-labs/font-size';
159
-
160
- const fontConfig: SuFontSizeConfig = {
161
- defaultSize: 'normal',
162
- storageKey: 'myApp:fontSize',
163
- cssVarPrefix: '--app-font-',
164
- sizes: {
165
- small: '14px',
166
- normal: '16px',
167
- large: '18px',
168
- 'x-large': '20px'
169
- }
170
- };
122
+ import { ApplicationConfig, inject, provideAppInitializer } from '@angular/core';
123
+ import { FontSizeService } from '@su-labs/font-size';
171
124
 
172
- export const appConfig = {
125
+ export const appConfig: ApplicationConfig = {
173
126
  providers: [
174
127
  provideAppInitializer(() => {
175
- inject(SuFontSizeService).init(fontConfig);
128
+ const fontSize = inject(FontSizeService);
129
+ fontSize.configure({
130
+ defaultSize: 16,
131
+ minSize: 12,
132
+ maxSize: 24,
133
+ step: 2,
134
+ storageKey: 'myApp:fontSize',
135
+ cssVarName: '--app-font-size',
136
+ });
137
+ fontSize.init();
176
138
  }),
177
139
  ],
178
140
  };
179
141
  ```
180
142
 
181
- ### Example 3: Using in Your Styles
143
+ ### Using the Size in Your Styles
182
144
 
183
- ```css
184
- /* Global styles.css */
185
- :root {
186
- --su-font-size-base: 16px;
187
- --su-font-size-small: 14px;
188
- --su-font-size-large: 18px;
189
- }
145
+ Set the variable on the root element and size everything else in `rem`, so the whole page scales:
190
146
 
191
- body {
192
- font-size: var(--su-font-size-base);
147
+ ```css
148
+ html {
149
+ font-size: var(--base-font-size);
193
150
  }
194
151
 
195
152
  h1 {
196
- font-size: calc(var(--su-font-size-base) * 2);
153
+ font-size: 2rem;
197
154
  }
198
155
 
199
- h2 {
200
- font-size: calc(var(--su-font-size-base) * 1.5);
201
- }
202
-
203
- .small-text {
204
- font-size: var(--su-font-size-small);
205
- }
206
-
207
- .large-text {
208
- font-size: var(--su-font-size-large);
156
+ small {
157
+ font-size: 0.875rem;
209
158
  }
210
159
  ```
211
160
 
212
161
  ## API Reference
213
162
 
214
- ### Service (SuFontSizeService)
163
+ ### Service (FontSizeService)
215
164
 
216
165
  #### Signals
217
166
 
218
167
  | Signal | Type | Description |
219
168
  |--------|------|-------------|
220
- | `size()` | `FontSize` | Current font size setting |
169
+ | `fontSize()` | `number` | Current font size in pixels |
170
+ | `defaultSize()` | `number` | Default size |
171
+ | `minSize()` | `number` | Minimum size |
172
+ | `maxSize()` | `number` | Maximum size |
173
+ | `step()` | `number` | Step used by `increase()` / `decrease()` |
174
+ | `isDefault()` | `boolean` | True when the size equals the default |
175
+ | `isMin()` | `boolean` | True when the size equals the minimum |
176
+ | `isMax()` | `boolean` | True when the size equals the maximum |
177
+
178
+ Change the limits and step with `configure()`, not by writing to their signals.
221
179
 
222
180
  #### Methods
223
181
 
224
182
  | Method | Parameters | Description |
225
183
  |--------|------------|-------------|
226
- | `init(config?)` | `SuFontSizeConfig` | Initialize with custom configuration |
227
- | `setSize(size, persist?)` | `FontSize, boolean` | Change font size (persist defaults to true) |
228
- | `getSize()` | - | Returns current font size |
229
- | `increase()` | - | Increase to next larger size |
230
- | `decrease()` | - | Decrease to next smaller size |
184
+ | `configure(config?)` | `Partial<FontSizeConfig>` | Update limits, step, storage key or CSS variable name. Omitted values keep their current setting. |
185
+ | `init()` | - | Load the saved size (or the default) and apply it. Call once at startup. |
186
+ | `setFontSize(size)` | `number` | Set, apply and save a size (clamped to the limits) |
187
+ | `increase(step?)` | `number` | Increase by `step` (defaults to the configured step) |
188
+ | `decrease(step?)` | `number` | Decrease by `step` (defaults to the configured step) |
189
+ | `reset()` | - | Return to the default size and remove the saved value |
231
190
 
232
191
  ### Types
233
192
 
234
193
  ```typescript
235
- type FontSize = 'small' | 'normal' | 'large' | 'x-large';
236
-
237
- interface SuFontSizeConfig {
238
- storageKey?: string; // Default: 'su:fontSize'
239
- cssVarPrefix?: string; // Default: '--su-font-size-'
240
- defaultSize?: FontSize; // Default: 'normal'
241
- sizes?: {
242
- [key in FontSize]?: string; // Custom pixel/rem values
243
- };
244
- }
194
+ type FontSizeConfig = {
195
+ defaultSize: number;
196
+ minSize: number;
197
+ maxSize: number;
198
+ step: number;
199
+ storageKey?: string;
200
+ cssVarName?: string;
201
+ };
245
202
  ```
246
203
 
247
204
  ## Configuration Options
248
205
 
249
206
  | Option | Description | Default Value |
250
207
  |--------|-------------|---------------|
251
- | `storageKey` | The key used to persist font size in `localStorage` | `'su:fontSize'` |
252
- | `cssVarPrefix` | Prefix for CSS variables | `'--su-font-size-'` |
253
- | `defaultSize` | Default font size if none saved | `'normal'` |
254
- | `sizes` | Custom size values (px, rem, em) | `{ small: '14px', normal: '16px', large: '18px', 'x-large': '20px' }` |
208
+ | `defaultSize` | Size used before the user picks one, and after `reset()` | `14` |
209
+ | `minSize` | Smallest allowed size | `10` |
210
+ | `maxSize` | Largest allowed size | `22` |
211
+ | `step` | Pixels added or removed by `increase()` / `decrease()` | `2` |
212
+ | `storageKey` | The key used to persist the size in `localStorage` | `'su:font-size'` |
213
+ | `cssVarName` | CSS custom property set on `<html>` | `'--base-font-size'` |
214
+
215
+ Invalid combinations are corrected: a maximum below the minimum is raised to the minimum, the default is clamped to the range, and a step of zero or less becomes `1`.
255
216
 
256
217
  ## Accessibility
257
218
 
@@ -301,7 +262,7 @@ Make sure localStorage is enabled and not blocked by privacy settings.
301
262
 
302
263
  ### CSS variables not updating?
303
264
 
304
- Ensure you're using the correct prefix in your CSS (`--su-font-size-` by default) and that you've initialized the service.
265
+ Ensure you're using the correct variable name in your CSS (`--base-font-size` by default) and that you've called `init()`.
305
266
 
306
267
  ### Layout breaking at larger sizes?
307
268
 
@@ -1,39 +1,31 @@
1
1
  import * as i0 from "@angular/core";
2
2
  import { Injectable, computed, signal } from "@angular/core";
3
+ import { SuFontSizeController } from "@su-labs/core/font-size";
3
4
  var FontSizeService = class FontSizeService {
4
- defaultSize = signal(14, ...ngDevMode ? [{ debugName: "defaultSize" }] : /* istanbul ignore next */ []);
5
- minSize = signal(10, ...ngDevMode ? [{ debugName: "minSize" }] : /* istanbul ignore next */ []);
6
- maxSize = signal(22, ...ngDevMode ? [{ debugName: "maxSize" }] : /* istanbul ignore next */ []);
7
- step = signal(2, ...ngDevMode ? [{ debugName: "step" }] : /* istanbul ignore next */ []);
8
- storageKey = "su:font-size";
9
- cssVarName = "--base-font-size";
10
- fontSize = signal(this.readFromStorage(), ...ngDevMode ? [{ debugName: "fontSize" }] : /* istanbul ignore next */ []);
5
+ controller = new SuFontSizeController();
6
+ initialLimits = this.controller.limits.get();
7
+ defaultSize = signal(this.initialLimits.defaultSize, ...ngDevMode ? [{ debugName: "defaultSize" }] : /* istanbul ignore next */ []);
8
+ minSize = signal(this.initialLimits.minSize, ...ngDevMode ? [{ debugName: "minSize" }] : /* istanbul ignore next */ []);
9
+ maxSize = signal(this.initialLimits.maxSize, ...ngDevMode ? [{ debugName: "maxSize" }] : /* istanbul ignore next */ []);
10
+ step = signal(this.initialLimits.step, ...ngDevMode ? [{ debugName: "step" }] : /* istanbul ignore next */ []);
11
+ fontSize = signal(this.controller.fontSize.get(), ...ngDevMode ? [{ debugName: "fontSize" }] : /* istanbul ignore next */ []);
11
12
  isDefault = computed(() => this.fontSize() === this.defaultSize(), ...ngDevMode ? [{ debugName: "isDefault" }] : /* istanbul ignore next */ []);
12
13
  isMax = computed(() => this.fontSize() === this.maxSize(), ...ngDevMode ? [{ debugName: "isMax" }] : /* istanbul ignore next */ []);
13
14
  isMin = computed(() => this.fontSize() === this.minSize(), ...ngDevMode ? [{ debugName: "isMin" }] : /* istanbul ignore next */ []);
15
+ unsubscribers = [this.controller.fontSize.subscribe((size) => this.fontSize.set(size)), this.controller.limits.subscribe((limits) => {
16
+ this.defaultSize.set(limits.defaultSize);
17
+ this.minSize.set(limits.minSize);
18
+ this.maxSize.set(limits.maxSize);
19
+ this.step.set(limits.step);
20
+ })];
14
21
  configure(cfg = {}) {
15
- if (cfg.minSize !== void 0) this.minSize.set(cfg.minSize);
16
- if (cfg.maxSize !== void 0) this.maxSize.set(cfg.maxSize);
17
- if (cfg.defaultSize !== void 0) this.defaultSize.set(cfg.defaultSize);
18
- if (cfg.step !== void 0) this.step.set(cfg.step);
19
- if (cfg.storageKey) this.storageKey = cfg.storageKey;
20
- if (cfg.cssVarName) this.cssVarName = cfg.cssVarName;
21
- if (this.minSize() > this.maxSize()) this.maxSize.set(this.minSize());
22
- if (this.defaultSize() < this.minSize()) this.defaultSize.set(this.minSize());
23
- if (this.defaultSize() > this.maxSize()) this.defaultSize.set(this.maxSize());
24
- if (this.step() <= 0 || !Number.isFinite(this.step())) this.step.set(1);
25
- this.fontSize.update((s) => this.clamp(s));
22
+ this.controller.configure(cfg);
26
23
  }
27
24
  init() {
28
- const size = this.readFromStorage();
29
- this.fontSize.set(this.clamp(size));
30
- this.applyCssVar(this.fontSize());
25
+ this.controller.init();
31
26
  }
32
27
  setFontSize(size) {
33
- const clamped = this.clamp(size);
34
- this.fontSize.set(clamped);
35
- this.applyCssVar(clamped);
36
- this.writeToStorage(clamped);
28
+ this.controller.setFontSize(size);
37
29
  }
38
30
  increase(step) {
39
31
  this.setFontSize(this.fontSize() + (step ?? this.step()));
@@ -42,24 +34,10 @@ var FontSizeService = class FontSizeService {
42
34
  this.setFontSize(this.fontSize() - (step ?? this.step()));
43
35
  }
44
36
  reset() {
45
- const d = this.defaultSize();
46
- this.fontSize.set(d);
47
- this.applyCssVar(d);
48
- localStorage.removeItem(this.storageKey);
37
+ this.controller.reset();
49
38
  }
50
- clamp(size) {
51
- return Math.min(Math.max(size, this.minSize()), this.maxSize());
52
- }
53
- readFromStorage() {
54
- const stored = localStorage.getItem(this.storageKey);
55
- const parsed = stored ? Number(stored) : this.defaultSize();
56
- return Number.isFinite(parsed) ? parsed : this.defaultSize();
57
- }
58
- writeToStorage(size) {
59
- localStorage.setItem(this.storageKey, String(size));
60
- }
61
- applyCssVar(size) {
62
- document.documentElement.style.setProperty(this.cssVarName, `${size}px`);
39
+ ngOnDestroy() {
40
+ this.unsubscribers.forEach((unsubscribe) => unsubscribe());
63
41
  }
64
42
  static ɵfac = i0.ɵɵngDeclareFactory({
65
43
  minVersion: "12.0.0",
@@ -1 +1 @@
1
- {"version":3,"file":"su-labs-font-size.mjs","names":[],"sources":["../../../../projects/su-labs/font-size/src/lib/font-size.service.ts"],"sourcesContent":["import { Injectable, signal, computed } from '@angular/core';\nimport { FontSizeConfig } from './font-size-config.interface';\n\n@Injectable({ providedIn: 'root' })\nexport class FontSizeService {\n /**\n * Default font size in pixels.\n * Can be overridden via {@link configure}.\n */\n defaultSize = signal(14);\n\n /**\n * Minimum allowed font size in pixels.\n * Can be overridden via {@link configure}.\n */\n minSize = signal(10);\n\n /**\n * Maximum allowed font size in pixels.\n * Can be overridden via {@link configure}.\n */\n maxSize = signal(22);\n\n /**\n * Default step value (in pixels) for increase/decrease operations.\n * Can be overridden via {@link configure} or per call in {@link increase} / {@link decrease}.\n */\n step = signal(2);\n\n /**\n * Key used for persisting the font size in localStorage.\n * Defaults to `\"app:font-size\"`.\n */\n private storageKey = 'su:font-size';\n\n /**\n * CSS custom property name where the font size is applied.\n * Defaults to `\"--base-font-size\"`.\n */\n private cssVarName = '--base-font-size';\n\n /**\n * Current font size state (reactive).\n */\n fontSize = signal<number>(this.readFromStorage());\n\n /**\n * True if current font size equals the default size.\n */\n isDefault = computed(() => this.fontSize() === this.defaultSize());\n\n /**\n * True if current font size equals the maximum size.\n */\n isMax = computed(() => this.fontSize() === this.maxSize());\n\n /**\n * True if current font size equals the minimum size.\n */\n isMin = computed(() => this.fontSize() === this.minSize());\n\n /**\n * Updates the configuration for font size handling.\n * Any omitted values will fall back to their defaults.\n *\n * @param cfg Partial configuration object (min, max, default, step, storage key, CSS variable name).\n */\n configure(cfg: Partial<FontSizeConfig> = {}): void {\n if (cfg.minSize !== undefined) this.minSize.set(cfg.minSize);\n if (cfg.maxSize !== undefined) this.maxSize.set(cfg.maxSize);\n if (cfg.defaultSize !== undefined) this.defaultSize.set(cfg.defaultSize);\n if (cfg.step !== undefined) this.step.set(cfg.step);\n if (cfg.storageKey) this.storageKey = cfg.storageKey;\n if (cfg.cssVarName) this.cssVarName = cfg.cssVarName;\n\n // Ensure logical invariants\n if (this.minSize() > this.maxSize()) this.maxSize.set(this.minSize());\n if (this.defaultSize() < this.minSize())\n this.defaultSize.set(this.minSize());\n if (this.defaultSize() > this.maxSize())\n this.defaultSize.set(this.maxSize());\n if (this.step() <= 0 || !Number.isFinite(this.step())) this.step.set(1);\n\n // Clamp current size after configuration changes\n this.fontSize.update((s) => this.clamp(s));\n }\n\n /**\n * Initializes the font size service.\n * Loads the persisted value (if available), clamps it,\n * applies it to the DOM, and updates internal state.\n * Should typically be called once at application startup.\n */\n init(): void {\n const size = this.readFromStorage();\n this.fontSize.set(this.clamp(size));\n this.applyCssVar(this.fontSize());\n }\n\n /**\n * Sets the font size to a specific value (clamped within min/max).\n *\n * @param size Desired font size in pixels.\n */\n setFontSize(size: number): void {\n const clamped = this.clamp(size);\n this.fontSize.set(clamped);\n this.applyCssVar(clamped);\n this.writeToStorage(clamped);\n }\n\n /**\n * Increases the font size.\n *\n * @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.\n */\n increase(step?: number): void {\n this.setFontSize(this.fontSize() + (step ?? this.step()));\n }\n\n /**\n * Decreases the font size.\n *\n * @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.\n */\n decrease(step?: number): void {\n this.setFontSize(this.fontSize() - (step ?? this.step()));\n }\n\n /**\n * Resets the font size to the configured {@link defaultSize}.\n * Also removes the persisted value from localStorage.\n */\n reset(): void {\n const d = this.defaultSize();\n this.fontSize.set(d);\n this.applyCssVar(d);\n localStorage.removeItem(this.storageKey);\n }\n\n /**\n * Clamps a given value between the configured {@link minSize} and {@link maxSize}.\n *\n * @param size Value to clamp.\n * @returns Clamped value within the allowed range.\n */\n private clamp(size: number): number {\n return Math.min(Math.max(size, this.minSize()), this.maxSize());\n }\n\n /**\n * Reads the font size from localStorage.\n * Falls back to {@link defaultSize} if no value is stored or value is invalid.\n *\n * @returns Stored font size or default.\n */\n private readFromStorage(): number {\n const stored = localStorage.getItem(this.storageKey);\n const parsed = stored ? Number(stored) : this.defaultSize();\n return Number.isFinite(parsed) ? parsed : this.defaultSize();\n }\n\n /**\n * Persists the current font size in localStorage.\n *\n * @param size Font size value to persist.\n */\n private writeToStorage(size: number): void {\n localStorage.setItem(this.storageKey, String(size));\n }\n\n /**\n * Applies the current font size to the DOM\n * using the configured CSS custom property name.\n *\n * @param size Font size value in pixels.\n */\n private applyCssVar(size: number): void {\n document.documentElement.style.setProperty(this.cssVarName, `${size}px`);\n }\n}\n"],"mappings":";;AAIA,IAAa,kBAAb,MAAa,gBAAe;CAK1B,cAAc,OAAO,IAAG,GAAA,YAAA,CAAA,EAAA,WAAA,cAAA,CAAA,+BAAA,CAAA,CAAA;CAMxB,UAAU,OAAO,IAAG,GAAA,YAAA,CAAA,EAAA,WAAA,UAAA,CAAA,+BAAA,CAAA,CAAA;CAMpB,UAAU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,OAAA,OAAA,GAAA,mBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAe,MAAA,CAAA;EAAA,QAAA,GAAA,gBAAA;CAAA,CAAA;CAAf,OAAA,QAAA,GAAA,sBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAe,YADF;CAAM,CAAA;;AACnB,GAAA,yBAAA;CAAA,YAAA;CAAA,SAAA;CAAA,UAAA;CAAA,MAAA;CAAe,YAAA,CAAA;EAD3B,MAAA;EAAW,MAAA,CAAA,EAAE,YAAY,OAAM,CAAE"}
1
+ {"version":3,"file":"su-labs-font-size.mjs","names":[],"sources":["../../../../projects/su-labs/font-size/src/lib/font-size.service.ts"],"sourcesContent":["import { computed, Injectable, OnDestroy, signal } from '@angular/core';\nimport { SuFontSizeController, SuFontSizeLimits } from '@su-labs/core/font-size';\nimport { FontSizeConfig } from './font-size-config.interface';\n\n/**\n * Angular service for managing the font size.\n *\n * Thin wrapper around the framework-agnostic `SuFontSizeController` from\n * `@su-labs/core/font-size` that exposes the size and limits as Angular signals.\n */\n@Injectable({ providedIn: 'root' })\nexport class FontSizeService implements OnDestroy {\n /** Framework-agnostic font size logic */\n private readonly controller = new SuFontSizeController();\n\n private readonly initialLimits: SuFontSizeLimits = this.controller.limits.get();\n\n /** Default font size in pixels. Change it with {@link configure}. */\n defaultSize = signal(this.initialLimits.defaultSize);\n\n /** Minimum font size in pixels. Change it with {@link configure}. */\n minSize = signal(this.initialLimits.minSize);\n\n /** Maximum font size in pixels. Change it with {@link configure}. */\n maxSize = signal(this.initialLimits.maxSize);\n\n /** Default step in pixels for {@link increase} / {@link decrease}. Change it with {@link configure}. */\n step = signal(this.initialLimits.step);\n\n /** Current font size in pixels */\n fontSize = signal<number>(this.controller.fontSize.get());\n\n /** True if the current size equals the default size */\n isDefault = computed(() => this.fontSize() === this.defaultSize());\n\n /** True if the current size equals the maximum size */\n isMax = computed(() => this.fontSize() === this.maxSize());\n\n /** True if the current size equals the minimum size */\n isMin = computed(() => this.fontSize() === this.minSize());\n\n /** Stop syncing the controller's stores into the signals */\n private readonly unsubscribers = [\n this.controller.fontSize.subscribe((size) => this.fontSize.set(size)),\n this.controller.limits.subscribe((limits) => {\n this.defaultSize.set(limits.defaultSize);\n this.minSize.set(limits.minSize);\n this.maxSize.set(limits.maxSize);\n this.step.set(limits.step);\n }),\n ];\n\n /**\n * Update the configuration. Omitted values keep their current setting.\n * @param cfg Partial configuration (limits, step, storage key, CSS variable name).\n */\n configure(cfg: Partial<FontSizeConfig> = {}): void {\n this.controller.configure(cfg);\n }\n\n /**\n * Load the saved size (or the default), clamp it and apply it to the document.\n * Call once at application startup, after {@link configure}.\n */\n init(): void {\n this.controller.init();\n }\n\n /**\n * Set, apply and persist a font size, clamped to the allowed range.\n * @param size Desired font size in pixels.\n */\n setFontSize(size: number): void {\n this.controller.setFontSize(size);\n }\n\n /**\n * Increase the font size.\n * @param step Pixels to add. Defaults to {@link step}.\n */\n increase(step?: number): void {\n this.setFontSize(this.fontSize() + (step ?? this.step()));\n }\n\n /**\n * Decrease the font size.\n * @param step Pixels to remove. Defaults to {@link step}.\n */\n decrease(step?: number): void {\n this.setFontSize(this.fontSize() - (step ?? this.step()));\n }\n\n /** Return to the default size and remove the saved value from localStorage */\n reset(): void {\n this.controller.reset();\n }\n\n /** Angular lifecycle hook for service destruction */\n ngOnDestroy(): void {\n this.unsubscribers.forEach((unsubscribe) => unsubscribe());\n }\n}\n"],"mappings":";;;AAWA,IAAa,kBAAb,MAAa,gBAAe;CAET,aAAa,IAAI,qBAAoB;CAErC,gBAAkC,KAAK,WAAW,OAAO,IAAG;CAG7E,cAAc,OAAO,KAAK,cAAc,aAAY,GAAA,YAAA,CAAA,EAAA,WAAA,cAAA,CAAA,+BAAA,CAAA,CAAA;CAGpD,UAAU,OAAO,KAAK,cAAc,SAAQ,GAAA,YAAA,CAAA,EAAA,WAAA,UAAA,CAAA,+BAAA,CAAA,CAAA;CAG5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AY,KAAK,SAAQ,KAAM,QAAQ,KAAK,KAAI,EAAG;CAC1D;CAGA,QAAK;EACH,KAAK,WAAW,MAAK;CACvB;CAGA,cAAW;EACT,KAAK,cAAc,SAAS,gBAAgB,YAAW,CAAE;CAC3D;CAzFW,OAAA,OAAA,GAAA,mBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAe,MAAA,CAAA;EAAA,QAAA,GAAA,gBAAA;CAAA,CAAA;CAAf,OAAA,QAAA,GAAA,sBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAe,YADF;CAAM,CAAA;;AACnB,GAAA,yBAAA;CAAA,YAAA;CAAA,SAAA;CAAA,UAAA;CAAA,MAAA;CAAe,YAAA,CAAA;EAD3B,MAAA;EAAW,MAAA,CAAA,EAAE,YAAY,OAAM,CAAE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@su-labs/font-size",
3
- "version": "22.0.0",
3
+ "version": "22.1.0",
4
4
  "description": "A lightweight Angular service for dynamic font size management.",
5
5
  "keywords": [
6
6
  "angular",
@@ -29,6 +29,7 @@
29
29
  "@angular/core": "^22.0.0"
30
30
  },
31
31
  "dependencies": {
32
+ "@su-labs/core": "^1.0.0",
32
33
  "tslib": "^2.3.0"
33
34
  },
34
35
  "repository": {
@@ -1,123 +1,65 @@
1
1
  import * as i0 from "@angular/core";
2
- export interface FontSizeConfig {
3
- defaultSize: number;
4
- minSize: number;
5
- maxSize: number;
6
- step: number;
7
- storageKey?: string;
8
- cssVarName?: string;
9
- }
10
- export declare class FontSizeService {
11
- /**
12
- * Default font size in pixels.
13
- * Can be overridden via {@link configure}.
14
- */
2
+ import { OnDestroy } from "@angular/core";
3
+ import { SuFontSizeConfig, SuFontSizeLimits } from "@su-labs/core/font-size";
4
+ /** Font size configuration: limits and step in pixels, plus optional storage key and CSS variable name */
5
+ export type FontSizeConfig = SuFontSizeLimits & Pick<SuFontSizeConfig, 'storageKey' | 'cssVarName'>;
6
+ /**
7
+ * Angular service for managing the font size.
8
+ *
9
+ * Thin wrapper around the framework-agnostic `SuFontSizeController` from
10
+ * `@su-labs/core/font-size` that exposes the size and limits as Angular signals.
11
+ */
12
+ export declare class FontSizeService implements OnDestroy {
13
+ /** Framework-agnostic font size logic */
14
+ private readonly controller;
15
+ private readonly initialLimits;
16
+ /** Default font size in pixels. Change it with {@link configure}. */
15
17
  defaultSize: import("@angular/core").WritableSignal<number>;
16
- /**
17
- * Minimum allowed font size in pixels.
18
- * Can be overridden via {@link configure}.
19
- */
18
+ /** Minimum font size in pixels. Change it with {@link configure}. */
20
19
  minSize: import("@angular/core").WritableSignal<number>;
21
- /**
22
- * Maximum allowed font size in pixels.
23
- * Can be overridden via {@link configure}.
24
- */
20
+ /** Maximum font size in pixels. Change it with {@link configure}. */
25
21
  maxSize: import("@angular/core").WritableSignal<number>;
26
- /**
27
- * Default step value (in pixels) for increase/decrease operations.
28
- * Can be overridden via {@link configure} or per call in {@link increase} / {@link decrease}.
29
- */
22
+ /** Default step in pixels for {@link increase} / {@link decrease}. Change it with {@link configure}. */
30
23
  step: import("@angular/core").WritableSignal<number>;
31
- /**
32
- * Key used for persisting the font size in localStorage.
33
- * Defaults to `"app:font-size"`.
34
- */
35
- private storageKey;
36
- /**
37
- * CSS custom property name where the font size is applied.
38
- * Defaults to `"--base-font-size"`.
39
- */
40
- private cssVarName;
41
- /**
42
- * Current font size state (reactive).
43
- */
24
+ /** Current font size in pixels */
44
25
  fontSize: import("@angular/core").WritableSignal<number>;
45
- /**
46
- * True if current font size equals the default size.
47
- */
26
+ /** True if the current size equals the default size */
48
27
  isDefault: import("@angular/core").Signal<boolean>;
49
- /**
50
- * True if current font size equals the maximum size.
51
- */
28
+ /** True if the current size equals the maximum size */
52
29
  isMax: import("@angular/core").Signal<boolean>;
53
- /**
54
- * True if current font size equals the minimum size.
55
- */
30
+ /** True if the current size equals the minimum size */
56
31
  isMin: import("@angular/core").Signal<boolean>;
32
+ /** Stop syncing the controller's stores into the signals */
33
+ private readonly unsubscribers;
57
34
  /**
58
- * Updates the configuration for font size handling.
59
- * Any omitted values will fall back to their defaults.
60
- *
61
- * @param cfg Partial configuration object (min, max, default, step, storage key, CSS variable name).
35
+ * Update the configuration. Omitted values keep their current setting.
36
+ * @param cfg Partial configuration (limits, step, storage key, CSS variable name).
62
37
  */
63
38
  configure(cfg?: Partial<FontSizeConfig>): void;
64
39
  /**
65
- * Initializes the font size service.
66
- * Loads the persisted value (if available), clamps it,
67
- * applies it to the DOM, and updates internal state.
68
- * Should typically be called once at application startup.
40
+ * Load the saved size (or the default), clamp it and apply it to the document.
41
+ * Call once at application startup, after {@link configure}.
69
42
  */
70
43
  init(): void;
71
44
  /**
72
- * Sets the font size to a specific value (clamped within min/max).
73
- *
45
+ * Set, apply and persist a font size, clamped to the allowed range.
74
46
  * @param size Desired font size in pixels.
75
47
  */
76
48
  setFontSize(size: number): void;
77
49
  /**
78
- * Increases the font size.
79
- *
80
- * @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.
50
+ * Increase the font size.
51
+ * @param step Pixels to add. Defaults to {@link step}.
81
52
  */
82
53
  increase(step?: number): void;
83
54
  /**
84
- * Decreases the font size.
85
- *
86
- * @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.
55
+ * Decrease the font size.
56
+ * @param step Pixels to remove. Defaults to {@link step}.
87
57
  */
88
58
  decrease(step?: number): void;
89
- /**
90
- * Resets the font size to the configured {@link defaultSize}.
91
- * Also removes the persisted value from localStorage.
92
- */
59
+ /** Return to the default size and remove the saved value from localStorage */
93
60
  reset(): void;
94
- /**
95
- * Clamps a given value between the configured {@link minSize} and {@link maxSize}.
96
- *
97
- * @param size Value to clamp.
98
- * @returns Clamped value within the allowed range.
99
- */
100
- private clamp;
101
- /**
102
- * Reads the font size from localStorage.
103
- * Falls back to {@link defaultSize} if no value is stored or value is invalid.
104
- *
105
- * @returns Stored font size or default.
106
- */
107
- private readFromStorage;
108
- /**
109
- * Persists the current font size in localStorage.
110
- *
111
- * @param size Font size value to persist.
112
- */
113
- private writeToStorage;
114
- /**
115
- * Applies the current font size to the DOM
116
- * using the configured CSS custom property name.
117
- *
118
- * @param size Font size value in pixels.
119
- */
120
- private applyCssVar;
61
+ /** Angular lifecycle hook for service destruction */
62
+ ngOnDestroy(): void;
121
63
  static ɵfac: i0.ɵɵFactoryDeclaration<FontSizeService, never>;
122
64
  static ɵprov: i0.ɵɵInjectableDeclaration<any>;
123
65
  }