QuickBase / babel-plugin-styled-components-css-namespace

A babel plugin to add css namespaces (and increase specificity) of styled-component classes.
MIT License
118 stars 32 forks source link

@quickbaseoss/babel-plugin-styled-components-css-namespace

Travis CI status and link to builds

Getting Started

  1. Add the plugin with yarn add @quickbaseoss/babel-plugin-styled-components-css-namespace or npm install @quickbaseoss/babel-plugin-styled-components-css-namespace
  2. Include the plugin in your babel configuration.
"babel": {
  "plugins": [
    "@quickbaseoss/babel-plugin-styled-components-css-namespace"
  ]
}

If you are also using babel-plugin-styled-components, you must place styled-components-css-namespace before babel-plugin-styled-components.

"babel": {
  "plugins": [
    "@quickbaseoss/babel-plugin-styled-components-css-namespace",
    "babel-plugin-styled-components"
  ]
}

Options

Default

Without adding options, this plugin will duplicate the class name generated by styled-components as suggested in this issue.

/* output */
.c0.c0 {
  background-color: blue;
}

Increasing Specificity

A common scenario when integrating styled-components into existing projects is fighting against extremely specific legacy CSS selectors such as #someId .grossly .nested section input {/* styles */}.

To increase the specificity that this plugin adds, you can leverage the recommended approach from the styled-components docs. Add the appropriate number of & selectors equal to the desired selector duplication as the cssNamespace option (the default behavior is x2 {"cssNamespace": "&&"}).

{
  "plugins": [
    [
      "@quickbaseoss/babel-plugin-styled-components-css-namespace",
      {"cssNamespace": "&&&"}
    ],
    "babel-plugin-styled-components"
  ]
}
/* output */
.c0.c0.c0 {
  background-color: blue;
}

Custom Namespace

You can provide a cssNamespace to use instead of duplicating the class name. Remember to include a DOM element with that class that wraps the styled-component. The cssNamespace takes the form of the selector you want to use to wrap all of your styles with.

"babel": {
  "plugins": [
    ["@quickbaseoss/babel-plugin-styled-components-css-namespace", {"cssNamespace": ".specific .moreSpecific .reallySpecific"}],
    "styled-components"
  ]
}
/* output */
.specific .moreSpecific .reallySpecific .c0 {
  background-color: blue;
}

where .c0 is the class added by styled-components to the element

Notes

Media Query String Interpolation

While an uncommon use-case, it can often be useful to interpolate media query at-rules in your css template string. Compared to the method for creating media queries from the styled-component docs, this method reduces the overhead of multiple calls of css while still allowing queries to be constructed without requiring nested template literals.

const mediaQuery = '@media only screen and (min-width: 426px)'

const StyledComponent = styled.div`
  background-color: red;
  ${mediaQuery} {
    background-color: blue;
  }
`

Unfortunately, this syntax is identical to the syntax used to refer to other components and this plugin cannot distinguish between the two and will produce broken CSS rules. Since referring to other components is more common, the below method of formatting @media inline can be used as a workaround.

const mediaQuery = 'only screen and (min-width: 426px)';

const StyledComponent = styled.div`
  background-color: red;
  @media ${mediaQuery} {
    background-color: blue;
  }
`;

Upgrade to version 1.0.0

Note that rawCssNamespace was dropped in favor of the single cssNamespace option. Additionally, support for an array of selectors was dropped as well. Update any references to rawCssNamespace with cssNamespace.

If you were already using cssNamespace, update your configuration to use a css selector rather than an array of classes. E.g., cssNamespace: 'moreSpecific' should be cssNamespace: '.moreSpecific' and cssNamespace: ['specific', 'verySpecific'] should be cssNamespace: '.specific .verySpecific'.

The Problem

styled-components is an awesome library for css-in-js and feels like a natural combination of React and CSS. It is easy to use and produces css instead of inline styles.

However, if you are trying to gradually introduce styled-components into a legacy website that might not have the best CSS, the legacy styles may bleed into your styled-components because they have more specificity than the single class styled-components.

The Solution

This plugin will automatically add additional css namespaces or duplicated classes to the selectors produced by styled components effectively creating a wall between the legacy css code and your new shiny styled components.

monty-python-castle

Styling frameworks

This plugin was built for Styled Components; however, since initially creating it, we at Quick Base have switched to Emotion. It works as an alternative to the stylis extra scope plugin which requires creating your own instance of stylis.

Developing

  1. Clone the repo with git clone https://github.com/QuickBase/babel-plugin-styled-components-css-namespace.git
  2. yarn install (prefer yarn although npm should work as well)
  3. yarn test to run the tests

Publishing

When we are ready to release a new version, one of the admins needs to run the following commands to publish the new version to npm. We probably need to invest in a better deploy and semver management system. Interested? See this issue.

Acknowledgements

Open source software is a group effort. This version of the plugin was heavily inspired by a fork of the original plugin from @TrevorBurnham.

We also would like to thank some of our contributors who helped solve some tough issues with the previous iteration of this plugin: