• Stars
    star
    413
  • Rank 104,801 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CSS-in-JS microlibrary for making design systems approachable with React

glaze

CSS-in-JS microlibrary for making design systems approachable with React

npm Language grade: JavaScript Travis (.com) Open Collective backers and sponsors

💡 Motivation

This project was born to combine the best of its predecessors into a single solution:

  • Utility-first CSS, as implemented by Tailwind CSS
    • Fully static, but customizable upfront
    • Embraces reusability with no duplicated rules
  • Constraint-based layouts, popularized by Theme UI
    • Highly dynamic, thankfully to Emotion
    • One-off styles can be defined naturally
  • Near-zero runtime, made possible by treat
    • Theming support with legacy browsers in mind
    • Static style extraction while retaining type safety

📚 Usage

  1. Install the package and its peer dependencies:

    npm install glaze treat
  2. Define a theme, preferably by overriding the default tokens:

    /* theme.treat.js */
    
    import { createTheme, defaultTokens } from 'glaze';
    
    export default createTheme({
      ...defaultTokens,
    
      // Customization
      scales: {
        ...defaultTokens.scales,
        color: {
          red: '#f8485e',
        },
      },
    });

    Keeping the runtime as small as possible, only a few tokens (breakpoints, shorthands and aliases) are embedded into production JavaScript bundles. Other values can only be accessed exclusively for styling, as shown later.

  3. Apply the theme through ThemeProvider:

    📝 The Gatsby plugin for glaze does this unobtrusively.

    import { ThemeProvider } from 'glaze';
    import theme from './theme.treat';
    
    export default function Layout({ children }) {
      return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
    }
  4. Style elements with the sx function:

    import { useStyling } from 'glaze';
    
    export default function Component() {
      const sx = useStyling();
    
      return (
        <p
          className={sx({
            px: 4, // Sets padding-left and padding-right to 1rem
            color: 'white',
            bg: 'red', // Sets background to #f8485e
          })}
        >
          Hello, world!
        </p>
      );
    }
  5. Set up static style extraction with the help of treat.

    📝 The Gatsby plugin for treat does this unobtrusively.

    • Afterwards, selector-based CSS rules may be created with globalStyle in *.treat.js files. They have to be applied as a side effect, e.g. from a top-level layout component:

      import './globalStyles.treat.js';
  6. Configure server-side rendering for dynamically created styles.

🤔 How it works

  • The sx function maps themed values to statically generated class names
    • If that fails, the style gets injected dynamically through the CSSOM
  • Dynamic styles which are not in use by any component get removed

Rule handling

  1. Transform each alias to its corresponding CSS property name or custom shorthand
  2. Resolve values from the scales available
    • CSS properties associated with a custom shorthand are resolved one by one

Example

Given the theme below:

import { createTheme } from 'glaze';

export default createTheme({
  scales: {
    spacing: { 4: '1rem' },
  },
  shorthands: {
    paddingX: ['paddingLeft', 'paddingRight'],
  },
  aliases: {
    px: 'paddingX',
  },
  matchers: {
    paddingLeft: 'spacing',
    paddingRight: 'spacing',
  },
});

An sx parameter is matched to CSS rules as follows:

  1. { px: 4 }
  2. { paddingX: 4 }, after transforming aliases
  3. { paddingLeft: 4, paddingRight: 4 }, after unfolding custom shorthands
  4. { paddingLeft: '1rem', paddingRight: '1rem' }, after applying matchers

Contributors

Thanks goes to these wonderful people (emoji key):


Kristóf Poduszló

🚧 💻 📖 💡 🤔 🚇

Jess Telford

📖

Corentin Leruth

📖 💻 ⚠️

Evan Hennessy

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Acknowledgements

Without its predecessors, glaze wouldn't exist. Thanks for all the wonderful people who have contributed towards the project, even indirectly.

The logo's donut emoji is courtesy of Twemoji.

More Repositories

1

otion

Atomic CSS-in-JS with a featherweight runtime
TypeScript
624
star
2

react-hooks

Essential set of React Hooks for convenient Web API consumption and state management.
TypeScript
553
star
3

react-polymorphic-box

Building blocks for strongly typed polymorphic components in React.
TypeScript
311
star
4

react-polymorphic-types

Zero-runtime polymorphic component definitions for React
169
star
5

uuidv7

UUIDv7 generator with millisecond precision
TypeScript
73
star
6

knex-orm

Knex-based object-relational mapping for JavaScript.
JavaScript
62
star
7

mental-poker

A purely functional mental poker library, based on the thesis of Choongmin Lee.
JavaScript
61
star
8

style-vendorizer

Tiny CSS vendor prefixer and property alias mapper for runtime styling solutions
TypeScript
56
star
9

keez

Frictionless hotkey handling for browsers
TypeScript
55
star
10

gatsby-starter-modern

DEPRECATED. Please use gatsby-starter-strict instead, see the linked website!
JavaScript
55
star
11

gatsby-starter-strict

A Gatsby starter with strict linting and auto-formatting rules.
TypeScript
41
star
12

material-components-react

Modular and customizable Material Design UI components for React.
JavaScript
32
star
13

PoloniexApi.Net

Application Programming Interface for data transmission and retrieval from Poloniex through the Microsoft .NET Framework
C#
32
star
14

css-homogenizer

Base CSS encouraging proper HTML semantics and the use of custom design tokens
CSS
31
star
15

next-starter-strict

A strict Next.js starter with Tailwind CSS, TypeScript, ESLint and Prettier
TypeScript
27
star
16

react-layout-components

Layout components for React, primarily inspired by Every Layout (https://every-layout.dev/)
TypeScript
26
star
17

elgamal.js

ElGamal cryptosystem for JavaScript.
JavaScript
25
star
18

css-shorthand-expanders

Type-safe functions to expand CSS shorthands into their longhand sub-properties
TypeScript
23
star
19

MoneroGui.Net

Provides a GUI to interact with the Monero Core assemblies.
C#
18
star
20

provably-fair

Tools for creating and verifying provably fair games.
JavaScript
16
star
21

wsx

Programmatically extensible lightweight WebSocket implementation in JavaScript.
JavaScript
13
star
22

MoneroApi.Net

API for data transmission and retrieval from Monero applications through the Microsoft .NET Framework
C#
10
star
23

bookshelf-validate

Validation for the Model objects of Bookshelf.js
JavaScript
8
star
24

polonibox

Easily check the legitimacy of Poloniex trollbox users and prove the existence of conversations, avoiding scammers to gain reputation.
PHP
8
star
25

exigo

Responsive Quiz Application made with React and Prisma Framework.
TypeScript
7
star
26

redux-for-beginners

An opinionated set of good practices to minimize the effort required to set up and maintain a Redux project.
JavaScript
6
star
27

provably-fair-algorithms-whitepaper

Introduction to Provably Fair Gaming Algorithms
TeX
5
star
28

react-typed-inputs

Strongly typed input components for React.
TypeScript
5
star
29

record-like-deep-assign

Recursively assigns enumerable own properties of the given sources to a target object
TypeScript
5
star
30

tailwindcss-inner-border

A Tailwind CSS plugin that provides utilities for creating inner borders with box-shadow.
JavaScript
4
star
31

astro-starter-strict

A strict Astro starter with Tailwind CSS, TypeScript, ESLint and Prettier
Astro
3
star
32

chip8-emulator

A CHIP-8 emulator written in JavaScript for educational purposes.
JavaScript
3
star
33

schdesign-web-workshop

Schönherz Design Stúdió frontend workshop
HTML
3
star
34

monero-prices.js

Wrapper for the API methods of moneropric.es, written in JavaScript.
JavaScript
3
star
35

gatsby-plugin-cesium

A Gatsby plugin to add support for CesiumJS
JavaScript
3
star
36

parcel-starter-modern

A modern Parcel starter aiming for high productivity with mostly vanilla techniques.
HTML
2
star
37

deckster

An open-source JavaScript library for creating card games.
JavaScript
2
star
38

express-route-fs

File system-based approach for handling routes in Express.
JavaScript
2
star
39

gatsby-plugin-preval

A Gatsby plugin for pre-evaluating code at build-time
JavaScript
1
star
40

podicons

Handwritten SVG icons with compressibility in mind
1
star
41

hmac-rng.js

HMAC-based random number generator written in JavaScript.
JavaScript
1
star
42

tailwindcss-bug-via-transparent

`via-transparent` utility broken in Safari
JavaScript
1
star
43

pcg.js

A functional implementation of the PCG family random number generators, written in JavaScript.
JavaScript
1
star