rtivital / hash-css-selector

A function to hash CSS selectors, can be used with postcss-modules to generate short static unique class names

Home Page:https://www.npmjs.com/package/hash-css-selector

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hash-css-selector

A function to hash CSS selectors, can be used with postcss-modules to generate short static unique class names.

Usage

hashCSSSelector returns a 8 character long hash of the given string. The function is deterministic, so the same input will always result in the same output.

import { hashCSSSelector } from 'hash-css-selector';

hashCSSSelector('test-class'); // -> 'm-a530cc7d'
hashCSSSelector('another-test-class'); // -> 'm-c43ac187'

// Default prefix is 'm'
// Customize prefix by passing a second argument
hashCSSSelector('test-class', 'my-prefix'); // -> 'my-prefix-a530cc7d'

Usage with postcss-modules

With default m prefix:

import { generateScopedName } from 'hash-css-selector';

postcss([
  require('postcss-modules')({
    generateScopedName: generateScopedName,
  }),
]);

With custom prefix:

import { createGenerateScopedName } from 'hash-css-selector';

postcss([
  require('postcss-modules')({
    generateScopedName: createGenerateScopedName('custom-prefix'),
  }),
]);

License

MIT

About

A function to hash CSS selectors, can be used with postcss-modules to generate short static unique class names

https://www.npmjs.com/package/hash-css-selector

License:MIT License


Languages

Language:TypeScript 84.7%Language:JavaScript 15.3%