Skip to content

Commit 0335154

Browse files
committed
chore: Handle visual mode only via URL query parameters
1 parent f30f1fb commit 0335154

File tree

4 files changed

+11
-26
lines changed

4 files changed

+11
-26
lines changed

pages/app/app-context.tsx

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@ interface AppUrlParams {
1414
visualRefresh: boolean;
1515
motionDisabled: boolean;
1616
appLayoutWidget: boolean;
17-
mode?: Mode;
17+
mode: Mode;
1818
}
1919

2020
export interface AppContextType<T = unknown> {
21-
mode: Mode;
2221
pageId?: string;
2322
urlParams: AppUrlParams & T;
2423
setUrlParams: (newParams: Partial<AppUrlParams & T>) => void;
2524
setMode: (newMode: Mode) => void;
2625
}
2726

2827
const appContextDefaults: AppContextType = {
29-
mode: Mode.Light,
3028
pageId: undefined,
3129
urlParams: {
3230
density: Density.Comfortable,
3331
direction: 'ltr',
3432
visualRefresh: THEME === 'default',
3533
motionDisabled: false,
3634
appLayoutWidget: false,
35+
mode: Mode.Light,
3736
},
3837
setMode: () => {},
3938
setUrlParams: () => {},
@@ -74,34 +73,21 @@ function formatQuery(params: AppUrlParams) {
7473
export function AppContextProvider({ children }: { children: React.ReactNode }) {
7574
const history = useHistory();
7675
const location = useLocation();
77-
const matchWithVisualMode = useRouteMatch<{ mode: Mode; pageId: string }>('/:mode(light|dark)/:pageId*');
78-
const matchWithoutVisualMode = useRouteMatch<{ pageId: string }>('/:pageId*');
79-
const pageId = (matchWithVisualMode ?? matchWithoutVisualMode)?.params.pageId ?? undefined;
76+
const pageId = useRouteMatch<{ pageId: string }>('/:pageId*')?.params.pageId ?? undefined;
8077
const urlParams = parseQuery(location.search) as AppUrlParams;
81-
const mode = matchWithVisualMode?.params.mode ?? urlParams.mode ?? Mode.Light;
8278

8379
function setUrlParams(newParams: Partial<AppUrlParams>) {
8480
const formattedQuery = formatQuery({ ...urlParams, ...newParams });
85-
if (matchWithVisualMode) {
86-
const pathname = [matchWithVisualMode.params.mode, pageId].filter(segment => !!segment).join('/') + '/';
87-
history.replace(`/${pathname}${formatQuery({ ...urlParams, ...newParams })}`);
88-
} else {
89-
const newUrl = pageId ? `/${pageId}${formattedQuery}` : formattedQuery;
90-
history.replace(newUrl);
91-
}
81+
const newUrl = pageId ? `/${pageId}${formattedQuery}` : formattedQuery;
82+
history.replace(newUrl);
9283
}
9384

9485
function updateMode(newMode: Mode) {
95-
if (matchWithVisualMode) {
96-
const pathname = [newMode, pageId].filter(segment => !!segment).join('/') + '/';
97-
history.replace('/' + pathname + location.search + location.hash);
98-
} else {
99-
setUrlParams({ mode: newMode });
100-
}
86+
setUrlParams({ mode: newMode });
10187
}
10288

10389
return (
104-
<AppContext.Provider value={{ mode, pageId, urlParams, setUrlParams: setUrlParams, setMode: updateMode }}>
90+
<AppContext.Provider value={{ pageId, urlParams, setUrlParams: setUrlParams, setMode: updateMode }}>
10591
{children}
10692
</AppContext.Provider>
10793
);

pages/app/components/header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import ThemeSwitcher from './theme-switcher';
1010
import styles from './header.scss';
1111

1212
export default function Header({ sticky }: { sticky?: boolean }) {
13-
const { mode } = useContext(AppContext);
13+
const { urlParams: mode } = useContext(AppContext);
1414
return (
1515
<>
1616
{/* #h selector for compatibility with global navigation */}

pages/app/components/theme-switcher.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SpaceBetween from '~components/space-between';
1010
import AppContext from '../app-context';
1111

1212
export default function ThemeSwitcher() {
13-
const { mode, urlParams, setUrlParams, setMode } = useContext(AppContext);
13+
const { urlParams, setUrlParams, setMode } = useContext(AppContext);
1414

1515
const vrSwitchProps: React.InputHTMLAttributes<HTMLInputElement> = {
1616
id: 'visual-refresh-toggle',
@@ -38,7 +38,7 @@ export default function ThemeSwitcher() {
3838
<input
3939
id="mode-toggle"
4040
type="checkbox"
41-
checked={mode === 'dark'}
41+
checked={urlParams.mode === 'dark'}
4242
onChange={event => setMode(event.target.checked ? Mode.Dark : Mode.Light)}
4343
/>
4444
Dark mode

pages/app/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ function isAppLayoutPage(pageId?: string) {
5757

5858
function App() {
5959
const {
60-
mode,
6160
pageId,
62-
urlParams: { density, motionDisabled },
61+
urlParams: { density, mode, motionDisabled },
6362
} = useContext(AppContext);
6463

6564
// AppLayout already contains <main>

0 commit comments

Comments
 (0)