CSX CSS Utilities for TypeStyle

10.0.2 · active · verified Sun Apr 19

CSX (CSS eXpressions) is a utility library designed to simplify the creation of strongly typed CSS values and functions within TypeScript environments, primarily serving as a companion to the TypeStyle library. It offers a comprehensive set of helpers for common CSS properties and units, such as `color`, `rgb`, `hsl`, `px`, `em`, `percent`, as well as shorthand properties like `margin` and `padding`. The current stable version, 10.0.2, reflects ongoing development with a recent focus on aligning its output more closely with the CSS Object Model (CSSOM) for improved consistency and testability across different browsers and testing setups. This includes changes to color function spacing and the addition of optional alpha parameters for `rgb()` and `hsl()`. CSX maintains an active release cadence, delivering continuous enhancements and bug fixes. Its core differentiator lies in providing a robust, type-safe API for dynamic CSS value generation, significantly reducing runtime errors and improving code maintainability compared to raw string concatenation or less-typed approaches.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining various CSS values and shorthands using CSX helpers like colors, units, margins, and then integrating them into a TypeStyle stylesheet.

import { color, rgb, hsl, px, em, rem, percent, viewHeight, viewWidth, margin, padding, url, quote } from 'csx';
import { style } from 'typestyle'; // Assuming TypeStyle is used as recommended

// Define some CSS values using CSX helpers
const brandColor = color('#1E90FF'); // Dodger Blue
const semiTransparent = rgb(30, 144, 255, 0.7); // RGB with alpha
const lightAccent = hsl(200, 80, 95); // HSL color

// Unit helpers
const defaultPadding = px(16);
const headingFontSize = em(2.5);
const bodyFontSize = rem(1.1);
const viewportWidth = viewWidth(100);
const elementHeight = viewHeight(50);
const responsiveWidth = percent(75);

// Shorthand properties
const cardMargin = margin(px(20), 'auto'); // Top/Bottom 20px, Left/Right auto
const elementPadding = padding(defaultPadding, px(10)); // Top/Bottom 16px, Left/Right 10px

// URL and quoting helpers
const backgroundImage = url('/assets/background.jpg');
const fontFaceName = quote('My Custom Font');

// Example of integrating with TypeStyle
const myStyledElement = style({
  color: brandColor.toString(),
  backgroundColor: semiTransparent.toString(),
  border: `${px(1)} solid ${lightAccent.toString()}`,
  padding: elementPadding.toString(),
  margin: cardMargin.toString(),
  width: responsiveWidth.toString(),
  height: elementHeight.toString(),
  fontSize: bodyFontSize.toString(),
  fontFamily: fontFaceName.toString(),
  backgroundImage: backgroundImage.toString(),
  $nest: {
    '& > h1': {
      fontSize: headingFontSize.toString(),
      textAlign: 'center',
    },
    '@media (max-width: 768px)': {
      width: viewportWidth.toString(),
      margin: margin(px(10)),
    },
  },
});

console.log('CSX Brand Color:', brandColor.toString());
console.log('CSX Element Padding:', elementPadding.toString());
console.log('CSX Card Margin:', cardMargin.toString());
console.log('CSX Background Image:', backgroundImage.toString());
console.log('CSX Font Face Name:', fontFaceName.toString());
console.log('Generated TypeStyle class name:', myStyledElement);

view raw JSON →