Skip to content

Commit ce4c1f2

Browse files
paodbjavier-godoy
authored andcommitted
feat: add feature to toggle between light & dark theme
Close #16
1 parent c07b736 commit ce4c1f2

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.vaadin.flow.component.Component;
2525
import com.vaadin.flow.component.HasElement;
2626
import com.vaadin.flow.component.checkbox.Checkbox;
27+
import com.vaadin.flow.component.dependency.JsModule;
2728
import com.vaadin.flow.component.dependency.StyleSheet;
2829
import com.vaadin.flow.component.html.Div;
2930
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
@@ -35,6 +36,7 @@
3536
import com.vaadin.flow.router.RouterLink;
3637

3738
@StyleSheet("context://frontend/styles/commons-demo/shared-styles.css")
39+
@JsModule("./toggle-theme.js")
3840
@SuppressWarnings("serial")
3941
public class TabbedDemo extends VerticalLayout implements RouterLayout {
4042

@@ -43,6 +45,7 @@ public class TabbedDemo extends VerticalLayout implements RouterLayout {
4345
private SplitLayoutDemo currentLayout;
4446
private Checkbox orientationCB;
4547
private Checkbox codeCB;
48+
private Checkbox themeCB;
4649

4750
public TabbedDemo() {
4851
tabs = new RouteTabs();
@@ -63,10 +66,17 @@ public TabbedDemo() {
6366
cb -> {
6467
updateSplitterPosition();
6568
});
69+
themeCB = new Checkbox("Dark Theme");
70+
themeCB.setValue(false);
71+
themeCB.addClassName("smallcheckbox");
72+
themeCB.addValueChangeListener(
73+
cb -> {
74+
updateDemoTheme();
75+
});
6676
footer = new HorizontalLayout();
6777
footer.setWidthFull();
6878
footer.setJustifyContentMode(JustifyContentMode.END);
69-
footer.add(codeCB, orientationCB);
79+
footer.add(codeCB, orientationCB, themeCB);
7080

7181
this.add(tabs);
7282
this.add(new Div());
@@ -156,12 +166,12 @@ public void showRouterLayoutContent(HasElement content) {
156166
content = new SplitLayoutDemo(demo, sourceCodeUrl);
157167
currentLayout = (SplitLayoutDemo) content;
158168
updateSplitterPosition();
159-
updateSplitterOrientation();
160-
this.footer.setVisible(true);
169+
updateSplitterOrientation();
161170
} else {
162-
currentLayout = null;
163-
this.footer.setVisible(false);
171+
currentLayout = null;
172+
demo.getElement().getStyle().set("height", "100%");
164173
}
174+
updateFooterButtonsVisibility();
165175
this.getElement().insertChild(1, content.getElement());
166176
}
167177

@@ -190,4 +200,13 @@ private void updateSplitterOrientation() {
190200
}
191201
}
192202

203+
private void updateDemoTheme() {
204+
this.getElement().executeJs("toggleTheme.applyTheme($0)", themeCB.getValue());
205+
}
206+
207+
private void updateFooterButtonsVisibility() {
208+
orientationCB.setVisible(currentLayout != null);
209+
codeCB.setVisible(currentLayout != null);
210+
}
211+
193212
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
window.toggleTheme = {
2+
3+
applyTheme: function(dark) {
4+
let theme = "";
5+
if(dark){
6+
theme = "dark";
7+
}
8+
document.documentElement.setAttribute("theme", theme);
9+
},
10+
11+
}

0 commit comments

Comments
 (0)