• Stars
    star
    137
  • Rank 265,172 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

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

React Free Style

NPM version NPM downloads Build status Test coverage Bundle size

React Free Style combines Free Style with React.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 be doing 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";

const Button = styled("button", {
  backgroundColor: "red",
});

const App = () => {
  return <Button css={{ color: "blue" }}>Hello world!</Button>;
};

JSX

/** @jsx jsx */

import { jsx } from "react-free-style";

const App = () => {
  return (
    <button css={{ color: "blue", backgroundColor: "red" }}>
      Hello world!
    </button>
  );
};

Imperative

import { css, useCss } from "react-free-style";

// Creates "cached CSS":
const style = css({ color: "red" });
// But you can also write `const style = { color: "red" }`.

const Button = () => {
  const className = useCss(style);

  return <button className={className}>Hello world!</button>;
};

This is how the styled and jsx work! Knowing how it works can help you when you need to extract the class name for integrating with an existing UI library using className.

Recipes

Valid Styles

Every CSS method accepts:

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

Composition

Components created using styled expose "cached CSS" on the style property.

const LargeButton = 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";

const style = css((Style) => {
  const animationName = Style.registerStyle({
    $global: true,
    "@keyframes &": styles,
  });

  return { animationName };
});

Themes

CSS Variables

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

// Register this CSS wherever you want the theme to apply, e.g. `:root`.
const theme = {
  "--color": "red",
};

const Button = styled("button", {
  color: "var(--color)",
});

// Later on you can change the theme.
const style = css({
  "--color": "blue",
});

Context

Use React.Context to define a theme and custom components with css props.

const ThemeContext = React.createContext({
  color: "red",
});

const Button = () => {
  const theme = React.useContext(ThemeContext);

  return <button css={{ 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();
const renderer = new StyleSheetRenderer();

React.render(
  <Context.Provider value={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();
const renderer = new MemoryRenderer();

const content = ReactDOM.renderToString(
  <Context.Provider value={renderer}>
    <App />
  </Context.Provider>,
  document.body
);

const html = `
<!doctype html>
<html>
  <head>
    ${renderer.toString()}
  </head>
  <body>
    <div id="content">
      ${content}
    </div>
  </body>
</html>
`;

License

MIT license

More Repositories

1

code-problems

Common code and interview problems solved in multiple languages
JavaScript
3,807
star
2

change-case

Convert strings between camelCase, PascalCase, Capital Case, snake_case and more
TypeScript
2,050
star
3

free-style

Make CSS easier and more maintainable by using JavaScript
TypeScript
698
star
4

atom-dash

Dash documentation integration with Atom
CoffeeScript
394
star
5

tslint-config-standard

A TSLint config for JavaScript Standard Style
JavaScript
358
star
6

sql-template-tag

ES2015 tagged template string for preparing SQL statements, works with `pg`, `mysql`, and `sqlite`
TypeScript
343
star
7

co-mocha

Enable support for generators in Mocha tests
JavaScript
216
star
8

javascript-stringify

Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`
TypeScript
133
star
9

compose-middleware

Compose an array of middleware into a single function for use in Express, Connect, router, etc
TypeScript
91
star
10

array-flatten

Flatten a multi-dimensional array in JavaScript.
JavaScript
87
star
11

writing

New posts will be on Substack
TypeScript
74
star
12

make-error-cause

Make your own nested errors
TypeScript
66
star
13

node-language-detect

Detect the programming language of any file in JavaScript
JavaScript
56
star
14

dombars

*DEPRECATED* DOM-based templating library with data-binding and built on Handlebars
JavaScript
51
star
15

language-map

JSON version of the programming language map used in Linguist
JavaScript
50
star
16

node-htmlmetaparser

A `htmlparser2` handler for parsing rich metadata from HTML. Includes HTML metadata, JSON-LD, RDFa, microdata, OEmbed, Twitter cards and AppLinks.
HTML
49
star
17

async-middleware

Wrap an asynchronous middleware (or handler) function for Express, Connect, router, etc.
TypeScript
49
star
18

metalsmith-pagination

A Metalsmith plugin for paginating arrays and collections
JavaScript
34
star
19

readable

Make articles readable again
JavaScript
33
star
20

keyboard-manager

Small keyboard shortcut management for DOM-based applications
TypeScript
30
star
21

stylus-type-utils

Stylus typography functions and type conversion
CSS
29
star
22

universal-base64

Small universal base64 functions for node.js and browsers
TypeScript
28
star
23

simulate-event

A library for triggering DOM events.
JavaScript
24
star
24

highlighter

Add syntax highlighting to all files, with built-in syntax highlighting of diffs and patches
JavaScript
21
star
25

node-numbered

Stringify any number into words, and parse number strings back to a number
JavaScript
20
star
26

deque

JavaScript implementation of a double-ended queue
TypeScript
18
star
27

node-immigration

Simple, no-frills migration utility
TypeScript
17
star
28

arity

Set the length of a function in JavaScript
JavaScript
17
star
29

iterative

Functions for working with iterators in JavaScript and TypeScript
TypeScript
17
star
30

node-module-template

My standard node module template
JavaScript
17
star
31

universal-base64url

Small universal base64url functions for node.js and browsers
TypeScript
16
star
32

code-challenge

Command line interface for running code challenges
JavaScript
16
star
33

is-generator

Check whether a given value is a generator function
JavaScript
15
star
34

kroute

Modular Koa router middleware with Express-style routes and middleware mounting
JavaScript
15
star
35

js-functools

Utilities for working with functions in JavaScript, with TypeScript
TypeScript
14
star
36

dotfiles

My OS X config
Ruby
13
star
37

decorator-debug

Simple ES7 decorator for debugging classes and methods
TypeScript
13
star
38

decorator-cache-getter

Simple decorator for caching getters on first access
TypeScript
13
star
39

just-css-properties

An array of all known browser CSS properties
JavaScript
12
star
40

typescript-simple-loader

Feature complete TypeScript loader for webpack
TypeScript
12
star
41

nary

Force a functions arity in JavaScript
TypeScript
12
star
42

metalsmith-snippet

A Metalsmith plugin for extracting snippets from files.
JavaScript
11
star
43

sql-type

Type-safe SQL builder using ES2015 template tags
TypeScript
11
star
44

react-location

Light-weight and universal react routing
TypeScript
11
star
45

atom-alignment

Align selections for Atom `⌘βŒ₯^ ]`
CoffeeScript
10
star
46

react-route

React Location with Path-to-RegExp
TypeScript
9
star
47

promise-finally

Simple wrapper to run promise "finally" logic
TypeScript
7
star
48

variadic

A JavaScript utility for creating variadic functions
JavaScript
7
star
49

retest

Request driven library for testing node.js HTTP servers
JavaScript
7
star
50

node-dombarsify

DOMBars pre-compilation plugin for Browserify.
JavaScript
6
star
51

partial

A functional utility for doing partial application in JavaScript
JavaScript
6
star
52

decorator-when

Simple ES7 decorator for short-cutting method execution
TypeScript
6
star
53

style-helper

Small helpers for working with CSS-in-JS
TypeScript
6
star
54

node-ranged

The missing JavaScript range function
JavaScript
6
star
55

decorator-memoize-one

Simple decorator for caching the result of a function based on the most recent arguments
TypeScript
6
star
56

metalsmith-collections-paginate

[DEPRECATED] Use https://github.com/blakeembrey/metalsmith-pagination
JavaScript
5
star
57

setvalue

Set a nested value from an object by path
TypeScript
5
star
58

dedent-tag

ES2015 tagged template string for removing indent from strings
TypeScript
5
star
59

a-brief-history-of-types-with-typescript

Talk for SF TS Meetup (2018-11-08)
5
star
60

base64url

Base64 URL for node.js buffers
TypeScript
5
star
61

server-address

Listen to a randomly available port and resolve urls relative to the server address
TypeScript
5
star
62

invoke

Functional utility for invoking the same method on every call in JavaScript
JavaScript
5
star
63

getvalue

Get a nested value from an object by path
TypeScript
5
star
64

js-template

Fast and simple string templates
TypeScript
5
star
65

back-row

A media server for watching movies and shows in a browser, streamed directly from a server
TypeScript
5
star
66

code-challenge-euler

Run Project Euler challenges in the command line
JavaScript
4
star
67

co-retest

Simple wrapper for the retest library for co-like interfaces (node.js generators)
JavaScript
4
star
68

node-exiftool2

Wrapper for efficiently working with `exiftool`
TypeScript
3
star
69

token-hash

Generate base64url encoding from the left-most half of the hash of a token.
TypeScript
3
star
70

map-pointer

Map from one object to another using JSON pointers
TypeScript
3
star
71

node-language-command

Look up the command for executing a program in any language
JavaScript
3
star
72

each

Iterate over an object, array or string in JavaScript
JavaScript
3
star
73

authwith

Re-usable authentication library
TypeScript
3
star
74

blakeembrey.me

Personal placeholder page and resume
CSS
3
star
75

node-language-detect-exec

Automatically execute a file in a known programming language using node
JavaScript
3
star
76

filter

Filter an object, array or string in JavaScript
JavaScript
2
star
77

typescript-talk

Presentation on TypeScript
HTML
2
star
78

node-alignment

Naive alignment of a block with variable assignments in JavaScript
TypeScript
2
star
79

node-immigration-rethinkdb

RethinkDB adapter for immigration
TypeScript
2
star
80

hydra-buttons

Consistent cross-browser button implementation
JavaScript
2
star
81

node-language-exec

Execute a file in any programming language using node
JavaScript
2
star
82

hydra-input-groups

Simple groups for inputs and buttons
JavaScript
2
star
83

hydra-form

Simple form style resets and beautification
JavaScript
2
star
84

node-trimmer

Quick module to trim any characters from either side of a string.
JavaScript
2
star
85

exif-date

Parse an EXIF date string into a `Date` object
TypeScript
1
star
86

hydra-code

Simple CSS reset for code blocks.
JavaScript
1
star
87

envify-config

Browserify transform for replacing process.env variables with the config module settings.
JavaScript
1
star
88

hasvalue

Check if a value exists at a nested property path
TypeScript
1
star
89

node-language-spawn

Spawn a file process in any programming language using node
JavaScript
1
star
90

code-challenge-n-queens

Solve various n-queens problems in the terminal
JavaScript
1
star
91

hydra-typography

Simple default typography and font style decisions
JavaScript
1
star
92

node-string-token

Generate a random token of length and characters
TypeScript
1
star
93

get-headers

Retrieve all HTTP(s) headers as an object with original casing
TypeScript
1
star
94

map

Map over an object, array or string in JavaScript
JavaScript
1
star
95

node-bit-string-mask

Mask a string using bits from an input number
TypeScript
1
star
96

node-language-detect-spawn

Automatically spawns a file in a known programming language using node
JavaScript
1
star
97

hydra-grid

Powerful fluid grid generator for Stylus and CSS
JavaScript
1
star
98

popsicle-proxy-agent

Enable proxy support for Popsicle (for node)
TypeScript
1
star