Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Reactive stylesheets for SolidJS

License

NotificationsYou must be signed in to change notification settings

lxsmnsyc/solid-styled

Repository files navigation

Reactive stylesheets for SolidJS

NPMJavaScript Style Guide

Install

npm i solid-stylednpm i -D postcss
yarn add solid-styledyarn add -D postcss
pnpm add solid-styledpnpm 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 thecss tagged template for writing stylesheets.

import{css}from'solid-styled';functionTitle(){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';functionButton(){const[color,setColor]=createSignal('red');css`button {color:${color()};    }  `;return(<buttononClick={()=>{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';functionForeignTitle(){return<h1>This is not affected</h1>;}functionTitle(){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';functionFeed(props){css`div>:global(*+*) {margin-top:0.5rem;    }  `;return(<div><Foreach={props.articles}>{(item)=>(// This item is affected by the CSS of the Feed component.<FeedArticledata={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

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

css`* {color: red;  }`;<Dynamiccomponent={props.as}use:solid-styled>{props.children}</Dynamic>

which compiles into

useSolidStyled('xxxx','*[s\\:xxxx]{color:red}');<Dynamiccomponent={props.as}s:xxxxstyle={vars()}>{props.children}</Dynamic>

<style jsx>

Inspired bystyled-jsx.

Use<style jsx> instead ofcss for declaring styles in JSX expressions. Both<style jsx> andcss functions the same.

functionButton(){const[color,setColor]=createSignal('red');return(<><stylejsx>{`          button {            color:${color()};          }        `}</style><buttononClick={()=>{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 fromsolid-styled-jsx tosolid-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 thestyles prop of<StyleRegistry>. This array collects all of the "critical" (initial render) stylesheets, that which you can render as a string withrenderSheets.

import{renderSheets}from'solid-styled';conststyles=[];renderToString(()=>(<StyleRegistrystyles={styles}><App/></StyleRegistry>));// Render sheets// You can use the resulting sheet by inserting// it into an HTML template.conststyles=renderSheets(styles);

Alternatively, for automatic sheet rendering (assuming that you are server-rendering the entire document), you can do

renderToString(()=>(<StyleRegistryauto><App/></StyleRegistry>));

CSS Processing

solid-styled usesLightningCSS 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

  • Scopingcss can only be called directly on components. This is so that the Babel plugin can find and transform the JSX of the component. Globalcss (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


[8]ページ先頭

©2009-2025 Movatter.jp