Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): switch the phone layout on the stylesheet query

Yudhi Armyndharisd9dc92810970
diff --git a/dashboard/src/components/Layout.css b/dashboard/src/components/Layout.css
index 717d74449..80c8064a2 100644
--- a/dashboard/src/components/Layout.css
+++ b/dashboard/src/components/Layout.css
@@ -340,6 +340,8 @@
display: none;
}
+/* Layout.tsx switches to the phone layout on this same query (MOBILE_QUERY), so the drawer markup and these styles
+ agree at every width, a fractional one included. */
@media (max-width: 767px) {
.mobile-header {
display: flex;
diff --git a/dashboard/src/components/Layout.css.test.ts b/dashboard/src/components/Layout.css.test.ts
index bf3f8fa41..93ffc33d1 100644
--- a/dashboard/src/components/Layout.css.test.ts
+++ b/dashboard/src/components/Layout.css.test.ts
@@ -46,15 +46,18 @@ const decl = (body: string, prop: string): string | undefined =>
new RegExp(`(?:^|;)\\s*${prop}\\s*:\\s*([^;]+)`).exec(body)?.[1].trim();
const find = (selector: string): Rule[] => all.filter(r => r.selectors.includes(selector));
-// Layout.tsx switches to the mobile layout below this width.
-const mobileBelow = Number(/innerWidth < (\d+)/.exec(tsx)?.[1]);
-
-test('every max-width query in Layout.css switches at the same width as the mobile layout', () => {
- assert.ok(mobileBelow > 0, 'Layout.tsx no longer names its mobile threshold');
- const widths = [...css.matchAll(/max-width:\s*(\d+)px/g)].map(m => Number(m[1]));
- assert.ok(widths.length > 0);
- for (const w of widths)
- assert.equal(w, mobileBelow - 1, `max-width: ${w}px disagrees with innerWidth < ${mobileBelow}`);
+// Layout.tsx switches to the phone layout on this media query.
+const mobileQuery = /const MOBILE_QUERY = '([^']+)'/.exec(tsx)?.[1];
+
+// A width test in script rounds: at 110% zoom innerWidth reads 767 for a 767.3px window, which (max-width: 767px)
+// does not match, and the desktop sidebar was drawn inert. One query for both leaves no width where they disagree.
+test('every media query in Layout.css is the one Layout.tsx switches to the phone layout on', () => {
+ assert.ok(mobileQuery, 'Layout.tsx no longer names its phone query');
+ assert.match(tsx, /matchMedia\(MOBILE_QUERY\)/);
+ assert.doesNotMatch(tsx, /innerWidth/, 'Layout.tsx tests the window width in script again');
+ const queries = [...css.matchAll(/@media\s*([^{]+?)\s*\{/g)].map(m => m[1]);
+ assert.ok(queries.length > 0);
+ for (const query of queries) assert.equal(query, mobileQuery);
});
test('the closed mobile sidebar slides off the right edge under RTL, and opens back in', () => {
diff --git a/dashboard/src/components/Layout.test.ts b/dashboard/src/components/Layout.test.ts
index 594089ac2..2c1752fe8 100644
--- a/dashboard/src/components/Layout.test.ts
+++ b/dashboard/src/components/Layout.test.ts
@@ -12,6 +12,28 @@ let RoleContext: (typeof import('../hooks/useRole.tsx'))['RoleContext'];
// Every URL the layout asked for.
const requested: string[] = [];
+// The window's width in CSS px. A media query sees it as it is; innerWidth is rounded to a whole pixel.
+let cssWidth = 1024;
+const changeListeners = new Set<{ query: string; last: boolean; listener: () => void }>();
+const answers = (query: string): boolean => {
+ const max = /\(max-width:\s*(\d+)px\)/.exec(query);
+ return max !== null && cssWidth <= Number(max[1]);
+};
+/** Resizes the window as a browser does: innerWidth and resize, and a change on every query whose answer flipped. */
+function resizeTo(width: number): void {
+ rtl.act(() => {
+ cssWidth = width;
+ window.innerWidth = Math.round(width);
+ window.dispatchEvent(new window.Event('resize'));
+ for (const entry of changeListeners) {
+ const now = answers(entry.query);
+ if (now === entry.last) continue;
+ entry.last = now;
+ entry.listener();
+ }
+ });
+}
+
/** The layout for a key of `userRole` (admin by default), restricted to selected sessions when `scoped` is set. */
function renderLayout(
scoped = false,
@@ -42,12 +64,17 @@ before(async () => {
const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');
await installJsdomGlobals();
(globalThis as Record<string, unknown>).__APP_VERSION__ = '0.0.0-test';
- // jsdom has no matchMedia; the theme hook reads it for the system preference.
+ // jsdom has no matchMedia; the theme hook reads it for the system preference, and the layout for the phone width.
window.matchMedia = ((query: string) => ({
- matches: false,
+ get matches() {
+ return answers(query);
+ },
media: query,
- addEventListener: () => undefined,
- removeEventListener: () => undefined,
+ addEventListener: (_type: string, listener: () => void) =>
+ changeListeners.add({ query, last: answers(query), listener }),
+ removeEventListener: (_type: string, listener: () => void) => {
+ for (const entry of changeListeners) if (entry.listener === listener) changeListeners.delete(entry);
+ },
})) as unknown as typeof window.matchMedia;
globalThis.fetch = ((input: RequestInfo | URL) => {
requested.push(String(input));
@@ -87,7 +114,6 @@ test('the theme button can return to following the system', () => {
// Collapsing is a desktop preference: the phone drawer has no collapse control, so a flag left over from a wider
// window must not strip its labels, and the window widening again must find the preference where it was left.
test('a collapsed sidebar shows its labels in the phone drawer and collapses again on desktop', () => {
- const width = window.innerWidth;
try {
const { container } = renderLayout();
const sidebar = container.querySelector('aside.sidebar')!;
@@ -95,8 +121,7 @@ test('a collapsed sidebar shows its labels in the phone drawer and collapses aga
assert.ok(sidebar.classList.contains('collapsed'));
assert.equal(container.querySelectorAll('.nav-item span').length, 0, 'the collapsed desktop sidebar shows labels');
- window.innerWidth = 500;
- rtl.fireEvent(window, new window.Event('resize'));
+ resizeTo(500);
assert.ok(sidebar.classList.contains('mobile'));
assert.ok(!sidebar.classList.contains('collapsed'), 'the phone drawer kept the collapsed class');
const labels = Array.from(container.querySelectorAll('.nav-item')).map(item => item.textContent);
@@ -105,11 +130,14 @@ test('a collapsed sidebar shows its labels in the phone drawer and collapses aga
assert.equal(container.querySelector('.logout-btn')!.textContent, 'Logout');
assert.ok(!container.querySelector('main')!.classList.contains('expanded'));
- window.innerWidth = width;
- rtl.fireEvent(window, new window.Event('resize'));
+ // A drawer left open closes when the window widens, so narrowing it again does not bring back the overlay.
+ rtl.fireEvent.click(container.querySelector('.mobile-menu-btn')!);
+ assert.ok(sidebar.classList.contains('open'));
+ resizeTo(1024);
assert.ok(sidebar.classList.contains('collapsed'), 'the desktop preference was lost');
+ assert.ok(!sidebar.classList.contains('open'), 'the drawer stayed open on desktop');
} finally {
- window.innerWidth = width;
+ resizeTo(1024);
}
});
@@ -117,9 +145,8 @@ test('a collapsed sidebar shows its labels in the phone drawer and collapses aga
// not expose whether the drawer was open, and Escape (the one way a keyboard or switch user dismisses an overlay) did
// nothing.
test('the phone menu button names its state and Escape closes the drawer and returns focus to it', () => {
- const width = window.innerWidth;
try {
- window.innerWidth = 390;
+ resizeTo(390);
const { container } = renderLayout();
const sidebar = container.querySelector('aside.sidebar')!;
const button = container.querySelector<HTMLButtonElement>('.mobile-menu-btn')!;
@@ -150,16 +177,15 @@ test('the phone menu button names its state and Escape closes the drawer and ret
rtl.fireEvent.keyDown(document, { key: 'Escape' });
assert.ok(document.activeElement !== button, 'Escape moved focus while the drawer was closed');
} finally {
- window.innerWidth = width;
+ resizeTo(1024);
}
});
// Closed, the phone drawer is only moved off screen: its links stayed in the tab order, so a keyboard user stepped
// through a dozen invisible stops after the menu button. Inert takes them out until the drawer opens.
test('the closed phone drawer is inert, the open one and the desktop sidebar are not', () => {
- const width = window.innerWidth;
try {
- window.innerWidth = 390;
+ resizeTo(390);
const phone = renderLayout();
const drawer = phone.container.querySelector('aside.sidebar')!;
assert.ok(drawer.hasAttribute('inert'), 'the closed drawer is still focusable');
@@ -169,19 +195,41 @@ test('the closed phone drawer is inert, the open one and the desktop sidebar are
assert.ok(drawer.hasAttribute('inert'), 'the drawer is focusable again after closing');
phone.unmount();
- window.innerWidth = 1024;
+ resizeTo(1024);
const desktop = renderLayout();
assert.ok(!desktop.container.querySelector('aside.sidebar')!.hasAttribute('inert'), 'the desktop sidebar is inert');
} finally {
- window.innerWidth = width;
+ resizeTo(1024);
+ }
+});
+
+// Between 767 and 767.5 CSS px (110% zoom of an 844px window, 125% of a 959px one) innerWidth reads 767 while the
+// phone styles' (max-width: 767px) does not match. The page draws the desktop sidebar there, and a width test in
+// script made it inert: no link, language, theme or logout button could be clicked or reached with Tab.
+test('the sidebar follows the phone media query, not the rounded window width', () => {
+ try {
+ resizeTo(767.27);
+ const { container } = renderLayout();
+ assert.equal(window.innerWidth, 767);
+ const sidebar = container.querySelector('aside.sidebar')!;
+ assert.ok(!sidebar.hasAttribute('inert'), 'the desktop sidebar is inert');
+ assert.ok(!sidebar.classList.contains('mobile'), 'the desktop sidebar has the phone class');
+ assert.ok(container.querySelector('.mobile-header') === null, 'a phone header beside the desktop sidebar');
+
+ resizeTo(766.8);
+ assert.equal(window.innerWidth, 767);
+ assert.ok(sidebar.classList.contains('mobile'), 'the phone width kept the desktop sidebar');
+ assert.ok(sidebar.hasAttribute('inert'), 'the closed phone drawer is focusable');
+ assert.ok(container.querySelector('.mobile-header'), 'no phone header at the phone width');
+ } finally {
+ resizeTo(1024);
}
});
// The language menu lives in the drawer footer; Escape dismisses the innermost overlay first.
test('Escape closes the open language menu before the phone drawer', () => {
- const width = window.innerWidth;
try {
- window.innerWidth = 390;
+ resizeTo(390);
const { container } = renderLayout();
const sidebar = container.querySelector('aside.sidebar')!;
rtl.fireEvent.click(container.querySelector('.mobile-menu-btn')!);
@@ -195,7 +243,7 @@ test('Escape closes the open language menu before the phone drawer', () => {
rtl.fireEvent.keyDown(document, { key: 'Escape' });
assert.ok(!sidebar.classList.contains('open'), 'a second Escape left the drawer open');
} finally {
- window.innerWidth = width;
+ resizeTo(1024);
}
});
diff --git a/dashboard/src/components/Layout.tsx b/dashboard/src/components/Layout.tsx
index 26b938174..4cd97616b 100644
--- a/dashboard/src/components/Layout.tsx
+++ b/dashboard/src/components/Layout.tsx
@@ -53,6 +53,11 @@ const allNavItems = [
const themeIcons = { light: Sun, dark: Moon, system: Monitor };
+// The phone layout switches on the query the phone styles in Layout.css sit under. A width test in script disagreed
+// with them at a fractional width: at 110% zoom the window width rounds 767.3 CSS px to 767, which the query does
+// not match, so the desktop sidebar was drawn and made inert.
+const MOBILE_QUERY = '(max-width: 767px)';
+
export function Layout({ onLogout, userRole }: LayoutProps) {
const { t, i18n } = useTranslation();
const { theme, toggleTheme } = useTheme();
@@ -70,7 +75,7 @@ export function Layout({ onLogout, userRole }: LayoutProps) {
const [isCollapsed, setIsCollapsed] = useState(false);
const [isMobileOpen, setIsMobileOpen] = useState(false);
- const [isMobile, setIsMobile] = useState(window.innerWidth < 768);
+ const [isMobile, setIsMobile] = useState(() => window.matchMedia(MOBILE_QUERY).matches);
// Collapsing is a desktop preference: the phone drawer has no collapse control, so it never renders collapsed,
// and the flag stays as it was for when the window widens again.
const collapsed = isCollapsed && !isMobile;
@@ -85,13 +90,13 @@ export function Layout({ onLogout, userRole }: LayoutProps) {
const mobileMenuButtonRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
- const handleResize = () => {
- const mobile = window.innerWidth < 768;
- setIsMobile(mobile);
- if (!mobile) setIsMobileOpen(false);
+ const phone = window.matchMedia(MOBILE_QUERY);
+ const handleChange = () => {
+ setIsMobile(phone.matches);
+ if (!phone.matches) setIsMobileOpen(false);
};
- window.addEventListener('resize', handleResize);
- return () => window.removeEventListener('resize', handleResize);
+ phone.addEventListener('change', handleChange);
+ return () => phone.removeEventListener('change', handleChange);
}, []);
useEffect(() => {