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 compatibility to Vue.js, Angular and React #31

Open
patrickpissurno opened this issue Mar 26, 2018 · 7 comments
Open

Add compatibility to Vue.js, Angular and React #31

patrickpissurno opened this issue Mar 26, 2018 · 7 comments

Comments

@patrickpissurno
Copy link

patrickpissurno commented Mar 26, 2018

The problem is: this library replaces the original elements in a way that breaks those frameworks.
Please add support for them as they're quite popular nowadays

@patrickpissurno
Copy link
Author

At the moment, a workaround I've found is to invoke textFit() in a given element before its template gets replaced by Vue.js. Works fine. Then you can call textFit() whenever you'd like to, and it won't break Vue.js's templating.

Not tested with Angular and React.

@loke-dev
Copy link

loke-dev commented Aug 6, 2018

Trying to get it to work with Vue.js. This turned out to be a much larger problem than i expected!

@gcleaves
Copy link

Trying to get it to work with Vue.js. This turned out to be a much larger problem than i expected!

did you get it to work? how?

@Sasi-007
Copy link

Sasi-007 commented May 6, 2020

Guys anyone please tell me how to install or use this library in scratch Html, CSS, Js webpage

@lhermann
Copy link

I managed to create a dirty workaround in Vue. Basically it uses a hidden clone to fit the text and then just extracts the font size. The class names are from https://tailwindcss.com

<template>
  <div ref="container" class="relative">
    <div
      ref="original"
      class="absolute z-10 top-0 left-0 w-full h-full"
      :style="{ fontSize: fontSize }"
    >
      <slot />
    </div>
    <div
      :id="_id"
      class="relative z-0 opacity-0 h-full"
    ></div>
  </div>
</template>

<script>
import textfit from 'textfit'

export default {
  props: {
    options: Object,
    refit: Number,
  },
  data () {
    return {
      fontSize: '0px',
    }
  },
  computed: {
    _options () {
      return {
        maxFontSize: 9999,
        ...this.options,
      }
    },
    _id () {
      return 'textfit-' + this._uid
    },
  },
  watch: {
    refit () {
      this.fit()
    },
  },
  mounted () {
    this.fit()
  },
  methods: {
    fit () {
      // this is a dirty workaround because textfit() replaces the original element thus breaking vue
      const el = this.$refs.container.querySelector(`#${this._id}`)
      el.innerHTML = this.$refs.original.innerHTML
      textfit(el, this._options)
      this.fontSize = el.querySelector('span.textFitted')?.style.fontSize
    },
  },
}
</script>

@pcherna
Copy link

pcherna commented Feb 28, 2021

I spent a bit of time trying to figure this out for Vue 3. One problem is making sure that the text to fit can be reactive (which is tricky because textfit creates an inner element in the DOM to hold the actual text, which bypasses and thus breaks Vue's dominion over the DOM). But the bigger problem is finding the right time to run textfit() for elements that use v-show and start off hidden. At the times I can hook, the geometry isn't yet there to do the fitting. See https://github.com/pcherna/vue-textfit-experiment for my work in progress, any insights would be appreciated.

@devellopah
Copy link

For me the binded style works pretty well in vue 2.

<template>
<div
  ref="number"
  :style="{height: height + 'px'}"
>
  {{ number }}
</div>
</template>

<script>
import textfit from 'textfit'

export default {
  props: ['number', 'height'],
  mounted() {
    textfit(this.$refs.number, { minFontSize: 20, maxFontSize: 200,})
  }
}
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants