GithubHelp home page GithubHelp logo

css-modules-helpers's Introduction

CSS Modules Helpers

JS Helpers for configuring CSS Modules and selecting hashed DOM selectors.

Setup

Install PostCSS Modules

npm i --save-dev postcss-modules css-modules-helpers

Configure PostCSS Modules

require the createManifest module and pass it to the PostCSS Modules getJSON option.

/* postcss.config.js */

const modules = require('postcss-modules');
const createManifest = require('css-modules-helpers/createManifest');

module.exports = () => ({
  plugins: [
    modules({
      generateScopedName: '[name]__[local]___[hash:base64:5]',
      getJSON: createManifest,
    }),
  ],
});

Usage

Use these functions to retrieve hashed classnames and selectors from the CSS Modules output.

Note: Each of these assumes a classname manifest attached to window.cssModulesClassnames.

getComponentClassnames

getComponentClassnames will return all classnames associated with a given component. These values are not suitable for use in selecting elements from the DOM, but would be used to add or remove values from an element's classList

import { getComponentClassnames } from 'css-modules-helpers';

const classnames = getComponentClassnames('site-header');
// { menuButton: 'site-header__menu-button___2EGUQ', menu: 'site-header__menu___9gIXs' }

getComponentSelectors

getComponentSelectors will return all of a component's classnames formatted as DOM selectors. Use this when configuring child selectors for js-component-framework. To get only one selector from a component, see getSelectorFromComponent.

import { getComponentSelectors } from 'css-modules-helpers';

const selectors = getComponentSelectors('site-header');
// { menuButton: '.site-header__menu-button___2EGUQ', menu: '.site-header__menu___9gIXs' }

getElementFromComponent

getElementFromComponent will return an element or elements from another component. By default, the function will return a single element; pass true as a third parameter to return a NodeList.

import { getElementFromComponent } from 'css-modules-helpers';

const element = getElementFromComponent('menu-button', 'site-header');
// <button class="site-header__menu-button___2EGUQ"></button>

getSelectorFromComponent

getSelectorFromComponent will return a specific selector from a component. See getComponentSelectors to get all of a component's selectors.

import { getSelectorFromComponent } from 'css-modules-helpers';

const menuButton = getSelectorFromComponent('menu-button', 'site-header');
// '.site-header__menu-button___2EGUQ'

css-modules-helpers's People

Contributors

goodguyry avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.