Skip to content

Commit

Permalink
fix #979 Add an Option to Delay Showing a Tooltip Upon Hovering
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Dec 8, 2024
1 parent f0637f9 commit 71073b9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © 2019 Dominokit
*
* 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 org.dominokit.domino.ui.config;

import org.dominokit.domino.ui.menu.direction.DropDirection;
import org.dominokit.domino.ui.utils.DominoUIConfig;

public interface PopoverConfig extends ZIndexConfig {

default DropDirection getDefaultDropDirection() {
return DropDirection.BEST_MIDDLE_UP_DOWN;
}

default boolean closeOnBlur() {
return DominoUIConfig.CONFIG.isClosePopupOnBlur();
}

default int openDelay() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ public interface UIConfig
CarouselConfig,
RichTextConfig,
SlidersConfig,
MenuConfig {}
MenuConfig,
PopoverConfig {}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.function.Supplier;
import jsinterop.base.Js;
import org.dominokit.domino.ui.config.HasComponentConfig;
import org.dominokit.domino.ui.config.ZIndexConfig;
import org.dominokit.domino.ui.config.PopoverConfig;
import org.dominokit.domino.ui.elements.DivElement;
import org.dominokit.domino.ui.events.EventType;
import org.dominokit.domino.ui.menu.direction.DropDirection;
Expand Down Expand Up @@ -58,7 +58,7 @@ public abstract class BasePopover<T extends BasePopover<T>>
implements IsPopup<T>,
PopoverStyles,
FollowOnScroll.ScrollFollower,
HasComponentConfig<ZIndexConfig> {
HasComponentConfig<PopoverConfig> {
protected final Element targetElement;
protected final EventListener closeAllListener;
private DivElement root;
Expand All @@ -67,14 +67,16 @@ public abstract class BasePopover<T extends BasePopover<T>>
private DivElement header;
private DivElement body;

private DropDirection popupPosition = DropDirection.BEST_MIDDLE_UP_DOWN;
private DropDirection popupPosition;

private boolean closeOthers = true;
protected final EventListener closeListener;
private final FollowOnScroll followOnScroll;
private Supplier<Boolean> openCondition = () -> true;
private EventListener lostFocusListener;
private boolean closeOnBlur = DominoUIConfig.CONFIG.isClosePopupOnBlur();
private boolean closeOnBlur;
private PopoverConfig ownConfig;
private int openDelay;

/**
* Constructs a new BasePopover associated with the provided target element. BasePopover is the
Expand All @@ -97,6 +99,10 @@ public BasePopover(Element target) {
.appendChild(body = div().addCss(dui_popover_body)));

init((T) this);
this.popupPosition = getConfig().getDefaultDropDirection();
this.closeOnBlur = getConfig().closeOnBlur();
this.openDelay = getConfig().openDelay();

followOnScroll = new FollowOnScroll(targetElement, this);

closeListener = getCloseListener();
Expand All @@ -108,13 +114,13 @@ public BasePopover(Element target) {
});
elementOf(targetElement)
.onDetached(
mutationRecord -> {
(e, mutationRecord) -> {
close();
DomGlobal.document.body.removeEventListener("blur", lostFocusListener, true);
});

onDetached(
mutationRecord -> {
(e, mutationRecord) -> {
body().removeEventListener(EventType.keydown.getName(), closeListener);
DomGlobal.document.body.removeEventListener("blur", lostFocusListener, true);
});
Expand Down Expand Up @@ -145,6 +151,30 @@ public BasePopover(Element target) {
};
}

@Override
public PopoverConfig getOwnConfig() {
return ownConfig;
}

public T setOwnConfig(PopoverConfig ownConfig) {
this.ownConfig = ownConfig;
return (T) this;
}

/** @return delay in milliseconds before showing the popover. */
public int getOpenDelay() {
return openDelay;
}

/**
* @param openDelay milliseconds to wait before showing the popover.
* @return same component instance
*/
public T setOpenDelay(int openDelay) {
this.openDelay = openDelay;
return (T) this;
}

/**
* Gets the EventListener responsible for closing the popover when certain events occur.
*
Expand Down Expand Up @@ -187,6 +217,14 @@ public T open() {
* the body, positions the popover, and starts monitoring for scroll events (if applicable).
*/
protected void doOpen() {
if (getOpenDelay() > 0) {
DelayedExecution.execute(this::openPopover, getOpenDelay());
} else {
openPopover();
}
}

private void openPopover() {
body().appendChild(root.element());
super.expand();
doPosition();
Expand Down

0 comments on commit 71073b9

Please sign in to comment.