get-max-width.ts
1 import { LayoutEdge, type LayoutNode } from './layout/node.js' 2 3 /** 4 * Returns the yoga node's content width (computed width minus padding and 5 * border). 6 * 7 * Warning: can return a value WIDER than the parent container. In a 8 * column-direction flex parent, width is the cross axis — align-items: 9 * stretch never shrinks children below their intrinsic size, so the text 10 * node overflows (standard CSS behavior). Yoga measures leaf nodes in two 11 * passes: the AtMost pass determines width, the Exactly pass determines 12 * height. getComputedWidth() reflects the wider AtMost result while 13 * getComputedHeight() reflects the narrower Exactly result. Callers that 14 * use this for wrapping should clamp to actual available screen space so 15 * the rendered line count stays consistent with the layout height. 16 */ 17 const getMaxWidth = (yogaNode: LayoutNode): number => { 18 return ( 19 yogaNode.getComputedWidth() - 20 yogaNode.getComputedPadding(LayoutEdge.Left) - 21 yogaNode.getComputedPadding(LayoutEdge.Right) - 22 yogaNode.getComputedBorder(LayoutEdge.Left) - 23 yogaNode.getComputedBorder(LayoutEdge.Right) 24 ) 25 } 26 27 export default getMaxWidth