@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.
51 lines (49 loc) • 1.46 kB
JavaScript
'use client';
import * as React from 'react';
import { useDialogRootContext } from "../root/DialogRootContext.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
import { useButton } from "../../use-button/index.js";
import { createChangeEventDetails } from "../../utils/createBaseUIEventDetails.js";
import { REASONS } from "../../utils/reasons.js";
/**
* A button that closes the dialog.
* Renders a `<button>` element.
*
* Documentation: [Base UI Dialog](https://base-ui.com/react/components/dialog)
*/
export const DialogClose = /*#__PURE__*/React.forwardRef(function DialogClose(componentProps, forwardedRef) {
const {
render,
className,
disabled = false,
nativeButton = true,
...elementProps
} = componentProps;
const {
store
} = useDialogRootContext();
const open = store.useState('open');
function handleClick(event) {
if (open) {
store.setOpen(false, createChangeEventDetails(REASONS.closePress, event.nativeEvent));
}
}
const {
getButtonProps,
buttonRef
} = useButton({
disabled,
native: nativeButton
});
const state = React.useMemo(() => ({
disabled
}), [disabled]);
return useRenderElement('button', componentProps, {
state,
ref: [forwardedRef, buttonRef],
props: [{
onClick: handleClick
}, elementProps, getButtonProps]
});
});
if (process.env.NODE_ENV !== "production") DialogClose.displayName = "DialogClose";