Skip to content

Commit

Permalink
themes: render theme selection based on theme definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamNowotny committed Aug 30, 2024
1 parent cbb01f2 commit e391c4c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 0 additions & 2 deletions docs/new-dashboard.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ export default ({ popup }: ThemeProps) => {
## Registration

After implementing the React component you need to register the theme in [dashboard/components/dashboardTheme](../src/dashboard/components/dashboardTheme.tsx#L6).

To be able to select the new theme, the next step is to add it to [theme selection options](../src/options/pages/viewPage.tsx#L26)
5 changes: 5 additions & 0 deletions src/common/components/react-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {
ViewConfig,
} from 'common/types';

export interface ThemeDefinition {
name: string;
theme?: Theme;
}

export type Theme = React.ComponentType<ThemeProps>;

export interface ThemeProps {
Expand Down
17 changes: 12 additions & 5 deletions src/dashboard/components/dashboardTheme.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { Theme, ViewConfigContext } from 'common/components/react-types';
import { ThemeDefinition, ViewConfigContext } from 'common/components/react-types';
import React, { useContext } from 'react';
import darkTheme from '../themes/dark/dark';
import lightTheme from '../themes/light/light';

const themes: Record<string, Theme> = {
dark: darkTheme,
light: lightTheme,
export const themeDefinition: Record<string, ThemeDefinition> = {
dark: {
name: 'Dark',
theme: darkTheme,
},
light: {
name: 'Light',
theme: lightTheme,
},
};

const DashboardTheme = ({ popup }: { popup: boolean }) => {
const viewConfig = useContext(ViewConfigContext);

const themeName = viewConfig.theme ?? 'dark';
const DashboardActiveTheme = themes[themeName];
const DashboardActiveTheme = themeDefinition[themeName].theme;
if (!DashboardActiveTheme) throw new Error(`No theme found for ${themeName}`);
return (
<div className={`theme theme-${themeName}`}>
<DashboardActiveTheme popup={popup} />
Expand Down
7 changes: 5 additions & 2 deletions src/options/pages/viewPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'common/components/forms/index';
import { ViewConfigContext } from 'common/components/react-types';
import core from 'common/core';
import DashboardTheme from 'dashboard/components/dashboardTheme';
import DashboardTheme, { themeDefinition } from 'dashboard/components/dashboardTheme';
import React, { useContext } from 'react';
import { Col, Container, Form, Row } from 'react-bootstrap';

Expand All @@ -16,14 +16,17 @@ export default () => {
core.setViews({ ...viewConfig, [name]: value });
};

const themeItems = Object.fromEntries(
Object.entries(themeDefinition).map(([key, value]) => [key, value.name]),
);
return (
<Container>
<Row>
<Col md={5}>
<Form>
<FormSelectField
label={'Theme'}
items={{ dark: 'Dark', light: 'Light' }}
items={themeItems}
activeItem={viewConfig.theme}
onSelect={value => {
setField('theme', value);
Expand Down

0 comments on commit e391c4c

Please sign in to comment.