const PageTitle = ({title}) => {
const titleHeaderRef = useRef(null as null | HTMLHeadingElement);
useEffect(() => {
if (titleHeaderRef.current !== null) {
// First, make the element focus-able.
titleHeaderRef.current.setAttribute('tabIndex', '-1');
// Reset focus to this primary header.
titleHeaderRef.current.focus();
titleHeaderRef.current.blur();
// Finally, make the element non-focus-able.
titleHeaderRef.current.removeAttribute('tabIndex');
// To see this behavior in action, wrap this dom calls in a 5 second setTimeout, and tab around the page before this timeout is hit.
}
}, []);
return (
<Typography ref={titleHeaderRef}>
{title}
</Typography>
);
};
1条答案
按热度按时间qlfbtfca1#