keeptrack-css 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Robin Poort
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,367 @@
1
+ # KeepTrack
2
+
3
+ KeepTrack reads computed CSS property values from elements and exposes them as CSS custom properties (variables). This lets you use values like an element's rendered `height` or `background-color` elsewhere in your CSS — something not normally possible.
4
+
5
+ It automatically updates when elements resize, when the DOM changes, or (optionally) on every animation frame for non-layout properties.
6
+
7
+ ## Use-cases
8
+
9
+ 1. **Scrollbar sizes** By default the plugin tracks the scrollbar-width of vertical scrollbars (can be disabled) so a `--scrollbar-width` custom property is available in the document root so you can do things like `calc(100vw - var(--scrollbar-width))`.
10
+ 2. **Anchor linking** When you add `data-keeptrack-scroll-padding` to one or more sticky or fixed elements, anchor links will take this into account when jumping to that anchor. Also works on pageload and using back/forward navigation.
11
+ 3. **Keep track of CSS values** Keep track of width or height to mimic this on other elements when `display: grid` or even `subgrid` can't help you out there.
12
+
13
+ ## Installation
14
+
15
+ ```
16
+ npm install keeptrack-css
17
+ ```
18
+
19
+ Include via a `<script>` tag, CommonJS, or ES modules:
20
+
21
+ ```html
22
+ <script src="keepTrack.min.js"></script>
23
+ ```
24
+
25
+ ```js
26
+ // CommonJS
27
+ const KeepTrack = require('keeptrack-css');
28
+
29
+ // ES modules
30
+ import KeepTrack from 'keeptrack-css/keepTrack.esm.js';
31
+ ```
32
+
33
+ ## Basic usage
34
+
35
+ ```js
36
+ const tracker = new KeepTrack();
37
+ ```
38
+
39
+ Add `data-keeptrack` to any HTML element with a comma-separated list of CSS properties to track:
40
+
41
+ ```html
42
+ <div data-keeptrack="height">...</div>
43
+ ```
44
+
45
+ This sets `--height` as an inline CSS variable on the element itself, updated whenever the element resizes.
46
+
47
+ You can track multiple properties:
48
+
49
+ ```html
50
+ <div data-keeptrack="height, width, padding-top">...</div>
51
+ ```
52
+
53
+ ## Where the CSS variable is set
54
+
55
+ The target for the CSS variable depends on the element's attributes:
56
+
57
+ ### On the element itself (default)
58
+
59
+ ```html
60
+ <!-- Input -->
61
+ <div data-keeptrack="height">...</div>
62
+
63
+ <!-- Result -->
64
+ <div data-keeptrack="height" style="--height: 64px">...</div>
65
+ ```
66
+
67
+ Multiple properties:
68
+
69
+ ```html
70
+ <!-- Input -->
71
+ <div data-keeptrack="height, width, padding-top">...</div>
72
+
73
+ <!-- Result -->
74
+ <div data-keeptrack="height, width, padding-top" style="--height: 64px; --width: 320px; --padding-top: 16px">...</div>
75
+ ```
76
+
77
+ ### On the document root (via `id`)
78
+
79
+ If the element has an `id`, the variable is set on `:root` with the id as a prefix:
80
+
81
+ ```html
82
+ <!-- Input -->
83
+ <header id="site-header" data-keeptrack="height">...</header>
84
+
85
+ <!-- Result: sets --site-header-height on :root -->
86
+ <html style="--site-header-height: 80px">
87
+ ...
88
+ <header id="site-header" data-keeptrack="height">...</header>
89
+ ...
90
+ </html>
91
+ ```
92
+
93
+ ```css
94
+ main {
95
+ padding-top: var(--site-header-height);
96
+ }
97
+ ```
98
+
99
+ ### On a target parent (via `data-keeptrack-target-parent`)
100
+
101
+ You can set the variable on a parent or any other element. The attribute accepts either a number (levels to traverse up) or a CSS selector:
102
+
103
+ ```html
104
+ <!-- Traverse 2 levels up -->
105
+ <!-- Input -->
106
+ <div class="grandparent">
107
+ <div class="parent">
108
+ <div data-keeptrack="height" data-keeptrack-target-parent="2">...</div>
109
+ </div>
110
+ </div>
111
+
112
+ <!-- Result: --height is set on .grandparent -->
113
+ <div class="grandparent" style="--height: 64px">
114
+ <div class="parent">
115
+ <div data-keeptrack="height" data-keeptrack-target-parent="2">...</div>
116
+ </div>
117
+ </div>
118
+ ```
119
+
120
+ ```html
121
+ <!-- Closest ancestor matching the selector -->
122
+ <!-- Input -->
123
+ <div class="wrapper">
124
+ <div>
125
+ <div data-keeptrack="height" data-keeptrack-target-parent=".wrapper">...</div>
126
+ </div>
127
+ </div>
128
+
129
+ <!-- Result: --height is set on .wrapper -->
130
+ <div class="wrapper" style="--height: 64px">
131
+ <div>
132
+ <div data-keeptrack="height" data-keeptrack-target-parent=".wrapper">...</div>
133
+ </div>
134
+ </div>
135
+ ```
136
+
137
+ When using a selector, KeepTrack first tries `el.closest(selector)` to find the nearest ancestor. If no ancestor matches, it falls back to `document.querySelector(selector)`.
138
+
139
+ If the element also has an `id`, the variable name includes the id:
140
+
141
+ ```html
142
+ <!-- Input -->
143
+ <div class="layout">
144
+ <div id="sidebar" data-keeptrack="width" data-keeptrack-target-parent=".layout">...</div>
145
+ </div>
146
+
147
+ <!-- Result: --sidebar-width is set on .layout -->
148
+ <div class="layout" style="--sidebar-width: 250px">
149
+ <div id="sidebar" data-keeptrack="width" data-keeptrack-target-parent=".layout">...</div>
150
+ </div>
151
+ ```
152
+
153
+ ## Scrollbar dimensions
154
+
155
+ By default, KeepTrack sets `--scrollbar-width` on `:root`, updated on viewport resize. You can also enable `--scrollbar-height`.
156
+
157
+ - `--scrollbar-width` is the width (thickness) of the **vertical** scrollbar
158
+ - `--scrollbar-height` is the height (thickness) of the **horizontal** scrollbar
159
+
160
+ ```js
161
+ new KeepTrack({
162
+ scrollbarWidth: true, // default: true
163
+ scrollbarHeight: true // default: false
164
+ });
165
+ ```
166
+
167
+ ```css
168
+ .full-width {
169
+ width: calc(100vw - var(--scrollbar-width));
170
+ }
171
+ ```
172
+
173
+ ## Scroll padding
174
+
175
+ Add `data-keeptrack-scroll-padding` to any element to automatically set `scroll-padding-top` on `:root`. This fixes anchor links (`<a href="#section">`) being hidden behind sticky headers. The element does not need `data-keeptrack` — `data-keeptrack-scroll-padding` works on its own.
176
+
177
+ ```html
178
+ <!-- Input -->
179
+ <header id="site-header" data-keeptrack="height" data-keeptrack-scroll-padding>
180
+ ...
181
+ </header>
182
+ <main>
183
+ <section id="about">...</section>
184
+ </main>
185
+
186
+ <!-- Result: scroll-padding-top is set on :root to the header's height -->
187
+ <html style="--site-header-height: 80px; scroll-padding-top: 80px">
188
+ ...
189
+ </html>
190
+ ```
191
+
192
+ If multiple elements have `data-keeptrack-scroll-padding`, their heights are summed:
193
+
194
+ ```html
195
+ <header data-keeptrack="height" data-keeptrack-scroll-padding>...</header>
196
+ <nav data-keeptrack="height" data-keeptrack-scroll-padding>...</nav>
197
+ <!-- scroll-padding-top = header height + nav height -->
198
+ ```
199
+
200
+ When `detectSticky` is enabled, only elements that are currently stuck contribute to `scroll-padding-top`. When clicking an anchor link, KeepTrack predicts which sticky elements will be stuck at the target position and adjusts `scroll-padding-top` before the browser scrolls. This ensures correct scroll offsets even when a sticky element's container ends before the anchor target.
201
+
202
+ ## Sticky detection
203
+
204
+ Enable `detectSticky` to detect when `position: sticky` elements become stuck. KeepTrack checks on scroll and exposes the state as:
205
+
206
+ - A `data-keeptrack-stuck` attribute on the element (for CSS targeting)
207
+ - A `--[id]-stuck` CSS variable on `:root` (`1` when stuck, `0` when not) if the element has an `id`
208
+ - A `--stuck` CSS variable on the element itself if it has no `id`
209
+
210
+ ```js
211
+ new KeepTrack({ detectSticky: true });
212
+ ```
213
+
214
+ ```html
215
+ <!-- Input -->
216
+ <header id="site-header" data-keeptrack="height" style="position: sticky; top: 0">
217
+ ...
218
+ </header>
219
+
220
+ <!-- Result when stuck -->
221
+ <html style="--site-header-height: 80px; --site-header-stuck: 1">
222
+ ...
223
+ <header id="site-header" data-keeptrack="height" data-keeptrack-stuck style="position: sticky; top: 0">
224
+ ...
225
+ </header>
226
+ ...
227
+ </html>
228
+ ```
229
+
230
+ ```css
231
+ /* Style changes when stuck */
232
+ [data-keeptrack-stuck] {
233
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
234
+ }
235
+ ```
236
+
237
+ The `onChange` callback also fires for sticky state changes with `prop` set to `"stuck"`:
238
+
239
+ ```js
240
+ new KeepTrack({
241
+ detectSticky: true,
242
+ onChange(el, prop, value) {
243
+ if (prop === 'stuck') {
244
+ console.log(el, value === '1' ? 'is stuck' : 'is not stuck');
245
+ }
246
+ }
247
+ });
248
+ ```
249
+
250
+ ## Sticky `top` resolution (calc/var) and caching
251
+
252
+ KeepTrack resolves sticky element `top` values to pixels for sticky detection and anchor prediction. It supports
253
+ `px`, `em`, `rem`, `%`, and complex values like `calc(...)` and `var(...)`.
254
+
255
+ Resolved `top` values are cached and only recomputed on resize, DOM mutations, or `recalculate()`. This caching only
256
+ affects sticky detection and anchor prediction. If your `top` value actually changes during scroll (rare), you can opt
257
+ into per-frame updates:
258
+
259
+ ```js
260
+ new KeepTrack({ stickyTopDynamic: true });
261
+ ```
262
+
263
+ ## Options
264
+
265
+ ```js
266
+ new KeepTrack({
267
+ scrollbarWidth: true, // Track scrollbar width as --scrollbar-width on :root
268
+ scrollbarHeight: false, // Track scrollbar height as --scrollbar-height on :root
269
+ debounceTime: 250, // Debounce delay in ms for resize and DOM changes
270
+ poll: false, // Enable requestAnimationFrame polling for non-layout changes
271
+ detectSticky: false, // Detect when sticky elements become stuck
272
+ stickyTopDynamic: false, // Update sticky top values every frame
273
+ onChange: null // Callback when a tracked value changes
274
+ });
275
+ ```
276
+
277
+ ### `poll`
278
+
279
+ Enable this to track properties that don't affect element size, like `background-color`, `color`, or `font-size`. When enabled, KeepTrack checks all tracked values every animation frame and only updates when a value has changed.
280
+
281
+ ```js
282
+ new KeepTrack({ poll: true });
283
+ ```
284
+
285
+ If the browser doesn't support `ResizeObserver`, enable `poll` to keep values in sync with size changes.
286
+
287
+ ### `detectSticky`
288
+
289
+ Enable this to detect when `position: sticky` elements are stuck. Uses a passive scroll listener throttled with `requestAnimationFrame` for minimal performance impact.
290
+
291
+ ```js
292
+ new KeepTrack({ detectSticky: true });
293
+ ```
294
+
295
+ ### `stickyTopDynamic`
296
+
297
+ When `false` (default), KeepTrack caches resolved sticky `top` values for performance. This only affects sticky
298
+ detection and anchor prediction. Set to `true` if your `top` value changes during scroll.
299
+
300
+ ```js
301
+ new KeepTrack({ stickyTopDynamic: true });
302
+ ```
303
+
304
+ ### `onChange`
305
+
306
+ Called whenever a tracked value changes (including sticky state). Receives the element, the property name, and the new value:
307
+
308
+ ```js
309
+ new KeepTrack({
310
+ onChange(el, prop, value) {
311
+ console.log(`${prop} changed to ${value}`, el);
312
+ }
313
+ });
314
+ ```
315
+
316
+ ## API
317
+
318
+ ### `init(options)`
319
+
320
+ Re-initializes with new options. Cleans up the previous instance first.
321
+
322
+ ```js
323
+ tracker.init({ poll: true });
324
+ ```
325
+
326
+ ### `destroy()`
327
+
328
+ Removes all event listeners, observers, and stops polling. Also cleans up all CSS variables, `scroll-padding-top`, and `data-keeptrack-stuck` attributes set by KeepTrack.
329
+
330
+ ```js
331
+ tracker.destroy();
332
+ ```
333
+
334
+ ### `recalculate()`
335
+
336
+ Manually trigger a recalculation of all tracked elements and scrollbar dimensions.
337
+
338
+ ```js
339
+ tracker.recalculate();
340
+ ```
341
+
342
+ ### `observe(element)`
343
+
344
+ Programmatically start tracking an element (must have a `data-keeptrack` attribute):
345
+
346
+ ```js
347
+ tracker.observe(document.querySelector('.my-element'));
348
+ ```
349
+
350
+ ### `unobserve(element)`
351
+
352
+ Stop tracking an element, remove its CSS variables, and clean up its caches:
353
+
354
+ ```js
355
+ tracker.unobserve(document.querySelector('.my-element'));
356
+ ```
357
+
358
+ ## How it works
359
+
360
+ KeepTrack uses multiple mechanisms to detect changes:
361
+
362
+ - **ResizeObserver** tracks size changes on individual `[data-keeptrack]` elements
363
+ - **MutationObserver** detects when tracked elements are added/removed from the DOM, when `data-keeptrack` is dynamically added/removed from elements, or when their `data-keeptrack-target-parent`, `data-keeptrack-scroll-padding`, or `id` attributes change
364
+ - **Scroll listener** (opt-in via `detectSticky: true`) detects when `position: sticky` elements become stuck, using a passive listener throttled with `requestAnimationFrame`
365
+ - **requestAnimationFrame polling** (opt-in via `poll: true`) catches computed style changes that don't affect element size, like color or font changes
366
+
367
+ All paths use a value cache to avoid unnecessary `setProperty` calls when nothing has changed. Calling `destroy()` or `unobserve()` fully cleans up any CSS variables and attributes that were set.