Skip to content

Declarative, nested, stateful, isomorphic body.className for React

License

Notifications You must be signed in to change notification settings

iest/react-body-classname

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-body-classname

NPM version Downloads Dependency status

Provides a declarative way to specify document.body.className in your react app. Supports server-side usage too.

Built with React Side Effect.


Install

yarn add react-body-classname

Dependencies: React >= 0.13.0

What it looks like

import BodyClassName from 'react-body-classname';

const Basic = () => (
  <BodyClassName className="helloworld">
    <h1>You ate a whole wheel of cheese?</h1>
  </BodyClassName>
);
// -> document.body.className === "helloworld"

const Nested = () => (
  <BodyClassName className="outside">
    <div>
      <BodyClassName className="inside">
        <p>I‘m not even mad</p>
      </BodyClassName>
    </div>
  </BodyClassName>
);
// -> document.body.className === "outside inside"

const GoCrazy = () => (
  <BodyClassName className={Array(8).join(''/0) + ' batman!'}>
    <h1>I'm impressed</h1>
  </BodyClassName>
);
// -> document.body.className === "NaNNaNNaNNaNNaNNaNNaN batman!"

Note: Only supports a single child as props.

Server Usage

When using server-side, use BodyClassName.rewind() after rendering components to string to retrieve the combined class name. Then chuck that into your HTML template.

Important: This component keeps track of mounted instances, so if you don't call BodyClassName.rewind() you'll get a memory leak.

About

Declarative, nested, stateful, isomorphic body.className for React

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%