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

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

v2.0.1

Published

A babel plugin to add css namespaces to all styled components.

Downloads

4

Readme

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

⚠️ ℹ️ : Releasing new npm package because the old org/package hasn't been updated in a while.

This fork contains new deps, and CVE patches as per dependabot

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

Getting Started

  1. Add the plugin with yarn add @optimumfuturist/babel-plugin-styled-components-css-namespace or npm install @optimumfuturist/babel-plugin-styled-components-css-namespace
  2. Include the plugin in your babel configuration.
"babel": {
  "plugins": [
    "@optimumfuturist/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": [
    "@optimumfuturist/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": [
    [
      "@optimumfuturist/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": [
    ["@optimumfuturist/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.

  • If needed, open a new PR to update the version in the package.json
  • Copy the commit hash from the commit log
  • Run git tag -a {version} {commit_hash}. For example: git tag -a 0.0.9 abf3123
  • In the editor, add a message about the changes in this version and save
  • Push the tag to GitHub with git push --follow-tags
  • Travis CI will build and publish the new version to npm

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:

  • @daniloster
  • @dan-kez
  • @prevostc
  • @danielhusar