Skip to content

Commit 4403e20

Browse files
committed
fix(sidebar): clamp the default width fallback to the viewport maximum
1 parent 14dc49d commit 4403e20

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

apps/sim/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
153153
var expandedWidth =
154154
typeof width === 'number' && isFinite(width)
155155
? Math.min(Math.max(width, 224), maxSidebarWidth)
156-
: defaultSidebarWidth;
156+
: Math.min(defaultSidebarWidth, maxSidebarWidth);
157157
document.documentElement.style.setProperty(
158158
'--sidebar-expanded-width',
159159
expandedWidth + 'px'

apps/sim/stores/sidebar/store.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ describe('sidebar width CSS variables', () => {
102102

103103
expect(widthVars().expanded).toBe(`${SIDEBAR_WIDTH.MIN}px`)
104104
})
105+
106+
it('clamps the default fallback to a viewport maximum below the default', () => {
107+
const innerWidth = window.innerWidth
108+
window.innerWidth = 800
109+
try {
110+
useSidebarStore.setState({ isCollapsed: false, sidebarWidth: Number.NaN })
111+
112+
useSidebarStore.getState().syncWidth()
113+
114+
expect(widthVars().expanded).toBe(`${getMaxSidebarWidth(800)}px`)
115+
expect(getMaxSidebarWidth(800)).toBeLessThan(SIDEBAR_WIDTH.DEFAULT)
116+
} finally {
117+
window.innerWidth = innerWidth
118+
}
119+
})
105120
})
106121

107122
describe('getMaxSidebarWidth', () => {

apps/sim/stores/sidebar/store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export function getMaxSidebarWidth(viewportWidth: number): number {
2121

2222
/** Clamps an expanded sidebar width into the valid range for the current viewport. */
2323
function clampSidebarWidth(width: number): number {
24-
if (!Number.isFinite(width)) return SIDEBAR_WIDTH.DEFAULT
24+
const target = Number.isFinite(width) ? width : SIDEBAR_WIDTH.DEFAULT
2525
const max =
2626
typeof window === 'undefined' ? Number.POSITIVE_INFINITY : getMaxSidebarWidth(window.innerWidth)
27-
return Math.min(Math.max(width, SIDEBAR_WIDTH.MIN), max)
27+
return Math.min(Math.max(target, SIDEBAR_WIDTH.MIN), max)
2828
}
2929

3030
/**

0 commit comments

Comments
 (0)