Dynamic styling for pseudo element's content in Vue2.x

blackSheep

I have a component that is basically an Icon and a tooltip; it shows the tooltip( whose text comes from the parent component as a prop) on mouse hover. I implemented the tooltip with the help of pseudo-elements so I need to update the pseudo element's content with the prop's value. I am also aware of the fact that Pseudo element rules of CSS aren’t part of the DOM, and therefore they can’t be altered using JavaScript methods, but I found a bunch of workarounds that basically work for any CSS property but not content(I can see they work (e.g. for width property of tooltip or its color ) but strangely not for the content property), here is what I have done so far, any help would be appreciated:

<template>
    <div>
        <!-- <style>
            :root {
            --info-msg: {{tooltipContent}};
            }
        </style> -->
        <div class="infoBar">
            <svg-icon
                fill="#d5d5dc"
                :width="16"
                :height="16"
                name="common/c-info"
                class="infoBar__icon"
            />
        </div>
    </div>

</template>

<script>
export default {
  name: 'ContentInfo',
  props: {
    tooltipContent: {
      type: String,
      default: 'hint text goes here'
    }
  },
  mounted () {
    console.log(this.tooltipContent)
    this.applyCSS()
  },

  beforeUpdate () {
    this.applyCSS()
  },
  methods: {
    applyCSS () {
      const cssRule = `
      .infoBar:after{
        content: ${this.tooltipContent};
      }`
      const style = document.createElement('style')
      style.type = 'text/css'
      this.$el.appendChild(style)
      style.innerHTML = cssRule
    }
  }

}
</script>

<style lang="postcss" >

  .infoBar{
    position: relative;
    margin-left: 24px;
    &::after {
      background-color: #000;
      font-size: 12px;
      color: #fff;
      border-radius: 2px;
      width: 198px;
      height: 40px;
     /*content: var(--info-msg)*/
      display: none;
      padding: 11px 26px 10px 27px;
      position: absolute;
      top: 0;
      left: 80%;
      transform: translate(-50%, calc(-100% - 10px));
      z-index: 999;
    }
    &::before {
      background-color: #000;
      content: '';
      display: none;
      position: absolute;
      width: 10px;
      height: 6px;
      z-index: 999;
      top: 0;
      left: 80%;
      transform: translate(-50%, calc(-100% - 10px)) rotate(45deg);
    }
    &:hover::after {
      display: block;
    }
    &:hover::before {
      display: block;
    }
    &__icon:hover{
        fill: #e4002b;
      }
  }

  </style>
hamid niakan

follow these steps and you can use props in the css as css variables:

  1. use props in a computed to return an object of css variables and bind this object to the style of the component
  2. you should give the component a class name as the css variables can be accessed from within the class name in the css style section
  3. as for css variables that should be in the string format to work correctly you should use JSON.stringify() (this is the case for the content css property)

run the example bellow and see for yourself, both color and text of the after element showing by hover are coming as a prop from the parent component:

Vue.component('pseudo', {
  data() {
    return {
      title: 'Hover me',
    }
  },
  props: {
    color: {
      type: String,
    },
    text: {
      type: String,
    }
  },
  computed: {
    cssVars() {
      return {
        '--color': this.color,
        '--text': JSON.stringify(this.text),
      }
    }
  },
  template: `<div class="content" :style="cssVars">{{title}}</div>`,
});

var vm = new Vue({
  el: '#app',
});
.content {
  color: red;
}

.content:hover::after {
  content: var(--text);
  color: var(--color);
  margin-left: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <pseudo color="green" text="I'm from Prop!!"></pseudo>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Is there a way to use SVG as content in a pseudo element :before or :after

From Java

What is the ::content/::slotted pseudo-element and how does it work?

From Dev

Use content of an element in attr() of pseudo-element

From Dev

Getting pseudo-element content in Chrome

From Dev

CSS Transition on the 'content' property of :before pseudo element

From Dev

before pseudo-element covering inner content

From Dev

Change content property of pseudo element within CSS animation

From Dev

Unicode in data-content for pseudo element content

From Dev

styling "pseudo-element" using Dart lang

From Dev

IE 11 considers pseudo content as part of element content

From Dev

Styling Pseudo Elements on hover

From Dev

Styling Kendo Window's content with Bootstrap

From Dev

Vertically/horizontally centering a pseudo element's generated content

From Dev

Retrieve a pseudo element's content property value using JavaScript

From Dev

How do I style part of the pseudo element content: "..."

From Dev

Dynamic styling not working in Angular2

From Dev

automate the content value of pseudo element depending which number the child is

From Dev

Styling an element within another styled as a pseudo element first-line

From Dev

Dynamic styling for pseudo element's content in Vue2.x

From Dev

jQuery dynamic element - styling not being applied

From Dev

Pseudo element content with JQuery

From Dev

CSS Transition on the 'content' property of :before pseudo element

From Dev

styling "pseudo-element" using Dart lang

From Dev

Styling Google Polymer's paper slider element

From Dev

Styling Pseudo Elements on hover

From Dev

CSS: Styling an input element's text (as opposed to the input element itself)?

From Dev

Move content added with pseudo-element:after

From Dev

angular2 pseudo selector styling

From Dev

disable touch on the pseudo content of an element

Related Related

  1. 1

    Is there a way to use SVG as content in a pseudo element :before or :after

  2. 2

    What is the ::content/::slotted pseudo-element and how does it work?

  3. 3

    Use content of an element in attr() of pseudo-element

  4. 4

    Getting pseudo-element content in Chrome

  5. 5

    CSS Transition on the 'content' property of :before pseudo element

  6. 6

    before pseudo-element covering inner content

  7. 7

    Change content property of pseudo element within CSS animation

  8. 8

    Unicode in data-content for pseudo element content

  9. 9

    styling "pseudo-element" using Dart lang

  10. 10

    IE 11 considers pseudo content as part of element content

  11. 11

    Styling Pseudo Elements on hover

  12. 12

    Styling Kendo Window's content with Bootstrap

  13. 13

    Vertically/horizontally centering a pseudo element's generated content

  14. 14

    Retrieve a pseudo element's content property value using JavaScript

  15. 15

    How do I style part of the pseudo element content: "..."

  16. 16

    Dynamic styling not working in Angular2

  17. 17

    automate the content value of pseudo element depending which number the child is

  18. 18

    Styling an element within another styled as a pseudo element first-line

  19. 19

    Dynamic styling for pseudo element's content in Vue2.x

  20. 20

    jQuery dynamic element - styling not being applied

  21. 21

    Pseudo element content with JQuery

  22. 22

    CSS Transition on the 'content' property of :before pseudo element

  23. 23

    styling "pseudo-element" using Dart lang

  24. 24

    Styling Google Polymer's paper slider element

  25. 25

    Styling Pseudo Elements on hover

  26. 26

    CSS: Styling an input element's text (as opposed to the input element itself)?

  27. 27

    Move content added with pseudo-element:after

  28. 28

    angular2 pseudo selector styling

  29. 29

    disable touch on the pseudo content of an element

HotTag

Archive