@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
79 lines (78 loc) • 2.92 kB
JavaScript
"use client";
import { rem } from "../../core/utils/units-converters/rem.mjs";
import { createVarsResolver } from "../../core/styles-api/create-vars-resolver/create-vars-resolver.mjs";
import { useProps } from "../../core/MantineProvider/use-props/use-props.mjs";
import { useStyles } from "../../core/styles-api/use-styles/use-styles.mjs";
import { factory } from "../../core/factory/factory.mjs";
import { Box } from "../../core/Box/Box.mjs";
import { Anchor } from "../Anchor/Anchor.mjs";
import Spoiler_module_default from "./Spoiler.module.mjs";
import { useElementSize, useId, useUncontrolled } from "@mantine/hooks";
import { jsx, jsxs } from "react/jsx-runtime";
//#region packages/@mantine/core/src/components/Spoiler/Spoiler.tsx
const defaultProps = {
maxHeight: 100,
defaultExpanded: false
};
const varsResolver = createVarsResolver((_, { transitionDuration }) => ({ root: { "--spoiler-transition-duration": transitionDuration !== void 0 ? `${transitionDuration}ms` : void 0 } }));
const Spoiler = factory((_props) => {
const props = useProps("Spoiler", defaultProps, _props);
const { classNames, className, style, styles, unstyled, vars, defaultExpanded, maxHeight, hideLabel, showLabel, children, controlRef, transitionDuration, id, expanded, onExpandedChange, showAriaLabel, hideAriaLabel, attributes, ...others } = props;
const getStyles = useStyles({
name: "Spoiler",
classes: Spoiler_module_default,
props,
className,
style,
classNames,
styles,
unstyled,
attributes,
vars,
varsResolver
});
const _id = useId(id);
const regionId = `${_id}-region`;
const [show, setShowState] = useUncontrolled({
value: expanded,
defaultValue: defaultExpanded,
finalValue: false,
onChange: onExpandedChange
});
const { ref: contentRef, height } = useElementSize();
const spoilerMoreContent = show ? hideLabel : showLabel;
const spoiler = spoilerMoreContent !== null && maxHeight < height;
const ariaLabel = show ? hideAriaLabel : showAriaLabel;
return /* @__PURE__ */ jsxs(Box, {
...getStyles("root"),
id: _id,
"data-has-spoiler": spoiler || void 0,
...others,
children: [spoiler && /* @__PURE__ */ jsx(Anchor, {
component: "button",
type: "button",
ref: controlRef,
onClick: () => setShowState(!show),
"aria-expanded": show,
"aria-controls": regionId,
"aria-label": ariaLabel,
...getStyles("control"),
children: spoilerMoreContent
}), /* @__PURE__ */ jsx("div", {
...getStyles("content", { style: { maxHeight: !show ? rem(maxHeight) : height ? rem(height) : void 0 } }),
"data-reduce-motion": true,
role: "region",
id: regionId,
children: /* @__PURE__ */ jsx("div", {
ref: contentRef,
children
})
})]
});
});
Spoiler.classes = Spoiler_module_default;
Spoiler.varsResolver = varsResolver;
Spoiler.displayName = "@mantine/core/Spoiler";
//#endregion
export { Spoiler };
//# sourceMappingURL=Spoiler.mjs.map