JSS - JavaScript Style Sheets

10.10.0 · abandoned · verified Sun Apr 19

JSS (JavaScript Style Sheets) is a library for generating and managing CSS rules with JavaScript, offering a modular and component-based approach to styling. It allows developers to define styles directly within their JavaScript code, providing dynamic theming, server-side rendering support, and efficient style injection. The current stable version is 10.10.0, although version 10.10.1 explicitly marked the project as no longer maintained. Its key differentiators include a plugin-based architecture for extending functionality, support for various CSS features like nesting and global styles via plugins, and integration with frameworks like React through `react-jss`. It provides a powerful API to create, update, and remove stylesheets dynamically, enabling highly interactive and configurable UI styling. However, due to its abandoned status, new projects should consider actively maintained alternatives.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize JSS, define styles, create and attach a stylesheet to the DOM, and prepare CSS for server-side rendering.

import { create } from 'jss';
import { SheetsRegistry } from 'jss';

// 1. Create a JSS instance
const jss = create();

// 2. Define your styles as a JavaScript object
const styles = {
  button: {
    background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
    borderRadius: 3,
    border: 0,
    color: 'white',
    height: 48,
    padding: '0 30px',
    boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
    cursor: 'pointer',
    '&:hover': {
      boxShadow: '0 5px 7px 3px rgba(255, 105, 135, .5)',
    },
  },
  container: {
    margin: '20px',
    textAlign: 'center',
    fontFamily: 'sans-serif',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    minHeight: '100px',
  },
};

// 3. Create a stylesheet from your styles
const sheet = jss.createStyleSheet(styles, { link: true });

// 4. Attach the stylesheet to the DOM (this injects the CSS)
sheet.attach();

console.log('JSS Stylesheet attached to DOM.');
console.log('Generated CSS classes:', sheet.classes);
console.log('Full generated CSS:\n', sheet.toString());

// Example: Dynamically update a style (if rules were functions)
// For simple object styles, you might modify the object and re-attach
// const dynamicSheet = jss.createStyleSheet({ dynamicColor: (props) => ({ color: props.color }) });
// dynamicSheet.update({ color: 'blue' }).attach();

// For server-side rendering, you'd collect sheets
const sheets = new SheetsRegistry();
sheets.add(sheet);
const serverSideCss = sheets.toString();
console.log('\nCSS for Server-Side Rendering:\n', serverSideCss);

// You can detach the stylesheet if no longer needed
// sheet.detach();

view raw JSON →