npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-bem-notation

v0.1.0

Published

Module for create reusable components with modifiers like in BEM methodology.

Downloads

3

Readme

React BEM Notation

ReactBemNotation is a small hook that can possible use mods and mix props for generate full classList string in BEM notation. Also you can use additionalMods and additionalMix for change result of generating full className string in render function.

* For any other situations you can use additional className param.

:octocat: Source code on github

Install

:package: To download this package from npm use following command in a terminal

npm install react-bem-notation

How to use

First, that you need is import hook

// top of component file
import { useBemNotation } from 'react-bem-notation';

// in body of component
const {getClassName, getClassNameElem} = useBemNotation(YourComponent, {className: "other classes that you need", mods, mix}, 'block-name-that-you-want-to-display-in-dom-tree');

// ...
return (
    <div className={getClassName({additionalMods, additionalMix})}>
        <div className={getClassNameElem(elem, additionalMods, additionalMix)}>elem</div>
    </div>
);

Important info about better perfomance.

All block modifiers that are changed only by the component itself (from state) and will not change from the outside (from props) should be passed from the component state

All block modifiers that are modified by the parent component (from props) and will not be modified internally (from state) must be passed from props

Example

This is a simly application that used modifiers and mixes, on a custom component ModificableButton. During use application:

  • if you click on the first button they’re changed mods inside component (they will set modificable-button_pressed class after mouseDown event and remove it after mouseUp event)
  • if you click on the second button then first button will changes their mods from App state (toggle class modificable-button_disabled)
/**
 * ModificableButton.jsx
 * button component with primitive props, some state and mixes
 */
import React, { useState, useMemo } from "react";
import useBemNotation from "react-bem-notation";

const ModificableButton = ({
    children,
    disabled = false,
    mods = {}, 
    mix = {}, 
    icon = undefined,
    onClick =  undefined,
    onMouseDown = undefined,
    onMouseUp = undefined,
}) => {
    const [pressed, setPressed] = useState(false);
    const {getClassName, getClassNameElem} = useBemNotation(ModificableButton, {className: "for-demo", mods, mix}, 'modificable-button');

    const iconComponent = useMemo(() => {
        if (typeof icon !== 'string') return null;
        const iconMods = {};
        iconMods[icon] = true;
        return (
            <span className={
                getClassNameElem({elem: 'icon', additionalMods: iconMods})
            }></span>
        );
    }, [icon]);

    const handleClick = (e) => {
        e.preventDefault();
        onClick?.(e);
    }

    const handleMouseEvents = (e) => {
        if (e.type === "mousedown") {
            setPressed(true);
            onMouseDown?.(e);
        }
        if (e.type === "mouseup") {
            setPressed(false);
            onMouseUp?.(e);
        }
    }

    return (
        <button className={
            getClassName({
                additionalMods: {"pressed": pressed, "disabled": disabled}, 
                additionalMix: {"mix-bool": true, "mix-string": "example", "mix-number": 0}
            })
        } onClick={handleClick} onMouseDown={handleMouseEvents} onMouseUp={handleMouseEvents}>
            {iconComponent}
            {children}
        </button>
    );
};

// Different method for initialize displayName
// ModificableButton.displayName = 'ModificableButton';
export default ModificableButton;

Then, we can use ModificableButton in App component.

/**
 * App.js
 */
import React, { useState } from "react";
import ModificableButton from "./components/ModificableButton/ModificableButton.jsx"

const App = () => {
	const [disabled, setDisabled] = useState(false);
	
	const toggleDisable = (e) => {
		setDisabled((prevState) => {
			return !prevState;
		})
	}

    return (
		<React.Fragment>
			<h1>react-modificable</h1>
			<div className="panel">
				<ModificableButton mods={{"theme": "default"}} mix={{"mix-button-demo-bool": true}} disabled={disabled} icon="star">Click me!</ModificableButton>
				<ModificableButton onClick={toggleDisable} mods={{"theme": "default"}}>{disabled ? 'Enable' : 'Disable'}</ModificableButton>
			</div>
		</React.Fragment>
	);
};

export default App;

After load application we will see two ModificableButton elements in DOM tree with classes modificable-button in BEM notation.

<button class="ModificableButton for-demo ModificableButton_theme_default mix-button-demo-bool mix-bool mix-string_example mix-number_0">
    <span class="modificable-button__icon modificable-button__icon_star"></span>
    Click me!
</button>
<button class="ModificableButton for-demo ModificableButton_theme_default mix-bool mix-string_example mix-number_0">Disable</button>