# react-critical-css

> React Critical CSS

Latest version **2.1.0** (published 2018-03-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-critical-css
pnpm add react-critical-css
yarn add react-critical-css
bun add react-critical-css
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2018-03-19 |
| First published | 2017-08-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 18 |
| Author | Sergei Zelinsky |
| Maintainers | sergei-zelinsky |
| Keywords | react, critical, css |

## Links

- npm: https://www.npmjs.com/package/react-critical-css
- Repository: https://github.com/sergei-zelinsky/react-critical-css
- Homepage: https://github.com/sergei-zelinsky/react-critical-css#readme
- Issues: https://github.com/sergei-zelinsky/react-critical-css/issues
- npm.io page: https://npm.io/package/react-critical-css

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2018-03-19
- 2.0.0 — 2017-08-19
- 1.0.1 — 2017-08-18
- 1.0.0 — 2017-08-17
- 0.0.1 — 2017-08-17

## README

# react-critical-css

Extracts your critical CSS.

[![Build Status](https://travis-ci.org/sergei-zelinsky/react-critical-css.svg?branch=master)](https://travis-ci.org/sergei-zelinsky/react-critical-css)
[![npm](https://img.shields.io/npm/v/react-critical-css.svg)](https://www.npmjs.com/package/react-critical-css)
[![npm](https://img.shields.io/npm/l/react-critical-css.svg)]()
[![npm](https://img.shields.io/npm/dt/react-critical-css.svg)]()
[![GitHub stars](https://img.shields.io/github/stars/sergei-zelinsky/react-critical-css.svg?style=social&label=Stars)]()


## Installation

```
npm install react-critical-css
```

or

```
yarn add react-critical-css
```

## Usage

```js
// ...
import {renderToString} from 'react-dom/server';
import {CriticalCSSProvider, StyleRegistry} from 'react-critical-css'; // <-

// ...

const styleRegistry = new StyleRegistry(); // create new style registry

const appString = renderToString(
    <CriticalCSSProvider registry={styleRegistry}> // <- wrap your App component with CriticalCSSProvider and pass styleRegistry to it
      <App/>
    </CriticalCSSProvider>
);

const criticalCSS = styleRegistry.getCriticalCSS(); // <- extract collected critical CSS

```

#### App component example:

```js
import React, {Component} from 'react';
import {withStyles} from 'react-critical-css'; // import 'withStyles'
import s from './index.css'; // import your styles, `s` should contain a string with style rules (see example webpack config below)

class App extends Component {
  // ...
  
  render(){
    return (
      <div>App component example</div>
    );
  }
}

// wrap App component with 'withStyles'

export default withStyles(s)(App);

```

### Example Webpack configuration for CSS files

```js
// ...

module.exports = {
// ...
  
  module: {
  // ...
    
    rules: [
    // ...
    
    {
        test: /\.css$/,
        loader: 'css-loader',
        // ...
      }
    ]
  }
};
```

## API

### StyleRegistry [Class]

#### Public API:
- **registerStyles** - adds styles into internal registry
- **getCriticalCSS** - returns critical stylesheets which has been registered through `registerStyles` previously 

#### Usage
```js
import {StyleRegistry} from 'react-critical-css';

// ...

const styleRegistry = new StyleRegistry();

// ..

styleRegistry.registerStyles('body {color: red}');  // <- register stylesheets

// ...

const criticalCSS = styleRegistry.getCriticalCSS() // <- retrieve critical CSS

```

### CriticalCSSProvider [React Component]

Passes context with registerStyles function through the React tree. 

#### Props
- **registry** - instance of StyleRegistry or other registry which supports the same API

### withStyles [Decorator]

Returns HOC which registers passed stylesheets.

#### Arguments of `withStyles`

- **styles** - string with CSS

#### Arguments of `withStyles(styles)`

- **Component** - React Component 

#### Usage

```js
// ...
import {withStyles} from 'react-critical-css';
import s from 'path/to/css/file';

// ...

class MyComponent extends Component {
  // ...
}

export default withStyles(s)(MyComponent);
```

or using `decorators`

```js
// ...
import {withStyles} from 'react-critical-css';
import s from 'path/to/css/file';

// ...

@withStyles(s)
class MyComponent extends Component {
  // ...
}

export default MyComponent;
```

---
_Source: https://npm.io/package/react-critical-css · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
