Skip to content

Commit

Permalink
Add a theme changer action for Carbon
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Oct 15, 2024
1 parent c20f3c3 commit fb77421
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/lib/themes/svelte/carbon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ const theme: ThemeConfig = {
};

export default theme;

export { default as ThemeChangerAction } from './lib/ThemeChangeMenu';
23 changes: 23 additions & 0 deletions src/lib/themes/svelte/carbon/lib/ThemeChangeMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { type ActionIcon, type ActionOptions, CallbackAction } from '$lib/Actions';
import { type MenuLink, Submenu } from '$lib/Menu';

type Optional<T> = T | null | undefined;

type CarbonTheme = "white"|"g10"|"g80"|"g90"|"g100";

export default class ThemeChangeMenu extends Submenu
{
static readonly availableThemes: Array<CarbonTheme> = ["white", "g10", "g80", "g90", "g100"];

constructor(icon?: Optional<ActionIcon>, options?: ActionOptions) {
const links: Array<MenuLink> = ThemeChangeMenu.availableThemes.map((theme) => {
return new CallbackAction('carbon.theme.'+theme, undefined, () => this.changeTheme(theme));
});

super('carbon.theme.change_action', icon, links, options);
}

changeTheme(newTheme: CarbonTheme) {
document.documentElement.setAttribute('theme', newTheme);
}
}
8 changes: 7 additions & 1 deletion src/lib/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ const dictionary: Dictionary = {
'datatable.filters.submit_filters': 'Filter results',
'datatable.filters.reset_filters': 'Reset filters',
'filters.date.from': 'From this date',
'filters.date.to': 'Up to this date'
'filters.date.to': 'Up to this date',
'carbon.theme.change_action': 'Change theme',
'carbon.theme.white': 'White',
'carbon.theme.g10': 'Light gray (g10)',
'carbon.theme.g80': 'Gray (g80)',
'carbon.theme.g90': 'Dark gray (g90)',
'carbon.theme.g100': 'Dark (g100)',
};

export default dictionary;
8 changes: 7 additions & 1 deletion src/lib/translations/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ const dictionary: Dictionary = {
'datatable.filters.submit_filters': 'Filtrer les résultats',
'datatable.filters.reset_filters': 'Réinitialiser les filtres',
'filters.date.from': 'À partir de cette date',
'filters.date.to': "Jusqu'à cette date"
'filters.date.to': "Jusqu'à cette date",
'carbon.theme.change_action': 'Changer le thème',
'carbon.theme.white': 'Blanc',
'carbon.theme.g10': 'Gris clair (g10)',
'carbon.theme.g80': 'Gris (g80)',
'carbon.theme.g90': 'Gris foncé (g90)',
'carbon.theme.g100': 'Sombre (g100)',
};

export default dictionary;
2 changes: 2 additions & 0 deletions src/testApp/Dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { carbon } from '$lib/themes/svelte';
import fr from './translations/fr';
import { bookCrud } from './BookCrud';
import { testCrud } from './TestCrud';
import { ThemeChangerAction } from '$lib/themes/svelte/carbon';

let newLinkIndex = 1;

Expand Down Expand Up @@ -45,6 +46,7 @@ export const dashboard = new DashboardDefinition({
])
],
topLeftMenu: [
new ThemeChangerAction(),
new UrlAction('API Docs', '/apidocs', Document, { htmlAttributes: { rel: 'external' } })
],
topRightMenu: [
Expand Down

0 comments on commit fb77421

Please sign in to comment.