Skip to content

Commit 827c256

Browse files
committed
feat: adds ability for theming via static.json file
1 parent 6232a8b commit 827c256

File tree

4 files changed

+124
-35
lines changed

4 files changed

+124
-35
lines changed

src/components/ResultListing.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export default function ResultListing({ gmeta }: { gmeta: GMetaResult }) {
127127
}>();
128128
getAttributeFrom<string>(gmeta, "components.ResultListing.heading").then(
129129
(result) => {
130-
console.log(result);
131130
setHeading(result);
132131
},
133132
);

src/theme.ts

Lines changed: 90 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,98 @@
1-
import { extendTheme } from "@chakra-ui/react";
1+
import { STATIC } from "../static";
2+
3+
import { extendTheme, withDefaultColorScheme } from "@chakra-ui/react";
24

35
import "@fontsource/ibm-plex-mono";
46
import "@fontsource/ibm-plex-sans";
57

6-
const theme = extendTheme({
7-
fonts: {
8-
heading: `'IBM Plex Sans', sans-serif`,
9-
body: `'IBM Plex Sans', sans-serif`,
10-
mono: `'IBM Plex Mono', monospace`,
11-
},
12-
colors: {
13-
brand: {
14-
"50": "#E5F2FF",
15-
"100": "#B8DBFF",
16-
"200": "#8AC4FF",
17-
"300": "#5CADFF",
18-
"400": "#2E96FF",
19-
"500": "#007FFF",
20-
"600": "#0066CC",
21-
"700": "#004C99",
22-
"800": "#00264c",
23-
"900": "#001933",
8+
export type ColorDefinition =
9+
| {
10+
50: string;
11+
100: string;
12+
200: string;
13+
300: string;
14+
400: string;
15+
500: string;
16+
600: string;
17+
700: string;
18+
800: string;
19+
900: string;
20+
}
21+
| string;
22+
23+
export type ThemeSettings = {
24+
/**
25+
* Apply a default color scheme to all components.
26+
* This supports all Chakra UI color schemes and is not provided by default.
27+
* @see https://v2.chakra-ui.com/docs/styled-system/theme#colors for available color schemes.
28+
*/
29+
colorScheme?: string;
30+
/**
31+
* Specific color definitions to use in the theme.
32+
* The most common use case is to define a `brand` color.
33+
* @example
34+
* ```json
35+
* {
36+
* "colors": {
37+
* "brand": {
38+
* "50": "#E5F2FF",
39+
* "100": "#B8DBFF",
40+
* "200": "#8AC4FF",
41+
* "300": "#5CADFF",
42+
* "400": "#2E96FF",
43+
* "500": "#007FFF",
44+
* "600": "#0066CC",
45+
* "700": "#004C99",
46+
* "800": "#00264c",
47+
* "900": "#001933"
48+
* }
49+
* }
50+
* }
51+
* ```
52+
* @see https://v2.chakra-ui.com/docs/styled-system/theme#colors
53+
*/
54+
colors?: Record<string, ColorDefinition>;
55+
/**
56+
* Extend the Chakra UI theme.
57+
* @see https://v2.chakra-ui.com/docs/styled-system/customize-theme#using-theme-extensions
58+
*/
59+
extendTheme?: Parameters<typeof extendTheme>[0];
60+
};
61+
62+
const brand: ColorDefinition = {
63+
"50": "#E5F2FF",
64+
"100": "#B8DBFF",
65+
"200": "#8AC4FF",
66+
"300": "#5CADFF",
67+
"400": "#2E96FF",
68+
"500": "#007FFF",
69+
"600": "#0066CC",
70+
"700": "#004C99",
71+
"800": "#00264c",
72+
"900": "#001933",
73+
};
74+
75+
let colorScheme = {};
76+
if (STATIC.data.attributes.theme?.colorScheme) {
77+
colorScheme = withDefaultColorScheme({
78+
colorScheme: STATIC.data.attributes.theme?.colorScheme,
79+
});
80+
}
81+
82+
const theme = extendTheme(
83+
{
84+
fonts: {
85+
heading: `'IBM Plex Sans', sans-serif`,
86+
body: `'IBM Plex Sans', sans-serif`,
87+
mono: `'IBM Plex Mono', monospace`,
88+
},
89+
colors: {
90+
brand,
91+
...(STATIC.data.attributes.theme?.colors || {}),
2492
},
2593
},
26-
});
94+
colorScheme,
95+
STATIC.data.attributes.theme?.extendTheme || {},
96+
);
2797

2898
export default theme;

static.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
"title": "Search | Globus",
1212
"description": "This is an example of a basis Globus Search interface generated from a static.json file."
1313
},
14+
"theme": {
15+
"colors": {
16+
"brand": {
17+
"50": "#e8f3ff",
18+
"100": "#cfd8e3",
19+
"200": "#b5bdcc",
20+
"300": "#97a3b4",
21+
"400": "#7b899d",
22+
"500": "#626f84",
23+
"600": "#4b5768",
24+
"700": "#343e4b",
25+
"800": "#1e2530",
26+
"900": "#070c18"
27+
}
28+
}
29+
},
1430
"content": {
1531
"logo": {
1632
"src": "https://www.usgs.gov/themes/custom/usgs_tantalum/usgs_logo.png",

static.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _STATIC from "./static.json";
22
import { defaultsDeep, get } from "lodash";
33
import type { ResultComponentOptions } from "@/components/Result";
44
import { ResultListingComponentOptions } from "@/components/ResultListing";
5+
import { ThemeSettings } from "@/theme";
56

67
/**
78
* The base type for a `static.json` file.
@@ -44,13 +45,25 @@ export type Data = {
4445
*/
4546
version: string;
4647
attributes: {
47-
metadata: {
48-
title: string;
49-
description: string;
48+
49+
features?: {
50+
/**
51+
* Enable JSONata support for processing the `static.json` file.
52+
* @see https://jsonata.org/
53+
*/
54+
jsonnata?: boolean;
55+
authentication?: boolean;
56+
};
57+
58+
theme?: ThemeSettings;
59+
60+
metadata?: {
61+
title?: string;
62+
description?: string;
5063
};
5164

52-
content: {
53-
headline: string;
65+
content?: {
66+
headline?: string;
5467
logo?: {
5568
src: string;
5669
alt?: string;
@@ -62,15 +75,6 @@ export type Data = {
6275
ResultListing?: ResultListingComponentOptions;
6376
};
6477

65-
features?: {
66-
/**
67-
* Enable JSONata support for processing the `static.json` file.
68-
* @see https://jsonata.org/
69-
*/
70-
jsonnata?: boolean;
71-
authentication?: boolean;
72-
};
73-
7478
globus: {
7579
/**
7680
* The Globus platform environment.

0 commit comments

Comments
 (0)