@@ -567,6 +567,8 @@ function readInitialWordWrapSetting(): boolean {
567567 * briefly unwrapped and restores it before the browser paints.
568568 */
569569function tableHasClippedCells ( container : HTMLElement , table : HTMLTableElement ) : boolean {
570+ const viewport = table . closest < HTMLElement > ( '[data-slot="scroll-area-viewport"]' ) ;
571+ const scrollLeft = viewport ?. scrollLeft ?? 0 ;
570572 const previous = container . dataset . expanded ;
571573 container . dataset . expanded = "false" ;
572574 try {
@@ -579,6 +581,8 @@ function tableHasClippedCells(container: HTMLElement, table: HTMLTableElement):
579581 } finally {
580582 if ( previous === undefined ) delete container . dataset . expanded ;
581583 else container . dataset . expanded = previous ;
584+ // The unwrapped layout can be narrower and clamp a sideways scroll.
585+ if ( viewport && scrollLeft > 0 ) viewport . scrollLeft = scrollLeft ;
582586 }
583587}
584588
@@ -589,24 +593,49 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
589593 const [ hasClippedCells , setHasClippedCells ] = useState ( false ) ;
590594 const [ copied , setCopied ] = useState ( false ) ;
591595 const copiedTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
596+ const wrapButtonRef = useRef < HTMLButtonElement | null > ( null ) ;
597+ const copyButtonRef = useRef < HTMLButtonElement | null > ( null ) ;
592598 const wrapLabel = expanded ? "Disable cell wrap" : "Wrap long cells" ;
593599 const copyLabel = copied ? "Copied" : "Copy table" ;
594600
595601 const measureClippedCells = useCallback ( ( ) => {
596602 const container = containerRef . current ;
597603 const table = tableRef . current ;
598- if ( container && table ) setHasClippedCells ( tableHasClippedCells ( container , table ) ) ;
604+ if ( ! container || ! table ) return ;
605+ const clipped = tableHasClippedCells ( container , table ) ;
606+ // Hiding the focused wrap button would drop keyboard focus to the page.
607+ if ( ! clipped && wrapButtonRef . current === document . activeElement ) {
608+ copyButtonRef . current ?. focus ( ) ;
609+ }
610+ setHasClippedCells ( clipped ) ;
599611 } , [ ] ) ;
600612
601613 // A streamed cell can grow until it clips while the table keeps its size,
602614 // so remeasure after every render, not only on resize.
603615 useLayoutEffect ( measureClippedCells ) ;
604616
605- // Font loads and column width changes resize the table without a render.
617+ // Column width changes, late fonts, and images settling inside cells can
618+ // clip a cell without rendering this table.
606619 useLayoutEffect ( ( ) => {
607620 const table = tableRef . current ;
608621 if ( ! table ) return ;
609- return observeResize ( table , measureClippedCells ) ;
622+ let frame = 0 ;
623+ const measureNextFrame = ( ) => {
624+ cancelAnimationFrame ( frame ) ;
625+ frame = requestAnimationFrame ( measureClippedCells ) ;
626+ } ;
627+ // Media load and error events don't bubble, so listen in the capture phase.
628+ table . addEventListener ( "load" , measureNextFrame , true ) ;
629+ table . addEventListener ( "error" , measureNextFrame , true ) ;
630+ document . fonts ?. addEventListener ( "loadingdone" , measureNextFrame ) ;
631+ const stopObserving = observeResize ( table , measureClippedCells ) ;
632+ return ( ) => {
633+ cancelAnimationFrame ( frame ) ;
634+ table . removeEventListener ( "load" , measureNextFrame , true ) ;
635+ table . removeEventListener ( "error" , measureNextFrame , true ) ;
636+ document . fonts ?. removeEventListener ( "loadingdone" , measureNextFrame ) ;
637+ stopObserving ( ) ;
638+ } ;
610639 } , [ measureClippedCells ] ) ;
611640
612641 function toggleExpanded ( ) {
@@ -686,6 +715,7 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
686715 < TooltipTrigger
687716 render = {
688717 < Button
718+ ref = { wrapButtonRef }
689719 type = "button"
690720 variant = { expanded ? "secondary" : "ghost-muted" }
691721 size = "icon-xs"
@@ -709,6 +739,7 @@ function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
709739 < MenuTrigger
710740 render = {
711741 < Button
742+ ref = { copyButtonRef }
712743 type = "button"
713744 variant = "ghost-muted"
714745 size = "icon-xs"
0 commit comments