prop-for-that (GitHub Repo)
The prop-for-that library exposes browser runtime data like battery status, scroll velocity, and mouse position as real-time CSS custom properties.
Deep dive
- Uses declarative data attributes to sync JS state with CSS custom properties.
- Batches updates into one requestAnimationFrame flush to prevent layout thrashing.
- Features tree-shakeable, opt-in plugins for 20+ sources including battery, network, CPU pressure, and pointer position.
- Includes a FOUC-safe (Flash of Unstyled Content) entry point for constant values like device memory or scrollbar width.
- Zero-dependency, TypeScript-native, and SSR-safe for modern web stacks.
Decoder
- CSS custom properties: Also known as CSS variables, these are entities defined by CSS authors that contain specific values to be reused throughout a document.
- FOUC (Flash of Unstyled Content): A phenomenon where a web page appears briefly without styling before the stylesheet is loaded and applied.
- RequestAnimationFrame: A browser API that allows developers to schedule a function to run before the next repaint, ensuring efficient animation updates.
Original article
prop-for-that
Expose what JavaScript knows but CSS can't see — as live CSS custom properties.
Sliders, pointer position, element visibility, viewport size, battery, network, sensors — JavaScript can read all of it; CSS can't. prop-for-that writes that runtime state into --live-* and --const-* custom properties — batched and diffed down to one setProperty per frame — so your CSS can compose and react to it with plain calc() and var().
Zero dependencies. TypeScript. ESM + CJS. SSR-safe.
npm i prop-for-that
Quick start
<script type="module">import 'prop-for-that/auto'</script>
<input type="range" data-props-for="range" />
/* the slider paints itself from its own value — no event listeners, no render loop */
input {
background: hsl(calc(var(--live-value-pct) * 120) 80% 50%);
}
Bind any element with data-props-for="key …" and read its --live-* properties in CSS. That's the whole idea.
Why
- CSS does the work. No per-element event handlers or render loops — bind once, compose in stylesheets.
- Fast by design. One
requestAnimationFrameflush per frame — idle when nothing changes, frozen while the tab is hidden — plus write-on-change diffing and a single sharedResizeObserver/IntersectionObserverfor the whole page. Continuously-sampling element sources pause while their element is off screen; event-driven ones (form fields, ranges, selects) run ungated. - Ship only what you use. Four lightweight core sources are built in; everything else is an opt-in, tree-shakeable plugin — and under
autoeach plugin loads on demand, the moment adata-props-forattribute asks for it. - Plays with the platform. Opt into typed
@propertyvalues for interpolation, or FOUC-safe constants written before first paint. - Tiny and dependency-free, in every bundle format.
What it can read
Core (built in): viewport, element size, visibility, and <input type="range"> values.
Plugins (opt-in): pointer position, battery, network, online status, page focus & visibility, navigation type, page meta tags, FPS, clock, scroll velocity, device orientation / motion, geolocation, CPU pressure, soft-keyboard geometry, media playback, form & field state, select & color-picker values, and dominant + accent colors extracted from images and video — 20+ in all.
Entry points
| Import | What it does |
|---|---|
prop-for-that/auto |
Zero-config & declarative: binds every data-props-for element — globals included, via <html data-props-for="…"> — loading plugin sources on demand, kept in sync with the DOM. |
prop-for-that |
Imperative API — propsFor(), register(), configure() — for explicit control and teardown. |
prop-for-that/head |
Synchronous, FOUC-safe constants (scrollbar width, DPR, core count, device memory) before first paint. |
prop-for-that/plugins |
The opt-in plugin catalog. |
autosees the light DOM only (not shadow roots — bind those withpropsFor(el, …)), and lazy-loads plugin chunks, so from a CDN use one that serves thedistfiles verbatim (unpkg / jsDelivr), not a rewriting CDN.
License
MIT © Adam Argyle
Fresh dev news, AI-digested daily. Never miss a trend at devoured.fyi