-
-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New props for selecting events to open/close the tooltip #1108
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,14 @@ import { getScrollParent } from 'utils/get-scroll-parent' | |
import { computeTooltipPosition } from 'utils/compute-positions' | ||
import coreStyles from './core-styles.module.css' | ||
import styles from './styles.module.css' | ||
import type { IPosition, ITooltip, PlacesType } from './TooltipTypes' | ||
import type { | ||
AnchorCloseEvents, | ||
AnchorOpenEvents, | ||
GlobalCloseEvents, | ||
IPosition, | ||
ITooltip, | ||
PlacesType, | ||
} from './TooltipTypes' | ||
|
||
const Tooltip = ({ | ||
// props | ||
|
@@ -34,6 +41,9 @@ const Tooltip = ({ | |
closeOnEsc = false, | ||
closeOnScroll = false, | ||
closeOnResize = false, | ||
openEvents, | ||
closeEvents, | ||
globalCloseEvents, | ||
style: externalStyles, | ||
position, | ||
afterShow, | ||
|
@@ -68,7 +78,49 @@ const Tooltip = ({ | |
const [anchorsBySelect, setAnchorsBySelect] = useState<HTMLElement[]>([]) | ||
const mounted = useRef(false) | ||
|
||
/** | ||
* @todo Update when deprecated stuff gets removed. | ||
*/ | ||
const shouldOpenOnClick = openOnClick || events.includes('click') | ||
const hasClickEvent = | ||
shouldOpenOnClick || openEvents?.click || openEvents?.dblclick || openEvents?.mousedown | ||
const actualOpenEvents: AnchorOpenEvents = openEvents | ||
? { ...openEvents } | ||
: { | ||
mouseenter: true, | ||
focus: true, | ||
click: false, | ||
dblclick: false, | ||
mousedown: false, | ||
} | ||
if (!openEvents && shouldOpenOnClick) { | ||
Object.assign(actualOpenEvents, { | ||
mouseenter: false, | ||
focus: false, | ||
click: true, | ||
}) | ||
} | ||
const actualCloseEvents: AnchorCloseEvents = closeEvents | ||
? { ...closeEvents } | ||
: { | ||
mouseleave: true, | ||
blur: true, | ||
click: false, | ||
} | ||
if (!closeEvents && shouldOpenOnClick) { | ||
Object.assign(actualCloseEvents, { | ||
mouseleave: false, | ||
blur: false, | ||
}) | ||
} | ||
const actualGlobalCloseEvents: GlobalCloseEvents = globalCloseEvents | ||
? { ...globalCloseEvents } | ||
: { | ||
escape: closeOnEsc || false, | ||
scroll: closeOnScroll || false, | ||
resize: closeOnResize || false, | ||
clickOutsideAnchor: hasClickEvent || false, | ||
} | ||
Comment on lines
84
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may look a little confusing at first, but it ensures |
||
|
||
/** | ||
* useLayoutEffect runs before useEffect, | ||
|
@@ -266,13 +318,6 @@ const Tooltip = ({ | |
lastFloatPosition.current = mousePosition | ||
} | ||
|
||
const handleClickTooltipAnchor = (event?: Event) => { | ||
handleShowTooltip(event) | ||
if (delayHide) { | ||
handleHideTooltipDelayed() | ||
} | ||
} | ||
|
||
const handleClickOutsideAnchors = (event: MouseEvent) => { | ||
const anchorById = document.querySelector<HTMLElement>(`[id='${anchorId}']`) | ||
const anchors = [anchorById, ...anchorsBySelect] | ||
|
@@ -371,13 +416,13 @@ const Tooltip = ({ | |
const anchorScrollParent = getScrollParent(activeAnchor) | ||
const tooltipScrollParent = getScrollParent(tooltipRef.current) | ||
|
||
if (closeOnScroll) { | ||
if (actualGlobalCloseEvents.scroll) { | ||
window.addEventListener('scroll', handleScrollResize) | ||
anchorScrollParent?.addEventListener('scroll', handleScrollResize) | ||
tooltipScrollParent?.addEventListener('scroll', handleScrollResize) | ||
} | ||
let updateTooltipCleanup: null | (() => void) = null | ||
if (closeOnResize) { | ||
if (actualGlobalCloseEvents.resize) { | ||
window.addEventListener('resize', handleScrollResize) | ||
} else if (activeAnchor && tooltipRef.current) { | ||
updateTooltipCleanup = autoUpdate( | ||
|
@@ -398,29 +443,63 @@ const Tooltip = ({ | |
} | ||
handleShow(false) | ||
} | ||
|
||
if (closeOnEsc) { | ||
if (actualGlobalCloseEvents.escape) { | ||
window.addEventListener('keydown', handleEsc) | ||
} | ||
|
||
if (actualGlobalCloseEvents.clickOutsideAnchor) { | ||
window.addEventListener('click', handleClickOutsideAnchors) | ||
} | ||
|
||
const enabledEvents: { event: string; listener: (event?: Event) => void }[] = [] | ||
|
||
if (shouldOpenOnClick) { | ||
window.addEventListener('click', handleClickOutsideAnchors) | ||
enabledEvents.push({ event: 'click', listener: handleClickTooltipAnchor }) | ||
} else { | ||
enabledEvents.push( | ||
{ event: 'mouseenter', listener: debouncedHandleShowTooltip }, | ||
{ event: 'mouseleave', listener: debouncedHandleHideTooltip }, | ||
{ event: 'focus', listener: debouncedHandleShowTooltip }, | ||
{ event: 'blur', listener: debouncedHandleHideTooltip }, | ||
) | ||
if (float) { | ||
enabledEvents.push({ | ||
event: 'mousemove', | ||
listener: handleMouseMove, | ||
}) | ||
const handleClickOpenTooltipAnchor = (event?: Event) => { | ||
if (show) { | ||
return | ||
} | ||
handleShowTooltip(event) | ||
} | ||
const handleClickCloseTooltipAnchor = () => { | ||
if (!show) { | ||
return | ||
} | ||
handleHideTooltip() | ||
} | ||
|
||
const regularEvents = ['mouseenter', 'mouseleave', 'focus', 'blur'] | ||
const clickEvents = ['click', 'dblclick', 'mousedown', 'mouseup'] | ||
|
||
Object.entries(actualOpenEvents).forEach(([event, enabled]) => { | ||
if (!enabled) { | ||
return | ||
} | ||
if (regularEvents.includes(event)) { | ||
enabledEvents.push({ event, listener: debouncedHandleShowTooltip }) | ||
} else if (clickEvents.includes(event)) { | ||
enabledEvents.push({ event, listener: handleClickOpenTooltipAnchor }) | ||
} else { | ||
// never happens | ||
} | ||
}) | ||
|
||
Object.entries(actualCloseEvents).forEach(([event, enabled]) => { | ||
if (!enabled) { | ||
return | ||
} | ||
if (regularEvents.includes(event)) { | ||
enabledEvents.push({ event, listener: debouncedHandleHideTooltip }) | ||
} else if (clickEvents.includes(event)) { | ||
enabledEvents.push({ event, listener: handleClickCloseTooltipAnchor }) | ||
} else { | ||
// never happens | ||
} | ||
}) | ||
|
||
if (float) { | ||
enabledEvents.push({ | ||
event: 'mousemove', | ||
listener: handleMouseMove, | ||
}) | ||
} | ||
|
||
const handleMouseEnterTooltip = () => { | ||
|
@@ -431,7 +510,9 @@ const Tooltip = ({ | |
handleHideTooltip() | ||
} | ||
|
||
if (clickable && !shouldOpenOnClick) { | ||
if (clickable && !hasClickEvent) { | ||
// used to keep the tooltip open when hovering content. | ||
// not needed if using click events. | ||
tooltipRef.current?.addEventListener('mouseenter', handleMouseEnterTooltip) | ||
tooltipRef.current?.addEventListener('mouseleave', handleMouseLeaveTooltip) | ||
} | ||
|
@@ -443,23 +524,23 @@ const Tooltip = ({ | |
}) | ||
|
||
return () => { | ||
if (closeOnScroll) { | ||
if (actualGlobalCloseEvents.scroll) { | ||
window.removeEventListener('scroll', handleScrollResize) | ||
anchorScrollParent?.removeEventListener('scroll', handleScrollResize) | ||
tooltipScrollParent?.removeEventListener('scroll', handleScrollResize) | ||
} | ||
if (closeOnResize) { | ||
if (actualGlobalCloseEvents.resize) { | ||
window.removeEventListener('resize', handleScrollResize) | ||
} else { | ||
updateTooltipCleanup?.() | ||
} | ||
if (shouldOpenOnClick) { | ||
if (actualGlobalCloseEvents.clickOutsideAnchor) { | ||
window.removeEventListener('click', handleClickOutsideAnchors) | ||
} | ||
if (closeOnEsc) { | ||
if (actualGlobalCloseEvents.escape) { | ||
window.removeEventListener('keydown', handleEsc) | ||
} | ||
if (clickable && !shouldOpenOnClick) { | ||
if (clickable && !hasClickEvent) { | ||
tooltipRef.current?.removeEventListener('mouseenter', handleMouseEnterTooltip) | ||
tooltipRef.current?.removeEventListener('mouseleave', handleMouseLeaveTooltip) | ||
} | ||
|
@@ -479,8 +560,11 @@ const Tooltip = ({ | |
rendered, | ||
anchorRefs, | ||
anchorsBySelect, | ||
closeOnEsc, | ||
events, | ||
// the effect uses the `actual*Events` objects, but this should work | ||
openEvents, | ||
closeEvents, | ||
globalCloseEvents, | ||
shouldOpenOnClick, | ||
]) | ||
|
||
useEffect(() => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
openOnClick
works as a shorthand forWithout setting
closeEvents
, the tooltip would keep the default behavior of closing onmouseleave
/blur
, which feels cumbersome for this simple use-case. So deprecatingopenOnClick
seems like a bad idea.