Skip to content

Commit f06d1a6

Browse files
claude0ski
authored andcommitted
fix(webapp): address review feedback for MiddleTruncate component
- Changed interface to type alias to match repo TS style conventions - Added re-measurement after enforcing minChars to prevent overflow in narrow containers - Changed min-w-[360px] to min-w-full to prevent layout overflow when popover width is constrained on narrow viewports https://claude.ai/code/session_01KrFXrmK7jz9sWTcS8pMtXL
1 parent 22db328 commit f06d1a6

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

apps/webapp/app/components/primitives/MiddleTruncate.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { useRef, useState, useLayoutEffect, useCallback } from "react";
22
import { cn } from "~/utils/cn";
33
import { SimpleTooltip } from "./Tooltip";
44

5-
interface MiddleTruncateProps {
5+
type MiddleTruncateProps = {
66
text: string;
77
className?: string;
8-
}
8+
};
99

1010
/**
1111
* A component that truncates text in the middle, showing the beginning and end.
@@ -84,13 +84,26 @@ export function MiddleTruncate({ text, className }: MiddleTruncateProps) {
8484

8585
// Ensure minimum characters on each side for readability
8686
const minChars = 4;
87+
const prevStartChars = startChars;
88+
const prevEndChars = endChars;
89+
8790
if (startChars < minChars && text.length > minChars * 2 + 1) {
8891
startChars = minChars;
8992
}
9093
if (endChars < minChars && text.length > minChars * 2 + 1) {
9194
endChars = minChars;
9295
}
9396

97+
// Re-measure after enforcing minChars to prevent overflow
98+
if (startChars !== prevStartChars || endChars !== prevEndChars) {
99+
measure.textContent = text.slice(0, startChars) + ellipsis + text.slice(-endChars);
100+
if (measure.offsetWidth > targetWidth) {
101+
// Revert to previous values if minChars enforcement causes overflow
102+
startChars = prevStartChars;
103+
endChars = prevEndChars;
104+
}
105+
}
106+
94107
// If combined chars would exceed text length, show full text
95108
if (startChars + endChars >= text.length) {
96109
setDisplayText(text);
@@ -124,7 +137,7 @@ export function MiddleTruncate({ text, className }: MiddleTruncateProps) {
124137
const content = (
125138
<span
126139
ref={containerRef}
127-
className={cn("block", isTruncated && "min-w-[360px]", className)}
140+
className={cn("block", isTruncated && "min-w-full", className)}
128141
>
129142
{/* Hidden span for measuring text width */}
130143
<span

0 commit comments

Comments
 (0)