How to change color of icon in Editable Svg Icons system in vuejs cookbook?












2















I'm trying to use the system proposed by vuejs cookbook for keeping svg icons (https://vuejs.org/v2/cookbook/editable-svg-icons.html)



So I have 2 components: IconBase and IconArrowUp. I need to change color of arrow icon on hover (also css transition should be applied).



In the article they have iconColor property. But I didn't manage to change the color using css, and it's not that convenient to use events like onmouseenter.



Here is the code of two components:



IconBase.vue:



<template>
<svg xmlns="http://www.w3.org/2000/svg"
:width="width"
:height="height"
viewBox="0 0 100 100"
role="presentation"
@click="$emit('click')">
<g :fill="iconColor">
<slot />
</g>
</svg>
</template>

<script>
export default {
props: {
width: {
type: [Number, String],
default: 18
},
height: {
type: [Number, String],
default: 18
},
iconColor: {
type: String,
default: 'currentColor'
}
}
}
</script>


And IconArrowUp.vue:



<template>
<path d="m12.398 40.102 13.402 13.5 14.699-14.801v58.699h19v-58.699l14.699 14.801 13.402-13.5-37.602-37.602z"/>
</template>


So this is how I use them (another component ScrollToTop.vue):



<template>
<div class="scroll-to-top">
<icon-base class="icon-arrow-up"
width="20"
height="20"
icon-color="#949494">
<icon-arrow-up/>
</icon-base>
</div>
</template>


How can I change to color of icon on hover? I want to keep IconArrowUp component clean though, so that if I need I use it without color change










share|improve this question





























    2















    I'm trying to use the system proposed by vuejs cookbook for keeping svg icons (https://vuejs.org/v2/cookbook/editable-svg-icons.html)



    So I have 2 components: IconBase and IconArrowUp. I need to change color of arrow icon on hover (also css transition should be applied).



    In the article they have iconColor property. But I didn't manage to change the color using css, and it's not that convenient to use events like onmouseenter.



    Here is the code of two components:



    IconBase.vue:



    <template>
    <svg xmlns="http://www.w3.org/2000/svg"
    :width="width"
    :height="height"
    viewBox="0 0 100 100"
    role="presentation"
    @click="$emit('click')">
    <g :fill="iconColor">
    <slot />
    </g>
    </svg>
    </template>

    <script>
    export default {
    props: {
    width: {
    type: [Number, String],
    default: 18
    },
    height: {
    type: [Number, String],
    default: 18
    },
    iconColor: {
    type: String,
    default: 'currentColor'
    }
    }
    }
    </script>


    And IconArrowUp.vue:



    <template>
    <path d="m12.398 40.102 13.402 13.5 14.699-14.801v58.699h19v-58.699l14.699 14.801 13.402-13.5-37.602-37.602z"/>
    </template>


    So this is how I use them (another component ScrollToTop.vue):



    <template>
    <div class="scroll-to-top">
    <icon-base class="icon-arrow-up"
    width="20"
    height="20"
    icon-color="#949494">
    <icon-arrow-up/>
    </icon-base>
    </div>
    </template>


    How can I change to color of icon on hover? I want to keep IconArrowUp component clean though, so that if I need I use it without color change










    share|improve this question



























      2












      2








      2








      I'm trying to use the system proposed by vuejs cookbook for keeping svg icons (https://vuejs.org/v2/cookbook/editable-svg-icons.html)



      So I have 2 components: IconBase and IconArrowUp. I need to change color of arrow icon on hover (also css transition should be applied).



      In the article they have iconColor property. But I didn't manage to change the color using css, and it's not that convenient to use events like onmouseenter.



      Here is the code of two components:



      IconBase.vue:



      <template>
      <svg xmlns="http://www.w3.org/2000/svg"
      :width="width"
      :height="height"
      viewBox="0 0 100 100"
      role="presentation"
      @click="$emit('click')">
      <g :fill="iconColor">
      <slot />
      </g>
      </svg>
      </template>

      <script>
      export default {
      props: {
      width: {
      type: [Number, String],
      default: 18
      },
      height: {
      type: [Number, String],
      default: 18
      },
      iconColor: {
      type: String,
      default: 'currentColor'
      }
      }
      }
      </script>


      And IconArrowUp.vue:



      <template>
      <path d="m12.398 40.102 13.402 13.5 14.699-14.801v58.699h19v-58.699l14.699 14.801 13.402-13.5-37.602-37.602z"/>
      </template>


      So this is how I use them (another component ScrollToTop.vue):



      <template>
      <div class="scroll-to-top">
      <icon-base class="icon-arrow-up"
      width="20"
      height="20"
      icon-color="#949494">
      <icon-arrow-up/>
      </icon-base>
      </div>
      </template>


      How can I change to color of icon on hover? I want to keep IconArrowUp component clean though, so that if I need I use it without color change










      share|improve this question
















      I'm trying to use the system proposed by vuejs cookbook for keeping svg icons (https://vuejs.org/v2/cookbook/editable-svg-icons.html)



      So I have 2 components: IconBase and IconArrowUp. I need to change color of arrow icon on hover (also css transition should be applied).



      In the article they have iconColor property. But I didn't manage to change the color using css, and it's not that convenient to use events like onmouseenter.



      Here is the code of two components:



      IconBase.vue:



      <template>
      <svg xmlns="http://www.w3.org/2000/svg"
      :width="width"
      :height="height"
      viewBox="0 0 100 100"
      role="presentation"
      @click="$emit('click')">
      <g :fill="iconColor">
      <slot />
      </g>
      </svg>
      </template>

      <script>
      export default {
      props: {
      width: {
      type: [Number, String],
      default: 18
      },
      height: {
      type: [Number, String],
      default: 18
      },
      iconColor: {
      type: String,
      default: 'currentColor'
      }
      }
      }
      </script>


      And IconArrowUp.vue:



      <template>
      <path d="m12.398 40.102 13.402 13.5 14.699-14.801v58.699h19v-58.699l14.699 14.801 13.402-13.5-37.602-37.602z"/>
      </template>


      So this is how I use them (another component ScrollToTop.vue):



      <template>
      <div class="scroll-to-top">
      <icon-base class="icon-arrow-up"
      width="20"
      height="20"
      icon-color="#949494">
      <icon-arrow-up/>
      </icon-base>
      </div>
      </template>


      How can I change to color of icon on hover? I want to keep IconArrowUp component clean though, so that if I need I use it without color change







      css svg vue.js vuejs2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 2:53









      tony19

      25.4k44378




      25.4k44378










      asked Nov 26 '18 at 0:11









      VictorVictor

      1,95244074




      1,95244074
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You could make the fill conditional on a local Boolean that changes based on mouseover/mouseout events:





          1. In IconBase.vue, define a local data variable (e.g., named isHover) to track the hover state, and a prop to set the color during the hover state:



            props: [
            // ...
            iconHoverColor: {
            type: String,
            default: "currentColor"
            }
            ],
            data() {
            return {
            isHover: false
            }
            }



          2. Edit IconBase.vue's template to make fill conditional on isHover, and set isHover based on mouseover/mouseout events:



            <g :fill="isHover ? iconHoverColor : iconColor"
            @mouseover="isHover = true"
            @mouseout="isHover = false">



          3. To set the color transition, use CSS in IconBase.vue on a class applied to <g>:



            <style scoped>
            /* <g class="icon" ...> */
            .icon {
            transition: fill .4s ease;
            }
            </style>



          4. In ScrollToTop.vue, edit the usage of IconBase to set the hover color:



            <icon-base icon-hover-color="#ff0000" ...>



          demo






          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%2f53473292%2fhow-to-change-color-of-icon-in-editable-svg-icons-system-in-vuejs-cookbook%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









            0














            You could make the fill conditional on a local Boolean that changes based on mouseover/mouseout events:





            1. In IconBase.vue, define a local data variable (e.g., named isHover) to track the hover state, and a prop to set the color during the hover state:



              props: [
              // ...
              iconHoverColor: {
              type: String,
              default: "currentColor"
              }
              ],
              data() {
              return {
              isHover: false
              }
              }



            2. Edit IconBase.vue's template to make fill conditional on isHover, and set isHover based on mouseover/mouseout events:



              <g :fill="isHover ? iconHoverColor : iconColor"
              @mouseover="isHover = true"
              @mouseout="isHover = false">



            3. To set the color transition, use CSS in IconBase.vue on a class applied to <g>:



              <style scoped>
              /* <g class="icon" ...> */
              .icon {
              transition: fill .4s ease;
              }
              </style>



            4. In ScrollToTop.vue, edit the usage of IconBase to set the hover color:



              <icon-base icon-hover-color="#ff0000" ...>



            demo






            share|improve this answer






























              0














              You could make the fill conditional on a local Boolean that changes based on mouseover/mouseout events:





              1. In IconBase.vue, define a local data variable (e.g., named isHover) to track the hover state, and a prop to set the color during the hover state:



                props: [
                // ...
                iconHoverColor: {
                type: String,
                default: "currentColor"
                }
                ],
                data() {
                return {
                isHover: false
                }
                }



              2. Edit IconBase.vue's template to make fill conditional on isHover, and set isHover based on mouseover/mouseout events:



                <g :fill="isHover ? iconHoverColor : iconColor"
                @mouseover="isHover = true"
                @mouseout="isHover = false">



              3. To set the color transition, use CSS in IconBase.vue on a class applied to <g>:



                <style scoped>
                /* <g class="icon" ...> */
                .icon {
                transition: fill .4s ease;
                }
                </style>



              4. In ScrollToTop.vue, edit the usage of IconBase to set the hover color:



                <icon-base icon-hover-color="#ff0000" ...>



              demo






              share|improve this answer




























                0












                0








                0







                You could make the fill conditional on a local Boolean that changes based on mouseover/mouseout events:





                1. In IconBase.vue, define a local data variable (e.g., named isHover) to track the hover state, and a prop to set the color during the hover state:



                  props: [
                  // ...
                  iconHoverColor: {
                  type: String,
                  default: "currentColor"
                  }
                  ],
                  data() {
                  return {
                  isHover: false
                  }
                  }



                2. Edit IconBase.vue's template to make fill conditional on isHover, and set isHover based on mouseover/mouseout events:



                  <g :fill="isHover ? iconHoverColor : iconColor"
                  @mouseover="isHover = true"
                  @mouseout="isHover = false">



                3. To set the color transition, use CSS in IconBase.vue on a class applied to <g>:



                  <style scoped>
                  /* <g class="icon" ...> */
                  .icon {
                  transition: fill .4s ease;
                  }
                  </style>



                4. In ScrollToTop.vue, edit the usage of IconBase to set the hover color:



                  <icon-base icon-hover-color="#ff0000" ...>



                demo






                share|improve this answer















                You could make the fill conditional on a local Boolean that changes based on mouseover/mouseout events:





                1. In IconBase.vue, define a local data variable (e.g., named isHover) to track the hover state, and a prop to set the color during the hover state:



                  props: [
                  // ...
                  iconHoverColor: {
                  type: String,
                  default: "currentColor"
                  }
                  ],
                  data() {
                  return {
                  isHover: false
                  }
                  }



                2. Edit IconBase.vue's template to make fill conditional on isHover, and set isHover based on mouseover/mouseout events:



                  <g :fill="isHover ? iconHoverColor : iconColor"
                  @mouseover="isHover = true"
                  @mouseout="isHover = false">



                3. To set the color transition, use CSS in IconBase.vue on a class applied to <g>:



                  <style scoped>
                  /* <g class="icon" ...> */
                  .icon {
                  transition: fill .4s ease;
                  }
                  </style>



                4. In ScrollToTop.vue, edit the usage of IconBase to set the hover color:



                  <icon-base icon-hover-color="#ff0000" ...>



                demo







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 26 '18 at 3:00

























                answered Nov 26 '18 at 2:53









                tony19tony19

                25.4k44378




                25.4k44378
































                    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%2f53473292%2fhow-to-change-color-of-icon-in-editable-svg-icons-system-in-vuejs-cookbook%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

                    Futebolista

                    Feedback on college project

                    Albești (Vaslui)