@@ -16,7 +16,8 @@ export function MiddleTruncate({
1616 showTitle ?: boolean ;
1717 variant ?: "default" | "cap-aligned" ;
1818} ) {
19- const split = splitForMiddleTruncate ( value , tail ) ;
19+ const raw = splitForMiddleTruncate ( value , tail ) ;
20+ const split = raw && collapseSegments ( raw ) ;
2021 const headRef = useRef < HTMLSpanElement > ( null ) ;
2122 const head = split ?. head ;
2223 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- fits the head span's DOM text and refits it through a ResizeObserver
@@ -42,10 +43,12 @@ export function MiddleTruncate({
4243 { split ? (
4344 < >
4445 { /* The tail carries the ellipsis so it sits flush; a CSS ellipsis leaves a ragged gap. */ }
45- < span ref = { headRef } className = "peer min-w-0 overflow-hidden" >
46+ < span ref = { headRef } className = "peer min-w-0 overflow-hidden whitespace-pre " >
4647 { split . head }
4748 </ span >
48- < span className = "shrink-0 peer-data-overflow:before:content-['…']" > { split . tail } </ span >
49+ < span className = "shrink-0 whitespace-pre peer-data-overflow:before:content-['…']" >
50+ { split . tail }
51+ </ span >
4952 </ >
5053 ) : (
5154 // Keyed so React does not reuse the head node and its imperative clip.
@@ -89,6 +92,16 @@ function fitHead(head: HTMLElement) {
8992 if ( tail instanceof HTMLElement ) tail . style . translate = slack > 0 ? `-${ slack } px 0` : "" ;
9093}
9194
95+ // The segments render pre so the space at the cut survives, so collapse here what CSS
96+ // would have (not \s: em and no-break spaces are kept). Split first, so the tail length
97+ // still counts the raw value.
98+ function collapseSegments ( split : { head : string ; tail : string } ) {
99+ const collapse = ( text : string ) => text . replace ( / [ \t \n \r \f ] + / g, " " ) ;
100+ const head = collapse ( split . head ) . replace ( / ^ / , "" ) ;
101+ const tail = collapse ( split . tail ) . replace ( / $ / , "" ) ;
102+ return { head, tail : head . endsWith ( " " ) ? tail . replace ( / ^ / , "" ) : tail } ;
103+ }
104+
92105const DEFAULT_TAIL = 10 ;
93106const MAX_SEGMENT_TAIL = 16 ;
94107const MIN_WORD_TAIL = 4 ;
0 commit comments