Skip to content

Commit 0248d2a

Browse files
authored
refactor: override HasStyle implementation in Notification (#4591)
1 parent 1b2b724 commit 0248d2a

File tree

3 files changed

+155
-43
lines changed

3 files changed

+155
-43
lines changed

vaadin-notification-flow-parent/vaadin-notification-flow/src/main/java/com/vaadin/flow/component/notification/Notification.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import com.vaadin.flow.component.dependency.JsModule;
3838
import com.vaadin.flow.component.dependency.NpmPackage;
3939
import com.vaadin.flow.component.shared.HasThemeVariant;
40+
import com.vaadin.flow.component.shared.internal.OverlayClassListProxy;
41+
import com.vaadin.flow.dom.ClassList;
4042
import com.vaadin.flow.dom.Element;
4143
import com.vaadin.flow.dom.ElementDetachListener;
4244
import com.vaadin.flow.dom.Style;
@@ -57,7 +59,6 @@
5759
@NpmPackage(value = "@vaadin/notification", version = "24.0.0-alpha12")
5860
@JsModule("@vaadin/notification/src/vaadin-notification.js")
5961
@JsModule("./flow-component-renderer.js")
60-
@JsModule("./notificationConnector.js")
6162
public class Notification extends Component implements HasComponents, HasStyle,
6263
HasThemeVariant<NotificationVariant> {
6364

@@ -575,7 +576,6 @@ protected void onAttach(AttachEvent attachEvent) {
575576
super.onAttach(attachEvent);
576577
getElement().executeJs(
577578
"Vaadin.FlowComponentHost.patchVirtualContainer(this)");
578-
initConnector();
579579
configureRenderer();
580580
updateVirtualChildNodeIds();
581581
}
@@ -598,9 +598,25 @@ protected void onDetach(DetachEvent detachEvent) {
598598
});
599599
}
600600

601-
private void initConnector() {
602-
getElement().executeJs(
603-
"window.Vaadin.Flow.notificationConnector.initLazy(this)");
601+
/**
602+
* Sets the CSS class names of the notification overlay element. This method
603+
* overwrites any previous set class names.
604+
*
605+
* @param className
606+
* a space-separated string of class names to set, or
607+
* <code>null</code> to remove all class names
608+
*/
609+
@Override
610+
public void setClassName(String className) {
611+
getClassNames().clear();
612+
if (className != null) {
613+
addClassNames(className.split(" "));
614+
}
615+
}
616+
617+
@Override
618+
public ClassList getClassNames() {
619+
return new OverlayClassListProxy(this);
604620
}
605621

606622
/**

vaadin-notification-flow-parent/vaadin-notification-flow/src/main/resources/META-INF/resources/frontend/notificationConnector.js

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright 2000-2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.notification;
17+
18+
import org.junit.After;
19+
import org.junit.Assert;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.mockito.Mockito;
23+
24+
import com.vaadin.flow.component.UI;
25+
import com.vaadin.flow.component.shared.internal.OverlayClassListProxy;
26+
import com.vaadin.flow.server.VaadinSession;
27+
28+
public class NotificationHasStyleTest {
29+
30+
private UI ui = new UI();
31+
private Notification notification;
32+
33+
@Before
34+
public void setup() {
35+
UI.setCurrent(ui);
36+
37+
VaadinSession session = Mockito.mock(VaadinSession.class);
38+
Mockito.when(session.hasLock()).thenReturn(true);
39+
ui.getInternals().setSession(session);
40+
41+
notification = new Notification();
42+
ui.add(notification);
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
UI.setCurrent(null);
48+
}
49+
50+
@Test
51+
public void addClassName_notificationHasOverlayClass() {
52+
notification.addClassName("foo");
53+
Assert.assertEquals(
54+
notification.getElement().getProperty("overlayClass"), "foo");
55+
56+
notification.addClassName("bar");
57+
Assert.assertEquals(
58+
notification.getElement().getProperty("overlayClass"),
59+
"foo bar");
60+
}
61+
62+
@Test
63+
public void removeClassName_notificationHasOverlayClass() {
64+
notification.addClassName("foo");
65+
66+
notification.removeClassName("foo");
67+
Assert.assertEquals(
68+
notification.getElement().getProperty("overlayClass"), null);
69+
}
70+
71+
@Test
72+
public void setClassNameString_notificationHasOverlayClass() {
73+
notification.setClassName("foo");
74+
Assert.assertEquals(
75+
notification.getElement().getProperty("overlayClass"), "foo");
76+
77+
notification.setClassName("bar");
78+
Assert.assertEquals(
79+
notification.getElement().getProperty("overlayClass"), "bar");
80+
}
81+
82+
@Test
83+
public void setClassNameBoolean_notificationHasOverlayClass() {
84+
notification.setClassName("foo", true);
85+
86+
notification.setClassName("foo", false);
87+
88+
Assert.assertEquals(
89+
notification.getElement().getProperty("overlayClass"), null);
90+
}
91+
92+
@Test
93+
public void setClassNameMultiple_notificationHasOverlayClass() {
94+
notification.setClassName("foo bar");
95+
notification.getClassNames().set("foo", false);
96+
97+
Assert.assertEquals(
98+
notification.getElement().getProperty("overlayClass"), "bar");
99+
}
100+
101+
@Test
102+
public void addClassNames_notificationHasOverlayClass() {
103+
notification.addClassNames("foo", "bar");
104+
Assert.assertEquals(
105+
notification.getElement().getProperty("overlayClass"),
106+
"foo bar");
107+
108+
notification.addClassNames("baz", "qux");
109+
Assert.assertEquals(
110+
notification.getElement().getProperty("overlayClass"),
111+
"foo bar baz qux");
112+
}
113+
114+
@Test
115+
public void removeClassNames_notificationHasOverlayClass() {
116+
notification.addClassNames("foo", "bar", "baz", "qux");
117+
118+
notification.removeClassNames("foo", "bar");
119+
Assert.assertEquals(
120+
notification.getElement().getProperty("overlayClass"),
121+
"baz qux");
122+
}
123+
124+
@Test(expected = UnsupportedOperationException.class)
125+
public void getStyle_unsupported() {
126+
notification.getStyle();
127+
}
128+
129+
@Test
130+
public void getClassNames_usesProxy() {
131+
Assert.assertTrue(
132+
notification.getClassNames() instanceof OverlayClassListProxy);
133+
}
134+
}

0 commit comments

Comments
 (0)