Giter VIP home page Giter VIP logo

postcss-local-scope's Introduction

Build Status npm

PostCSS Local Scope

PostCSS plugin to transform global selectors into the local scope format of Webpack's css-loader.

WARNING: This project depends on css-loader's local scope feature, which is marked as experimental and is very likely to change in the future.

Why?

Everyone agrees that dumping JavaScript in the global scope is a terrible idea. Why is CSS any different?

Imagine if we could import the CSS that a component needs without leaking selectors into the global scope. We wouldn't need naming conventions like BEM to avoid naming collisions, and we could prevent accidental coupling between components by ensuring our CSS follows the same scoping rules as any JavaScript module.

Webpack allows local scope in CSS with css-loader, but it's opt-in via a special .local[identifier] syntax.

This plugin transforms standard class selectors into local identifiers so that local scope is the default and global styles are the exception, just like any sane module system.

Usage example

Local identifiers use standard class syntax:

.foo { /* ... */ }

.foo .bar { /* ... */ }

Any global selectors need to be explicitly prefixed:

:global(.global .selector) { /* ... */ }

:global .another .global .selector { /* ... */ }

Local and global selectors can also be used simultaneously:

.foo :global .global { /* ... */ }

.foo :global(.global) .bar { /* ... */ }

These selectors are then transformed into a format that css-loader understands. To use these scoped classes, Webpack now allows us to import them like any other module.

For example, when using React:

import styles from './MyComponent.css';

import React from 'react';

export default class MyComponent extends React.Component {
  render() {
    return (
      <div className={styles.foo}>
        <div className={styles.bar}>
          Local scope!
        </div>
      </div>
    );
  }
};

In this case, styles is an object that maps identifiers to classes.

Classes are dynamically generated at build time by css-loader, so components are unable to depend on classes that they haven't explicitly imported.

Show me a working example

Okay.

Transformation examples

.foo { ... } /* => */ .local[foo] { ... }

.foo .bar { ... } /* => */ .local[foo] .local[bar] { ... }

/* Shorthand global selector */

:global .foo .bar { ... } /* => */ .foo .bar { ... }

.foo :global .bar { ... } /* => */ .local[foo] .bar { ... }

/* Targeted global selector */

:global(.foo) .bar { ... } /* => */ .foo .local[bar] { ... }

.foo:global(.bar) { ... } /* => */ .local[foo].bar { ... }

.foo :global(.bar) .baz { ... } /* => */ .local[foo] .bar .local[baz] { ... }

.foo:global(.bar) .baz { ... } /* => */ .local[foo].bar .local[baz] { ... }

postcss-local-scope's People

Contributors

markdalgleish avatar

Watchers

 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.