Standardize rounded glass tooltips across application surfaces

This commit is contained in:
Codex
2026-11-16 14:33:59 +03:00
parent ae82c01049
commit f8aa8022f1
18 changed files with 164 additions and 25 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@nodedc/ui-react",
"version": "0.7.0",
"version": "0.7.1",
"type": "module",
"files": [
"dist"
@@ -21,7 +21,7 @@
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/sortable": "^10.0.0",
"@dnd-kit/utilities": "^3.2.2",
"@nodedc/ui-core": "0.7.0",
"@nodedc/ui-core": "0.7.1",
"lucide-react": "^0.468.0"
},
"peerDependencies": {
+1 -1
View File
@@ -62,7 +62,7 @@ export function Tooltip({children,content,label,focusable=false,disabled=false,p
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})}
{focusable||!isValidElement(children)?children:cloneElement(children as ReactElement<{'aria-describedby'?:string;title?:string}>,{title:undefined,'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>;
+73
View File
@@ -0,0 +1,73 @@
import {createContext,useContext,useEffect,useId,useLayoutEffect,useRef,useState,type CSSProperties,type ReactNode} from 'react';
import {createPortal} from 'react-dom';
import {computeFloatingPosition} from '@nodedc/ui-core';
import {TooltipTitles} from './tooltipTitles.js';
const Installed=createContext(false);
/** One application-wide bridge for native title hints. Nested modules reuse the host. */
export function TooltipProvider({children}:{children?:ReactNode}){
const installed=useContext(Installed);
return installed?<>{children}</>:<Installed.Provider value={true}><TitleTooltips/>{children}</Installed.Provider>;
}
function TitleTooltips(){
const id=`tooltip-layer-${useId().replaceAll(':','')}`;
const [target,setTarget]=useState<{element:Element;text:string}|null>(null);
const [style,setStyle]=useState<CSSProperties>({visibility:'hidden'});
const surface=useRef<HTMLDivElement>(null),titles=useRef(new TooltipTitles());
const hide=useRef(()=>{});
useEffect(()=>{
let anchor:Element|null=null,timer:ReturnType<typeof setTimeout>|undefined,leaving:ReturnType<typeof setTimeout>|undefined,observer:MutationObserver|null=null;
const clear=()=>{clearTimeout(timer);clearTimeout(leaving);};
const close=()=>{clear();observer?.disconnect();observer=null;anchor=null;titles.current.restore();setTarget(null);};hide.current=close;
const show=(element:Element,text:string)=>{
titles.current.describe(element,id);setTarget({element,text});
window.dispatchEvent(new CustomEvent('nodedc-tooltip-open',{detail:id}));
};
const enter=(node:EventTarget|null,keyboard=false)=>{
if(!(node instanceof Element))return;
if(surface.current?.contains(node)){clear();return;}
if(anchor?.contains(node)){clearTimeout(leaving);if(keyboard){const text=titles.current.text(anchor);if(text)show(anchor,text);}return;}
close();
// Explicit Tooltip already owns rich content and its accessible description.
if(node.closest('.nodedc-tooltip-anchor,[data-nodedc-tooltip="off"]'))return;
const found=titles.current.find(node);if(!found)return;
anchor=found.element;titles.current.suppress(anchor);
observer=new MutationObserver(()=>{
if(!anchor)return;
const updated=anchor.getAttribute('data-nodedc-tooltip')??anchor.getAttribute('title');if(updated===null)return;
titles.current.suppress(anchor);if(updated.trim())show(anchor,updated);else close();
});observer.observe(anchor,{attributes:true,attributeFilter:['title','data-nodedc-tooltip']});
if(keyboard)show(anchor,found.text);else timer=setTimeout(()=>{if(anchor)show(anchor,found.text);},220);
};
const leave=(related:EventTarget|null)=>{
if(related instanceof Node&&(anchor?.contains(related)||surface.current?.contains(related)))return;
if(anchor?.contains(document.activeElement))return;
clearTimeout(leaving);leaving=setTimeout(close,140);
};
const over=(event:PointerEvent)=>{if(event.pointerType!=='touch')enter(event.composedPath()[0]??event.target);};
const out=(event:PointerEvent)=>leave(event.relatedTarget);
const focus=(event:FocusEvent)=>enter(event.composedPath()[0]??event.target,true);
const blur=(event:FocusEvent)=>{if(!(event.relatedTarget instanceof Node)||!anchor?.contains(event.relatedTarget))close();};
const down=()=>{clear();setTarget(null);};
const key=(event:KeyboardEvent)=>{if(event.key==='Escape'&&anchor){close();event.preventDefault();event.stopImmediatePropagation();}};
const other=(event:Event)=>{if((event as CustomEvent<string>).detail!==id)close();};
document.addEventListener('pointerover',over,true);document.addEventListener('pointerout',out,true);
document.addEventListener('focusin',focus,true);document.addEventListener('focusout',blur,true);
document.addEventListener('pointerdown',down,true);document.addEventListener('keydown',key,true);
document.addEventListener('fullscreenchange',close);window.addEventListener('blur',close);window.addEventListener('nodedc-tooltip-open',other);
return()=>{close();document.removeEventListener('pointerover',over,true);document.removeEventListener('pointerout',out,true);document.removeEventListener('focusin',focus,true);document.removeEventListener('focusout',blur,true);document.removeEventListener('pointerdown',down,true);document.removeEventListener('keydown',key,true);document.removeEventListener('fullscreenchange',close);window.removeEventListener('blur',close);window.removeEventListener('nodedc-tooltip-open',other);};
},[id]);
useLayoutEffect(()=>{
if(!target)return;
const position=()=>{
if(!surface.current||!target.element.isConnected){hide.current();return;}
const rect=target.element.getBoundingClientRect();if(rect.bottom<=0||rect.top>=window.innerHeight||rect.right<=0||rect.left>=window.innerWidth){hide.current();return;}
const size=surface.current.getBoundingClientRect(),p=computeFloatingPosition({anchor:rect,surfaceWidth:size.width,surfaceHeight:size.height,placement:'bottom-start',offset:8});
setStyle({left:p.left,top:p.top,maxHeight:p.maxHeight,visibility:'visible'});
};
position();const observer=new ResizeObserver(position);observer.observe(target.element);if(surface.current)observer.observe(surface.current);
window.addEventListener('resize',position);window.addEventListener('scroll',position,true);
return()=>{observer.disconnect();window.removeEventListener('resize',position);window.removeEventListener('scroll',position,true);};
},[target]);
return target&&typeof document!=='undefined'?createPortal(<div ref={surface} id={id} role="tooltip" className="nodedc-tooltip" style={style}>{target.text}</div>,document.fullscreenElement??document.body):null;
}
+2
View File
@@ -58,3 +58,5 @@ export {SceneAnnotation} from "./SceneAnnotation.js";
export {LevelButton,type LevelButtonProps} from "./LevelButton.js";
export {Tooltip,type TooltipProps} from "./Tooltip.js";
export {TooltipProvider} from "./TooltipProvider.js";
+20
View File
@@ -0,0 +1,20 @@
/** Suppress native tooltips only during interaction; preserve changing titles and ARIA. */
export class TooltipTitles {
private titles=new Map<Element,string>();
private description:{element:Element;id:string;label?:string}|null=null;
text(element:Element){return element.getAttribute('data-nodedc-tooltip')??this.titles.get(element)??element.getAttribute('title');}
find(start:Element){for(let element:Element|null=start;element;element=element.parentElement){const text=element.getAttribute('data-nodedc-tooltip')??element.getAttribute('title');if(text!==null)return text.trim()?{element,text}:null;}return null;}
suppress(start:Element){for(let element:Element|null=start;element;element=element.parentElement){const text=element.getAttribute('title');if(text!==null){this.titles.set(element,text);element.removeAttribute('title');}}}
describe(element:Element,id:string){
const ids=new Set((element.getAttribute('aria-describedby')??'').split(/\s+/).filter(Boolean));ids.add(id);element.setAttribute('aria-describedby',[...ids].join(' '));
let label=this.description?.label;
if(!element.hasAttribute('aria-label')&&!element.hasAttribute('aria-labelledby')&&!element.textContent?.trim()){
label=this.text(element)??undefined;if(label)element.setAttribute('aria-label',label);
}
this.description={element,id,label};
}
restore(){
for(const [element,text] of this.titles)if(!element.hasAttribute('title'))element.setAttribute('title',text);this.titles.clear();
if(this.description){const {element,id,label}=this.description;const ids=(element.getAttribute('aria-describedby')??'').split(/\s+/).filter(value=>value&&value!==id);if(ids.length)element.setAttribute('aria-describedby',ids.join(' '));else element.removeAttribute('aria-describedby');if(label&&element.getAttribute('aria-label')===label)element.removeAttribute('aria-label');this.description=null;}
}
}