• Stars
    star
    206
  • Rank 189,435 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 21 days ago

Reviews

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

Repository Details

Dynamic, theme-driven, style props for vanilla-extract.

Rainbow Sprinkles 🧁

Dynamic, theme-driven, style props for vanilla-extract

Release license: MIT Contributor Covenant Maintainer

Rainbow sprinkles works similarly to @vanilla-extract/sprinkles. Like sprinkles, it generates custom CSS utility classes at build time. While sprinkles requires a pre-defined list of available values, Rainbow Sprinkles uses CSS custom properties to allow dynamic values using inline style variable assignments.

Compared to sprinkles:

  • Rainbow sprinkles ships a fraction of the CSS. For each property, Sprinkles produces CSS that's a factor of [pre-defined values] * [possible conditions]. Rainbow sprinkles produces CSS that only scales with the number of conditions.
  • Supports dynamic values. Rainbow Sprinkles uses dynamic inline style assignments to set the value of each property. You still get the TypeScript editor suggestions, but the ability to use any valid CSS value for that property.

function App() {
  return (
    // Use pre-defined values
    <Box bg="$blue50" margin="$large">
      {/* Or any valid CSS value */}
      <Box textAlign="center" fontSize="30px">
        Hello world!
      </Box>
    </Box>
  );
}

See a stackblitz demo here!


Setup

Install Rainbow Sprinkles.

npm install rainbow-sprinkles

Create a rainbow-sprinkles.css.ts file, then create and export your rainbowSprinkles function:

// rainbow-sprinkles.css.ts
import { defineProperties, createRainbowSprinkles } from 'rainbow-sprinkles';

// or import a theme (e.g. `createTheme`, `createThemeContract`)
const vars = {
  space: {
    none: 0,
    small: '4px',
    medium: '8px',
    large: '16px',
    // etc.
  },
  colors: {
    blue50: '#eff6ff',
    blue100: '#dbeafe',
    blue200: '#bfdbfe',
    gray700: '#374151',
    gray800: '#1f2937',
    gray900: '#111827',
    // etc.
  },
};

const responsiveProperties = defineProperties({
  conditions: {
    mobile: {},
    tablet: { '@media': 'screen and (min-width: 768px)' },
    desktop: { '@media': 'screen and (min-width: 1024px)' },
  },
  defaultCondition: 'mobile',
  dynamicProperties: {
    // Define pre-determined values, which will be autosuggested
    color: vars.colors,
    backgroundColor: vars.colors,
    margin: vars.space,
    marginTop: vars.space,
    marginLeft: vars.space,
    marginRight: vars.space,
    marginBottom: vars.space,
    // Will work with any CSS value
    display: true,
    textAlign: true,
    flexDirection: true,
    justifyContent: true,
    alignItems: true,
  },
  staticProperties: {
    // Build out utility classes that don't use CSS variables
    display: ['block', 'flex', 'inline-block', 'inline-flex'],
  },
  shorthands: {
    bg: ['backgroundColor'],
    m: ['margin'],
    mr: ['marginRight'],
    ml: ['marginLeft'],
    mt: ['marginTop'],
    mb: ['marginBottom'],
    marginX: ['marginLeft', 'marginRight'],
    marginY: ['marginTop', 'marginBottom'],
    mx: ['marginLeft', 'marginRight'],
    my: ['marginTop', 'marginBottom'],
  },
});

export const rainbowSprinkles(responsiveProperties)

export type Sprinkles = Parameters<typeof rainbowSprinkles>[0];

Then set-up in your "host" component (in this case, a Box component):

// Box.tsx
import { rainbowSprinkles, Sprinkles } from './rainbow-sprinkles.css';

interface BoxProps extends Sprinkles {
  children?: React.ReactNode;
}

export const Box = ({ children, ...props }: BoxProps) => {
  const { className, style, otherProps } = rainbowSprinkles(props);

  return (
    <div className={className} style={style} {...otherProps}>
      {children}
    </div>
  );
};

🎉 Good to go!

// App.tsx
import { Box } from './Box';

function App() {
  return (
    // Use pre-defined values
    <Box bg="$blue50" margin="$medium $large">
      {/* Or any valid CSS value */}
      <Box textAlign="center" fontSize={{ mobile: '16px', desktop: '32px' }}>
        Hello world!
      </Box>
    </Box>
  );
}

dynamicProperties vs staticProperties

One trade off that's made for supporting dynamic values is that we have to increase the size of the document. Instead of just appending a single class to an element to add a style, both a utility class and an inline style assignment is added to an element. While this setup will still produce an overall smaller bundle in many cases, some large applications may observe frequent recurrence of specific combinations of CSS properties and values. In these cases, those combinations can be set-up in staticProperties in the initial configuration. staticProperties will produce typical CSS utility classes. The runtime portion of Rainbow Sprinkles will defer to the CSS classes created by staticProperties and not apply any inline style assignments.

Here's an example scenario in which this property could be valuable. Your organization sets up Rainbow Sprinkles and sees widespread adoption. Your metrics reveal that the most frequently used prop/value combinations is display="flex" and margin with the application's theme variables. You can run an experiment to evaluate whether making these property/values combination static improves the bundle size.

createRainbowSprinkles({
  dynamicProperties: {
    // Still support arbitrary values
    display: true,
    margin: true,
  },
  staticProperties: {
    // Also produce fixed CSS classes
    display: ['flex'],
    margin: vars.space,
  },
});

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated. For detailed contributing guidelines, please see CONTRIBUTING.md

Thanks

  • Vanilla Extract for creating an inovative and configurable CSS preprocessor
  • Styled System for inventing theme-driven style props
  • Homebase, Wayfair's design system, for providing interesting problems to solve

License

Distributed under the MIT License. See LICENSE for more information.

Contact

More Repositories

1

pylift

Uplift modeling package.
Python
367
star
2

hypernova-php

PHP client for rendering your React components via Hypernova.
PHP
76
star
3

WANDS

Jupyter Notebook
56
star
4

git-parse

JavaScript library that generates an array of JavaScript objects, representing the current branch of a local git repository's commit history
TypeScript
41
star
5

statsdcc

Statsdcc is a Statsd-compatible high performance multi-threaded network daemon written in C++. It aggregates stats and sends aggregates to backends.
C++
36
star
6

data-snapshot

📸 data-snapshot is a generic mock data manager for JavaScript tests, that helps you use data from real endpoints in tests.
JavaScript
33
star
7

brickkit-android

BrickKit for Android
Kotlin
27
star
8

hussar

PHP static analysis with HHVM
Shell
27
star
9

terraform-provider-foreman

Go
24
star
10

one-version

Implementation of Google’s One Version Rule for JS monorepos
JavaScript
23
star
11

keras_image_similarity_training

A Jupyter Notebook and python scripts that allows users to easily train a siamese network on image similarity, export the model to a SavedModel file, and index images for kNN search.
Jupyter Notebook
19
star
12

bagel

🥯 Flexible rendering service for server-rendered React applications ⚛︎
JavaScript
12
star
13

vsm-ios

An iOS framework for the VSM Architecture
Swift
9
star
14

ospo-automation

Automation workflows for Wayfair's Open Source Program Office
4
star
15

cream

Redis analytics tool
C
4
star
16

wayfair.github.io

Wayfair Open Source Program Office outward-facing site
JavaScript
4
star
17

ngx_http_txtset

ngx_http_txtset module
C
3
star
18

psVaultUtils

A PowerShell Module for interacting with Hashicorp Vault.
PowerShell
2
star