Skip to content

!global and !default support? #54

Closed

Description

@ryoikarashi

Does this plugin support !global and !default as SASS does support or any postCSS plugins that support these?

What I'm trying to do is to override variables, which is very useful for theming.

.foo {
   $foo: 'bar' !global; 
}

$foo: 'foo' !default;

// $foo => 'bar'

Activity

  1. ai commented on Jul 10, 2016

    @ai
    Member

    Nope #52

  2. ai commented on Jul 10, 2016

    @ai
    Member

    Can you show me your real case. You need !default only because you are thinking in Sass-way. In PostCSS you have much more power, so you could find much better code design.

  3. ryoikarashi commented on Jul 10, 2016

    @ryoikarashi
    Author

    @ai
    If I could make variables defined in local scope global, I could override them and switch themes only by changing class names. Here is the real case belowl.
    Are there any better code design for theming?

    index.html

    <html>
    <head>
    <link href="/assets/main.css" rel="stylesheet">
    </head>
    <body class="theme-A">
    ...
    </body>
    </html>

    main.css

    @import 'themes.css';
    @import 'variables.css';
    @import 'base.css';
    

    themes.css

    .theme-A {
       $bg-color: red !global;
       $text-color: blue !global;
    }
    .theme-B {
       $bg-color: green !global;
       $text-color: yellow !global;
    }

    variables.css

    $bg-color: white !default;
    $text-color: black !default;

    base.css

    body {
       background: $bg-color;
    }
    p {
       color: $text-color;
    }
  4. ai commented on Jul 10, 2016

    @ai
    Member

    This code will not work in Sass.

    The best way is to use CSS Custom Variables, they support it native in browser. But not all browsers support CSS Custom Variables. In PostCSS we have polyfill, but it works only for :root definitions, so it is useless here.

  5. ai commented on Jul 10, 2016

    @ai
    Member

    But I have awesome idea. Let’s write a polyfill for dynamic CSS Custom Properties!

    You pass this <body> switching classes to plugin:

    require('postcss-dynamic-custom-props')({ rootClasses: ['theme-A', 'theme-B'] })

    Then plugin will find all overrides definitions like and remember a variable names:

    .theme-A {
       --bg-color: red;
       --text-color: blue;
    }

    Then it will find all rules, where this variables used, like:

    p {
       color: var(--text-color);
    }

    On final step it merge themes classes and overrides and generate this:

    .themeA p {
       color: blue;
    }

    Sorry, maybe my description is little confused. Please ask me any question about it.

    What do you think about this idea? Do you have time for it? (I will help you step-by-step)

  6. ryoikarashi commented on Jul 10, 2016

    @ryoikarashi
    Author

    @ai Your idea looks great but the npm module you provided, postcss-dynamic-custom-props is not found in npm registries.
    Is it already deprecated?

  7. ai commented on Jul 10, 2016

    @ai
    Member

    😆 it is only idea. We need to write it 😉.

    Do you want to do it?

  8. ryoikarashi commented on Jul 10, 2016

    @ryoikarashi
    Author

    Oh I got it. 😆
    I'm trying to do it 👍

  9. ryoikarashi commented on Jul 10, 2016

    @ryoikarashi
    Author

    @ai Thanks! ;)

  10. dominictobias commented on Aug 22, 2016

    @dominictobias

    My use-case would be consuming node_modules which use postcss, and being able to override variables defined there before compilation time, thus being able to theme them. For example:

    $ns_bg_color: 'red';
    
    body {
      background-color: $ns_bg_color;
    }
    @import "./test.css";
    
    $ns_bg_color: 'blue';
    

    Background should be blue.

  11. ai commented on Aug 22, 2016

    @ai
    Member

    @dominictobias use mixin for it ;)

    This variables logic is non-intuitive, there is no languages with such logic

  12. dominictobias commented on Aug 22, 2016

    @dominictobias

    @ai thanks, do you mind giving a basic example of what you mean pls?

  13. ai commented on Aug 22, 2016

    @ai
    Member

    @dominictobias

    @mixin myTheme(blue);
  14. dominictobias commented on Aug 22, 2016

    @dominictobias

    Hm not sure how that would be implemented in a real world scenario - for example

    node_modules/slider/slider.js <-- imports ./slider.css which has $slider-color: 'black'

    app:
    index.js <--- imports './bla.css'
    import 'slider'; <-- some module in app imports this which imports slider.css

    How would you go about changing the colour of the slider in that scenario using mixins? (Without literally overriding the output css of the slider module)?

  15. ai commented on Aug 22, 2016

    @ai
    Member

    @dominictobias are you a developer pf that node_modules/slider?

  16. dominictobias commented on Aug 22, 2016

    @dominictobias

    Yep I am so can modify it however - this is for an sdk of components and
    consuming apps (we control both)

    On Monday, 22 August 2016, Andrey Sitnik [email protected] wrote:

    @dominictobias https://github.com/DominicTobias are you a developer pf
    that node_modules/slider?

    —
    You are receiving this because you were mentioned.
    Reply to this email directly, view it on GitHub
    #54 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AAuZ-hgWkMcr3jSqlpmmn4Ebuz5M7UgMks5qic4jgaJpZM4JIzlz
    .

  17. ai commented on Aug 22, 2016

    @ai
    Member

    @dominictobias so, just define a mixin in that node_modules/slider/slider.css ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions