@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
78 lines • 3.13 kB
JavaScript
;
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.useExpandable = void 0;
const react_1 = require("react");
/**
* A hook to manage the expandable state of a content container.
* @param maxHeight - The maximum height of the content container.
* @param dependency - A dependency to watch for changes.
* @returns An object containing the container height, whether the content is overflowing, and the expandable state.
*/
const useExpandable = (maxHeight, dependency) => {
const ref = (0, react_1.useRef)(null);
const [containerHeight, setContainerHeight] = (0, react_1.useState)(`${maxHeight}px`);
const [isExpanded, setIsExpanded] = (0, react_1.useState)(maxHeight === undefined);
const [hasOverflowingContent, setHasOverflowingContent] = (0, react_1.useState)(false);
(0, react_1.useEffect)(() => {
var _a, _b, _c, _d;
if (((_b = (_a = ref.current) === null || _a === void 0 ? void 0 : _a.scrollHeight) !== null && _b !== void 0 ? _b : 0) <= ((_d = (_c = ref.current) === null || _c === void 0 ? void 0 : _c.clientHeight) !== null && _d !== void 0 ? _d : 0)) {
setIsExpanded(true);
}
else {
setIsExpanded(false);
}
}, []);
(0, react_1.useLayoutEffect)(() => {
var _a, _b, _c, _d;
if (maxHeight === undefined) {
return;
}
if (((_b = (_a = ref.current) === null || _a === void 0 ? void 0 : _a.clientHeight) !== null && _b !== void 0 ? _b : 0) >= ((_d = (_c = ref.current) === null || _c === void 0 ? void 0 : _c.scrollHeight) !== null && _d !== void 0 ? _d : 0)) {
setContainerHeight(`fit-content`);
setHasOverflowingContent(false);
}
else {
setContainerHeight(`${maxHeight}px`);
setHasOverflowingContent(true);
}
}, [maxHeight, dependency]);
const toggleExpand = () => {
if (isExpanded) {
setContainerHeight(`${maxHeight}px`);
setIsExpanded(false);
}
else {
setContainerHeight(`fit-content`);
setIsExpanded(true);
}
};
return {
containerHeight,
hasOverflowingContent,
isExpanded,
ref,
toggleExpand,
};
};
exports.useExpandable = useExpandable;
//# sourceMappingURL=use-expandable.js.map