npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

css-expr

v1.0.3

Published

Expression-driven CSS Property Binder - Dynamic CSS values using JavaScript expressions

Readme

css-expr

Tests npm version bundle size License: MIT

Dynamic CSS property binding using JavaScript expressions

Create responsive layouts that go beyond CSS media queries. Bind CSS properties to element dimensions, implement conditional styling, and build mathematical relationships between elements with declarative expressions.

Features

  • Declarative Syntax - Express complex responsive behavior in HTML attributes
  • Rich Expressions - Mathematical functions, conditionals, logical operators
  • Unit Support - Seamless conversion between px, vw, vh, em, rem, %

Quick Start

Installation

npm install css-expr

Basic Usage

<div id="container" style="width: 1000px; height: 600px;">
  <div data-expression="
    this.width = el('#container').width * 0.6;
    this.height = el('#container').height * 0.4;
    this.backgroundColor = this.width > 400 ? 'lightblue' : 'lightcoral';
  ">
    Responsive content that adapts to its container
  </div>
</div>

CDN Usage

<script src="https://unpkg.com/css-expr@latest/dist/css-expr.min.js"></script>

Documentation

Core Concepts

Element References

// Reference the current element
this.height = this.width * 0.618; // Golden ratio

// Reference other elements by selector
this.width = el('#sidebar').width + el('#content').width;

// Use base values (preserved from initial state)
this.fontSize = this.baseFontSize * 1.2;

Available Properties

Access comprehensive element measurements:

Geometric Properties: width, height, top, left, right, bottom Font Properties: fontSize, lineHeight, letterSpacing, fontWeight Base Values: baseWidth, baseHeight, baseFontSize, etc.

Rich Expression Syntax

// Mathematical operations
this.width = Math.max(200, el('#container').width * 0.3);

// Conditional styling
this.display = el('#menu').width > 768 ? 'flex' : 'block';

// Logical operators
this.opacity = this.width > 500 && this.height > 300 ? 1 : 0.7;

// Automatic font fitting
this.fontSize = this.fitFont(12, 48);

Use Cases

Responsive Design Beyond Media Queries

<!-- 3-column grid that adapts to container width -->
<div class="grid-item" data-expression="
  this.width = el('#grid').width > 1200 ? 
    el('#grid').width / 4 - 20px :
    el('#grid').width > 800 ? 
    el('#grid').width / 2 - 15px :
    el('#grid').width - 10px;
">Grid Item</div>

Element-Dependent Styling

<!-- Sidebar that affects main content layout -->
<main data-expression="
  this.marginLeft = el('#sidebar').width + 'px';
  this.padding = el('#sidebar').width > 0 ? '40px' : '20px';
">Content adapts to sidebar presence</main>

Dynamic Typography

<!-- Text that scales with container and maintains readability -->
<h1 data-expression="
  this.fontSize = Math.max(24, Math.min(72, this.width / 15));
  this.lineHeight = this.fontSize * 1.2;
  this.letterSpacing = this.fontSize > 48 ? '2px' : '1px';
">Responsive Heading</h1>

API

Global API

import { cssExpr } from 'css-expr';

cssExpr.start();                                    // Initialize engine
cssExpr.reload();                                   // Restart and rediscover
cssExpr.addBinding(element, 'width', 'expression'); // Add binding programmatically  
cssExpr.removeBindings(element);                    // Remove element bindings
cssExpr.getBindings();                              // Get all bindings (debug)
cssExpr.destroy();                                  // Cleanup

Programmatic Usage

import { BindingEngine, Parser, Interpreter } from 'css-expr';

// Custom engine instance
const engine = new BindingEngine();
engine.discover();
engine.updateAllBindings();

// Parse and evaluate expressions
const parser = new Parser('this.width = el("#container").width * 0.5');
const ast = parser.parse();

Required APIs:

  • ResizeObserver
  • CSS Custom Properties
  • ES Modules

Performance

css-expr is optimized for production use:

  • Lightweight: ~20KB minified + gzipped
  • Efficient: ResizeObserver-based updates, intelligent caching
  • Non-blocking: Asynchronous expression evaluation
  • Memory-conscious: WeakMap-based element tracking

Security

  • No eval(): Expressions are parsed into AST and safely evaluated
  • Sandboxed execution: Limited to predefined functions and properties
  • CSP compatible: Works with Content Security Policy restrictions
  • XSS protection: String values are properly escaped

Testing

npm test              # Run test suite
npm run test:coverage # Run with coverage
npm run test:ui       # Interactive test UI

Development

npm install           # Install dependencies
npm run dev          # Development build with watch
npm run build        # Production build
npm run typecheck    # TypeScript validation

Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Submit a Pull Request

License

MIT License - see LICENSE file for details.