• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 10 months ago

Reviews

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

Repository Details

Reactive stylesheets for SolidJS

solid-styled

Reactive stylesheets for SolidJS

NPM JavaScript Style Guide

Install

npm i solid-styled
npm i -D postcss
yarn add solid-styled
yarn add -D postcss
pnpm add solid-styled
pnpm add -D postcss

Features

  • Render stylesheets only once
  • Fine-grained reactive CSS properties
  • Scoped stylesheets
  • :global selector
  • @global at-rule
  • SSR
  • Near zero-runtime
  • <style jsx>

Examples

  • Vite - Open in CodeSandbox
  • Astro - Open in CodeSandbox

Usage

Integrations

css

Use the css tagged template for writing stylesheets.

import { css } from 'solid-styled';

function Title() {
  css`
    h1 {
      color: red;
    }
  `;

  return <h1>Hello World</h1>;
}

The template is also reactive. It works by replacing all templating values with a CSS variable. This allows the stylesheet to be only rendered once and can be shared by multiple components of the same scope.

import { css } from 'solid-styled';

function Button() {
  const [color, setColor] = createSignal('red');
  css`
    button {
      color: ${color()};
    }
  `;

  return (
    <button onClick={() => {
      setColor((c) => (c === 'red' ? 'blue' : 'red'));
    }}>
      Current color: {color()}
    </button>
  );
}

By default, all styles are scoped to its component and cannot leak into another component. The scoping only applies to all DOM nodes that can be found in the component, including the children of the external components.

import { css } from 'solid-styled';

function ForeignTitle() {
  return <h1>This is not affected</h1>;
}

function Title() {
  css`
    h1 {
      color: red;
    }
  `;

  return (
    <>
      <h1>This is affected.</h1>
      <ForeignTitle />
      <Container>
        <h1>This is also affected.</h1>
      </Container>
    </>
  )
}

:global

Since all selectors in a given stylesheet are scoped by default, you can use the :global pseudo selector to opt-out of scoping:

import { css } from 'solid-styled';

function Feed(props) {
  css`
    div > :global(* + *) {
      margin-top: 0.5rem;
    }
  `;

  return (
    <div>
      <For each={props.articles}>
        {(item) => (
          // This item is affected by the CSS of the Feed component.
          <FeedArticle data={item} />
        )}
      </For>
    </div>
  );
}

@global

You can use @global instead of :global if you want to declare multiple global styles

css`
  /* this is global */
  @global {
    body {
      background-color: black;
    }

    main {
      padding: 0.5rem;
    }
  }

  h1 {
    color: white;
  }
`;

Forward scope

Since solid-styled scopes DOM elements and not components by default, it doesn't affect things like <Dynamic>. Using use:solid-styled, we can forward the current scope/sheet to the component.

css`
  * {
    color: red;
  }
`;

<Dynamic component={props.as} use:solid-styled>
  {props.children}
</Dynamic>

which compiles into

useSolidStyled('xxxx', '*[s\\:xxxx]{color:red}');

<Dynamic component={props.as} s:xxxx style={vars()}>
  {props.children}
</Dynamic>

<style jsx>

Inspired by styled-jsx.

Use <style jsx> instead of css for declaring styles in JSX expressions. Both <style jsx> and css functions the same.

function Button() {
  const [color, setColor] = createSignal('red');
  return (
    <>
      <style jsx>
        {`
          button {
            color: ${color()};
          }
        `}
      </style>
      <button onClick={() => {
        setColor((c) => (c === 'red' ? 'blue' : 'red'));
      }}>
        Current color: {color()}
      </button>
    </>
  );
}

You can also use <style jsx global> for declaring global styles.

The main motivation for writing an alternative way of declaring styles with <style jsx> is to facilitate the migration from solid-styled-jsx to solid-styled. Possibly, some developers may as well use <style jsx> because of their familiarity with adding the styles inside the JSX.

SSR

<StyleRegistry>

For SSR, you can pass an array to the styles prop of <StyleRegistry>. This array collects all of the "critical" (initial render) stylesheets, that which you can render as a string with renderSheets.

import { renderSheets } from 'solid-styled';

const styles = [];

renderToString(() => (
  <StyleRegistry styles={styles}>
    <App />
  </StyleRegistry>
));

// Render sheets
// You can use the resulting sheet by inserting
// it into an HTML template.
const styles = renderSheets(styles);

CSS Processing

solid-styled uses LightningCSS to preprocess CSS templates as well as apply CSS scoping and transformations. By default, CSS Nesting and Custom Media Queries are enabled by default.

Limitations

  • Scoping css can only be called directly on components. This is so that the Babel plugin can find and transform the JSX of the component. Global css (i.e. :global or @global) can be used inside other functions i.e. hooks, utilities.
  • Dynamic styles are only limited to CSS properties.

Sponsors

Sponsors

License

MIT © lxsmnsyc

More Repositories

1

terracotta

Headless UI for SolidJS
TypeScript
660
star
2

seroval

Stringify JS values
TypeScript
476
star
3

forgetti

Solve your hook spaghetti (with more spaghetti). Inspired by React Forget.
TypeScript
347
star
4

solid-labels

Simple, reactive labels for SolidJS
TypeScript
241
star
5

solid-use

A collection of SolidJS utilities
TypeScript
186
star
6

awesome-islands

All about Islands Architecture and Partial Hydration
167
star
7

caldaria

An SSR framework for SolidJS
TypeScript
130
star
8

solid-tiptap

SolidJS bindings for tiptap
TypeScript
107
star
9

solid-floating-ui

SolidJS bindings for Floating UI
TypeScript
82
star
10

solid-marked

MDX Compiler for SolidJS
TypeScript
64
star
11

thaler

Write server-side functions in an isomorphic way
TypeScript
64
star
12

tailwind-layouts

Collection of Tailwind Layouts
TypeScript
63
star
13

solid-pebble

State management library for SolidJS
TypeScript
53
star
14

waystone

MPAs that feels like SPAs
TypeScript
49
star
15

solid-mason

Masonry layout for SolidJS
TypeScript
40
star
16

solid-auto-animate

SolidJS bindings for FormKit's AutoAnimate
TypeScript
37
star
17

solid-tippy

SolidJS bindings for Tippy.js
TypeScript
34
star
18

solid-popper

SolidJS bindings for Popper.js
TypeScript
33
star
19

solid-cache

Resource caching in SolidJS
TypeScript
33
star
20

dismantle

Universal semi-automatic code-splitter
TypeScript
33
star
21

stellis

a no-VDOM JSX framework for SSR.
TypeScript
32
star
22

blurhash-as

Blurhash implementation in AssemblyScript
TypeScript
31
star
23

compostate

Composable and reactive state management library
TypeScript
30
star
24

silmaril

Compile-time reactivity for JS
TypeScript
30
star
25

solid-emoji-picker

Unstyled emoji picker for SolidJS
TypeScript
29
star
26

re-use

⚛️ 🎣 A collection of hooks for ReasonReact
Reason
27
star
27

luact

Build reactive user interfaces on Lua
Lua
25
star
28

use-server-directive

Universal "use server" functions
TypeScript
25
star
29

pridepack

CLI for building TypeScript packages
TypeScript
25
star
30

solid-sfc

Experimental SFC compiler for SolidJS
TypeScript
24
star
31

scoped-model

Contextual state management for React, ReasonReact and Preact
TypeScript
23
star
32

solid-tiny-router

Tiny routing library for SolidJS
TypeScript
23
star
33

solid-error-overlay

Unstyled, dev error overlays for SolidJS
TypeScript
22
star
34

solid-uppy

SolidJS bindings for Uppy
TypeScript
22
star
35

venatu

Dead-simple Vite SSR library
TypeScript
21
star
36

isolid

Solid components in isolation
TypeScript
17
star
37

TimSort

TimSort implementation in JS/ES.
JavaScript
16
star
38

re-view

Renderer-agnostic reactive user interfaces in ReasonML
Reason
14
star
39

astro-solid-spa

SPA Template using Astro and SolidJS
TypeScript
14
star
40

tailwind-converter

Convert TailwindCSS classes to a CSS sheet
TypeScript
13
star
41

solid-optimizer

Experimental compile-time optimizer for SolidJS
TypeScript
10
star
42

poneglyph

a React SSR framework, powered by ESBuild
TypeScript
10
star
43

solidtude

TypeScript
9
star
44

react-provider

Flutter's Provider pattern in React
TypeScript
9
star
45

solid-sfc-styled-labels-starter

A SolidJS starter template with solid-labels, solid-sfc and solid-styled
TypeScript
9
star
46

re-scoped-model

Scoped Model pattern in ReasonReact (but with Hooks!)
JavaScript
8
star
47

use-dispose

Component-based disposal for React
TypeScript
8
star
48

valtio-element

Create reactive, declarative custom elements with valtio
TypeScript
8
star
49

swr-store

Reactive, data-fetching stores with stale-while-revalidate strategy
TypeScript
7
star
50

solid-ably

SolidJS bindings for Ably
TypeScript
7
star
51

graph-state

Digraph-based state management library for JavaScript
TypeScript
7
star
52

eslint-config-lxsmnsyc

My personal ESLint config
JavaScript
7
star
53

lxsmnsyc.github.io

CSS
7
star
54

graph-state-devtools

Chrome DevTool extension for graph-state
TypeScript
6
star
55

base16-ts

Base16 Scheme collection in TypeScript
TypeScript
6
star
56

baybayin

A simple in-browser code editor
TypeScript
6
star
57

ecmason

JSON parse/stringify for modern ES objects
TypeScript
6
star
58

laze

Lazily render components
TypeScript
6
star
59

typescript-graphql-parser

Typescript type for parsing GraphQL type strings
5
star
60

solid-parcel-template

SolidJS + solid-refresh + Parcel template
TypeScript
5
star
61

store-adapter

Connect stores and mutable sources to React and Preact the correct way
TypeScript
5
star
62

the-raven-demo

TypeScript
5
star
63

fine-grained-reactivity-benchmarks

Benchmark tool for selected fine-grained reactivity libraries
TypeScript
5
star
64

react-byblos

PDF Viewer component for React
TypeScript
5
star
65

solid-giphy

SolidJS bindings for GIPHY Web SDK
TypeScript
5
star
66

vite-solid-ssr

TypeScript
5
star
67

use-container-query

React hook for element-based media queries
TypeScript
5
star
68

Brain

Brain is a Lua library for building multilayer perceptrons or neural networks (or just multilayer perceptrons).
Lua
5
star
69

rescript-react-hooks

A collection of React hooks for ReScript
ReScript
4
star
70

import-scheduler

Dynamic import scheduler
TypeScript
4
star
71

ThreadPoolLite

A library for creating Web Worker pools.
JavaScript
4
star
72

my-website

My personal website
TypeScript
4
star
73

awesome-polyglot

😎 A curated list of libraries/tools that compiles a given language to other languages.
4
star
74

ecmason-view

Inspector for modern ES objects
TypeScript
4
star
75

LiteObservable

Cold Observables for JS in a lightweight fashion
TypeScript
4
star
76

react-suspense-cache

React library for creating Suspense-ful cached resources
TypeScript
4
star
77

thumbgen

Thumbnail generator app made with SolidJS
TypeScript
4
star
78

seroval-playground

TypeScript
4
star
79

RxLua

Reactive Extensions for Lua
Lua
4
star
80

unpleasant.js

💫 Noise functions for JS
JavaScript
4
star
81

esbuild-plugins

My collection of esbuild plugins
TypeScript
3
star
82

react-use-websockets

React Hooks + WebSockets API
TypeScript
3
star
83

riptide

An experimental state management library. Write Observables like you write React Hooks
TypeScript
3
star
84

Lua-Wc3-Module

module system for Lua Warcraft 3
Lua
3
star
85

react-external-subject

Wrap mutable sources into React-safe mutable sources
TypeScript
3
star
86

Math

Collection of my personal mathematics libraries for Lua.
Lua
3
star
87

react-fallback

Easy fallback handling in React
TypeScript
3
star
88

Px

Extension functions for JS Promise
JavaScript
3
star
89

react-hooks

My home-made React Hooks
TypeScript
3
star
90

LuaMeta

A collection of metaprogramming examples for Lua
Lua
3
star
91

dendro

simple, reactive, composable state management library. Inspired by Recoil
TypeScript
3
star
92

relays.js

A library for controlling asynchronous flow of execution
JavaScript
3
star
93

indigo-player-2

A rewrite for the https://github.com/matvp91/indigo-player
TypeScript
3
star
94

astro-solid-spa-nested

TypeScript
2
star
95

preact-suspense-poc

TypeScript
2
star
96

re-matrix

Matrix and Vector operations for ReasonML
Reason
2
star
97

react-async-fc

Write asynchronous functional components in React
TypeScript
2
star
98

discord-bot

TypeScript
2
star
99

regrets.js

⏰ Asynchronous control structures in JS
JavaScript
2
star
100

forgetti-playground

TypeScript
2
star