Repository navigation
fix: close tooltip on Escape #540
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ import type { | |
| TriggerRef, | ||
| } from '@rc-component/trigger'; | ||
| import Trigger from '@rc-component/trigger'; | ||
| import { useId } from '@rc-component/util'; | ||
| import { useControlledState, useEvent, useId } from '@rc-component/util'; | ||
| import { clsx } from 'clsx'; | ||
| import * as React from 'react'; | ||
| import { useImperativeHandle, useRef } from 'react'; | ||
|
|
@@ -77,6 +77,8 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => { | |
| align = {}, | ||
| destroyOnHidden = false, | ||
| defaultVisible, | ||
| visible, | ||
| popupVisible, | ||
| getTooltipContainer, | ||
| arrowContent, | ||
| overlay, | ||
|
|
@@ -89,14 +91,36 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => { | |
|
|
||
| const mergedId = useId(id); | ||
| const triggerRef = useRef<TriggerRef>(null); | ||
| const controlledVisible = 'visible' in props ? visible : popupVisible; | ||
| const [mergedVisible, setMergedVisible] = useControlledState( | ||
| defaultVisible ?? false, | ||
| controlledVisible, | ||
| ); | ||
|
|
||
| const onInternalVisibleChange = useEvent((nextVisible: boolean) => { | ||
| setMergedVisible(nextVisible); | ||
| onVisibleChange?.(nextVisible); | ||
| }); | ||
|
|
||
| useImperativeHandle(ref, () => triggerRef.current); | ||
|
|
||
| const extraProps: Partial<TooltipProps & TriggerProps> = { ...restProps }; | ||
|
|
||
| if ('visible' in props) { | ||
| extraProps.popupVisible = props.visible; | ||
| } | ||
| React.useEffect(() => { | ||
| if (!mergedVisible) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const targetWindow = triggerRef.current?.nativeElement?.ownerDocument.defaultView; | ||
| const onKeyDown = (event: KeyboardEvent) => { | ||
| if (event.key === 'Escape') { | ||
|
Member
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 bypasses Portal's IME guards ( |
||
| onInternalVisibleChange(false); | ||
| } | ||
| }; | ||
|
|
||
| targetWindow?.addEventListener('keydown', onKeyDown); | ||
|
Member
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.
|
||
| return () => targetWindow?.removeEventListener('keydown', onKeyDown); | ||
| }, [mergedVisible, onInternalVisibleChange]); | ||
|
|
||
| // ========================= Arrow ========================== | ||
| // Process arrow configuration | ||
|
|
@@ -148,7 +172,8 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => { | |
| ref={triggerRef} | ||
| popupAlign={align} | ||
| getPopupContainer={getTooltipContainer} | ||
| onOpenChange={onVisibleChange} | ||
| popupVisible={mergedVisible} | ||
|
Member
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. Always passing |
||
| onOpenChange={onInternalVisibleChange} | ||
| afterOpenChange={afterVisibleChange} | ||
| popupMotion={motion} | ||
| defaultPopupVisible={defaultVisible} | ||
|
|
||
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.
While
disabled, Trigger hides the popup but preserves its raw open state. This effect still listens becausemergedVisibleis true, so Escape clears that state and the popup no longer returns when re-enabled.