-
-
Notifications
You must be signed in to change notification settings - Fork 198
feat: better a11y #509
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
feat: better a11y #509
Changes from 6 commits
dece1b0
b7388d4
2a4f330
625204d
e081d1b
3c7e909
095b9cb
e505c88
fe2e017
403dd52
e1082bf
ac227c5
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 |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ import type { ActionType, AlignType } from '@rc-component/trigger/lib/interface' | |
| import useId from '@rc-component/util/lib/hooks/useId'; | ||
| import { clsx } from 'clsx'; | ||
| import * as React from 'react'; | ||
| import { useImperativeHandle, useRef } from 'react'; | ||
| import { useImperativeHandle, useRef, useEffect, useCallback } from 'react'; | ||
| import { placements } from './placements'; | ||
| import Popup from './Popup'; | ||
|
|
||
|
|
@@ -79,6 +79,7 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => { | |
| showArrow = true, | ||
| classNames, | ||
| styles, | ||
| forceRender, | ||
| ...restProps | ||
| } = props; | ||
|
|
||
|
|
@@ -93,6 +94,51 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => { | |
| extraProps.popupVisible = props.visible; | ||
| } | ||
|
|
||
| const isControlled = 'visible' in props; | ||
| const mergedVisible = props.visible; | ||
| const [popupMounted, setPopupMounted] = React.useState(() => { | ||
| if (forceRender) { | ||
| return true; | ||
| } | ||
| if (isControlled) { | ||
| return mergedVisible; | ||
| } | ||
| return defaultVisible; | ||
| }); | ||
|
|
||
| const updatePopupMounted = useCallback( | ||
| (nextVisible: boolean) => { | ||
| setPopupMounted((prev) => { | ||
| if (nextVisible) { | ||
| return true; | ||
| } | ||
|
|
||
| if (destroyOnHidden) { | ||
| return false; | ||
| } | ||
|
|
||
| return prev; | ||
| }); | ||
| }, | ||
| [destroyOnHidden], | ||
| ); | ||
|
|
||
| const handleVisibleChange = (nextVisible: boolean) => { | ||
|
||
| updatePopupMounted(nextVisible); | ||
| onVisibleChange?.(nextVisible); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (forceRender) { | ||
| setPopupMounted(true); | ||
| return; | ||
| } | ||
|
|
||
| if (isControlled) { | ||
| updatePopupMounted(mergedVisible); | ||
| } | ||
| }, [forceRender, isControlled, mergedVisible, updatePopupMounted]); | ||
|
|
||
| // ========================= Arrow ========================== | ||
| // Process arrow configuration | ||
| const mergedArrow = React.useMemo(() => { | ||
|
|
@@ -118,7 +164,7 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => { | |
| const originalProps = child?.props || {}; | ||
| const childProps = { | ||
| ...originalProps, | ||
| 'aria-describedby': overlay ? mergedId : null, | ||
| 'aria-describedby': overlay && popupMounted ? mergedId : null, | ||
| }; | ||
| return React.cloneElement<any>(children, childProps) as any; | ||
| }; | ||
|
|
@@ -145,10 +191,11 @@ const Tooltip = React.forwardRef<TooltipRef, TooltipProps>((props, ref) => { | |
| ref={triggerRef} | ||
| popupAlign={align} | ||
| getPopupContainer={getTooltipContainer} | ||
| onOpenChange={onVisibleChange} | ||
| onOpenChange={handleVisibleChange} | ||
| afterOpenChange={afterVisibleChange} | ||
| popupMotion={motion} | ||
| defaultPopupVisible={defaultVisible} | ||
| forceRender={forceRender} | ||
|
||
| autoDestroy={destroyOnHidden} | ||
| mouseLeaveDelay={mouseLeaveDelay} | ||
| popupStyle={styles?.root} | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||
| "declaration": true, | ||||||
| "skipLibCheck": true, | ||||||
| "esModuleInterop": true, | ||||||
| "types": ["@testing-library/jest-dom"], | ||||||
|
Contributor
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. By adding the
Suggested change
Rules ReferencesFootnotes
|
||||||
| "paths": { | ||||||
| "@/*": [ | ||||||
| "src/*" | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.