@su-labs/font-size 1.0.1
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/LICENSE +21 -0
- package/README.md +145 -0
- package/fesm2022/su-labs-font-size.mjs +184 -0
- package/fesm2022/su-labs-font-size.mjs.map +1 -0
- package/index.d.ts +128 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yevheniy Alekseyev <yev.alekseyev@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# FontSizeService
|
|
2
|
+
|
|
3
|
+
A lightweight Angular service for dynamic font size management. Easily increase, decrease, or reset font sizes in your Angular applications, with persistent settings in `localStorage`. Designed as a reusable library for multiple projects.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Set a default font size and apply it globally.
|
|
8
|
+
- Increase or decrease font size with configurable step.
|
|
9
|
+
- Reset to default font size.
|
|
10
|
+
- Automatically persist user preferences in `localStorage`.
|
|
11
|
+
- Provides reactive `Signal`s to integrate with Angular's reactive system.
|
|
12
|
+
- Limits font size to configurable minimum and maximum values.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
You can install this library via npm:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @su-labs/font-size
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Peer Dependencies
|
|
23
|
+
|
|
24
|
+
This library requires the following peer dependencies:
|
|
25
|
+
|
|
26
|
+
- `@angular/common`: `>=20.1.0`
|
|
27
|
+
- `@angular/core`: `>=20.1.0`
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
### Provide the service in your application
|
|
31
|
+
|
|
32
|
+
You can provide the service in your main.ts (or wherever your ApplicationConfig is defined) and initialize it automatically:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
export const appConfig: ApplicationConfig = {
|
|
36
|
+
providers: [
|
|
37
|
+
FontSizeService,
|
|
38
|
+
provideAppInitializer(() => {
|
|
39
|
+
|
|
40
|
+
const fontService = inject(FontSizeService);
|
|
41
|
+
fontService.init(); // initializes font size from localStorage and applies CSS variable
|
|
42
|
+
}),
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
```
|
|
46
|
+
Using the service in components
|
|
47
|
+
|
|
48
|
+
Inject the service into any component and access reactive signals:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { Component, effect } from '@angular/core';
|
|
52
|
+
import { FontSizeService } from 'settings-utils';
|
|
53
|
+
|
|
54
|
+
@Component({
|
|
55
|
+
selector: 'app-example',
|
|
56
|
+
template: `
|
|
57
|
+
<div>
|
|
58
|
+
<p>Current font size: {{ fontSize() }}</p>
|
|
59
|
+
<button (click)="increase()">A+</button>
|
|
60
|
+
<button (click)="decrease()">A-</button>
|
|
61
|
+
<button (click)="reset()">Reset</button>
|
|
62
|
+
</div>` ,
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
export class ExampleComponent {
|
|
66
|
+
private fontService = inject(FontSizeService);
|
|
67
|
+
fontSize = computed(() => this.fontService.fontSize());
|
|
68
|
+
|
|
69
|
+
increase(step?: number) {
|
|
70
|
+
this.fontService.increase(step);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
decrease(step?: number) {
|
|
74
|
+
this.fontService.decrease(step);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
reset() {
|
|
78
|
+
this.fontService.reset();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Optional configuration
|
|
84
|
+
|
|
85
|
+
The service can be configured to fit your application's needs. You can customize the min/max/default font size, step, storage key, and CSS variable name by calling the `configure` method.
|
|
86
|
+
|
|
87
|
+
| Option | Description | Default Value |
|
|
88
|
+
|--------------|---------------------------------------------------------------------------------------------------------|-----------------|
|
|
89
|
+
| `minSize` | The minimum allowed font size in pixels. | `10` |
|
|
90
|
+
| `maxSize` | The maximum allowed font size in pixels. | `22` |
|
|
91
|
+
| `defaultSize`| The initial font size and the value used for resetting. | `14` |
|
|
92
|
+
| `step` | The number of pixels to increase or decrease the font size by. | `2` |
|
|
93
|
+
| `storageKey` | The key used to persist the font size in `localStorage`. | `'su:font-size'`|
|
|
94
|
+
| `cssVarName` | The name of the CSS custom property that will be applied to the `<html>` element. | `'--base-font-size'`|
|
|
95
|
+
|
|
96
|
+
**Example:**
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
this.fontService.configure({
|
|
100
|
+
minSize: 12,
|
|
101
|
+
maxSize: 24,
|
|
102
|
+
defaultSize: 16,
|
|
103
|
+
step: 2,
|
|
104
|
+
storageKey: 'my-app-font-size',
|
|
105
|
+
cssVarName: '--app-font-size'
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Styling (CSS)
|
|
110
|
+
|
|
111
|
+
The service applies the current font size to a CSS custom property (variable) on the `<html>` element. By default, this is `--base-font-size`.
|
|
112
|
+
|
|
113
|
+
To use this feature in your application's CSS, you should use this variable to define your font size, for example, in your `styles.scss` or `styles.css`:
|
|
114
|
+
|
|
115
|
+
```css
|
|
116
|
+
:root {
|
|
117
|
+
/* Define a fallback in case the service isn't initialized yet */
|
|
118
|
+
--base-font-size: 14px;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
body {
|
|
122
|
+
font-size: var(--base-font-size);
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Or, if you configured a different name:
|
|
127
|
+
|
|
128
|
+
CSS
|
|
129
|
+
```css
|
|
130
|
+
body {
|
|
131
|
+
font-size: var(--app-font-size);
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
This setup ensures that your font size is reactive, persistent, and easily configurable across your Angular app.
|
|
136
|
+
|
|
137
|
+
## Contributing
|
|
138
|
+
|
|
139
|
+
If you find any bugs or have feature requests, please open an issue or submit a pull request on our GitHub repository.
|
|
140
|
+
|
|
141
|
+
To contribute code, please ensure your changes include unit tests using **Vitest** to maintain code quality.
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
This project is licensed under the MIT License. See the [LICENSE](https://github.com/Greezaaa/settings-utils-lib/blob/main/projects/su-labs/font-size/LICENSE) file for details.
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { signal, computed, Injectable } from '@angular/core';
|
|
3
|
+
|
|
4
|
+
class FontSizeService {
|
|
5
|
+
/**
|
|
6
|
+
* Default font size in pixels.
|
|
7
|
+
* Can be overridden via {@link configure}.
|
|
8
|
+
*/
|
|
9
|
+
defaultSize = signal(14, ...(ngDevMode ? [{ debugName: "defaultSize" }] : []));
|
|
10
|
+
/**
|
|
11
|
+
* Minimum allowed font size in pixels.
|
|
12
|
+
* Can be overridden via {@link configure}.
|
|
13
|
+
*/
|
|
14
|
+
minSize = signal(10, ...(ngDevMode ? [{ debugName: "minSize" }] : []));
|
|
15
|
+
/**
|
|
16
|
+
* Maximum allowed font size in pixels.
|
|
17
|
+
* Can be overridden via {@link configure}.
|
|
18
|
+
*/
|
|
19
|
+
maxSize = signal(22, ...(ngDevMode ? [{ debugName: "maxSize" }] : []));
|
|
20
|
+
/**
|
|
21
|
+
* Default step value (in pixels) for increase/decrease operations.
|
|
22
|
+
* Can be overridden via {@link configure} or per call in {@link increase} / {@link decrease}.
|
|
23
|
+
*/
|
|
24
|
+
step = signal(2, ...(ngDevMode ? [{ debugName: "step" }] : []));
|
|
25
|
+
/**
|
|
26
|
+
* Key used for persisting the font size in localStorage.
|
|
27
|
+
* Defaults to `"app:font-size"`.
|
|
28
|
+
*/
|
|
29
|
+
storageKey = 'su:font-size';
|
|
30
|
+
/**
|
|
31
|
+
* CSS custom property name where the font size is applied.
|
|
32
|
+
* Defaults to `"--base-font-size"`.
|
|
33
|
+
*/
|
|
34
|
+
cssVarName = '--base-font-size';
|
|
35
|
+
/**
|
|
36
|
+
* Current font size state (reactive).
|
|
37
|
+
*/
|
|
38
|
+
fontSize = signal(this.readFromStorage(), ...(ngDevMode ? [{ debugName: "fontSize" }] : []));
|
|
39
|
+
/**
|
|
40
|
+
* True if current font size equals the default size.
|
|
41
|
+
*/
|
|
42
|
+
isDefault = computed(() => this.fontSize() === this.defaultSize(), ...(ngDevMode ? [{ debugName: "isDefault" }] : []));
|
|
43
|
+
/**
|
|
44
|
+
* True if current font size equals the maximum size.
|
|
45
|
+
*/
|
|
46
|
+
isMax = computed(() => this.fontSize() === this.maxSize(), ...(ngDevMode ? [{ debugName: "isMax" }] : []));
|
|
47
|
+
/**
|
|
48
|
+
* True if current font size equals the minimum size.
|
|
49
|
+
*/
|
|
50
|
+
isMin = computed(() => this.fontSize() === this.minSize(), ...(ngDevMode ? [{ debugName: "isMin" }] : []));
|
|
51
|
+
/**
|
|
52
|
+
* Updates the configuration for font size handling.
|
|
53
|
+
* Any omitted values will fall back to their defaults.
|
|
54
|
+
*
|
|
55
|
+
* @param cfg Partial configuration object (min, max, default, step, storage key, CSS variable name).
|
|
56
|
+
*/
|
|
57
|
+
configure(cfg = {}) {
|
|
58
|
+
if (cfg.minSize !== undefined)
|
|
59
|
+
this.minSize.set(cfg.minSize);
|
|
60
|
+
if (cfg.maxSize !== undefined)
|
|
61
|
+
this.maxSize.set(cfg.maxSize);
|
|
62
|
+
if (cfg.defaultSize !== undefined)
|
|
63
|
+
this.defaultSize.set(cfg.defaultSize);
|
|
64
|
+
if (cfg.step !== undefined)
|
|
65
|
+
this.step.set(cfg.step);
|
|
66
|
+
if (cfg.storageKey)
|
|
67
|
+
this.storageKey = cfg.storageKey;
|
|
68
|
+
if (cfg.cssVarName)
|
|
69
|
+
this.cssVarName = cfg.cssVarName;
|
|
70
|
+
// Ensure logical invariants
|
|
71
|
+
if (this.minSize() > this.maxSize())
|
|
72
|
+
this.maxSize.set(this.minSize());
|
|
73
|
+
if (this.defaultSize() < this.minSize())
|
|
74
|
+
this.defaultSize.set(this.minSize());
|
|
75
|
+
if (this.defaultSize() > this.maxSize())
|
|
76
|
+
this.defaultSize.set(this.maxSize());
|
|
77
|
+
if (this.step() <= 0 || !Number.isFinite(this.step()))
|
|
78
|
+
this.step.set(1);
|
|
79
|
+
// Clamp current size after configuration changes
|
|
80
|
+
this.fontSize.update((s) => this.clamp(s));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Initializes the font size service.
|
|
84
|
+
* Loads the persisted value (if available), clamps it,
|
|
85
|
+
* applies it to the DOM, and updates internal state.
|
|
86
|
+
* Should typically be called once at application startup.
|
|
87
|
+
*/
|
|
88
|
+
init() {
|
|
89
|
+
const size = this.readFromStorage();
|
|
90
|
+
this.fontSize.set(this.clamp(size));
|
|
91
|
+
this.applyCssVar(this.fontSize());
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Sets the font size to a specific value (clamped within min/max).
|
|
95
|
+
*
|
|
96
|
+
* @param size Desired font size in pixels.
|
|
97
|
+
*/
|
|
98
|
+
setFontSize(size) {
|
|
99
|
+
const clamped = this.clamp(size);
|
|
100
|
+
this.fontSize.set(clamped);
|
|
101
|
+
this.applyCssVar(clamped);
|
|
102
|
+
this.writeToStorage(clamped);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Increases the font size.
|
|
106
|
+
*
|
|
107
|
+
* @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.
|
|
108
|
+
*/
|
|
109
|
+
increase(step) {
|
|
110
|
+
this.setFontSize(this.fontSize() + (step ?? this.step()));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Decreases the font size.
|
|
114
|
+
*
|
|
115
|
+
* @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.
|
|
116
|
+
*/
|
|
117
|
+
decrease(step) {
|
|
118
|
+
this.setFontSize(this.fontSize() - (step ?? this.step()));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Resets the font size to the configured {@link defaultSize}.
|
|
122
|
+
* Also removes the persisted value from localStorage.
|
|
123
|
+
*/
|
|
124
|
+
reset() {
|
|
125
|
+
const d = this.defaultSize();
|
|
126
|
+
this.fontSize.set(d);
|
|
127
|
+
this.applyCssVar(d);
|
|
128
|
+
localStorage.removeItem(this.storageKey);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Clamps a given value between the configured {@link minSize} and {@link maxSize}.
|
|
132
|
+
*
|
|
133
|
+
* @param size Value to clamp.
|
|
134
|
+
* @returns Clamped value within the allowed range.
|
|
135
|
+
*/
|
|
136
|
+
clamp(size) {
|
|
137
|
+
return Math.min(Math.max(size, this.minSize()), this.maxSize());
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Reads the font size from localStorage.
|
|
141
|
+
* Falls back to {@link defaultSize} if no value is stored or value is invalid.
|
|
142
|
+
*
|
|
143
|
+
* @returns Stored font size or default.
|
|
144
|
+
*/
|
|
145
|
+
readFromStorage() {
|
|
146
|
+
const stored = localStorage.getItem(this.storageKey);
|
|
147
|
+
const parsed = stored ? Number(stored) : this.defaultSize();
|
|
148
|
+
return Number.isFinite(parsed) ? parsed : this.defaultSize();
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Persists the current font size in localStorage.
|
|
152
|
+
*
|
|
153
|
+
* @param size Font size value to persist.
|
|
154
|
+
*/
|
|
155
|
+
writeToStorage(size) {
|
|
156
|
+
localStorage.setItem(this.storageKey, String(size));
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Applies the current font size to the DOM
|
|
160
|
+
* using the configured CSS custom property name.
|
|
161
|
+
*
|
|
162
|
+
* @param size Font size value in pixels.
|
|
163
|
+
*/
|
|
164
|
+
applyCssVar(size) {
|
|
165
|
+
document.documentElement.style.setProperty(this.cssVarName, `${size}px`);
|
|
166
|
+
}
|
|
167
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.7", ngImport: i0, type: FontSizeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
168
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.7", ngImport: i0, type: FontSizeService, providedIn: 'root' });
|
|
169
|
+
}
|
|
170
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.7", ngImport: i0, type: FontSizeService, decorators: [{
|
|
171
|
+
type: Injectable,
|
|
172
|
+
args: [{ providedIn: 'root' }]
|
|
173
|
+
}] });
|
|
174
|
+
|
|
175
|
+
/*
|
|
176
|
+
* Public API Surface of font-size
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Generated bundle index. Do not edit.
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
export { FontSizeService };
|
|
184
|
+
//# sourceMappingURL=su-labs-font-size.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"su-labs-font-size.mjs","sources":["../../../../projects/su-labs/font-size/src/lib/font-size.service.ts","../../../../projects/su-labs/font-size/src/public-api.ts","../../../../projects/su-labs/font-size/src/su-labs-font-size.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","/*\n * Public API Surface of font-size\n */\n\nexport * from './lib/font-size.service';\nexport * from './lib/font-size-config.interface';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;MAIa,eAAe,CAAA;AAC1B;;;AAGG;AACH,IAAA,WAAW,GAAG,MAAM,CAAC,EAAE,uDAAC;AAExB;;;AAGG;AACH,IAAA,OAAO,GAAG,MAAM,CAAC,EAAE,mDAAC;AAEpB;;;AAGG;AACH,IAAA,OAAO,GAAG,MAAM,CAAC,EAAE,mDAAC;AAEpB;;;AAGG;AACH,IAAA,IAAI,GAAG,MAAM,CAAC,CAAC,gDAAC;AAEhB;;;AAGG;IACK,UAAU,GAAG,cAAc;AAEnC;;;AAGG;IACK,UAAU,GAAG,kBAAkB;AAEvC;;AAEG;IACH,QAAQ,GAAG,MAAM,CAAS,IAAI,CAAC,eAAe,EAAE,oDAAC;AAEjD;;AAEG;AACH,IAAA,SAAS,GAAG,QAAQ,CAAC,MAAM,IAAI,CAAC,QAAQ,EAAE,KAAK,IAAI,CAAC,WAAW,EAAE,qDAAC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AAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,eAAe,cADF,MAAM,EAAA,CAAA;;2FACnB,eAAe,EAAA,UAAA,EAAA,CAAA;kBAD3B,UAAU;mBAAC,EAAE,UAAU,EAAE,MAAM,EAAE;;;ACHlC;;AAEG;;ACFH;;AAEG;;;;"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import * as _angular_core from '@angular/core';
|
|
2
|
+
|
|
3
|
+
interface FontSizeConfig {
|
|
4
|
+
defaultSize: number;
|
|
5
|
+
minSize: number;
|
|
6
|
+
maxSize: number;
|
|
7
|
+
step: number;
|
|
8
|
+
storageKey?: string;
|
|
9
|
+
cssVarName?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare class FontSizeService {
|
|
13
|
+
/**
|
|
14
|
+
* Default font size in pixels.
|
|
15
|
+
* Can be overridden via {@link configure}.
|
|
16
|
+
*/
|
|
17
|
+
defaultSize: _angular_core.WritableSignal<number>;
|
|
18
|
+
/**
|
|
19
|
+
* Minimum allowed font size in pixels.
|
|
20
|
+
* Can be overridden via {@link configure}.
|
|
21
|
+
*/
|
|
22
|
+
minSize: _angular_core.WritableSignal<number>;
|
|
23
|
+
/**
|
|
24
|
+
* Maximum allowed font size in pixels.
|
|
25
|
+
* Can be overridden via {@link configure}.
|
|
26
|
+
*/
|
|
27
|
+
maxSize: _angular_core.WritableSignal<number>;
|
|
28
|
+
/**
|
|
29
|
+
* Default step value (in pixels) for increase/decrease operations.
|
|
30
|
+
* Can be overridden via {@link configure} or per call in {@link increase} / {@link decrease}.
|
|
31
|
+
*/
|
|
32
|
+
step: _angular_core.WritableSignal<number>;
|
|
33
|
+
/**
|
|
34
|
+
* Key used for persisting the font size in localStorage.
|
|
35
|
+
* Defaults to `"app:font-size"`.
|
|
36
|
+
*/
|
|
37
|
+
private storageKey;
|
|
38
|
+
/**
|
|
39
|
+
* CSS custom property name where the font size is applied.
|
|
40
|
+
* Defaults to `"--base-font-size"`.
|
|
41
|
+
*/
|
|
42
|
+
private cssVarName;
|
|
43
|
+
/**
|
|
44
|
+
* Current font size state (reactive).
|
|
45
|
+
*/
|
|
46
|
+
fontSize: _angular_core.WritableSignal<number>;
|
|
47
|
+
/**
|
|
48
|
+
* True if current font size equals the default size.
|
|
49
|
+
*/
|
|
50
|
+
isDefault: _angular_core.Signal<boolean>;
|
|
51
|
+
/**
|
|
52
|
+
* True if current font size equals the maximum size.
|
|
53
|
+
*/
|
|
54
|
+
isMax: _angular_core.Signal<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* True if current font size equals the minimum size.
|
|
57
|
+
*/
|
|
58
|
+
isMin: _angular_core.Signal<boolean>;
|
|
59
|
+
/**
|
|
60
|
+
* Updates the configuration for font size handling.
|
|
61
|
+
* Any omitted values will fall back to their defaults.
|
|
62
|
+
*
|
|
63
|
+
* @param cfg Partial configuration object (min, max, default, step, storage key, CSS variable name).
|
|
64
|
+
*/
|
|
65
|
+
configure(cfg?: Partial<FontSizeConfig>): void;
|
|
66
|
+
/**
|
|
67
|
+
* Initializes the font size service.
|
|
68
|
+
* Loads the persisted value (if available), clamps it,
|
|
69
|
+
* applies it to the DOM, and updates internal state.
|
|
70
|
+
* Should typically be called once at application startup.
|
|
71
|
+
*/
|
|
72
|
+
init(): void;
|
|
73
|
+
/**
|
|
74
|
+
* Sets the font size to a specific value (clamped within min/max).
|
|
75
|
+
*
|
|
76
|
+
* @param size Desired font size in pixels.
|
|
77
|
+
*/
|
|
78
|
+
setFontSize(size: number): void;
|
|
79
|
+
/**
|
|
80
|
+
* Increases the font size.
|
|
81
|
+
*
|
|
82
|
+
* @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.
|
|
83
|
+
*/
|
|
84
|
+
increase(step?: number): void;
|
|
85
|
+
/**
|
|
86
|
+
* Decreases the font size.
|
|
87
|
+
*
|
|
88
|
+
* @param step Optional custom step (pixels). Falls back to configured {@link step} if omitted.
|
|
89
|
+
*/
|
|
90
|
+
decrease(step?: number): void;
|
|
91
|
+
/**
|
|
92
|
+
* Resets the font size to the configured {@link defaultSize}.
|
|
93
|
+
* Also removes the persisted value from localStorage.
|
|
94
|
+
*/
|
|
95
|
+
reset(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Clamps a given value between the configured {@link minSize} and {@link maxSize}.
|
|
98
|
+
*
|
|
99
|
+
* @param size Value to clamp.
|
|
100
|
+
* @returns Clamped value within the allowed range.
|
|
101
|
+
*/
|
|
102
|
+
private clamp;
|
|
103
|
+
/**
|
|
104
|
+
* Reads the font size from localStorage.
|
|
105
|
+
* Falls back to {@link defaultSize} if no value is stored or value is invalid.
|
|
106
|
+
*
|
|
107
|
+
* @returns Stored font size or default.
|
|
108
|
+
*/
|
|
109
|
+
private readFromStorage;
|
|
110
|
+
/**
|
|
111
|
+
* Persists the current font size in localStorage.
|
|
112
|
+
*
|
|
113
|
+
* @param size Font size value to persist.
|
|
114
|
+
*/
|
|
115
|
+
private writeToStorage;
|
|
116
|
+
/**
|
|
117
|
+
* Applies the current font size to the DOM
|
|
118
|
+
* using the configured CSS custom property name.
|
|
119
|
+
*
|
|
120
|
+
* @param size Font size value in pixels.
|
|
121
|
+
*/
|
|
122
|
+
private applyCssVar;
|
|
123
|
+
static ɵfac: _angular_core.ɵɵFactoryDeclaration<FontSizeService, never>;
|
|
124
|
+
static ɵprov: _angular_core.ɵɵInjectableDeclaration<FontSizeService>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { FontSizeService };
|
|
128
|
+
export type { FontSizeConfig };
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@su-labs/font-size",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "A lightweight Angular service for dynamic font size management.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"angular",
|
|
7
|
+
"angular-library",
|
|
8
|
+
"typescript",
|
|
9
|
+
"font",
|
|
10
|
+
"font-size",
|
|
11
|
+
"dynamic-font-size",
|
|
12
|
+
"font-scaling",
|
|
13
|
+
"font-size-management",
|
|
14
|
+
"accessibility",
|
|
15
|
+
"ui",
|
|
16
|
+
"utility",
|
|
17
|
+
"service",
|
|
18
|
+
"signals",
|
|
19
|
+
"localstorage"
|
|
20
|
+
],
|
|
21
|
+
"homepage": "https://github.com/Greezaaa/settings-utils-lib/tree/main/projects/su-labs/font-size#readme",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Yevheniy Alekseyev",
|
|
24
|
+
"email": "yev.alekseyev@gmail.com",
|
|
25
|
+
"url": "https://github.com/Greezaaa"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@angular/common": "^20.1.0",
|
|
29
|
+
"@angular/core": "^20.1.0"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"tslib": "^2.3.0"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/Greezaaa/settings-utils-lib.git",
|
|
37
|
+
"directory": "projects/su-labs/font-size"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/Greezaaa/settings-utils-lib/issues"
|
|
41
|
+
},
|
|
42
|
+
"sideEffects": false,
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"module": "fesm2022/su-labs-font-size.mjs",
|
|
45
|
+
"typings": "index.d.ts",
|
|
46
|
+
"exports": {
|
|
47
|
+
"./package.json": {
|
|
48
|
+
"default": "./package.json"
|
|
49
|
+
},
|
|
50
|
+
".": {
|
|
51
|
+
"types": "./index.d.ts",
|
|
52
|
+
"default": "./fesm2022/su-labs-font-size.mjs"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|