@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.
59 lines (57 loc) • 1.66 kB
JavaScript
'use client';
import * as React from 'react';
import { useId } from '@base-ui-components/utils/useId';
import { useIsoLayoutEffect } from '@base-ui-components/utils/useIsoLayoutEffect';
import { useToastRootContext } from "../root/ToastRootContext.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
/**
* A description that describes the toast.
* Can be used as the default message for the toast when no title is provided.
* Renders a `<p>` element.
*
* Documentation: [Base UI Toast](https://base-ui.com/react/components/toast)
*/
export const ToastDescription = /*#__PURE__*/React.forwardRef(function ToastDescription(componentProps, forwardedRef) {
const {
render,
className,
id: idProp,
children: childrenProp,
...elementProps
} = componentProps;
const {
toast
} = useToastRootContext();
const children = childrenProp ?? toast.description;
const shouldRender = Boolean(children);
const id = useId(idProp);
const {
setDescriptionId
} = useToastRootContext();
useIsoLayoutEffect(() => {
if (!shouldRender) {
return undefined;
}
setDescriptionId(id);
return () => {
setDescriptionId(undefined);
};
}, [shouldRender, id, setDescriptionId]);
const state = React.useMemo(() => ({
type: toast.type
}), [toast.type]);
const element = useRenderElement('p', componentProps, {
ref: forwardedRef,
state,
props: {
...elementProps,
id,
children
}
});
if (!shouldRender) {
return null;
}
return element;
});
if (process.env.NODE_ENV !== "production") ToastDescription.displayName = "ToastDescription";