• Stars
    star
    264
  • Rank 155,103 (Top 4 %)
  • Language
    TypeScript
  • License
    Other
  • Created almost 2 years ago
  • Updated 9 months ago

Reviews

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

Repository Details

the predefinite CSS-in-JS library for vite

🎈 ecsstatic

The predefinite CSS-in-JS library for Vite.

  • fully static. compiles away like it never existed.
  • uses regular css syntax, not javascript objects.
  • minimal api surface: you write some styles, you get back a scoped class.
  • suppports nesting selectors and at-rules, including @container, and @layer.
  • supports sass itself!

Try one of the starter templates on stackblitz:

Also check out the landing page: ecsstatic.dev.

Usage

Install:

npm install --save-dev @acab/ecsstatic

Add the vite plugin to your config:

import { ecsstatic } from '@acab/ecsstatic/vite';

export default defineConfig({
  plugins: [ecsstatic()],
});

Start using css in any JS/TS file:

import { css } from '@acab/ecsstatic';

export const Button = (props) => {
  return <button {...props} className={button} />;
};

const button = css`
  all: unset;
  font: inherit;
  color: #862e9c;
  border: 1px solid;
  border-radius: 4px;
  padding: 0.5rem 1rem;

  &:hover,
  &:focus {
    color: #be4bdb;
  }
`;

Or use with /scss:

import { css } from '@acab/ecsstatic/scss';

export const Button = (props) => {
  return <button {...props} className={button} />;
};

const button = css`
  @use 'open-props-scss' as op;

  // ...
  color: op.$purple-9;

  &:hover,
  &:focus {
    color: op.$purple-6;
  }
`;

Evaluating expressions (interpolation)

Evaluating expressions interpolated in the template strings works out-of-the-box for many cases but might not work perfectly in big files/projects. If you are seeing unexpected results, try moving your component out to a smaller file.

By default, npm packages are not processed (they are "external"-ized) before evaluating expressions. This requires the package to be compatible with Node ESM. If it doesn't work, you can pass its name to the resolvePackages option to force it to be processed before evaluating expressions.

export default defineConfig({
  plugins: [ecsstatic({ resolvePackages: ['some-non-esm-pkg'] })],
});

Global styles

The createGlobalStyle function can be used to apply unscoped, global styles. Note that this is unnecessary in most cases as you can just create a regular .css/.scss file, but it can be useful for interpolating values that only exist in JS.

import { createGlobalStyle } from '@acab/ecsstatic';

createGlobalStyle`
  :root {
    --foo: ${1 + 1};
  }
`;

Syntax highlighting

For syntax highlighting and intellisense, use the vscode-styled-components extension.

Atomic classes

There is an experimental flag marqueeMode. When enabled, the prod build output will contain atomic classes, where one class maps to one declaration. This can potentially result in a smaller CSS file, at the cost of bloating the markup with lots of classes. This tradeoff can be worth it for large sites where the size of the CSS would be a concern.

export default defineConfig({
  plugins: [ecsstatic({ marqueeMode: true })],
});

Prior art

Huge shoutout to the previous libraries that came before this; ecsstatic would not have been possible without them paving the way.

  • styled-components / emotion
  • css modules
  • linaria

Contributing

Open an issue or see CONTRIBUTING.md.

More Repositories

1

reset.css

a css reset for 2024 and beyond.
CSS
310
star
2

astro-css-in-js-tests

Examples of using various CSS-in-JS libs in Astro (repo for withastro/astro#4432)
TypeScript
22
star
3

open-props-scss

open-props as sass variables
JavaScript
20
star
4

live-announcer

web component that makes it easier to work with live regions
JavaScript
16
star
5

tabs

accessible, unstyled, API-less tabs
HTML
13
star
6

postcss-use-logical

auto convert physical CSS properties to logical ones
JavaScript
12
star
7

vite-plugin-webc

JavaScript
12
star
8

riffs

a web app for clipping parts of a song
TypeScript
11
star
9

astro-vercel-edge

Deploy Astro v4 on Vercel Edge Functions
JavaScript
9
star
10

tooltip-element

an accessible tooltip web component built using popover API and floating-ui
JavaScript
8
star
11

webc-astro-demo

webc + astro = πŸ’œ
Astro
8
star
12

wordel

a more accessible wordle clone built using preact
TypeScript
6
star
13

programming-languages

Course content for CS 403
Scheme
4
star
14

11ty-jsx-deno

TypeScript
3
star
15

astro-minimal-starter

An opinionated minimal starter template for astro with prettier, tsconfig aliases, dark theme, and a basic css reset
Astro
2
star
16

enhance-demo

CSS
2
star
17

visually-hidden

Inclusively hide content using a custom element.
JavaScript
2
star
18

11ty-webc-starter

A barebones starter template using the latest canary of 11ty and WebC
JavaScript
2
star
19

open-styleable

experimental server+client "polyfill" for openly styling shadow-roots
HTML
2
star
20

astro-form-progressive

TypeScript
2
star
21

remix-djent-stack

Remix stack with basic postcss-preset-env, code formatting, linting etc
JavaScript
2
star
22

do-transitions

view transitions + FLIP animation fallback
HTML
2
star
23

astro-api-starter

A starter template for deploying Astro SSR API routes to Vercel
Astro
1
star
24

tokencss-linaria-demo

Astro
1
star
25

remix-vite-test

TypeScript
1
star
26

declarative-shadow-dom

Use declarative shadow DOM inside frameworks
TypeScript
1
star
27

lyrica

A PWA that uses Web Share Target for quickly finding song lyrics
HTML
1
star
28

astro-layers-demo

A fun demo showcasing cascade layers in astro
Astro
1
star
29

x-flip-demo

TypeScript
1
star