@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
58 lines (56 loc) • 1.52 kB
JavaScript
'use client';
import * as React from 'react';
import { useIsoLayoutEffect } from '@base-ui-components/utils/useIsoLayoutEffect';
import { useId } from '@base-ui-components/utils/useId';
import { useToastRootContext } from "../root/ToastRootContext.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
/**
* A title that labels the toast.
* Renders an `<h2>` element.
*
* Documentation: [Base UI Toast](https://base-ui.com/react/components/toast)
*/
export const ToastTitle = /*#__PURE__*/React.forwardRef(function ToastTitle(componentProps, forwardedRef) {
const {
render,
className,
id: idProp,
children: childrenProp,
...elementProps
} = componentProps;
const {
toast
} = useToastRootContext();
const children = childrenProp ?? toast.title;
const shouldRender = Boolean(children);
const id = useId(idProp);
const {
setTitleId
} = useToastRootContext();
useIsoLayoutEffect(() => {
if (!shouldRender) {
return undefined;
}
setTitleId(id);
return () => {
setTitleId(undefined);
};
}, [shouldRender, id, setTitleId]);
const state = React.useMemo(() => ({
type: toast.type
}), [toast.type]);
const element = useRenderElement('h2', componentProps, {
ref: forwardedRef,
state,
props: {
...elementProps,
id,
children
}
});
if (!shouldRender) {
return null;
}
return element;
});
if (process.env.NODE_ENV !== "production") ToastTitle.displayName = "ToastTitle";