mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:29:37 +08:00
* fix(web): refine knowledge and settings accessibility and layout Name row controls from their visible titles, portal tooltip descriptions out of tablists, and make the knowledge tabs keyboard-complete. Also stack the KB master-detail on phones, add a section-scroll hint, demote the indexing diagnostic to its detail tab, and consolidate the knowledge type scale. * perf(web): cut knowledge and settings payload Serve raster engine marks through next/image, stop preloading the serif face on pages that do not use it, and prefetch settings routes only on hover or keyboard focus instead of on first paint. * docs(perf): record knowledge and settings audit Document the protocol, baseline and after medians, payload and accessibility results, limits, and reproduction commands, with mobile before/after screenshots. * test(web): match the model row's visible accessible name The row no longer overrides its name with a provider-first aria-label; select it by the model-first text a screen reader now reads, which also contains the visible default/task badge. * fix(web): keep the MinerU download-type name in the locale catalog Reuse the existing Download models and Type strings instead of introducing a new t() literal, so the strict i18n audit stays green. * docs(perf): link the UI audit from the handbook index The docs contract requires every guide under docs-for-dev to be reachable from README.md. * docs(perf): record the isolated knowledge-detail LCP Re-measured the detail route with the host pinned away from unrelated work; the 668 ms row was contention, while the isolated median is 568 ms and the stable repeats overlap the baseline. --------- Co-authored-by: Antonio-Faure <209759495+Antonio-Faure@users.noreply.github.com>
214 lines
6.3 KiB
TypeScript
214 lines
6.3 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
cloneElement,
|
|
isValidElement,
|
|
lazy,
|
|
Suspense,
|
|
useCallback,
|
|
useEffect,
|
|
useId,
|
|
useRef,
|
|
useState,
|
|
useSyncExternalStore,
|
|
type ReactElement,
|
|
} from "react";
|
|
import { createPortal } from "react-dom";
|
|
import type { TooltipSide } from "./tooltip-position";
|
|
import type { Shortcut } from "@/lib/shortcuts";
|
|
|
|
const loadTooltipLayer = () => import("./TooltipLayer");
|
|
const TooltipLayer = lazy(loadTooltipLayer);
|
|
const subscribeToNothing = () => () => {};
|
|
|
|
function isNestedControl(target: EventTarget): boolean {
|
|
return target instanceof Element &&
|
|
Boolean(target.closest("button, a, input, select, textarea"));
|
|
}
|
|
|
|
export interface TooltipProps {
|
|
label: string;
|
|
description?: string;
|
|
children: ReactElement<{ "aria-describedby"?: string }>;
|
|
/** Choose a semantic wrapper when a span cannot contain the trigger. */
|
|
as?: "span" | "div" | "li";
|
|
className?: string;
|
|
side?: TooltipSide;
|
|
delay?: number;
|
|
/** Suppress a tooltip while its trigger owns an open popover or menu. */
|
|
suppressed?: boolean;
|
|
/** Keys that trigger the same action, printed after the label. */
|
|
shortcut?: Shortcut;
|
|
}
|
|
|
|
export function Tooltip({
|
|
label,
|
|
description,
|
|
children,
|
|
as: Wrapper = "span",
|
|
className,
|
|
side = "bottom",
|
|
delay = 180,
|
|
suppressed = false,
|
|
shortcut,
|
|
}: TooltipProps) {
|
|
const id = useId();
|
|
const wrapperRef = useRef<HTMLElement>(null);
|
|
const setWrapperRef = useCallback((node: HTMLElement | null) => {
|
|
wrapperRef.current = node;
|
|
}, []);
|
|
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
const hoverRef = useRef(false);
|
|
const keyboardFocusRef = useRef(false);
|
|
const pointerFocusRef = useRef(false);
|
|
const touchRef = useRef(false);
|
|
const [visible, setVisible] = useState(false);
|
|
const [layerMounted, setLayerMounted] = useState(false);
|
|
// The description must live outside wrappers that enforce a role's allowed
|
|
// children (a `role="tooltip"` inside a `tablist` is invalid). Portalling it
|
|
// after hydration keeps SSR deterministic and keeps `aria-describedby` valid.
|
|
const portalReady = useSyncExternalStore(
|
|
subscribeToNothing,
|
|
() => true,
|
|
() => false,
|
|
);
|
|
const renderedVisible = visible && !suppressed;
|
|
const markLayerMounted = useCallback(() => setLayerMounted(true), []);
|
|
|
|
const clearTimer = useCallback(() => {
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
timerRef.current = null;
|
|
}, []);
|
|
const hide = useCallback(() => {
|
|
clearTimer();
|
|
touchRef.current = false;
|
|
setVisible(false);
|
|
}, [clearTimer]);
|
|
const show = useCallback(
|
|
(immediate: boolean) => {
|
|
clearTimer();
|
|
if (suppressed) return;
|
|
void loadTooltipLayer();
|
|
if (immediate) setVisible(true);
|
|
else timerRef.current = setTimeout(() => setVisible(true), delay);
|
|
},
|
|
[clearTimer, delay, suppressed],
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!suppressed) return;
|
|
const timeout = window.setTimeout(hide, 0);
|
|
return () => window.clearTimeout(timeout);
|
|
}, [hide, suppressed]);
|
|
useEffect(() => clearTimer, [clearTimer]);
|
|
|
|
useEffect(() => {
|
|
if (!renderedVisible) return;
|
|
const dismissTouch = (event: PointerEvent) => {
|
|
if (
|
|
touchRef.current &&
|
|
wrapperRef.current &&
|
|
!wrapperRef.current.contains(event.target as Node)
|
|
) {
|
|
hide();
|
|
}
|
|
};
|
|
document.addEventListener("pointerdown", dismissTouch, true);
|
|
return () => document.removeEventListener("pointerdown", dismissTouch, true);
|
|
}, [hide, renderedVisible]);
|
|
|
|
if (!isValidElement(children)) return children;
|
|
const describedBy = [children.props["aria-describedby"], id]
|
|
.filter(Boolean)
|
|
.join(" ");
|
|
const tooltipText = description ? `${label}. ${description}` : label;
|
|
|
|
return (
|
|
<Wrapper
|
|
ref={setWrapperRef}
|
|
className={Wrapper === "span" ? `inline-flex ${className ?? ""}` : className}
|
|
onPointerEnter={(event) => {
|
|
if (event.pointerType === "touch") return;
|
|
if (Wrapper === "li" && isNestedControl(event.target)) return;
|
|
hoverRef.current = true;
|
|
show(false);
|
|
}}
|
|
onPointerOverCapture={(event) => {
|
|
if (Wrapper !== "li" || !isNestedControl(event.target)) return;
|
|
hoverRef.current = false;
|
|
hide();
|
|
}}
|
|
onPointerLeave={(event) => {
|
|
if (event.pointerType === "touch") return;
|
|
hoverRef.current = false;
|
|
if (!keyboardFocusRef.current) hide();
|
|
}}
|
|
onPointerDown={(event) => {
|
|
pointerFocusRef.current = true;
|
|
if (Wrapper === "li" && isNestedControl(event.target)) {
|
|
hide();
|
|
return;
|
|
}
|
|
if (event.pointerType !== "touch") return;
|
|
touchRef.current = true;
|
|
if (renderedVisible) hide();
|
|
else {
|
|
show(true);
|
|
timerRef.current = setTimeout(hide, 3000);
|
|
}
|
|
}}
|
|
onPointerUp={() => {
|
|
queueMicrotask(() => {
|
|
pointerFocusRef.current = false;
|
|
});
|
|
}}
|
|
onFocusCapture={(event) => {
|
|
if (Wrapper === "li" && isNestedControl(event.target)) {
|
|
hide();
|
|
return;
|
|
}
|
|
if (pointerFocusRef.current) return;
|
|
keyboardFocusRef.current = true;
|
|
show(true);
|
|
}}
|
|
onBlurCapture={(event) => {
|
|
if (event.currentTarget.contains(event.relatedTarget as Node | null)) return;
|
|
keyboardFocusRef.current = false;
|
|
if (!hoverRef.current) hide();
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (event.key === "Escape") hide();
|
|
}}
|
|
>
|
|
{cloneElement(children, { "aria-describedby": describedBy })}
|
|
{portalReady && (!renderedVisible || !layerMounted)
|
|
? createPortal(
|
|
<span id={id} role="tooltip" className="sr-only">
|
|
{tooltipText}
|
|
</span>,
|
|
document.body,
|
|
)
|
|
: null}
|
|
{renderedVisible && typeof document !== "undefined"
|
|
? (
|
|
<Suspense fallback={null}>
|
|
<TooltipLayer
|
|
anchorRef={wrapperRef}
|
|
id={id}
|
|
label={label}
|
|
description={description}
|
|
shortcut={shortcut}
|
|
side={side}
|
|
onMount={markLayerMounted}
|
|
/>
|
|
</Suspense>
|
|
)
|
|
: null}
|
|
</Wrapper>
|
|
);
|
|
}
|
|
|
|
export { placeTooltip, type TooltipSide } from "./tooltip-position";
|
|
|
|
export default Tooltip;
|