Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

unique-classname.macro

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unique-classname.macro

Babel Plugin Macro to generate unique className

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-71.43%
Maintainers
1
Weekly downloads
 
Created
Source

unique-classname.macro

Build Status

This is a babel-macros to generate unique className for css-in-js.

If you are coming from CSS modules and want to convert a large code base to emotion, sometimes you might have some empty classNames added to the container div as state of the component.

For example. You might have React component like this

import classNames from 'classnames'
import styles from './styles.css'

const MyComponent = ({ isDisabled }) => (
  <div
    className={classNames(
      styles.container,
      isDisabled ? styles.isDisabled : false
    )}
  >
    <div className={styles.content}>Content</div>
  </div>
)

and your styles looks like this:

.container {
  background: white;
}

.content {
  padding: 10px;

  .disabled & {
    cursor: not-allowed;
    color: '#777';
  }
}

With unique-classname.macro, it will help you generate a hash from both filename and variable name.

Usage

With babel-macros and this package installed, you can use it like this.

import className from 'unique-classname.macro'
import { css } from 'emotion'

const disabledClass = className() // or const disabled = className('disabled')

const containerClass = css`
  background: white;
`

const contentClass = css`
  padding: 10px;

  .${disabled} & {
    cursor: not-allowed;
    color: '#777';
  }
`

const MyComponent = ({ isDisabled }) => (
  <div
    className={classNames(containerClass, isDisabled ? disabledClass : false)}
  >
    <div className={contentClass}>Content</div>
  </div>
)

The above snippet will be compiled in build time:

import className from 'unique-classname.macro'
import { css } from 'emotion'

const disabled = 'disabled-1u6x1nq'

const container = css`
  background: white;
`

const content = css`
  padding: 10px;

  .${disabled} & {
    cursor: not-allowed;
    color: '#777';
  }
`

Keywords

FAQs

Package last updated on 10 Oct 2018

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc