question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] CSS Variables

See original GitHub issue

I know CSS variables has already been discussed (#26596) and I saw this reply https://github.com/twbs/bootstrap/issues/26596#issuecomment-403342215

So I come with a proposal which not requires heavy refactoring

Proposal

Instead of using directly native functions like darken or lighten we should use wrappers Ex : try-darken

@function try-darken($color, $percent) {

  @if (not $enable-variables){
    @return darken($color, $percent);
  }

  @if str_slice("#{$color}", 0, 3) == "var" {
    $percent-str: "#{$percent * 100}";
    $percent-value: str_slice($percent-str, 0, str_length($percent-str) - 1);
    $original: str_slice($color, 7, str_length($color) - 1);
    $variable: --#{$original}-darken-#{$percent-value};
    @return var($variable);
  }

  @return darken($color, $percent);
}

This wrapper handles the variable construction and create shades of colors.

Then we need to generate all shades of variables

:root {
  @if ($enable-variables){
    @each $color, $value in $theme-colors-values {
      --#{$color}: #{$value};
      --#{$color}-yiq: #{color-yiq($value)};

      @each $shade in $theme-colors-shades {
        --#{$color}-darken-#{$shade * 100}: #{darken($value, $shade)};
        --#{$color}-darken-#{$shade * 100}-yiq: #{color-yiq(darken($value, $shade))};
        --#{$color}-lighten-#{$shade * 100}: #{lighten($value, $shade)};
        --#{$color}-lighten-#{$shade * 100}-yiq: #{color-yiq(lighten($value, $shade))};
      }
    }
  }
  @else{
    @each $color, $value in $theme-colors {
      --#{$color}: #{$value};
    }
  }
}

This creates all necessary variables. As you see, this requires to list all necessary shades, and concret theme color values

$primary:    var(--primary) !default;
$secondary:  var(--secondary) !default;
$success:    var(--success) !default;
$info:       var(--info) !default;
$warning:    var(--warning) !default;
$danger:     var(--danger) !default;
$light:      var(--light) !default;
$dark:       var(--dark) !default;

$theme-colors-values: map-merge(
  (
    "primary":     $blue,
    "secondary":   $gray-600,
    "success":     $green,
    "info":        $cyan,
    "warning":     $yellow,
    "danger":      $red,
    "light":       $gray-100,
    "dark":        $gray-800,
  ),
  $theme-colors-values
);

$theme-colors-shades: (7.5, 10, 12.5, 25, 35, 40) !default;

With theses few tricks I managed to create all variables I needed for theme colors, and I can change it on the fly with few javascript

Demo

Here is a demo site with an implementation https://felx-b.github.io/docs/4.3/getting-started/introduction/ You can change the primary color on fly in the nav bar

Source

Here are the sources https://github.com/Felx-B/bootstrap-css-variable

Alternative

If the proposal is rejected, it would worth nothing to wrap native function calls (like darken/lighten/mix …) into upper functions (try-darken, try-lighten …) so we could override these behaviors to implement CSS variable ourselves.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
guledalicommented, Apr 28, 2019

@piernik and it’s easy too, not that the current scss is difficult but this is “easier”

1reaction
piernikcommented, Apr 27, 2019

@Felx-B I love Your idea. I think css variables is must-have in bootstrap. With that every user could change interface colors - It’s great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS Custom Properties for Cascading Variables Module Level 1
This module introduces cascading variables as a new primitive value type that is accepted by all CSS properties, and custom properties for ...
Read more >
A Complete Guide To CSS Variables [With Examples]
In this guide, we delve deeper into what CSS variables are and how to use them to create beautiful responsive websites, along with...
Read more >
CSS Variables: What They Are and How They Work
CSS variables can take on any CSS value. For this example, we'll use color values (represented as hex codes) and font families.
Read more >
CSS Variables Draft — Tab Completion
This is a draft proposal for CSS Variables, which allow you to store CSS values into variables for later use in properties. Variables...
Read more >
CSS Variables explained with 5 examples | by Daniel — JS Craft
The plan for this article is to show you how native CSS variables work, and how you can use them to make your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found