Skip to content

Commit

Permalink
feat: add static methods to set Popover global delays (#6566)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored and vursen committed Sep 17, 2024
1 parent 83b3c7a commit 081c7fd
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.popover.tests;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.popover.Popover;
import com.vaadin.flow.router.PreserveOnRefresh;
import com.vaadin.flow.router.Route;

@PreserveOnRefresh
@Route("vaadin-popover/delay-defaults")
public class PopoverDelayDefaultsPage extends Div {
public PopoverDelayDefaultsPage() {
Popover popover = new Popover();

NativeButton setDefaultDelaysTo2000Button = new NativeButton(
"Set default delays to 2000", e -> {
Popover.setDefaultFocusDelay(2000);
Popover.setDefaultHoverDelay(2000);
Popover.setDefaultHideDelay(2000);
});
setDefaultDelaysTo2000Button.setId("set-default-delays-to-2000");

NativeButton setDefaultDelaysTo5000Button = new NativeButton(
"Set default delays to 5000", e -> {
Popover.setDefaultFocusDelay(5000);
Popover.setDefaultHoverDelay(5000);
Popover.setDefaultHideDelay(5000);
});
setDefaultDelaysTo5000Button.setId("set-default-delays-to-5000");

add(popover, setDefaultDelaysTo2000Button,
setDefaultDelaysTo5000Button);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.popover.tests;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.popover.testbench.PopoverElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.tests.AbstractComponentIT;

@TestPath("vaadin-popover/delay-defaults")
public class PopoverDelayDefaultsIT extends AbstractComponentIT {

private PopoverElement popover;

@Before
public void init() {
open();
popover = $(PopoverElement.class).first();
}

@Test
public void changeDefaults_checkPopoverConfig() {
$("button").id("set-default-delays-to-2000").click();
Assert.assertEquals(2000, getActiveHideDelay(popover));
Assert.assertEquals(2000, getActiveFocusDelay(popover));
Assert.assertEquals(2000, getActiveHoverDelay(popover));
}

@Test
public void changeDefaults_refreshPage_checkPopoverConfig() {
$("button").id("set-default-delays-to-5000").click();
getDriver().navigate().refresh();
popover = $(PopoverElement.class).first();
Assert.assertEquals(5000, getActiveHideDelay(popover));
Assert.assertEquals(5000, getActiveFocusDelay(popover));
Assert.assertEquals(5000, getActiveHoverDelay(popover));
}

private int getActiveFocusDelay(PopoverElement popover) {
return getOpenedStateControllerPropertyValue(popover, "__focusDelay");
}

private int getActiveHoverDelay(PopoverElement popover) {
return getOpenedStateControllerPropertyValue(popover, "__hoverDelay");
}

private int getActiveHideDelay(PopoverElement popover) {
return getOpenedStateControllerPropertyValue(popover, "__hideDelay");
}

private int getOpenedStateControllerPropertyValue(PopoverElement popover,
String propertyName) {
var value = executeScript(
"return arguments[0]._openedStateController[arguments[1]];",
popover, propertyName);
return ((Number) value).intValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.vaadin.flow.dom.ElementDetachEvent;
import com.vaadin.flow.dom.ElementDetachListener;
import com.vaadin.flow.dom.Style;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.shared.Registration;

import elemental.json.Json;
Expand All @@ -59,9 +60,15 @@
@NpmPackage(value = "@vaadin/popover", version = "24.5.0-alpha9")
@JsModule("@vaadin/polymer-legacy-adapter/style-modules.js")
@JsModule("@vaadin/popover/src/vaadin-popover.js")
@JsModule("./vaadin-popover/popover.ts")
public class Popover extends Component implements HasAriaLabel, HasComponents,
HasThemeVariant<PopoverVariant> {

private static Integer defaultHideDelay;
private static Integer defaultFocusDelay;
private static Integer defaultHoverDelay;
private static boolean uiInitListenerRegistered = false;

private Component target;
private Registration targetAttachRegistration;

Expand Down Expand Up @@ -94,6 +101,88 @@ public Popover(Component... components) {
add(components);
}

/**
* Sets the default focus delay to be used by all popover instances (running
* in the same JVM), except for those that have focus delay configured using
* {@link #setFocusDelay(int)}.
*
* @param defaultFocusDelay
* the default focus delay
*/
public static void setDefaultFocusDelay(int defaultFocusDelay) {
Popover.defaultFocusDelay = defaultFocusDelay;
applyConfiguration();
}

/**
* Sets the default hide delay to be used by all popover instances (running
* in the same JVM), except for those that have hide delay configured using
* {@link #setHideDelay(int)}.
*
* @param defaultHideDelay
* the default hide delay
*/
public static void setDefaultHideDelay(int defaultHideDelay) {
Popover.defaultHideDelay = defaultHideDelay;
applyConfiguration();
}

/**
* Sets the default hover delay to be used by all popover instances (running
* in the same JVM), except for those that have hover delay configured using
* {@link #setHoverDelay(int)}.
*
* @param defaultHoverDelay
* the default hover delay
*/
public static void setDefaultHoverDelay(int defaultHoverDelay) {
Popover.defaultHoverDelay = defaultHoverDelay;
applyConfiguration();
}

private static void applyConfiguration() {
if (UI.getCurrent() != null) {
// Apply the default popover configuration for the current UI
applyConfigurationForUI(UI.getCurrent());
}

if (!uiInitListenerRegistered) {
// Apply the popover configuration for all new UIs
VaadinService.getCurrent()
.addUIInitListener(e -> applyConfigurationForUI(e.getUI()));
uiInitListenerRegistered = true;
}
}

private static void applyConfigurationForUI(UI ui) {
ui.getElement().executeJs(
"((window.Vaadin ||= {}).Flow ||= {}).popover ||= {}");

if (defaultHideDelay != null) {
ui.getElement().executeJs(
"const popover = window.Vaadin.Flow.popover;"
+ "popover.defaultHideDelay = $0;"
+ "popover.setDefaultHideDelay?.($0)",
defaultHideDelay);
}

if (defaultFocusDelay != null) {
ui.getElement().executeJs(
"const popover = window.Vaadin.Flow.popover;"
+ "popover.defaultFocusDelay = $0;"
+ "popover.setDefaultFocusDelay?.($0)",
defaultFocusDelay);
}

if (defaultHoverDelay != null) {
ui.getElement().executeJs(
"const popover = window.Vaadin.Flow.popover;"
+ "popover.defaultHoverDelay = $0;"
+ "popover.setDefaultHoverDelay?.($0)",
defaultHoverDelay);
}
}

/**
* {@code opened-changed} event is sent when the overlay opened state
* changes.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Popover } from '@vaadin/popover/src/vaadin-popover.js';

const _window = window as any;
_window.Vaadin ||= {};
_window.Vaadin.Flow ||= {};
_window.Vaadin.Flow.popover ||= {};

Object.assign(_window.Vaadin.Flow.popover, {
setDefaultHideDelay: (hideDelay: number) => Popover.setDefaultHideDelay(hideDelay),
setDefaultFocusDelay: (focusDelay: number) => Popover.setDefaultFocusDelay(focusDelay),
setDefaultHoverDelay: (hoverDelay: number) => Popover.setDefaultHoverDelay(hoverDelay)
});

const { defaultHideDelay, defaultFocusDelay, defaultHoverDelay } = _window.Vaadin.Flow.popover;

if (defaultHideDelay) {
Popover.setDefaultHideDelay(defaultHideDelay);
}

if (defaultFocusDelay) {
Popover.setDefaultFocusDelay(defaultFocusDelay);
}

if (defaultHoverDelay) {
Popover.setDefaultHoverDelay(defaultHoverDelay);
}

0 comments on commit 081c7fd

Please sign in to comment.