Skip to content

Commit 512165c

Browse files
authored
fix(tui): share dialog width instead of one resize listener per row (#56234)
1 parent 65932e8 commit 512165c

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎packages/tui/src/ui/dialog-select.tsx‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
250250
// Under new designs an overflowing list gives its last row to a "…" indicator.
251251
const overflows = createMemo(() => newDesigns() && rows() > limit())
252252
const height = createMemo(() => Math.min(rows(), limit()) - (overflows() ? 1 : 0))
253+
// Computed once here so per-row details do not each call useTerminalDimensions(),
254+
// which registers one renderer resize listener per mounted row.
255+
const detailsWidth = createMemo(() =>
256+
dialogSelectContentWidth(Math.min(dialogWidth(dialog.size), dimensions().width)),
257+
)
253258
const [moreBelow, setMoreBelow] = createSignal(false)
254259
function updateMoreBelow() {
255260
if (!scroll || scroll.isDestroyed) return
@@ -778,7 +783,7 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
778783
gutter={option.gutter}
779784
margin={option.margin}
780785
/>
781-
<DialogSelectDetails details={option.details} />
786+
<DialogSelectDetails details={option.details} width={detailsWidth()} />
782787
</box>
783788
)
784789
}}
@@ -899,11 +904,8 @@ function DialogSelectRow(props: {
899904
)
900905
}
901906

902-
function DialogSelectDetails(props: { details?: string[] }) {
907+
function DialogSelectDetails(props: { details?: string[]; width: number }) {
903908
const theme = useTheme().surface("dialog")
904-
const dialog = useDialog()
905-
const dimensions = useTerminalDimensions()
906-
const width = createMemo(() => dialogSelectContentWidth(Math.min(dialogWidth(dialog.size), dimensions().width)))
907909
const newDesigns = useNewDesigns()
908910
return (
909911
<For each={props.details}>
@@ -913,7 +915,7 @@ function DialogSelectDetails(props: { details?: string[] }) {
913915
paddingRight={DIALOG_LIST_INSET - (newDesigns() ? 0 : 1)}
914916
>
915917
<text fg={theme.text.muted} wrapMode="none">
916-
{Locale.truncateMiddle(detail, Math.max(1, width()))}
918+
{Locale.truncateMiddle(detail, Math.max(1, props.width))}
917919
</text>
918920
</box>
919921
)}

‎packages/tui/test/cli/tui/dialog-select.test.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,25 @@ test("shows no-match and still closes after a flat filter goes empty", async ()
627627
}
628628
})
629629

630+
test("dialog rows do not add renderer resize listeners", async () => {
631+
await using tmp = await tmpdir()
632+
const options = ["first", "second", "third"].map((value) => ({ title: value, value }))
633+
const select = await mountSelect(tmp.path, options)
634+
635+
try {
636+
const count = () =>
637+
(select.app.renderer as unknown as { listenerCount(event: string): number }).listenerCount("resize")
638+
const before = count()
639+
// Mounting 30 rows must not add one resize listener per row (Node warns
640+
// past 10 listeners on one emitter).
641+
select.replaceOptions(Array.from({ length: 30 }, (_, index) => ({ title: `Item ${index}`, value: `item-${index}` })))
642+
await select.app.waitForVisualIdle()
643+
expect(count()).toBe(before)
644+
} finally {
645+
select.app.renderer.destroy()
646+
}
647+
})
648+
630649
test("keeps the first row selected when current is only a marker", async () => {
631650
await using tmp = await tmpdir()
632651
const project = { title: "project", value: "project" }

0 commit comments

Comments
 (0)