npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-free-style

v11.1.0

Published

Make React components easier and more maintainable by using JavaScript with Free Style

Downloads

378

Readme

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