@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
177 lines (174 loc) • 5.54 kB
JavaScript
import React from 'react';
import cx from 'clsx';
import { useClickOutside, useFocusTrap, useReducedMotion, useId } from '@mantine/hooks';
import { Transition } from '../Transition/Transition.js';
import { ArrowBody } from '../ArrowBody/ArrowBody.js';
import { CloseButton } from '../ActionIcon/CloseButton/CloseButton.js';
import { Text } from '../Text/Text.js';
import useStyles from './Popover.styles.js';
import { useMantineTheme } from '../../theme/use-mantine-theme/use-mantine-theme.js';
import { mergeStyles } from '../../theme/utils/merge-styles/merge-styles.js';
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
function Popover(_a) {
var _b = _a, {
className,
style,
themeOverride,
children,
target,
title,
onClose,
opened,
zIndex = 1e3,
arrowSize = 4,
withArrow = false,
transition = "fade",
transitionDuration = 200,
transitionTimingFunction,
gutter = 10,
position = "left",
placement = "center",
disabled = false,
noClickOutside = false,
noFocusTrap = false,
noEscape = false,
withCloseButton = false,
radius = "sm",
spacing = "md",
shadow = "sm",
closeButtonLabel,
id,
classNames,
styles
} = _b, others = __objRest(_b, [
"className",
"style",
"themeOverride",
"children",
"target",
"title",
"onClose",
"opened",
"zIndex",
"arrowSize",
"withArrow",
"transition",
"transitionDuration",
"transitionTimingFunction",
"gutter",
"position",
"placement",
"disabled",
"noClickOutside",
"noFocusTrap",
"noEscape",
"withCloseButton",
"radius",
"spacing",
"shadow",
"closeButtonLabel",
"id",
"classNames",
"styles"
]);
const theme = useMantineTheme(themeOverride);
const classes = useStyles({ theme, radius, spacing, shadow }, classNames, "popover");
const _styles = mergeStyles(classes, styles);
const handleClose = () => typeof onClose === "function" && onClose();
const useClickOutsideRef = useClickOutside(() => !noClickOutside && handleClose());
const focusTrapRef = useFocusTrap(!noFocusTrap);
const reduceMotion = useReducedMotion();
const handleKeydown = (event) => {
if (!noEscape && event.nativeEvent.code === "Escape") {
handleClose();
}
};
const uuid = useId(id);
const titleId = `${uuid}-title`;
const bodyId = `${uuid}-body`;
return /* @__PURE__ */ React.createElement("div", __spreadValues({
className: cx(classes.root, className),
ref: useClickOutsideRef,
id,
style: __spreadValues(__spreadValues({}, style), _styles.root)
}, others), /* @__PURE__ */ React.createElement(Transition, {
mounted: opened && !disabled,
transition,
duration: reduceMotion ? 0 : transitionDuration,
timingFunction: transitionTimingFunction || theme.transitionTimingFunction
}, (transitionStyles) => /* @__PURE__ */ React.createElement("div", {
role: "dialog",
tabIndex: -1,
"aria-labelledby": titleId,
"aria-describedby": bodyId,
className: classes.wrapper,
style: __spreadValues(__spreadValues({}, transitionStyles), _styles.wrapper),
ref: focusTrapRef,
onKeyDownCapture: handleKeydown
}, /* @__PURE__ */ React.createElement(ArrowBody, {
withArrow,
arrowSize,
position,
placement,
gutter,
className: classes.popover,
classNames: { arrow: classes.arrow },
styles: { arrow: _styles.arrow },
style: __spreadValues({ zIndex }, _styles.popover)
}, /* @__PURE__ */ React.createElement("div", {
className: classes.body,
style: _styles.body
}, !!title && /* @__PURE__ */ React.createElement("div", {
className: classes.header,
style: _styles.header
}, /* @__PURE__ */ React.createElement(Text, {
size: "sm",
id: titleId,
className: classes.title,
style: _styles.title
}, title)), withCloseButton && /* @__PURE__ */ React.createElement(CloseButton, {
themeOverride,
size: "sm",
onClick: handleClose,
"aria-label": closeButtonLabel,
className: classes.close,
style: _styles.close
}), /* @__PURE__ */ React.createElement("div", {
className: classes.inner,
id: bodyId,
style: _styles.inner
}, children))))), /* @__PURE__ */ React.createElement("div", {
className: classes.target,
style: _styles.target
}, target));
}
Popover.displayName = "@mantine/core/Popover";
export { Popover };
//# sourceMappingURL=Popover.js.map