Accessing color variables in each component












0















Im looking for a trick to make my life easier. I want to style each component in my nuxtjs application with a similar color palette, but I do need to enter the color palette in each component. Tried to use scss for the first time. How do I put variables more globally and how to reach them?



I tried to put the code into assets/scss/styles.scss But components know nothing, about remote scss.



$color1: #808060;



$color2: #3D3D34;



$color3: #151510;



$color4: #090906;










share|improve this question























  • Read through this thread: (github.com/vuejs/vue-loader/issues/328) particularly towards the end. It can tell you how to define some SCSS variables and have them globally available in your Vue components.

    – Jayce444
    Jan 2 at 3:22
















0















Im looking for a trick to make my life easier. I want to style each component in my nuxtjs application with a similar color palette, but I do need to enter the color palette in each component. Tried to use scss for the first time. How do I put variables more globally and how to reach them?



I tried to put the code into assets/scss/styles.scss But components know nothing, about remote scss.



$color1: #808060;



$color2: #3D3D34;



$color3: #151510;



$color4: #090906;










share|improve this question























  • Read through this thread: (github.com/vuejs/vue-loader/issues/328) particularly towards the end. It can tell you how to define some SCSS variables and have them globally available in your Vue components.

    – Jayce444
    Jan 2 at 3:22














0












0








0








Im looking for a trick to make my life easier. I want to style each component in my nuxtjs application with a similar color palette, but I do need to enter the color palette in each component. Tried to use scss for the first time. How do I put variables more globally and how to reach them?



I tried to put the code into assets/scss/styles.scss But components know nothing, about remote scss.



$color1: #808060;



$color2: #3D3D34;



$color3: #151510;



$color4: #090906;










share|improve this question














Im looking for a trick to make my life easier. I want to style each component in my nuxtjs application with a similar color palette, but I do need to enter the color palette in each component. Tried to use scss for the first time. How do I put variables more globally and how to reach them?



I tried to put the code into assets/scss/styles.scss But components know nothing, about remote scss.



$color1: #808060;



$color2: #3D3D34;



$color3: #151510;



$color4: #090906;







vue.js sass frontend nuxt.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 0:53









Kamil Septio TrojnarKamil Septio Trojnar

717




717













  • Read through this thread: (github.com/vuejs/vue-loader/issues/328) particularly towards the end. It can tell you how to define some SCSS variables and have them globally available in your Vue components.

    – Jayce444
    Jan 2 at 3:22



















  • Read through this thread: (github.com/vuejs/vue-loader/issues/328) particularly towards the end. It can tell you how to define some SCSS variables and have them globally available in your Vue components.

    – Jayce444
    Jan 2 at 3:22

















Read through this thread: (github.com/vuejs/vue-loader/issues/328) particularly towards the end. It can tell you how to define some SCSS variables and have them globally available in your Vue components.

– Jayce444
Jan 2 at 3:22





Read through this thread: (github.com/vuejs/vue-loader/issues/328) particularly towards the end. It can tell you how to define some SCSS variables and have them globally available in your Vue components.

– Jayce444
Jan 2 at 3:22












1 Answer
1






active

oldest

votes


















1














As @jayce444 mentioned, this thread will give you multiple options to achieve the task.



However, you need to think before you take this approach. In general, you should import your variables file in each component SCSS:



<style lang="scss">
@import "<PATH_TO_ROOT>/assets/scss/styles.scss";

.someclass { color: $some-variable; }
</style>


By doing this, you will protect yourself for many uncertain future possibilities. Some of them are:




  • Splitting repository into multiple micro front-ends

  • Moving into Lerna like Mono repo setup

  • Reusing component in other code-bases


Being explicit is more maintainable than having magical auto/global imports. We, as developers, spend more time maintaining code than writing new code.



Alternately, another clean solution is not using vue-loader for managing SCSS. It means you should not use style tag inside .vue files.



Create one master style.scss file. For each component create dedicated .scss file. And import all these files into master style.scss like:



// External third party scss from node_modules
@import '~@material/button/button`;

// Base color style sheet (SCSS variable are global)
// By importing it here, all the subsequent .scss file have access to variables
@import './styles/colors`;

@import './components/component-1`;
@import './components/component-2`;

// .... Add remaining component
@import './components/component-n`;


There are a few advantages. Your stylesheet is no longer tied to the framework specific abstraction. You can reuse your style more easily with other code bases built on top of other frameworks. Of course, if you need to have Scoped-CSS which .vue files provide out-of-box, consider using BEM notation.



Finally, if you decide to import variables .scss file in each component, then you can use node-sass and webpack aliases to shorten the import path.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000139%2faccessing-color-variables-in-each-component%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As @jayce444 mentioned, this thread will give you multiple options to achieve the task.



    However, you need to think before you take this approach. In general, you should import your variables file in each component SCSS:



    <style lang="scss">
    @import "<PATH_TO_ROOT>/assets/scss/styles.scss";

    .someclass { color: $some-variable; }
    </style>


    By doing this, you will protect yourself for many uncertain future possibilities. Some of them are:




    • Splitting repository into multiple micro front-ends

    • Moving into Lerna like Mono repo setup

    • Reusing component in other code-bases


    Being explicit is more maintainable than having magical auto/global imports. We, as developers, spend more time maintaining code than writing new code.



    Alternately, another clean solution is not using vue-loader for managing SCSS. It means you should not use style tag inside .vue files.



    Create one master style.scss file. For each component create dedicated .scss file. And import all these files into master style.scss like:



    // External third party scss from node_modules
    @import '~@material/button/button`;

    // Base color style sheet (SCSS variable are global)
    // By importing it here, all the subsequent .scss file have access to variables
    @import './styles/colors`;

    @import './components/component-1`;
    @import './components/component-2`;

    // .... Add remaining component
    @import './components/component-n`;


    There are a few advantages. Your stylesheet is no longer tied to the framework specific abstraction. You can reuse your style more easily with other code bases built on top of other frameworks. Of course, if you need to have Scoped-CSS which .vue files provide out-of-box, consider using BEM notation.



    Finally, if you decide to import variables .scss file in each component, then you can use node-sass and webpack aliases to shorten the import path.






    share|improve this answer




























      1














      As @jayce444 mentioned, this thread will give you multiple options to achieve the task.



      However, you need to think before you take this approach. In general, you should import your variables file in each component SCSS:



      <style lang="scss">
      @import "<PATH_TO_ROOT>/assets/scss/styles.scss";

      .someclass { color: $some-variable; }
      </style>


      By doing this, you will protect yourself for many uncertain future possibilities. Some of them are:




      • Splitting repository into multiple micro front-ends

      • Moving into Lerna like Mono repo setup

      • Reusing component in other code-bases


      Being explicit is more maintainable than having magical auto/global imports. We, as developers, spend more time maintaining code than writing new code.



      Alternately, another clean solution is not using vue-loader for managing SCSS. It means you should not use style tag inside .vue files.



      Create one master style.scss file. For each component create dedicated .scss file. And import all these files into master style.scss like:



      // External third party scss from node_modules
      @import '~@material/button/button`;

      // Base color style sheet (SCSS variable are global)
      // By importing it here, all the subsequent .scss file have access to variables
      @import './styles/colors`;

      @import './components/component-1`;
      @import './components/component-2`;

      // .... Add remaining component
      @import './components/component-n`;


      There are a few advantages. Your stylesheet is no longer tied to the framework specific abstraction. You can reuse your style more easily with other code bases built on top of other frameworks. Of course, if you need to have Scoped-CSS which .vue files provide out-of-box, consider using BEM notation.



      Finally, if you decide to import variables .scss file in each component, then you can use node-sass and webpack aliases to shorten the import path.






      share|improve this answer


























        1












        1








        1







        As @jayce444 mentioned, this thread will give you multiple options to achieve the task.



        However, you need to think before you take this approach. In general, you should import your variables file in each component SCSS:



        <style lang="scss">
        @import "<PATH_TO_ROOT>/assets/scss/styles.scss";

        .someclass { color: $some-variable; }
        </style>


        By doing this, you will protect yourself for many uncertain future possibilities. Some of them are:




        • Splitting repository into multiple micro front-ends

        • Moving into Lerna like Mono repo setup

        • Reusing component in other code-bases


        Being explicit is more maintainable than having magical auto/global imports. We, as developers, spend more time maintaining code than writing new code.



        Alternately, another clean solution is not using vue-loader for managing SCSS. It means you should not use style tag inside .vue files.



        Create one master style.scss file. For each component create dedicated .scss file. And import all these files into master style.scss like:



        // External third party scss from node_modules
        @import '~@material/button/button`;

        // Base color style sheet (SCSS variable are global)
        // By importing it here, all the subsequent .scss file have access to variables
        @import './styles/colors`;

        @import './components/component-1`;
        @import './components/component-2`;

        // .... Add remaining component
        @import './components/component-n`;


        There are a few advantages. Your stylesheet is no longer tied to the framework specific abstraction. You can reuse your style more easily with other code bases built on top of other frameworks. Of course, if you need to have Scoped-CSS which .vue files provide out-of-box, consider using BEM notation.



        Finally, if you decide to import variables .scss file in each component, then you can use node-sass and webpack aliases to shorten the import path.






        share|improve this answer













        As @jayce444 mentioned, this thread will give you multiple options to achieve the task.



        However, you need to think before you take this approach. In general, you should import your variables file in each component SCSS:



        <style lang="scss">
        @import "<PATH_TO_ROOT>/assets/scss/styles.scss";

        .someclass { color: $some-variable; }
        </style>


        By doing this, you will protect yourself for many uncertain future possibilities. Some of them are:




        • Splitting repository into multiple micro front-ends

        • Moving into Lerna like Mono repo setup

        • Reusing component in other code-bases


        Being explicit is more maintainable than having magical auto/global imports. We, as developers, spend more time maintaining code than writing new code.



        Alternately, another clean solution is not using vue-loader for managing SCSS. It means you should not use style tag inside .vue files.



        Create one master style.scss file. For each component create dedicated .scss file. And import all these files into master style.scss like:



        // External third party scss from node_modules
        @import '~@material/button/button`;

        // Base color style sheet (SCSS variable are global)
        // By importing it here, all the subsequent .scss file have access to variables
        @import './styles/colors`;

        @import './components/component-1`;
        @import './components/component-2`;

        // .... Add remaining component
        @import './components/component-n`;


        There are a few advantages. Your stylesheet is no longer tied to the framework specific abstraction. You can reuse your style more easily with other code bases built on top of other frameworks. Of course, if you need to have Scoped-CSS which .vue files provide out-of-box, consider using BEM notation.



        Finally, if you decide to import variables .scss file in each component, then you can use node-sass and webpack aliases to shorten the import path.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 6:47









        Harshal PatilHarshal Patil

        3,07111247




        3,07111247
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000139%2faccessing-color-variables-in-each-component%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]