1.2.8 • Published 2 years ago

@stitches/core v1.2.8

Weekly downloads
3,243
License
MIT
Repository
github
Last release
2 years ago

Why

  • Atomic mindset: Each CSS property is an atomic part of your complete CSS
  • Reusability: Each CSS property, given the same screen, pseudo and value is considered the same, giving high degree of reusability
  • Optimal injection: You can compose your styles outside of your UI, but no injection happens until it is actually used
  • Tokenized: Configure with tokens to give design restrictions
  • Screens: Define a set of media queries as screens to easily express CSS active within a screen
  • Utils: Create your own CSS properties
  • Typed: Fully typed API, even though you are not using Typescript
  • Specificity solved: No more specificity issues, as an atomic mindset opens up a more efficient and straight forward way to solve it
  • Token based theming: Tokens are CSS variables. Create themes overriding the tokens and expose themes to any parts of your app

A simple benchmark VS styled-components

Get started

npm install @stitches/core

import { css } from '@stitches/core';

const button = css({
  color: 'gray',
  '&:hover': {
    color: 'black',
  },
  borderColor: 'black',
  padding: '1rem',
});

const alertButton = css(button, {
  borderColor: 'red',
});

const dynamicButton = (disabled = false) =>
  css(
    button,
    disabled && {
      opacity: 0.5,
    }
  );

Configure an instance

import { createCss } from '@stitches/core';

export const css = createCss({
  // Optinally add a prefix to all classnames to avoid crashes
  prefix: 'my-lib',
  // Maps tokens to properties. Follows the system-ui theme specification: https://system-ui.com/theme
  tokens: {
    colors: {
      RED: 'tomato',
    },
    space: {
      0: '1rem',
    },
    fontSizes: {},
    fonts: {},
    fontWeights: {},
    lineHeights: {},
    letterSpacings: {},
    sizes: {},
    borderWidths: {},
    borderStyles: {},
    radii: {},
    shadows: {},
    zIndices: {},
    transitions: {},
  },
  // Create screens with media queries. Note that the media queriy with the
  // highest specificity should go last
  breakpoints: {
    tablet: (rule) => `@media (min-width: 700px) { ${rule} }`,
  },
  // Create your own custom CSS properties. Here the functional syntax
  // shines to handle pseudo selectors
  utils: {
    marginX: (config) => (value: number | string) => ({
      marginLeft: value,
      marginRight: value,
    }),
  },
});

css({
  color: 'RED', // Creates "tomato"
  tablet: {
    color: 'blue', // Color is "blue" when media query is active
  },
  marginX: 0, // Creates "1rem", as it composes margin, using "space" from tokens
  border: '1px solid RED', // creates a "tomato" border
  border: ['1px', 'solid', 'RED'], // You can also use array syntax to get typing
  boxShadow: ['1px', '1px', '1px', 'RED'], // You can also use array syntax with shadow
});

Utility first

Stitches also allows you to put your utils at the front. That means you can create your very own CSS abstraction, where the underlying CSS properties are secondary.

import { createCss } from '@stitches/core';

export const css = createCss({
  utilityFirst: true,
  utils: {
    text: (config) => (value: { color?: string; size?: number }) => ({
      ...(color ? { color } : {}),
      ...(size ? { fontSize: size + 'rem' } : {}),
    }),
  },
});

css({
  text: {
    color: 'red',
    size: 2,
  },
  ':hover': {
    text: {
      color: 'blue',
    },
  },
  // Override is a property that allows you to override
  // with specific low level CSS properties
  override: {
    padding: '2rem',
  },
});

Themes

You can create theme instances which overrides tokens:

import { createCss } from '@stitches/core';

export const css = createCss({
  tokens: {
    colors: {
      primary: 'tomato',
    },
  },
});

export const funnyTheme = css.theme({
  colors: {
    primary: 'pink',
  },
});

This theme represents a classname which can be added at any point in your DOM tree. You can add multiple themes, which overrides each other by the nested level you apply them.

Server side rendering

The createCss factory automatically detects if you are in a browser or server environment. That means when you this factory on the server it will hash the classnames (for rehydration abilities) and allow you to collect the styling to include in the responded html:

import { createCss } from '@stitches/core';

const css = createCss({});
const { result, styles } = css.getStyles(() => renderSomething(css));

Note that server produced CSS does not contain vendor prefixes, as there is no browser environment to look at. If you have a server rendered application you can either manually add the vendor prefixes you need:

css({
  WebkitFontSmoothing: 'antialiased',
  MozOsxFontSmoothing: 'grayscale',
});

Or you can use a postcss to do the conversion:

import { createCss } from '@stitches/core';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';

const css = createCss({});
const { result, styles } = css.getStyles(() => renderSomething(css));

Promise.all(
  styles.map((style) => postcss([autoprefixer({ browsers: ['> 1%', 'last 2 versions'] })]).process(style))
).then((styles) => {
  // styles with vendor prefixes
});
zodiac-ui-styles@amedia/brick-tokens@neptuno-ui/web@tresnpm/stitch@everything-registry/sub-chunk-874@easyblocks/reactvue-auth-uisandpack-vue3react-zero-to-onevue3-stitcheshua123an-solidjupiter-vuenatto-hacky-dark-themenatto-hacky-dark-theme-1nika-stylespasubot-auth-ui-reactsoild-stitchessolid-stitches-jssolid-leva@asherng/styled-system@asng/styled-system@airbridge/component@aychtang/auth-ui-react@carecar-evo/core-ui@c3/stitches@chefu/auth-ui-react@codesandbox/sandpack-react@davidmccabe/sandpack-reactwebxdc-dev@easyblocks/core@ed-ui/styles@heliumblocks/core@halka/halka-ui@fleetworks/supabase-auth-ui-react@everfund/sdk@euclidesdry/electron-pretty-titlebar@freedom-ds/styles@grantscout/auth-ui-svelte@kozmodb/auth-ui-react@kozmodb/auth-ui-solid@kozmodb/auth-ui-svelte@lucasdellabella/supabase-auth-ui-react@lotte-innovate/lui@lotte-innovate/ui-component-test@maroxy/auth-ui-react@maroxy/auth-ui-solid@maroxy/auth-ui-svelte@marcosmoreirasalvini/shared@nelson-ui/core@nandu/dashboard@nanduland/dashboard@ezcater/recipe@mediaplatform/basketball-widgets@mediaplatform/collaborative-editor@mediaplatform/football-widgets@mediaplatform/ice-hockey-widgets@mediaplatform/multisport-widgets@mediaplatform/tennis-widgets@makinox/makinox-ui@jimmydc/stitches-ui@jimmydc/package-react-minimal@joaopsguimaraes/core@joaopsguimaraes/react@kite_aerolab/shared@jrhizor/auth-ui-react@jupiterds/styles@jupiterds/tokens@kabuki/ui@pvdevs/ui@scgdigital/scg-ui-package@stelarys/core@passcard/ui@osener/supabase-auth-ui-react@shopstory/react@skillsociety/auth-ui-react@styless-uikits/stitches@svelteuidev/core@swell/easyblocks-core@supabase/auth-ui-react@supabase/auth-ui-solid@supabase/auth-ui-svelte@sportsuex/react@resembli/ui@scute/ui-shared@shopforge/core@drycstud.io/electron-titlebar@collai/auth-ui-react@cookbook-ui/tokens@blankui-org/button@rtzr/auth-ui-reacteasyblocks_coreds-king@ucam/helix@unstyled-ui/stitches@uniqe/design@weareredlight/design-system@webxdc/webxdc-dev@wootiv/auth-ui-react@winter-love/hyper-components@winter-love/stitches
1.3.1-1

2 years ago

1.3.1-0

2 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.7-0

3 years ago

1.2.6

3 years ago

1.2.6-1

3 years ago

1.2.6-0

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

1.0.0-canary.19

3 years ago

1.0.0-canary.17

3 years ago

1.0.0-canary.18

3 years ago

1.0.0-canary.16

3 years ago

1.0.0-canary.13

3 years ago

1.0.0-canary.14

3 years ago

1.0.0-canary.15

3 years ago

1.0.0-canary.12

3 years ago

1.0.0-canary.11

3 years ago

1.0.0-canary.10

3 years ago

1.0.0-canary.8

3 years ago

1.0.0-canary.9

3 years ago

1.0.0-canary.6

3 years ago

1.0.0-canary.7

3 years ago

0.2.5

3 years ago

1.0.0-canary.4

3 years ago

1.0.0-canary.5

3 years ago

1.0.0-canary.3

3 years ago

1.0.0-canary.0

3 years ago

1.0.0-canary.1

3 years ago

1.0.0-canary.2

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

4 years ago

0.2.0-canary.8

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.0-canary.6

4 years ago

0.2.0-canary.7

4 years ago

0.2.0-canary.5

4 years ago

0.2.0-canary.4

4 years ago

0.2.0-canary.3

4 years ago

0.2.0-canary.2

4 years ago

0.2.0-canary.1

4 years ago

0.2.0-canary.0

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.3

4 years ago

0.1.0

4 years ago

0.1.0-canary.21

4 years ago

0.1.0-canary.20

4 years ago

0.1.0-canary.18

4 years ago

0.1.0-canary.19

4 years ago

0.1.0-canary.17

4 years ago

0.1.0-canary.16

4 years ago

0.1.0-canary.15

4 years ago

0.1.0-canary.13

4 years ago

0.1.0-canary.14

4 years ago

0.1.0-canary.12

4 years ago

0.1.0-canary.11

4 years ago

0.1.0-canary.10

4 years ago

0.1.0-canary.9

4 years ago

0.1.0-canary.8

4 years ago

0.1.0-canary.7

4 years ago

0.1.0-canary.5

4 years ago

0.1.0-canary.4

4 years ago

0.1.0-canary.6

4 years ago

0.1.0-canary.3

4 years ago

0.1.0-canary.2

4 years ago

0.1.0-canary.1

4 years ago

0.1.0-canary.0

4 years ago

0.0.3-canary.4

4 years ago

0.0.3-canary.2

4 years ago

0.0.3-canary.1

4 years ago

0.0.2

4 years ago

0.0.3-canary.0

4 years ago

0.0.2-canary.0

4 years ago

0.0.1

4 years ago