feat: render settings as a shallow desktop modal
This commit is contained in:
5 files changed
+157
-4
No files matched your search
@@ -1,11 +1,22 @@
|
||||
import { act, render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { MemoryRouter, Route, Routes, useLocation, useNavigationType } from 'react-router-dom';
|
||||
import type { ReactNode } from 'react';
|
||||
import {
|
||||
MemoryRouter,
|
||||
Route,
|
||||
Routes,
|
||||
useLocation,
|
||||
useNavigate,
|
||||
useNavigationType,
|
||||
} from 'react-router-dom';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { SettingTile } from '$components/setting-tile';
|
||||
import { ClientRouteOutlet } from '$pages/client/ClientRouteOutlet';
|
||||
import { ScreenSize, ScreenSizeProvider } from '$hooks/useScreenSize';
|
||||
import { getHomePath, getSettingsPath } from '$pages/pathUtils';
|
||||
import { SETTINGS_PATH } from '$pages/paths';
|
||||
import { SettingsRoute } from './SettingsRoute';
|
||||
import { SettingsShallowRouteRenderer } from './SettingsShallowRouteRenderer';
|
||||
import { SettingsSectionPage } from './SettingsSectionPage';
|
||||
import { focusedSettingTile } from './styles.css';
|
||||
import { useSettingsFocus } from './useSettingsFocus';
|
||||
@@ -49,6 +60,10 @@ vi.mock('$state/hooks/settings', () => ({
|
||||
useSetting: mockUseSetting,
|
||||
}));
|
||||
|
||||
vi.mock('$components/Modal500', () => ({
|
||||
Modal500: ({ children }: { children: ReactNode }) => <div>{children}</div>,
|
||||
}));
|
||||
|
||||
vi.mock('./general', () => ({
|
||||
General: createSectionMock('General section'),
|
||||
}));
|
||||
@@ -115,6 +130,48 @@ function LocationProbe() {
|
||||
);
|
||||
}
|
||||
|
||||
function HomePage() {
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Home route</h1>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
navigate(getSettingsPath('notifications'), {
|
||||
state: { backgroundLocation: location },
|
||||
})
|
||||
}
|
||||
>
|
||||
Open settings
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function renderClientShell(
|
||||
screenSize: ScreenSize,
|
||||
options?: { initialEntries?: string[]; initialIndex?: number }
|
||||
) {
|
||||
const initialEntries = options?.initialEntries ?? [getHomePath()];
|
||||
return render(
|
||||
<MemoryRouter initialEntries={initialEntries} initialIndex={options?.initialIndex}>
|
||||
<ScreenSizeProvider value={screenSize}>
|
||||
<LocationProbe />
|
||||
<Routes>
|
||||
<Route element={<ClientRouteOutlet />}>
|
||||
<Route path={getHomePath()} element={<HomePage />} />
|
||||
<Route path={SETTINGS_PATH} element={<SettingsRoute />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
<SettingsShallowRouteRenderer />
|
||||
</ScreenSizeProvider>
|
||||
</MemoryRouter>
|
||||
);
|
||||
}
|
||||
|
||||
function renderSettingsRoute(
|
||||
path: string,
|
||||
screenSize: ScreenSize,
|
||||
@@ -126,7 +183,7 @@ function renderSettingsRoute(
|
||||
<ScreenSizeProvider value={screenSize}>
|
||||
<LocationProbe />
|
||||
<Routes>
|
||||
<Route path="/settings/:section?/" element={<SettingsRoute />} />
|
||||
<Route path={SETTINGS_PATH} element={<SettingsRoute />} />
|
||||
</Routes>
|
||||
</ScreenSizeProvider>
|
||||
</MemoryRouter>
|
||||
@@ -258,6 +315,39 @@ describe('SettingsRoute', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Settings shallow route shell', () => {
|
||||
it('keeps the desktop background route mounted when settings opens shallow', async () => {
|
||||
const user = userEvent.setup();
|
||||
|
||||
renderClientShell(ScreenSize.Desktop);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Open settings' }));
|
||||
|
||||
expect(screen.getByRole('heading', { name: 'Home route' })).toBeInTheDocument();
|
||||
expect(screen.getByRole('heading', { name: 'Notifications section' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders mobile settings as a full page without retaining the background outlet', async () => {
|
||||
const user = userEvent.setup();
|
||||
|
||||
renderClientShell(ScreenSize.Mobile);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Open settings' }));
|
||||
|
||||
expect(screen.queryByRole('heading', { name: 'Home route' })).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('heading', { name: 'Notifications section' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders desktop direct entry settings as a full page without retaining the background outlet', () => {
|
||||
renderClientShell(ScreenSize.Desktop, {
|
||||
initialEntries: [getSettingsPath('devices')],
|
||||
});
|
||||
|
||||
expect(screen.queryByRole('heading', { name: 'Home route' })).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('heading', { name: 'Devices section' })).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('useSettingsFocus', () => {
|
||||
it('highlights a focus target from the query string', async () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Route, Routes, useNavigate, useLocation } from 'react-router-dom';
|
||||
import { useScreenSizeContext } from '$hooks/useScreenSize';
|
||||
import { Modal500 } from '$components/Modal500';
|
||||
import { isShallowSettingsRoute } from '$pages/client/ClientRouteOutlet';
|
||||
import { SETTINGS_PATH } from '$pages/paths';
|
||||
import { SettingsRoute } from './SettingsRoute';
|
||||
|
||||
export function SettingsShallowRouteRenderer() {
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const screenSize = useScreenSizeContext();
|
||||
|
||||
if (!isShallowSettingsRoute(location.pathname, location.state, screenSize)) return null;
|
||||
|
||||
return (
|
||||
<Modal500 requestClose={() => navigate(-1)}>
|
||||
<Routes>
|
||||
<Route path={SETTINGS_PATH} element={<SettingsRoute />} />
|
||||
</Routes>
|
||||
</Modal500>
|
||||
);
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import * as Sentry from '@sentry/react';
|
||||
|
||||
import { ClientConfig } from '$hooks/useClientConfig';
|
||||
import { ErrorPage } from '$components/DefaultErrorPage';
|
||||
import { SettingsRoute } from '$features/settings';
|
||||
import { SettingsShallowRouteRenderer } from '$features/settings/SettingsShallowRouteRenderer';
|
||||
import { Room } from '$features/room';
|
||||
import { Lobby } from '$features/lobby';
|
||||
import { PageRoot } from '$components/page';
|
||||
@@ -49,6 +51,7 @@ import {
|
||||
SERVER_PATH_SEGMENT,
|
||||
CREATE_PATH,
|
||||
TO_ROOM_EVENT_PATH,
|
||||
SETTINGS_PATH,
|
||||
} from './paths';
|
||||
import {
|
||||
getAppPathFromHref,
|
||||
@@ -59,7 +62,7 @@ import {
|
||||
getOriginBaseUrl,
|
||||
getSpaceLobbyPath,
|
||||
} from './pathUtils';
|
||||
import { ClientBindAtoms, ClientLayout, ClientRoot } from './client';
|
||||
import { ClientBindAtoms, ClientLayout, ClientRoot, ClientRouteOutlet } from './client';
|
||||
import { HandleNotificationClick, ClientNonUIFeatures } from './client/ClientNonUIFeatures';
|
||||
import { Home, HomeRouteRoomProvider, HomeSearch } from './client/home';
|
||||
import { Direct, DirectCreate, DirectRouteRoomProvider } from './client/direct';
|
||||
@@ -182,7 +185,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
</MobileFriendlyClientNav>
|
||||
}
|
||||
>
|
||||
<Outlet />
|
||||
<ClientRouteOutlet />
|
||||
</ClientLayout>
|
||||
<CallStatusRenderer />
|
||||
</CallEmbedProvider>
|
||||
@@ -191,6 +194,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
<CreateRoomModalRenderer />
|
||||
<CreateSpaceModalRenderer />
|
||||
<BugReportModalRenderer />
|
||||
<SettingsShallowRouteRenderer />
|
||||
<RoomSettingsRenderer />
|
||||
<SpaceSettingsRenderer />
|
||||
<GlobalKeyboardShortcuts />
|
||||
@@ -340,6 +344,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
<Route path={SERVER_PATH_SEGMENT} element={<PublicRooms />} />
|
||||
</Route>
|
||||
<Route path={CREATE_PATH} element={<Create />} />
|
||||
<Route path={SETTINGS_PATH} element={<SettingsRoute />} />
|
||||
<Route
|
||||
path={INBOX_PATH}
|
||||
element={
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { useRef } from 'react';
|
||||
import { matchPath, useLocation, useOutlet } from 'react-router-dom';
|
||||
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
|
||||
import { SETTINGS_PATH } from '../paths';
|
||||
|
||||
type BackgroundLocationState = {
|
||||
backgroundLocation?: unknown;
|
||||
};
|
||||
|
||||
export const isShallowSettingsRoute = (
|
||||
pathname: string,
|
||||
state: unknown,
|
||||
screenSize: ScreenSize
|
||||
): boolean => {
|
||||
if (screenSize === ScreenSize.Mobile) return false;
|
||||
if (!matchPath(SETTINGS_PATH, pathname)) return false;
|
||||
|
||||
const backgroundLocation = (state as BackgroundLocationState | null)?.backgroundLocation;
|
||||
return !!backgroundLocation;
|
||||
};
|
||||
|
||||
export function ClientRouteOutlet() {
|
||||
const outlet = useOutlet();
|
||||
const location = useLocation();
|
||||
const screenSize = useScreenSizeContext();
|
||||
const cachedOutletRef = useRef(outlet);
|
||||
const shallowSettings = isShallowSettingsRoute(location.pathname, location.state, screenSize);
|
||||
|
||||
if (!shallowSettings) {
|
||||
cachedOutletRef.current = outlet;
|
||||
return outlet;
|
||||
}
|
||||
|
||||
return cachedOutletRef.current;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './ClientRoot';
|
||||
export * from './ClientBindAtoms';
|
||||
export * from './ClientLayout';
|
||||
export * from './ClientRouteOutlet';
|
||||
Reference in new issue
Block a user