Skip to content

Commit c0f8544

Browse files
authored
feat: theme in url (#88)
* fix: do not show empty theme tables * feat: encode theme in URL
1 parent 9204766 commit c0f8544

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export default factory(function App({ properties, middleware: { block, icache, t
6363
const readmeFilenames = getReadmeFileNames(config);
6464
const isCodeSandbox = !!global.window.csbJsonP;
6565
const changeTheme = (themeName: string) => {
66-
const newTheme = config.themes[themeName].theme;
66+
const { theme: newTheme, label } = config.themes[themeName];
67+
window.location.search = `theme=${label}`;
6768
theme.set(newTheme);
6869
};
6970

src/Example.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ export default factory(function Example({
7070

7171
const widgetReadme = widgetReadmes[readmePath];
7272
const widgetExample = widgetExamples[examplePath];
73-
const { properties: widgetProperty, children: widgetChildren, messages, locales } = widgetProperties[widgetName] || {} as any;
73+
const { properties: widgetProperty, children: widgetChildren, messages, locales } =
74+
widgetProperties[widgetName] || ({} as any);
7475
const widgetTheme = widgetThemes[widgetName];
76+
const hasThemeKeys = Object.keys(widgetTheme).length > 0;
7577

7678
const dimensions = postMessage();
7779

@@ -122,9 +124,23 @@ export default factory(function Example({
122124
{isOverview && widgetChildren && widgetChildren.length && (
123125
<InterfaceTable props={widgetChildren} tableName="Children" />
124126
)}
125-
{isOverview && messages.length && <InterfaceTable props={messages} tableName="i18n Messages" descriptionLabel="Default" showTypes={false}/>}
126-
{isOverview && locales.length && <InterfaceTable props={locales} tableName="Supported Locales" showComments={false} showTypes={false}/>}
127-
{isOverview && <ThemeTable themes={widgetTheme} />}
127+
{isOverview && messages.length && (
128+
<InterfaceTable
129+
props={messages}
130+
tableName="i18n Messages"
131+
descriptionLabel="Default"
132+
showTypes={false}
133+
/>
134+
)}
135+
{isOverview && locales.length && (
136+
<InterfaceTable
137+
props={locales}
138+
tableName="Supported Locales"
139+
showComments={false}
140+
showTypes={false}
141+
/>
142+
)}
143+
{isOverview && hasThemeKeys && <ThemeTable themes={widgetTheme} />}
128144
</div>
129145
);
130146
});

src/SideMenu.tsx

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
import { create, tsx } from '@dojo/framework/core/vdom';
22
import theme from '@dojo/framework/core/middleware/theme';
3-
import { isThemeInjectorPayloadWithVariant } from '@dojo/framework/core/ThemeInjector';
43

54
import ActiveLink from './ActiveLink';
5+
import { Config } from '.';
6+
import { getThemeFromConfig } from './utils';
67

78
const factory = create({ theme }).properties<{
89
widgetName: string;
9-
config: any;
10+
config: Config;
1011
onThemeChange: (theme: string) => void;
1112
}>();
1213

1314
export default factory(function SideBar({ properties, middleware: { theme } }) {
1415
const { widgetName, config, onThemeChange } = properties();
16+
const currentTheme = getThemeFromConfig(config);
1517

16-
const currentTheme = theme.get();
17-
let currentThemeIndex = 0;
18-
config.themes.some((theme: any, i: number) => {
19-
if (isThemeInjectorPayloadWithVariant(currentTheme)) {
20-
if (currentTheme.theme === theme.theme.theme) {
21-
currentThemeIndex = i;
22-
return true;
23-
}
24-
} else if (currentTheme === theme.theme) {
25-
currentThemeIndex = i;
26-
return true;
27-
}
28-
});
2918
return (
3019
<div classes="flex flex-col justify-between overflow-y-auto sticky top-16 max-h-(screen-16) pt-12 pb-4 -mt-12">
3120
<div classes="mb-8">
@@ -96,7 +85,10 @@ export default factory(function SideBar({ properties, middleware: { theme } }) {
9685
>
9786
{config.themes.map((theme: any, i: number) => {
9887
return (
99-
<option selected={i === currentThemeIndex} value={`${i}`}>
88+
<option
89+
selected={theme.label === currentTheme.label}
90+
value={`${i}`}
91+
>
10092
{theme.label}
10193
</option>
10294
);

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import './main.css';
1010

1111
import routes from './routes';
1212
import App from './App';
13+
import { getThemeFromConfig } from './utils';
1314

1415
export interface ConfigThemes {
1516
label: string;
@@ -57,8 +58,8 @@ export default ({ config }: { config: Config }) => {
5758
}
5859
});
5960
} else {
61+
const theme = getThemeFromConfig(config);
6062
const registry = new Registry();
61-
const [theme] = themes;
6263
registerThemeInjector(theme.theme, registry);
6364
registerRouterInjector(routes, registry);
6465

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Config, ConfigThemes } from '.';
2+
3+
export function getThemeFromConfig(config: Config): ConfigThemes {
4+
const { searchParams } = new URL(window.location.href);
5+
const themeParam = searchParams.get('theme');
6+
const theme = config.themes.find((theme) => theme.label === themeParam) || config.themes[0];
7+
return theme;
8+
}

0 commit comments

Comments
 (0)