Skip to content

Commit f97c6cc

Browse files
committed
chore: Set all parameters from dev pages in a consistent way
1 parent b3fd3e9 commit f97c6cc

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

pages/app/page.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { Suspense, useEffect } from "react";
5-
import { useSearchParams } from "react-router-dom";
4+
import { Suspense } from "react";
65

76
import { pagesMap } from "../pages";
8-
7+
import useModes from "./use-modes";
98
export interface PageProps {
109
pageId: string;
1110
}
12-
1311
export default function Page({ pageId }: PageProps) {
14-
const [searchParams] = useSearchParams();
15-
const direction = searchParams.get("direction") ?? "ltr";
16-
17-
useEffect(() => {
18-
document.documentElement.setAttribute("dir", direction);
19-
}, [direction]);
12+
useModes();
2013

2114
const Component = pagesMap[pageId];
2215

pages/app/use-modes.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { useEffect } from "react";
4+
import { useSearchParams } from "react-router-dom";
5+
6+
import { applyDensity, applyMode, disableMotion } from "@cloudscape-design/global-styles";
7+
8+
import { AppUrlParams, formatQuery, parseQuery } from "./utils";
9+
10+
export default function useModes() {
11+
const [searchParams, setSearchParams] = useSearchParams();
12+
const urlParams = parseQuery(searchParams) as AppUrlParams;
13+
const { density, direction, mode, motionDisabled } = urlParams;
14+
15+
function setParams(newParams: Partial<AppUrlParams>) {
16+
setSearchParams(formatQuery({ ...urlParams, ...newParams }));
17+
18+
if (newParams.direction && newParams.direction !== direction) {
19+
window.location.reload();
20+
}
21+
}
22+
23+
useEffect(() => {
24+
applyMode(mode);
25+
}, [mode]);
26+
27+
useEffect(() => {
28+
applyDensity(density);
29+
}, [density]);
30+
31+
useEffect(() => {
32+
disableMotion(motionDisabled);
33+
}, [motionDisabled]);
34+
35+
document.documentElement.setAttribute("dir", urlParams.direction);
36+
37+
return { density, direction, mode, motionDisabled, setParams };
38+
}

pages/app/utils.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import mapValues from "lodash/mapValues";
5+
6+
import { Density, Mode } from "@cloudscape-design/global-styles";
7+
8+
export interface AppUrlParams {
9+
mode: Mode;
10+
density: Density;
11+
direction: "ltr" | "rtl";
12+
motionDisabled: boolean;
13+
}
14+
15+
export const defaults: AppUrlParams = {
16+
mode: Mode.Light,
17+
density: Density.Comfortable,
18+
direction: "ltr",
19+
motionDisabled: false,
20+
};
21+
22+
export function parseQuery(urlParams: URLSearchParams) {
23+
const queryParams: Record<string, any> = { ...defaults };
24+
urlParams.forEach((value, key) => (queryParams[key] = value));
25+
26+
return mapValues(queryParams, (value) => {
27+
if (value === "true" || value === "false") {
28+
return value === "true";
29+
}
30+
return value;
31+
});
32+
}
33+
34+
export function formatQuery(params: AppUrlParams) {
35+
const query: Record<string, string> = {};
36+
for (const [key, value] of Object.entries(params)) {
37+
if (value === defaults[key as keyof AppUrlParams]) {
38+
continue;
39+
}
40+
query[key as keyof AppUrlParams] = String(value);
41+
}
42+
return query;
43+
}

0 commit comments

Comments
 (0)