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

Make React components easier and more maintainable by using inline style objects

License

NotificationsYou must be signed in to change notification settings

blakeembrey/react-free-style

Repository files navigation

NPM versionNPM downloadsBuild statusTest coverageBundle size

React Free Style combinesFree Style withReact.js by managing the style of React components dynamically. Works with server-side rendering, where only styles of rendered components will print.

Why?

Check out why you should bedoing CSS in JS. This module exposes the API directly to React.js.

Even more improvements with React Free Style

  • Modular React.js components
  • Style debugging in development mode
  • Fast renders with automatic style for rendered React components
  • Supports universal/isomorphic applications

Installation

npm install react-free-style --save

Usage

Styled

import{styled}from"react-free-style";constButton=styled("button",{backgroundColor:"red",});constApp=()=>{return<Buttoncss={{color:"blue"}}>Hello world!</Button>;};

JSX

/**@jsx jsx */import{jsx}from"react-free-style";constApp=()=>{return(<buttoncss={{color:"blue",backgroundColor:"red"}}>      Hello world!</button>);};

Imperative

import{css,useCss}from"react-free-style";// Creates "cached CSS":conststyle=css({color:"red"});// But you can also write `const style = { color: "red" }`.constButton=()=>{constclassName=useCss(style);return<buttonclassName={className}>Hello world!</button>;};

This is how thestyled andjsx work! Knowing how it works can help you when you need to extract the class name for integrating with an existing UI library usingclassName.

Recipes

Valid Styles

Every CSS method accepts:

  • CSS-in-JS object
  • String, i.e. a class name
  • Cached CSS, created using thecss(...) method
  • Computed CSS, a function which acceptsStyle and returns a valid style
  • Array of the above

Composition

Components created usingstyled expose "cached CSS" on thestyle property.

constLargeButton=styled("button",[{fontSize:16,},Button.style,{marginBottom:8,},]);

Animations

A "computed CSS" function can be used to register and use@keyframes.

import{css}from"react-free-style";conststyle=css((Style)=>{constanimationName=Style.registerStyle({$global:true,"@keyframes &":styles,});return{ animationName};});

Themes

CSS Variables

The most effective CSS themes I've seen useCSS variables to dynamically change styles.

// Register this CSS wherever you want the theme to apply, e.g. `:root`.consttheme={"--color":"red",};constButton=styled("button",{color:"var(--color)",});// Later on you can change the theme.conststyle=css({"--color":"blue",});

Context

UseReact.Context to define a theme and custom components withcss props.

constThemeContext=React.createContext({color:"red",});constButton=()=>{consttheme=React.useContext(ThemeContext);return<buttoncss={{color:theme.color}}>Hello world!</button>;};

Rendering

By default, CSS output is discarded (a "no op" useful for testing) because you may have different output requirements depending on the environment.

Client-side Rendering

StyleSheetRenderer is an efficient CSS renderer for browsers.

import{StyleSheetRenderer,Context}from"react-free-style";// const renderer = new NoopRenderer();constrenderer=newStyleSheetRenderer();React.render(<Context.Providervalue={renderer}><App/></Context.Provider>,document.body);

Server-side Rendering

MemoryRenderer collects all styles in-memory for output at a later time.

import{MemoryRenderer,Context}from"react-free-style";// const renderer = new NoopRenderer();constrenderer=newMemoryRenderer();constcontent=ReactDOM.renderToString(<Context.Providervalue={renderer}><App/></Context.Provider>,document.body);consthtml=`<!doctype html><html>  <head>${renderer.toString()}  </head>  <body>    <div>${content}    </div>  </body></html>`;

License

MIT license

About

Make React components easier and more maintainable by using inline style objects

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp