UNPKG

vuestic-ui

Version:
1 lines 2.55 kB
{"version":3,"file":"useLineHeight.mjs","sources":["../../../../../../src/components/va-textarea/composables/useLineHeight.ts"],"sourcesContent":["import { Ref, ref, watch } from 'vue'\nimport { useResizeObserver } from '../../../composables'\n\nconst makeTextElement = (textarea: HTMLTextAreaElement) => {\n const div = document.createElement('div')\n div.style.position = 'absolute'\n div.style.top = '0'\n div.style.left = '0'\n div.style.width = 'auto'\n const { font } = window.getComputedStyle(textarea)\n div.style.font = font\n div.textContent = 'Vuestic'\n div.style.zIndex = '-1'\n div.style.pointerEvents = 'none'\n div.style.opacity = '0'\n div.ariaHidden = 'true'\n div.innerText = textarea.value\n\n return div\n}\n\n/**\n * Used to get textarea textHeight. Uses resizeObserver and fake div, because\n * font family may be loaded after the component is mounted.\n */\nexport const useTextHeight = (textarea: Ref<HTMLTextAreaElement | undefined>, text: Ref<string | number>) => {\n const textElement = ref<HTMLElement>()\n const textHeight = ref<number>()\n\n watch(textarea, (el) => {\n if (el) {\n textElement.value = makeTextElement(el)\n textarea.value?.parentElement?.appendChild(textElement.value)\n }\n })\n\n useResizeObserver(textElement, (newElement) => {\n if (!newElement || !textarea.value) { return }\n\n textHeight.value = newElement[0].contentRect.height\n })\n\n watch(text, (newText) => {\n if (!textElement.value) { return }\n textElement.value.innerText = String(newText)\n // Add space to correctly handle new lines with br\n textElement.value.innerHTML += '&nbsp;;'\n })\n\n return textHeight\n}\n"],"names":[],"mappings":";;AAGA,MAAM,kBAAkB,CAAC,aAAkC;AACnD,QAAA,MAAM,SAAS,cAAc,KAAK;AACxC,MAAI,MAAM,WAAW;AACrB,MAAI,MAAM,MAAM;AAChB,MAAI,MAAM,OAAO;AACjB,MAAI,MAAM,QAAQ;AAClB,QAAM,EAAE,KAAS,IAAA,OAAO,iBAAiB,QAAQ;AACjD,MAAI,MAAM,OAAO;AACjB,MAAI,cAAc;AAClB,MAAI,MAAM,SAAS;AACnB,MAAI,MAAM,gBAAgB;AAC1B,MAAI,MAAM,UAAU;AACpB,MAAI,aAAa;AACjB,MAAI,YAAY,SAAS;AAElB,SAAA;AACT;AAMa,MAAA,gBAAgB,CAAC,UAAgD,SAA+B;AAC3G,QAAM,cAAc;AACpB,QAAM,aAAa;AAEb,QAAA,UAAU,CAAC,OAAO;;AACtB,QAAI,IAAI;AACM,kBAAA,QAAQ,gBAAgB,EAAE;AACtC,2BAAS,UAAT,mBAAgB,kBAAhB,mBAA+B,YAAY,YAAY;AAAA,IACzD;AAAA,EAAA,CACD;AAEiB,oBAAA,aAAa,CAAC,eAAe;AAC7C,QAAI,CAAC,cAAc,CAAC,SAAS,OAAO;AAAE;AAAA,IAAO;AAE7C,eAAW,QAAQ,WAAW,CAAC,EAAE,YAAY;AAAA,EAAA,CAC9C;AAEK,QAAA,MAAM,CAAC,YAAY;AACnB,QAAA,CAAC,YAAY,OAAO;AAAE;AAAA,IAAO;AACrB,gBAAA,MAAM,YAAY,OAAO,OAAO;AAE5C,gBAAY,MAAM,aAAa;AAAA,EAAA,CAChC;AAEM,SAAA;AACT;"}