Skip to content

Commit 306d80a

Browse files
committed
fix(home): keep a focused divider's value current through window resizes
The window-resize clamp updated the panel width but not the focused divider's aria-valuemax/valuenow, so assistive tech kept the old bounds until the divider was focused again. The clamp now also reports to the divider while it holds focus, including when the pinned width stays within the new bounds.
1 parent c2e05c4 commit 306d80a

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/home/hooks/use-mothership-resize.test.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,31 @@ describe('useMothershipResize keyboard divider', () => {
198198
await unmount()
199199
})
200200

201+
it('keeps a focused divider reporting the width a window resize clamps it to', async () => {
202+
const { separator, panel, press, unmount } = await mountDivider()
203+
vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
204+
callback(0)
205+
return 1
206+
})
207+
await act(async () => separator.focus())
208+
press('End')
209+
const wideMax = maxPanelWidth(VIEWPORT, CONTAINER)
210+
expect(separator.getAttribute('aria-valuenow')).toBe(String(Math.round(wideMax)))
211+
212+
const narrowViewport = 800
213+
vi.stubGlobal('innerWidth', narrowViewport)
214+
act(() => {
215+
window.dispatchEvent(new Event('resize'))
216+
})
217+
218+
const narrowMax = Math.round(maxPanelWidth(narrowViewport, CONTAINER))
219+
expect(narrowMax).toBeLessThan(Math.round(wideMax))
220+
expect(panel.style.width).toBe(`${maxPanelWidth(narrowViewport, CONTAINER)}px`)
221+
expect(separator.getAttribute('aria-valuemax')).toBe(String(narrowMax))
222+
expect(separator.getAttribute('aria-valuenow')).toBe(String(narrowMax))
223+
await unmount()
224+
})
225+
201226
it('leaves modified and unrelated keys to the rest of the page', async () => {
202227
const { panel, press, unmount } = await mountDivider()
203228
for (const event of [

‎apps/sim/app/workspace/[workspaceId]/home/hooks/use-mothership-resize.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function syncDividerValue(handle: HTMLElement, el: HTMLElement, maxWidth = measu
118118
export function useMothershipResize(desktopScopeId: string) {
119119
const mothershipRef = useRef<HTMLDivElement | null>(null)
120120
const cleanupRef = useRef<(() => void) | null>(null)
121+
const focusedDividerRef = useRef<HTMLElement | null>(null)
121122
const desktopScopeIdRef = useRef(desktopScopeId)
122123
desktopScopeIdRef.current = desktopScopeId
123124

@@ -256,11 +257,14 @@ export function useMothershipResize(desktopScopeId: string) {
256257
const clampWidth = () => {
257258
rafId = null
258259
const el = mothershipRef.current
259-
const pinned = el?.style.width
260-
if (!el || !pinned) return
260+
if (!el) return
261+
const pinned = el.style.width
262+
const divider = focusedDividerRef.current
263+
const reportsToDivider = divider !== null && document.activeElement === divider
264+
if (!pinned && !reportsToDivider) return
261265
const maxWidth = measureMaxWidth(el)
262-
if (Number.parseFloat(pinned) <= maxWidth) return
263-
writeWidthInstantly(el, maxWidth)
266+
if (pinned && Number.parseFloat(pinned) > maxWidth) writeWidthInstantly(el, maxWidth)
267+
if (reportsToDivider) syncDividerValue(divider, el, maxWidth)
264268
}
265269

266270
const handleWindowResize = () => {
@@ -288,8 +292,9 @@ export function useMothershipResize(desktopScopeId: string) {
288292
syncDividerValue(e.currentTarget, el, maxWidth)
289293
}, [])
290294

291-
/** Reports the current width when the divider takes focus. */
295+
/** Reports the current width when the divider takes focus, and while it keeps focus. */
292296
const handleResizeFocus = useCallback((e: React.FocusEvent<HTMLElement>) => {
297+
focusedDividerRef.current = e.currentTarget
293298
const el = mothershipRef.current
294299
if (el) syncDividerValue(e.currentTarget, el)
295300
}, [])

0 commit comments

Comments
 (0)