Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

postcss-custom-properties-fallback

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-custom-properties-fallback

Adds fallbacks to your CSS var() functions

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.5K
decreased by-21.99%
Maintainers
1
Weekly downloads
 
Created
Source

PostCSS Custom Properties Fallback

This plugins adds fallbacks to your CSS Custom Properties and works well as a compantion to PostCSS Custom Properties.

Pop Quiz!

If the following change happens, what color will h1 have in modern browsers?

:root {
-  --color: red;
}

body {
  color: green;
}

h1 {
  color: red;
  color: var(--color);
}

Red or green, expand the right answer (no cheating/googling!):

h1 is red

The text "Wrong answer!" over a cat screaming while firing an automatic rifle

Nope, it's green!

Intuitively it's easy to think that if --color isn't defined, then the browser should skip the color: var(--color) and use the valid color: red above it. Especially since this is what happens in older browsers that don't support CSS Custom Properties.

The right answer is to use the second argument in var() (see Example 10 in the spec), also known as the fallback argument:

color: var(--color, red);

Now it works like expected. See the spec for more information on how invalid/missing values are treated.

h1 is green

The text "Yes!" over a smiling and nodding Jack Nicholson

Right answer! Check the wrong answer to learn why that is.

Usage

Add PostCSS Custom Properties Fallback to your project:

npm install postcss-custom-properties--fallback --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssCustomPropertiesFallback = require('postcss-custom-properties-fallback');

postcss([postcssCustomPropertiesFallback(/* pluginOptions */)]).process(
  YOUR_CSS /*, processOptions */
);

Options

importFrom

The importFrom option is required. It works like from CSS Custom Properties, except it doesn't support importing from CSS yet.

postcssCustomPropertiesFallback({
  importFrom: { customProperties: { '--color': 'red' } },
});
h1 {
  color: var(--color);
}

/* becomes */

h1 {
  color: var(--color, red);
}

Keywords

FAQs

Package last updated on 20 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc