@@ -206,6 +206,7 @@ import {
206206 resolvePullRequestPreviewTarget ,
207207 useOpenChangeRequestLink ,
208208} from "~/lib/openPullRequestLink" ;
209+ import { observeResize } from "~/lib/observeResize" ;
209210import { useOpenLink } from "../browser/useOpenLink" ;
210211import { writeTextToClipboard } from "../hooks/useCopyToClipboard" ;
211212import { isPreviewAvailableFor } from "../browser/previewRuntime" ;
@@ -535,15 +536,59 @@ function readInitialWordWrapSetting(): boolean {
535536 return getClientSettings ( ) . wordWrap ;
536537}
537538
539+ /**
540+ * Whether any cell is clipped in the unwrapped layout. Only then does the wrap
541+ * toggle change anything, so short tables hide it. Measures with the container
542+ * briefly unwrapped and restores it before the browser paints.
543+ */
544+ function tableHasClippedCells ( container : HTMLElement , table : HTMLTableElement ) : boolean {
545+ const viewport = table . closest < HTMLElement > ( '[data-slot="scroll-area-viewport"]' ) ;
546+ const scrollLeft = viewport ?. scrollLeft ?? 0 ;
547+ const previous = container . dataset . expanded ;
548+ container . dataset . expanded = "false" ;
549+ try {
550+ for ( const row of table . rows ) {
551+ for ( const cell of row . cells ) {
552+ if ( cell . scrollWidth > cell . clientWidth ) return true ;
553+ }
554+ }
555+ return false ;
556+ } finally {
557+ if ( previous === undefined ) delete container . dataset . expanded ;
558+ else container . dataset . expanded = previous ;
559+ // The unwrapped layout can be narrower and clamp a sideways scroll.
560+ if ( viewport && scrollLeft > 0 ) viewport . scrollLeft = scrollLeft ;
561+ }
562+ }
563+
538564function MarkdownTable ( { children, ...props } : React . ComponentProps < "table" > ) {
539565 const containerRef = useRef < HTMLDivElement | null > ( null ) ;
540566 const tableRef = useRef < HTMLTableElement | null > ( null ) ;
541567 const [ expanded , setExpanded ] = useState ( readInitialWordWrapSetting ) ;
568+ const [ hasClippedCells , setHasClippedCells ] = useState ( false ) ;
542569 const [ copied , setCopied ] = useState ( false ) ;
543570 const copiedTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
544571 const expandLabel = expanded ? "Collapse table cells" : "Expand table cells" ;
545572 const copyLabel = copied ? "Copied" : "Copy table" ;
546573
574+ const measureClippedCells = ( ) => {
575+ const container = containerRef . current ;
576+ const table = tableRef . current ;
577+ if ( ! container || ! table ) return ;
578+ setHasClippedCells ( tableHasClippedCells ( container , table ) ) ;
579+ } ;
580+
581+ // A streamed cell can grow until it clips while the table keeps its size,
582+ // so remeasure after every render, not only on resize.
583+ useLayoutEffect ( measureClippedCells ) ;
584+
585+ // Column width changes can clip a cell without rendering this table.
586+ useLayoutEffect ( ( ) => {
587+ const table = tableRef . current ;
588+ if ( ! table ) return ;
589+ return observeResize ( table , measureClippedCells ) ;
590+ } , [ measureClippedCells ] ) ;
591+
547592 function toggleExpanded ( ) {
548593 const table = tableRef . current ;
549594 if ( ! table ) return ;
@@ -616,23 +661,27 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
616661 </ table >
617662 </ ScrollArea >
618663 < div className = "mt-0.5 flex items-center justify-between select-none" >
619- < Tooltip >
620- < TooltipTrigger
621- render = {
622- < Button
623- type = "button"
624- variant = { expanded ? "secondary" : "ghost-muted" }
625- size = "icon-xs"
626- aria-pressed = { expanded }
627- onClick = { toggleExpanded }
628- aria-label = { expandLabel }
629- />
630- }
631- >
632- < MorphIcon className = "size-3" icon = { expanded ? Minimize2 : Maximize2 } />
633- </ TooltipTrigger >
634- < TooltipPopup side = "top" > { expandLabel } </ TooltipPopup >
635- </ Tooltip >
664+ { hasClippedCells ? (
665+ < Tooltip >
666+ < TooltipTrigger
667+ render = {
668+ < Button
669+ type = "button"
670+ variant = { expanded ? "secondary" : "ghost-muted" }
671+ size = "icon-xs"
672+ aria-pressed = { expanded }
673+ onClick = { toggleExpanded }
674+ aria-label = { expandLabel }
675+ />
676+ }
677+ >
678+ < MorphIcon className = "size-3" icon = { expanded ? Minimize2 : Maximize2 } />
679+ </ TooltipTrigger >
680+ < TooltipPopup side = "top" > { expandLabel } </ TooltipPopup >
681+ </ Tooltip >
682+ ) : (
683+ < span />
684+ ) }
636685 < Menu >
637686 < Tooltip >
638687 < TooltipTrigger
0 commit comments