feat(ui): add accessible tooltips and enforce compact operational typography
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import {cloneElement,isValidElement,useCallback,useEffect,useId,useLayoutEffect,useRef,useState,type CSSProperties,type ReactNode,type ReactElement} from 'react';
|
||||
import {createPortal} from 'react-dom';
|
||||
import {computeFloatingPosition,type FloatingPlacement} from '@nodedc/ui-core';
|
||||
import {cn} from './cn.js';
|
||||
|
||||
export interface TooltipProps {
|
||||
children:ReactNode;
|
||||
content:ReactNode;
|
||||
label?:string;
|
||||
/** Use for a read-only graphic; buttons already supply their own focus target. */
|
||||
focusable?:boolean;
|
||||
disabled?:boolean;
|
||||
placement?:FloatingPlacement;
|
||||
className?:string;
|
||||
}
|
||||
|
||||
/** Non-interactive explanation; selection/actions belong to Dropdown. */
|
||||
export function Tooltip({children,content,label,focusable=false,disabled=false,placement='top-start',className}:TooltipProps){
|
||||
const id=`tooltip-${useId().replaceAll(':','')}`;
|
||||
const anchor=useRef<HTMLSpanElement>(null),surface=useRef<HTMLDivElement>(null);
|
||||
const timer=useRef<ReturnType<typeof setTimeout>|undefined>(undefined);
|
||||
const [open,setOpen]=useState(false),[style,setStyle]=useState<CSSProperties>({visibility:'hidden'});
|
||||
const clear=useCallback(()=>{clearTimeout(timer.current);},[]);
|
||||
const close=useCallback(()=>{clear();setOpen(false);},[clear]);
|
||||
const show=useCallback(()=>{clear();if(!disabled)setOpen(true);},[clear,disabled]);
|
||||
const leave=()=>{
|
||||
clear();
|
||||
if(anchor.current?.contains(document.activeElement))return;
|
||||
timer.current=setTimeout(close,140);
|
||||
};
|
||||
useEffect(()=>()=>clear(),[clear]);
|
||||
useEffect(()=>{if(disabled)close();},[disabled,close]);
|
||||
useLayoutEffect(()=>{
|
||||
if(!open)return;
|
||||
const position=()=>{
|
||||
if(!anchor.current||!surface.current)return;
|
||||
const rect=anchor.current.getBoundingClientRect(),size=surface.current.getBoundingClientRect();
|
||||
if(rect.bottom<=0||rect.top>=window.innerHeight||rect.right<=0||rect.left>=window.innerWidth){close();return;}
|
||||
const p=computeFloatingPosition({anchor:rect,surfaceWidth:size.width,surfaceHeight:size.height,placement,offset:8});
|
||||
setStyle({left:p.left,top:p.top,maxHeight:p.maxHeight,visibility:'visible'});
|
||||
};
|
||||
position();
|
||||
const observer=new ResizeObserver(position);
|
||||
if(anchor.current)observer.observe(anchor.current);
|
||||
if(surface.current)observer.observe(surface.current);
|
||||
window.addEventListener('resize',position);window.addEventListener('scroll',position,true);
|
||||
document.addEventListener('fullscreenchange',close);
|
||||
return()=>{observer.disconnect();window.removeEventListener('resize',position);window.removeEventListener('scroll',position,true);document.removeEventListener('fullscreenchange',close);};
|
||||
},[open,placement,close]);
|
||||
useEffect(()=>{
|
||||
if(!open)return;
|
||||
const key=(e:KeyboardEvent)=>{if(e.key==='Escape'){close();e.preventDefault();e.stopImmediatePropagation();}};
|
||||
const outside=(e:PointerEvent)=>{if(e.target instanceof Node&&!anchor.current?.contains(e.target)&&!surface.current?.contains(e.target))close();};
|
||||
const other=(e:Event)=>{if((e as CustomEvent<string>).detail!==id)close();};
|
||||
document.addEventListener('keydown',key,true);document.addEventListener('pointerdown',outside,true);
|
||||
window.addEventListener('nodedc-tooltip-open',other);
|
||||
window.dispatchEvent(new CustomEvent('nodedc-tooltip-open',{detail:id}));
|
||||
return()=>{document.removeEventListener('keydown',key,true);document.removeEventListener('pointerdown',outside,true);window.removeEventListener('nodedc-tooltip-open',other);};
|
||||
},[open,id,close]);
|
||||
return <span ref={anchor} className={cn('nodedc-tooltip-anchor',className)}
|
||||
tabIndex={focusable&&!disabled?0:undefined} role={focusable?'img':undefined} aria-label={label}
|
||||
aria-describedby={open?id:undefined} onPointerEnter={e=>{if(e.pointerType!=='touch')show();}}
|
||||
onPointerLeave={leave} onFocus={show} onBlur={e=>{if(!e.currentTarget.contains(e.relatedTarget)&&!e.currentTarget.matches(':hover'))close();}}
|
||||
onPointerDown={e=>{if(e.pointerType==='touch'){if(open)close();else show();}}}>
|
||||
{focusable||!isValidElement(children)?children:cloneElement(children as ReactElement<{'aria-describedby'?:string}>,{'aria-describedby':[children.props && (children.props as {'aria-describedby'?:string})['aria-describedby'],open?id:undefined].filter(Boolean).join(' ')||undefined})}
|
||||
{open&&typeof document!=='undefined'&&createPortal(<div ref={surface} id={id} role="tooltip"
|
||||
className="nodedc-tooltip" style={style} onPointerEnter={clear} onPointerLeave={leave}>{content}</div>,document.fullscreenElement??document.body)}
|
||||
</span>;
|
||||
}
|
||||
@@ -56,3 +56,5 @@ export {useApplicationPanelFullscreen} from "./ApplicationPanelFullscreen.js";
|
||||
export {SceneAnnotation} from "./SceneAnnotation.js";
|
||||
|
||||
export {LevelButton,type LevelButtonProps} from "./LevelButton.js";
|
||||
|
||||
export {Tooltip,type TooltipProps} from "./Tooltip.js";
|
||||
|
||||
Reference in New Issue
Block a user