@@ -45,7 +45,7 @@ import {
4545 WrapTextIcon ,
4646 type LucideIcon ,
4747} from "lucide-react" ;
48- import { Check , Copy , Maximize2 , Minimize2 } from "lucide" ;
48+ import { Check , Copy } from "lucide" ;
4949import {
5050 AuthPreviewOperateScope ,
5151 type AssetResource ,
@@ -88,6 +88,7 @@ import React, {
8888 useCallback ,
8989 memo ,
9090 useEffect ,
91+ useLayoutEffect ,
9192 useMemo ,
9293 useRef ,
9394 useState ,
@@ -201,6 +202,7 @@ import {
201202 resolvePullRequestPreviewTarget ,
202203 useOpenChangeRequestLink ,
203204} from "~/lib/openPullRequestLink" ;
205+ import { observeResize } from "~/lib/observeResize" ;
204206import { useOpenLink } from "../browser/useOpenLink" ;
205207import { writeTextToClipboard } from "../hooks/useCopyToClipboard" ;
206208import { isPreviewAvailableFor } from "../browser/previewRuntime" ;
@@ -559,15 +561,47 @@ function readInitialWordWrapSetting(): boolean {
559561 return getClientSettings ( ) . wordWrap ;
560562}
561563
564+ /**
565+ * Whether any cell is clipped in the unwrapped layout. Only then does the wrap
566+ * toggle change anything, so short tables hide it. Measures with the container
567+ * briefly unwrapped and restores it before the browser paints.
568+ */
569+ function tableHasClippedCells ( container : HTMLElement , table : HTMLTableElement ) : boolean {
570+ const previous = container . dataset . expanded ;
571+ container . dataset . expanded = "false" ;
572+ try {
573+ for ( const row of table . rows ) {
574+ for ( const cell of row . cells ) {
575+ if ( cell . scrollWidth > cell . clientWidth ) return true ;
576+ }
577+ }
578+ return false ;
579+ } finally {
580+ if ( previous === undefined ) delete container . dataset . expanded ;
581+ else container . dataset . expanded = previous ;
582+ }
583+ }
584+
562585function MarkdownTable ( { children, ...props } : React . ComponentProps < "table" > ) {
563586 const containerRef = useRef < HTMLDivElement | null > ( null ) ;
564587 const tableRef = useRef < HTMLTableElement | null > ( null ) ;
565588 const [ expanded , setExpanded ] = useState ( readInitialWordWrapSetting ) ;
589+ const [ hasClippedCells , setHasClippedCells ] = useState ( false ) ;
566590 const [ copied , setCopied ] = useState ( false ) ;
567591 const copiedTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
568- const expandLabel = expanded ? "Collapse table cells " : "Expand table cells" ;
592+ const wrapLabel = expanded ? "Disable cell wrap " : "Wrap long cells" ;
569593 const copyLabel = copied ? "Copied" : "Copy table" ;
570594
595+ // Streaming rows, font loads, and column width changes all resize the table.
596+ useLayoutEffect ( ( ) => {
597+ const container = containerRef . current ;
598+ const table = tableRef . current ;
599+ if ( ! container || ! table ) return ;
600+ const measure = ( ) => setHasClippedCells ( tableHasClippedCells ( container , table ) ) ;
601+ measure ( ) ;
602+ return observeResize ( table , measure ) ;
603+ } , [ ] ) ;
604+
571605 function toggleExpanded ( ) {
572606 const table = tableRef . current ;
573607 if ( ! table ) return ;
@@ -640,23 +674,27 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
640674 </ table >
641675 </ ScrollArea >
642676 < div className = "mt-0.5 flex items-center justify-between select-none" >
643- < Tooltip >
644- < TooltipTrigger
645- render = {
646- < Button
647- type = "button"
648- variant = { expanded ? "secondary" : "ghost-muted" }
649- size = "icon-xs"
650- aria-pressed = { expanded }
651- onClick = { toggleExpanded }
652- aria-label = { expandLabel }
653- />
654- }
655- >
656- < MorphIcon className = "size-3" icon = { expanded ? Minimize2 : Maximize2 } />
657- </ TooltipTrigger >
658- < TooltipPopup side = "top" > { expandLabel } </ TooltipPopup >
659- </ Tooltip >
677+ { hasClippedCells ? (
678+ < Tooltip >
679+ < TooltipTrigger
680+ render = {
681+ < Button
682+ type = "button"
683+ variant = { expanded ? "secondary" : "ghost-muted" }
684+ size = "icon-xs"
685+ aria-pressed = { expanded }
686+ onClick = { toggleExpanded }
687+ aria-label = { wrapLabel }
688+ />
689+ }
690+ >
691+ < WrapTextIcon className = "size-3" />
692+ </ TooltipTrigger >
693+ < TooltipPopup side = "top" > { wrapLabel } </ TooltipPopup >
694+ </ Tooltip >
695+ ) : (
696+ < span />
697+ ) }
660698 < Menu >
661699 < Tooltip >
662700 < TooltipTrigger
0 commit comments