Skip to content

Commit 688715a

Browse files
authored
Merge pull request #42310 from phillip-kruger/dev-ui-theme-switch
Fix Dev UI Theme switch
2 parents 39ec432 + 7d1b7eb commit 688715a

File tree

3 files changed

+103
-115
lines changed

3 files changed

+103
-115
lines changed

bom/dev-ui/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<description>Dependency management for dev-ui. Importable by third party extension developers.</description>
1414

1515
<properties>
16-
<vaadin.version>24.4.4</vaadin.version>
16+
<vaadin.version>24.4.5</vaadin.version>
1717
<lit.version>3.1.4</lit.version>
1818
<lit-element.version>4.0.6</lit-element.version>
1919
<lit-html.version>3.1.4</lit-html.version>

extensions/vertx-http/dev-ui-resources/src/main/resources/dev-ui/qwc/qwc-header.js

Lines changed: 4 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
import { LitElement, html, css } from 'lit';
22
import { RouterController } from 'router-controller';
3-
import { StorageController } from 'storage-controller';
43
import { notifier } from 'notifier';
54
import { observeState } from 'lit-element-state';
65
import { themeState } from 'theme-state';
76
import { devuiState } from 'devui-state';
8-
import '@vaadin/menu-bar';
97
import '@vaadin/tabs';
10-
import '@vaadin/button';
118
import 'qwc/qwc-extension-link.js';
12-
9+
import './qwc-theme-switch.js';
1310
/**
1411
* This component represent the Dev UI Header
1512
*/
1613
export class QwcHeader extends observeState(LitElement) {
1714

1815
routerController = new RouterController(this);
19-
storageControl = new StorageController(this);
2016

2117
static styles = css`
2218
@@ -31,7 +27,7 @@ export class QwcHeader extends observeState(LitElement) {
3127
display: flex;
3228
justify-content: space-around;
3329
align-items: center;
34-
padding-right: 60px;
30+
padding-right: 10px;
3531
}
3632
3733
.logo-title {
@@ -88,13 +84,6 @@ export class QwcHeader extends observeState(LitElement) {
8884
align-items: center;
8985
}
9086
91-
.themeDropdown {
92-
position: absolute;
93-
right: 0px;
94-
top: 10px;
95-
z-index: 3;
96-
}
97-
9887
.hidden {
9988
display:none;
10089
}
@@ -103,10 +92,7 @@ export class QwcHeader extends observeState(LitElement) {
10392
static properties = {
10493
_title: {state: true},
10594
_subTitle: {state: true},
106-
_rightSideNav: {state: true},
107-
_selectedTheme: {state: true},
108-
_themeOptions: {state: true},
109-
_desktopTheme: {state: true}
95+
_rightSideNav: {state: true}
11096
};
11197

11298
constructor() {
@@ -115,45 +101,13 @@ export class QwcHeader extends observeState(LitElement) {
115101
this._subTitle = null;
116102
this._rightSideNav = "";
117103

118-
this._createThemeItems();
119-
this._restoreThemePreference();
120-
this._createThemeOptions();
121-
122104
window.addEventListener('vaadin-router-location-changed', (event) => {
123105
this._updateHeader(event);
124106
});
125107
}
126108

127109
connectedCallback() {
128110
super.connectedCallback();
129-
// Get desktop theme setting
130-
this._desktopTheme = "dark";
131-
if(window.matchMedia){
132-
if(window.matchMedia('(prefers-color-scheme: light)').matches){
133-
this._desktopTheme = "light";
134-
}
135-
136-
// Change theme setting when OS theme change
137-
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', e => {
138-
if(e.matches){
139-
this._desktopTheme = "light";
140-
}else{
141-
this._desktopTheme = "dark";
142-
}
143-
this._changeToSelectedTheme();
144-
});
145-
}
146-
147-
this._changeToSelectedTheme();
148-
}
149-
150-
_restoreThemePreference() {
151-
const storedValue = this.storageControl.get("theme-preference");
152-
if(storedValue){
153-
this._selectedTheme = storedValue;
154-
}else {
155-
this._selectedTheme = "desktop";
156-
}
157111
}
158112

159113
render() {
@@ -197,71 +151,7 @@ export class QwcHeader extends observeState(LitElement) {
197151
}
198152

199153
_renderThemeOptions(){
200-
return html`<vaadin-menu-bar theme="tertiary-inline" class="themeDropdown"
201-
.items="${this._themeOptions}"
202-
@item-selected="${(e) => this._changeThemeOption(e)}">
203-
</vaadin-menu-bar>`;
204-
}
205-
206-
_changeThemeOption(e){
207-
this._selectedTheme = e.detail.value.name;
208-
this._createThemeOptions();
209-
this._changeToSelectedTheme();
210-
this.storageControl.set('theme-preference', this._selectedTheme);
211-
}
212-
213-
_changeToSelectedTheme(){
214-
if(this._selectedTheme === "desktop"){
215-
themeState.changeTo(this._desktopTheme);
216-
}else {
217-
themeState.changeTo(this._selectedTheme);
218-
}
219-
}
220-
221-
_createThemeOptions(){
222-
223-
let selectedComponent = this._desktopThemeItem;
224-
if(this._selectedTheme === "dark"){
225-
selectedComponent = this._darkThemeItem;
226-
}else if(this._selectedTheme === "light"){
227-
selectedComponent = this._lightThemeItem;
228-
}
229-
230-
this._themeOptions = [
231-
{
232-
component: selectedComponent,
233-
children: [
234-
{
235-
component: this._darkThemeItem,
236-
name: "dark"
237-
},
238-
{
239-
component: this._lightThemeItem,
240-
name: "light"
241-
},
242-
{
243-
component: this._desktopThemeItem,
244-
name: "desktop"
245-
}
246-
]
247-
}
248-
249-
];
250-
}
251-
252-
_createThemeItems() {
253-
this._darkThemeItem = this._createThemeItem("moon", "dark");
254-
this._lightThemeItem = this._createThemeItem("sun", "light");
255-
this._desktopThemeItem = this._createThemeItem("desktop", "desktop");
256-
}
257-
258-
_createThemeItem(iconName, ariaLabel) {
259-
const item = document.createElement('vaadin-context-menu-item');
260-
const icon = document.createElement('vaadin-icon');
261-
item.setAttribute('aria-label', ariaLabel);
262-
icon.setAttribute('icon', `font-awesome-solid:${iconName}`);
263-
item.appendChild(icon);
264-
return item;
154+
return html`<qwc-theme-switch></qwc-theme-switch>`;
265155
}
266156

267157
_updateHeader(event){
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { LitElement, html, css } from 'lit';
2+
import { themeState } from 'theme-state';
3+
import { StorageController } from 'storage-controller';
4+
import '@vaadin/button';
5+
6+
/**
7+
* Basic theme switch
8+
*/
9+
export class QwcThemeSwitch extends LitElement {
10+
storageControl = new StorageController(this);
11+
12+
themes = [
13+
{ id: 0, name: 'Desktop', icon: 'font-awesome-solid:desktop' },
14+
{ id: 1, name: 'Light', icon: 'font-awesome-solid:sun' },
15+
{ id: 2, name: 'Dark', icon: 'font-awesome-solid:moon' }
16+
];
17+
18+
static styles = css`
19+
.themeButton {
20+
padding-left: 10px;
21+
}
22+
.button {
23+
--vaadin-button-background: var(--lumo-base-color);
24+
}
25+
`;
26+
27+
static properties = {
28+
_selectedThemeIndex: {state: true},
29+
};
30+
31+
constructor() {
32+
super();
33+
this._restoreThemePreference();
34+
}
35+
36+
connectedCallback() {
37+
super.connectedCallback();
38+
this._desktopTheme = "dark"; // default
39+
if(window.matchMedia){
40+
if(window.matchMedia('(prefers-color-scheme: light)').matches){
41+
this._desktopTheme = "light";
42+
}
43+
44+
// Change theme setting when OS theme change
45+
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', e => {
46+
if(e.matches){
47+
this._desktopTheme = "light";
48+
}else{
49+
this._desktopTheme = "dark";
50+
}
51+
if(this._selectedThemeIndex===0){
52+
this._changeToSelectedThemeIndex();
53+
}
54+
});
55+
}
56+
57+
this._changeToSelectedThemeIndex();
58+
}
59+
60+
render() {
61+
let theme = this.themes[this._selectedThemeIndex];
62+
63+
return html`<div class="themeButton">
64+
<vaadin-button theme="icon" aria-label="${theme.name}" title="${theme.name} theme" class="button" @click="${this._nextTheme}">
65+
<vaadin-icon icon="${theme.icon}"></vaadin-icon>
66+
</vaadin-button>
67+
</div>`;
68+
}
69+
70+
_nextTheme(e){
71+
this._selectedThemeIndex = (this._selectedThemeIndex + 1) % this.themes.length;
72+
this._changeToSelectedThemeIndex();
73+
}
74+
75+
_changeToSelectedThemeIndex(){
76+
let theme = this.themes[this._selectedThemeIndex];
77+
this.storageControl.set('theme-preference', theme.id);
78+
79+
if(theme.id === 0){ // Desktop
80+
themeState.changeTo(this._desktopTheme);
81+
}else {
82+
themeState.changeTo(theme.name.toLowerCase());
83+
}
84+
85+
}
86+
87+
_restoreThemePreference() {
88+
const storedValue = this.storageControl.get("theme-preference");
89+
if(storedValue){
90+
this._selectedThemeIndex = storedValue;
91+
} else {
92+
this._selectedThemeIndex = 0;
93+
}
94+
}
95+
96+
97+
}
98+
customElements.define('qwc-theme-switch', QwcThemeSwitch);

0 commit comments

Comments
 (0)