use-pretext
v0.1.0
Published
Vue 3 composables for @chenglou/pretext text measurement and line layout.
Maintainers
Readme
use-pretext
Vue 3 composables for @chenglou/pretext.
This package wraps Pretext with Vue 3 composables for:
- measuring multiline text height from a reactive width or DOM target
- reading wrapped line output for custom renderers
- using the same text layout logic in DOM and canvas flows
Install
npm install vue @chenglou/pretext use-pretextusePretext
Use this when you want the measured block height and line count for reactive text.
import { ref } from 'vue'
import { usePretext } from 'use-pretext'
const target = ref<HTMLElement | null>(null)
const text = ref('AGI 春天到了. بدأت الرحلة')
const layout = usePretext({
target,
text,
lineHeight: 24,
font: {
size: '16px',
family: 'Inter',
weight: 500,
},
})<template>
<div ref="target">
Height: {{ layout.height }}
Lines: {{ layout.lineCount }}
</div>
</template>You can also pass a reactive width directly instead of a target.
usePretextLines
Use this when you also need the actual wrapped lines.
import { computed, ref } from 'vue'
import { usePretextLines } from 'use-pretext'
const width = ref(320)
const text = ref('Manual rendering is easier when you already know the line breaks.')
const layout = usePretextLines({
width,
text,
lineHeight: 22,
font: '16px "Helvetica Neue"',
})
const labels = computed(() => layout.lines.value.map((line) => line.text))Demo
This repo includes a small Vite demo app under demo/.
npm install
npm run demoIt shows:
usePretext(...)with a real observed DOM containerusePretextLines(...)with explicit width control and per-line output
Each use case is split into its own page in the demo so you can inspect them independently.
Options
text:string | Ref<string> | () => stringfont: Pretext font string or an object withsize,family, and optionalstyle,variant,weight,stretchlineHeight: number used forpretext.layout(...)width: optional explicit widthtarget: optional element ref to observe withResizeObserverwhiteSpace:'normal' | 'pre-wrap'locale: forwarded topretext.setLocale(...)observeResize: defaults totrue
Notes
- If both
widthandtargetare provided,widthwins. localeis global inside Pretext. Changing it clears Pretext's shared cache.
Publishing
This repo is set up to be published as an npm package.
Before publishing:
- Confirm that the package name
use-pretextis available on npm, or change"name"in package.json to a scoped name such as@your-scope/use-pretext. - Install dependencies and run:
npm install
npm run test
npm run check
npm run build- Log in to npm:
npm login- Publish:
npm publishIf you switch to a scoped package name and want it public, publish with:
npm publish --access public