Socket
Book a DemoInstallSign in
Socket

preact-body-class

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

preact-body-class

A declarative, nested way to manipulate your body's className

1.1.0
latest
Source
npmnpm
Version published
Weekly downloads
2
Maintainers
2
Weekly downloads
 
Created
Source

preact-body-class

NPM version Downloads Dependency status

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

Built with Preact Side Effect.

Install

npm install --save react-body-class

Dependencies: Preact >= 8.2.0

What it looks like

import BodyClass from 'react-body-class';

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

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

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

Note: Only supports a single child as props.

Server Usage

When using server-side, use BodyClass.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 BodyClass.rewind() you'll get a memory leak.

Keywords

preact

FAQs

Package last updated on 11 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.