import { Popover as _Popover } from '@base-ui/react/popover';
import { useMergeRefs } from '@wordpress/compose';
import { forwardRef, useEffect, useRef } from '@wordpress/element';
import { Text } from '../text';
import { usePopoverValidationContext } from './context';
import styles from './style.module.scss';
import type { TitleProps } from './types';
/**
* Renders a heading that labels the popover for accessibility.
*
* **Required** — every popover must include a `Popover.Title`, even if
* visually hidden. The rendered element is linked to the popup via
* `aria-labelledby`. Uses the `heading-xl` text variant, matching Dialog.
*
* To visually hide the title while keeping it accessible, wrap it with
* `VisuallyHidden` using the `render` prop:
*
* ```jsx
* }>
* Accessible title text
*
* ```
*/
const Title = forwardRef< HTMLHeadingElement, TitleProps >(
function PopoverTitle( { children, ...props }, forwardedRef ) {
const validationContext = usePopoverValidationContext();
const internalRef = useRef< HTMLHeadingElement >( null );
const mergedRef = useMergeRefs( [ internalRef, forwardedRef ] );
useEffect( () => {
if ( validationContext ) {
return validationContext.registerTitle( internalRef.current );
}
return undefined;
}, [ validationContext ] );
return (
}
className={ styles.title }
>
{ children }
);
}
);
export { Title };