Skip to content

Commit 43070a4

Browse files
committed
fix(ui): remeasure overflow text changes
1 parent a0c5ed5 commit 43070a4

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

packages/emcn/src/components/overflow-text/overflow-text.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,21 @@ describe('OverflowText', () => {
137137

138138
expect(resizeObserverCount).toBe(1)
139139
})
140+
141+
it('remeasures when an existing label changes', () => {
142+
act(() => root.render(<OverflowText label='Short' />))
143+
const label = host.querySelector<HTMLElement>('span')
144+
if (!label) throw new Error('Overflow label did not render')
145+
146+
setWidths(label, 100, 60)
147+
expect(label.className).not.toContain('mask-image:linear-gradient')
148+
149+
Object.defineProperty(label, 'scrollWidth', { configurable: true, value: 180 })
150+
act(() => root.render(<OverflowText label='A newly long workflow name' />))
151+
expect(label.className).toContain('mask-image:linear-gradient')
152+
153+
Object.defineProperty(label, 'scrollWidth', { configurable: true, value: 60 })
154+
act(() => root.render(<OverflowText label='Short again' />))
155+
expect(label.className).not.toContain('mask-image:linear-gradient')
156+
})
140157
})

packages/emcn/src/components/overflow-text/overflow-text.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const OverflowText = memo(function OverflowText({
3939
className,
4040
showWhen,
4141
}: OverflowTextProps) {
42-
const { ref: textRef, node, isOverflowing } = useIsOverflowing<HTMLSpanElement>()
42+
const { ref: textRef, node, isOverflowing } = useIsOverflowing<HTMLSpanElement>(label)
4343
const { state, handlers } = useFloatingTooltip(() => {
4444
const element = node.current
4545
if (!element || label.length === 0) return false

packages/emcn/src/components/tooltip/tooltip.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,20 @@ function unobserveOverflow(element: Element | null) {
254254
}
255255

256256
/**
257-
* Tracks whether an element's text is horizontally clipped, re-measuring via a
258-
* shared `ResizeObserver` (or window resizes when the API is unavailable).
257+
* Tracks whether an element's text is horizontally clipped, re-measuring when
258+
* `measurementKey` changes and via a shared `ResizeObserver` (or window resizes
259+
* when the API is unavailable).
259260
*
260261
* Returns a callback `ref` to attach to the element — the observer follows the
261262
* element across mount, unmount, and reassignment, so it is safe to use on
262263
* conditionally rendered children. `node` is a stable ref for reading the
263264
* current element (e.g. for live measurements in event handlers).
265+
*
266+
* @param measurementKey - Value whose changes may alter the element's rendered width.
264267
*/
265-
export function useIsOverflowing<T extends HTMLElement = HTMLElement>(): {
268+
export function useIsOverflowing<T extends HTMLElement = HTMLElement>(
269+
measurementKey?: unknown
270+
): {
266271
ref: (node: T | null) => void
267272
node: React.RefObject<T | null>
268273
isOverflowing: boolean
@@ -297,6 +302,10 @@ export function useIsOverflowing<T extends HTMLElement = HTMLElement>(): {
297302
}
298303
}, [measure])
299304

305+
React.useLayoutEffect(() => {
306+
measure()
307+
}, [measure, measurementKey])
308+
300309
return { ref, node: nodeRef, isOverflowing }
301310
}
302311

0 commit comments

Comments
 (0)