All Projects → meduzen → v-helper

meduzen / v-helper

Licence: WTFPL license
A shorter SCSS access to CSS custom properties.

Programming Languages

SCSS
7915 projects

Projects that are alternatives of or similar to v-helper

postcss-variable-compress
Minifies css variable names
Stars: ✭ 18 (+28.57%)
Mutual labels:  css-custom-properties, css-variables
theme-change
Change CSS theme with toggle, buttons or select using CSS custom properties and localStorage
Stars: ✭ 283 (+1921.43%)
Mutual labels:  css-custom-properties, css-variables
Halfmoon
Front-end framework with a built-in dark mode and full customizability using CSS variables; great for building dashboards and tools.
Stars: ✭ 2,583 (+18350%)
Mutual labels:  css-custom-properties, css-variables
fylgja
The modular highly customisable CSS framework. Powered by CSS Components, Utilities and Props for building your Web UI.
Stars: ✭ 65 (+364.29%)
Mutual labels:  css-custom-properties, css-variables
Core
Native HTML Elements with CSS superpowers. 🕶
Stars: ✭ 237 (+1592.86%)
Mutual labels:  css-variables
Ie11customproperties
CSS variables (Custom Properties) polyfill for IE11
Stars: ✭ 765 (+5364.29%)
Mutual labels:  css-variables
Must Watch Css
A useful list of must-watch talks about CSS
Stars: ✭ 3,966 (+28228.57%)
Mutual labels:  css-variables
React Native Css Modules
Style React Native components using CSS, PostCSS, Sass, Less or Stylus.
Stars: ✭ 207 (+1378.57%)
Mutual labels:  css-variables
Grayshift
A lightweight front-end component library for developing fast and powerful web interfaces.
Stars: ✭ 304 (+2071.43%)
Mutual labels:  css-variables
Css Vars
Sass mixin to use CSS Custom Properties with Sass
Stars: ✭ 164 (+1071.43%)
Mutual labels:  css-variables
Svelte Css Vars
✨ Ever wanted to have reactive css variables in svelte? What if I told you there's a way?
Stars: ✭ 150 (+971.43%)
Mutual labels:  css-variables
Cssplus
CSSplus is a collection of CSS Reprocessor plugins that dynamically update CSS variables
Stars: ✭ 141 (+907.14%)
Mutual labels:  css-variables
Snack Helper
🗃 A universal CSS helper library.
Stars: ✭ 50 (+257.14%)
Mutual labels:  css-variables
Just Animate
Making Animation Simple
Stars: ✭ 242 (+1628.57%)
Mutual labels:  css-variables
Darken
🌑 Dark mode made easy
Stars: ✭ 571 (+3978.57%)
Mutual labels:  css-variables
open-props
CSS custom properties to help accelerate adaptive and consistent design.
Stars: ✭ 1,703 (+12064.29%)
Mutual labels:  css-custom-properties
Tailwindcss Theming
Tailwind CSS plugin for client-side theming using CSS variables, with dark mode support
Stars: ✭ 349 (+2392.86%)
Mutual labels:  css-variables
pollen
The CSS variables build system
Stars: ✭ 754 (+5285.71%)
Mutual labels:  css-variables
Theming Demo
https://codesandbox.io/s/github/juliaqiuxy/theming-demo/tree/master/?from-embed
Stars: ✭ 59 (+321.43%)
Mutual labels:  css-variables
swatch
A lightweight, modern theming library based on CSS variables and the setter/getter pattern.
Stars: ✭ 14 (+0%)
Mutual labels:  css-custom-properties

v.scss

v.scss brings a single SCSS function for shorter access (4 characters saved!) to CSS custom properties : v(propName) instead of var(--propName). It also improves fallbacks chaining.

Note

This helper has cool features, but I have decided to not use it outside of personal projects. Compared to IDE auto-completion, its benefits might not be worth the cognitive load linked to an additional abstraction.

Table of contents

Installation

  1. npm install v.scss pulls the package into your project.
  2. @import '~v.scss'; in a SCSS files makes v() available.

Usage

Declare your CSS custom properties as you usually do:

:root {
  --primary: #000;
  --bg: #fff;
}

Then, access them with v().

html {
  background: v(bg);
  color: v(primary);
}

That’s it! Here’s the generated CSS:

html {
  background: var(--bg);
  color: var(--primary);
}

Fallback value as optional second parameter

The CSS var() function can take a fallback value as second parameter: if the wanted custom property isn’t defined or valid for the browser, the fallback will be used.

:root {
  --primary: cyan;
  --bg: #433221;
}

html {
  background: v(bg, brown); // `background: var(--bg, brown);`
  color: v(primaryyyy, yellow); // `color: var(--primaryyyy, yellow);`
}

The background will be #433221 (--bg value) but the color will be yellow because --primaryyyy doesn’t exist.

Note

If you need the last parameter to be a string, wrap its quotes in more quotes:

.shrug::after {
  content: v(shrug-emoji, "\_(ツ)_/¯'"); /* double quotes around single ones */

  // generates
  content: var(--shrug-emoji, '¯\_(ツ)_/¯'); /* single quotes */
}

You can swap double and single quotes. CSS will be fine.

Multiple fallbacks

You can have multiple fallbacks by chaining multiple custom properties names. The last parameter is always a fallback value.

html {
  color: v(primary, accent, bg, #f0f0f0);

  // generates
  color: var(--primary, var(--accent, var(--bg, #f0f0f0)));
}

If you need the last parameter to not be a fallback value, replace it by null:

html {
  color: v(primary, accent, null);

  // generates
  color: var(--primary, var(--accent));
}

Note

If you need a list of values to not be considered as fallback, wrap them in quotes: as described in a comment, the list will be considered as one value, and the quotes will be stripped.

.my-class {
  transition-property: v(transition-properties, 'opacity, visibility');

  // generates
  transition-property: var(--transition-properties, opacity, visibility);
}

Edge cases

SCSS interpolation

In order to assign a value to a custom property using a SCSS variable or a SCSS function, interpolation is required:

$primary: #000;

.my-class {
  --primary: $primary; // error 🚫, custom property assignment needs interpolation
  --primary: #{$primary}; // correct ✅, value interpolated with `#{}`
  --primary: #000; // correct ✅, regular syntax

  --accent: v(secondary); // error 🚫, custom property assignment needs interpolation
  --accent: #{v(secondary)}; // correct ✅, function interpolated
  --accent: var(--secondary); // correct ✅, regular syntax

  color: v(accent); // correct ✅, `color` is not a custom property
}

Interpolation means “Have a look at #{what is inside the curly braced} and replace the $value-string by its computed value ($000)”.

In situations where interpolation is needed, using v() is less readable (#{v(propName)}) than the standard syntax (var(--propName)).

-- is a valid custom property name

It turns out that -- is a valid name for a CSS custom property.

Declaring and using it is all about edge cases:

.my-class {
  --: .5; // error 🚫
  --#{''}: .5; // correct ✅
  #{'--'}: .5; // correct ✅

  opacity: var(--); // error 🚫
  opacity: var(#{'--'}); // correct ✅
  opacity: v(); // correct ✅, thanks to v() ✌️
}

Another example, with three dashes:

.my-class-with-more-dashes {
  --#{'-'}: .5; // correct ✅
  #{'---'}: .5; // correct ✅

  opacity: var(#{'---'}); // correct ✅, interpolated
  opacity: v('-'); // correct ✅, thanks to v() ✌️
}

Changelog

See releases.

See also

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].