Capítulo 5 de 20
Pretext can estimate wrapped text height before DOM measurement, while TanStack Virtual continues to own scrolling, range calculation, positioning, and scroll-to behavior. It is suited to text-heavy rows with predictable typography.
prepare(): Expensive text/style preparation that should be cached by text and style inputs.layout(): Width-dependent layout that should be rerun when content width changes.measure() after document.fonts.ready.import { clearCache, layout, prepare } from '@chenglou/pretext'
const font = '14px Arial'
const lineHeight = 20
const preparedCache = new Map<string, ReturnType<typeof prepare>>()
function estimateRowHeight(row, contentWidth: number) {
const key = `${row.id}:${font}:${row.text}`
let prepared = preparedCache.get(key)
if (!prepared) {
prepared = prepare(row.text, font, { whiteSpace: 'pre-wrap', letterSpacing: 0 })
preparedCache.set(key, prepared)
}
const text = layout(prepared, contentWidth, lineHeight)
return Math.max(lineHeight, text.height) + 24
}
layout(), not prepare(), for ordinary width changes.measureElement or resizeItem for media and mixed-content rows.measure() and resizeItem() are the reset and external sizing tools.