Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cssvar() function #36597

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add a cssvar() function #36597

wants to merge 1 commit into from

Conversation

mdo
Copy link
Member

@mdo mdo commented Jun 17, 2022

Potentially addresses #36595 by implementing a new cssvar() function that checks for null values in Sass variables.

@mdo mdo requested a review from a team as a code owner June 17, 2022 01:44
Copy link

@hebertcisco hebertcisco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a function to check for null values ​​in Sass variables really solves the issue #36595. Now scss don’t produces broken css with missing values

@julien-deramond
Copy link
Member

We are only adressing here --bs-heading-color: ; amongst the 15 cases mentioned in the issue. The remainings are (for a basic usage without overriding the vars):

  --bs-btn-font-family: ;
  --bs-dropdown-box-shadow: ;
  --bs-nav-link-font-weight: ;
  --bs-card-box-shadow: ;
  --bs-card-cap-color: ;
  --bs-card-height: ;
  --bs-card-color: ;
  --bs-breadcrumb-bg: ;
  --bs-breadcrumb-border-radius: ;
  --bs-toast-color: ;
  --bs-modal-color: ;
  --bs-modal-footer-bg: ;
  --bs-tooltip-margin: ;
  --bs-offcanvas-color: ;

Don't we want to wait for using this mixin everywhere?

If it is applied everywhere, it'll have an impact on the documentation where we need to define if it remains understandable for the users. It becomes a little bit less obvious with @include cssvar( everywhere:

Screenshot from 2022-06-24 23-22-26

Rather than using a mixin, can't we rather drop the empty CSS vars after/during the generation of the CSS file?

@mdo
Copy link
Member Author

mdo commented Jul 6, 2022

That'd be awesome, @julien-deramond, and would keep things less abstracted. I haven't looked into a linter for this yet, let me know if you have one in mind :).

@mdo
Copy link
Member Author

mdo commented Jul 6, 2022

I don't see options in our existing tooling I think, but I did find https://www.npmjs.com/package/postcss-dropunusedvars.

@julien-deramond
Copy link
Member

julien-deramond commented Jul 7, 2022

I don't see neither options in our existing tooling. Let's try https://www.npmjs.com/package/postcss-dropunusedvars!.

@julien-deramond
Copy link
Member

julien-deramond commented Jul 7, 2022

First basic test with https://www.npmjs.com/package/postcss-dropunusedvars.

diff --git a/scss/_accordion.scss b/scss/_accordion.scss
index b306540d7..35576ad76 100644
--- a/scss/_accordion.scss
+++ b/scss/_accordion.scss
@@ -3,6 +3,9 @@
 //

 .accordion {
+  --test-component-unused-var: 10px;
+  --test-component-empty-var: ;
+
   // scss-docs-start accordion-css-vars
   --#{$prefix}accordion-color: #{color-contrast($accordion-bg)};
   --#{$prefix}accordion-bg: #{$accordion-bg};
@@ -35,6 +38,7 @@
   align-items: center;
   width: 100%;
   padding: var(--#{$prefix}accordion-btn-padding-y) var(--#{$prefix}accordion-btn-padding-x);
+  font-size: var(--test-component-empty-var);
   @include font-size($font-size-base);
   color: var(--#{$prefix}accordion-btn-color);
   text-align: left; // Reset button style

Without the plugin both variables stay in dist/bootstrap.css.

Install the plugin with npm i postcss-dropunusedvars --save-dev and change our postcss.config.js to:

diff --git a/build/postcss.config.js b/build/postcss.config.js
index 7f8186d10..5b780fe35 100644
--- a/build/postcss.config.js
+++ b/build/postcss.config.js
@@ -10,6 +10,7 @@ module.exports = context => {
   return {
     map: context.file.dirname.includes('examples') ? false : mapConfig,
     plugins: {
+      "postcss-dropunusedvars": {},
       autoprefixer: {
         cascade: false
       },

With the plugin --test-component-unused-var: 10px; is removed from dist/bootstrap.css but since --test-component-empty-var is in fact used (even if empty), it stays in the file.

Haven't looked at if postcss-dropunusedvars can be configured yet (source code), but the basic configuration is not really what we want here.
If it doesn't exist we could write our own postcss-dropemptyvars plugin 🤷

@@ -1,3 +1,9 @@
@mixin cssvar($name, $value) {
@if $value != null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's invalid because css var can be null too, it's can be useful to reset var, better solution:

@mixin cssvar($name, $value: false) {
  @if $value != false {
      @if $value == null {
        $value: initial;
      }
      --#{$prefix}#{$name}: #{$value};
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Review in progress
Development

Successfully merging this pull request may close these issues.

4 participants