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

classnamesplus-loader

v1.1.1

Published

Alternate version of classnames Webpack loader, with merging of styles and global vs non global css modules.

Downloads

4

Readme

classnamesplus-loader

npm version

This is a webpack loader that automatically bind css-modules to classnames.

This is a modified version of the original classnames-loader with changed behavior. I'm not sure my changes fit with the authors intent for classnames-loader as it does more than just classnames binding, so decided to fork and rename the project.

This module will bind exports to classnames, but also used with css-modules, will include the normal and module name so both are in the class list still (useful for automated tests where module based class names are not reliable)

This version also provides a mergeStyles method. This will take 2 classnames instances, and combine the classnames.

So if you have a base button stylesheet, and take that stylesheet and call .mergeStyles() with another stylesheet, the resulting stylesheet will have classes for both. See example below.

This is primarily for taking presentational components, and letting the user of the component pass in an additional style object to extend the base one with, enabling overrides.

Installation

npm install --save-dev classnamesplus-loader

API

mergeStyles

import styles from "./styles.css";
import styles1 from "../styles1.css";
import styles2 from "../styles2.css";

const newStyles = styles.mergeStyles(styles1, styles2);
//styles.myStyle = myStyle styles_myStyle
// newStyles.myStyle = myStyle styles_myStyle styles1_myStyle styles2_myStyle

importStyles

import styles from "./styles.css";
import styles1 from "../styles1.css";
import styles2 from "../styles2.css";

styles.importStyles(styles1, styles2);

// styles.myStyle = myStyle styles_myStyle styles1_myStyle styles2_myStyle

Usage

To enable this loader add classnamesplus-loader before style-loader in webpack config:

{
  test: /\.css$/,
  loader: 'classnamesplus-loader!style-loader!css-loader')
}

If you're using ExtractTextPlugin your webpack config should look like this:

{
  test: /\.css$/,
  loaders: ['classnamesplus-loader', ExtractTextPlugin.extract('style-loader', 'css-loader')])
}

Example usage in component:

import { Component } from 'react';
import baseButton from '../buttons.css';
import buttonStyles from './submit-button.css';
const cx = baseButton.mergeStyles(buttonStyles); // styles now have base button classes, with buttonStyles appended after it
// example: cx.base = base BaseButton_base SubmitButton_base 

export default class SubmitButton extends Component {
  render () {
    let text = this.props.store.submissionInProgress ? 'Processing...' : 'Submit';
    let className = cx({
      base: true,
      inProgress: this.props.store.submissionInProgress,
      error: this.props.store.errorOccurred,
      disabled: !this.props.form.valid,
    });
    return <button className={className}>{text}</button>;
  } 
}

You can also access the class names just as you would do that with css-modules:

import { Component } from 'react';
import styles from './submit-button.css';

export default class SubmitButton extends Component {
  render () {
    let text = this.props.store.submissionInProgress ? 'Processing...' : 'Submit';
    return <button className={styles.submitButton}>{text}</button>;
  } 
}

Sadly, babel-plugin-react-css-modules does not appear to be using the combined class names for styleName. It appears it is due to that plugin resolving to the module imported directly, and not using the actual variable in scope.

You will need to use className={cx('base', 'button')} syntax.

To be honest, forcing that plugin to get the combined classes was a goal of this extension, but didn't work out as I had hoped.

Thanks

@itsmepetrov for original classnames-loader @JedWatson for classnames module

License

MIT