Code Monkey home page Code Monkey logo

postcss-custom-properties-fallback's Introduction

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 we remove --color from :root, 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);
}

postcss-custom-properties-fallback's People

Contributors

julczka avatar renovate-bot avatar renovate[bot] avatar stipsan avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

julczka

postcss-custom-properties-fallback's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.


Using a preset maintained by team Ecosystem at


Sanity: The Composable Content Cloud

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/node.js.yml
  • actions/checkout v3
  • actions/setup-node v3
npm
package.json
  • postcss-value-parser ^4.2.0
  • postcss-values-parser ^6.0.2
  • eslint ^8.11.0
  • microbundle ^0.15.0
  • postcss ^8.4.12
  • tap ^16.0.0
  • postcss ^8.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Fallback values box shadow with multiple shadows are not handles correctly

If a custom property is set to a list of shadows, the fallback values are not inserted correctly.

Property declaration:

  --shadow-depth-1: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);

CSS before:

div {
  box-shadow: var(--shadow-depth-1);
}

CSS after:

div {
  box-shadow: var(--uui-shadow-depth-5,0,19px,38px,rgba(0,0,0,0.30),,,0,15px,12px,rgba(0,0,0,0.22));
}

Replacing functional notation not possible

Firstly, thanks for a great plugin!

I've noticed a problem trying to replace properties with comma-separated strings (in my use-case, some functional notation), in that I think this plugin only supports single postcss word nodes, or only ones separated by spaces, as the fallback argument.

I think this may be a similar issue to #45

This unit test fails:

tap.test('plugin() - rgba', async (t) => {
  const rgba = `
    .test {
      color: rbga(var(--color-rgb), 0.5);
    }
  `;
  const { css } = await postcss(
    plugin({
      importFrom: {
        customProperties: { '--color-rgb': '45,45,45' },
      },
    })
  ).process(rgba, { from: undefined });

  t.matchSnapshot(clean(css), 'functional notation example');
  t.end();
});

...this produces a snapshot:

exports[`test/plugin.mjs TAP plugin() - rgba > functional notation example 1`] = `

    .test {
      color: rbga(var(--color-rgb,45,,,45,,,45), 0.5);
    }
  
`

...but would expect:

exports[`test/plugin.mjs TAP plugin() - rgba > functional notation example 1`] = `

    .test {
      color: rbga(var(--color-rgb,45,45,45), 0.5);
    }
  
`

The issue appears to be the parse that has been done on the values in the provided config; I am unclear what the parse achieves:

function getCustomPropertiesFromObject(object) {
  const customProperties = Object.assign(
    {},
    Object(object).customProperties,
    Object(object)['custom-properties']
  );

  for (const key in customProperties) {
    customProperties[key] = parse(String(customProperties[key])).nodes;
  }

  return customProperties;
}

..because when removing that, to become:

function getCustomPropertiesFromObject(object) {
  const customProperties = Object.assign(
    {},
    Object(object).customProperties,
    Object(object)['custom-properties']
  );

  return customProperties;
}

...the existing unit tests (and the new one above) seem to pass.

Interestingly, using a css4 space-separated notation rather than commas seems to work - so I can get the following snapshot OK:

exports[`test/plugin.mjs TAP plugin() - rgba > functional notation example 2`] = `

    .test {
      color: rbga(var(--color-rgb,45 45 45) / 0.5);
    }
  
`

But I would like to support Safari 12.0, which doesn't yet support that notation.

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.