Skip to content

Commit fb77421

Browse files
committed
Add a theme changer action for Carbon
1 parent c20f3c3 commit fb77421

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

src/lib/themes/svelte/carbon/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,5 @@ const theme: ThemeConfig = {
119119
};
120120

121121
export default theme;
122+
123+
export { default as ThemeChangerAction } from './lib/ThemeChangeMenu';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { type ActionIcon, type ActionOptions, CallbackAction } from '$lib/Actions';
2+
import { type MenuLink, Submenu } from '$lib/Menu';
3+
4+
type Optional<T> = T | null | undefined;
5+
6+
type CarbonTheme = "white"|"g10"|"g80"|"g90"|"g100";
7+
8+
export default class ThemeChangeMenu extends Submenu
9+
{
10+
static readonly availableThemes: Array<CarbonTheme> = ["white", "g10", "g80", "g90", "g100"];
11+
12+
constructor(icon?: Optional<ActionIcon>, options?: ActionOptions) {
13+
const links: Array<MenuLink> = ThemeChangeMenu.availableThemes.map((theme) => {
14+
return new CallbackAction('carbon.theme.'+theme, undefined, () => this.changeTheme(theme));
15+
});
16+
17+
super('carbon.theme.change_action', icon, links, options);
18+
}
19+
20+
changeTheme(newTheme: CarbonTheme) {
21+
document.documentElement.setAttribute('theme', newTheme);
22+
}
23+
}

src/lib/translations/en.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ const dictionary: Dictionary = {
3434
'datatable.filters.submit_filters': 'Filter results',
3535
'datatable.filters.reset_filters': 'Reset filters',
3636
'filters.date.from': 'From this date',
37-
'filters.date.to': 'Up to this date'
37+
'filters.date.to': 'Up to this date',
38+
'carbon.theme.change_action': 'Change theme',
39+
'carbon.theme.white': 'White',
40+
'carbon.theme.g10': 'Light gray (g10)',
41+
'carbon.theme.g80': 'Gray (g80)',
42+
'carbon.theme.g90': 'Dark gray (g90)',
43+
'carbon.theme.g100': 'Dark (g100)',
3844
};
3945

4046
export default dictionary;

src/lib/translations/fr.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ const dictionary: Dictionary = {
3535
'datatable.filters.submit_filters': 'Filtrer les résultats',
3636
'datatable.filters.reset_filters': 'Réinitialiser les filtres',
3737
'filters.date.from': 'À partir de cette date',
38-
'filters.date.to': "Jusqu'à cette date"
38+
'filters.date.to': "Jusqu'à cette date",
39+
'carbon.theme.change_action': 'Changer le thème',
40+
'carbon.theme.white': 'Blanc',
41+
'carbon.theme.g10': 'Gris clair (g10)',
42+
'carbon.theme.g80': 'Gris (g80)',
43+
'carbon.theme.g90': 'Gris foncé (g90)',
44+
'carbon.theme.g100': 'Sombre (g100)',
3945
};
4046

4147
export default dictionary;

src/testApp/Dashboard.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { carbon } from '$lib/themes/svelte';
1010
import fr from './translations/fr';
1111
import { bookCrud } from './BookCrud';
1212
import { testCrud } from './TestCrud';
13+
import { ThemeChangerAction } from '$lib/themes/svelte/carbon';
1314

1415
let newLinkIndex = 1;
1516

@@ -45,6 +46,7 @@ export const dashboard = new DashboardDefinition({
4546
])
4647
],
4748
topLeftMenu: [
49+
new ThemeChangerAction(),
4850
new UrlAction('API Docs', '/apidocs', Document, { htmlAttributes: { rel: 'external' } })
4951
],
5052
topRightMenu: [

0 commit comments

Comments
 (0)