@signalwire/docusaurus-theme-llms-txt
Version:
Docusaurus theme components for llms-txt plugin including CopyPageButton
45 lines (44 loc) • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useDropdownState;
/**
* Copyright (c) SignalWire, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const react_1 = require("react");
const router_1 = require("@docusaurus/router");
function useDropdownState() {
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
const dropdownRef = (0, react_1.useRef)(null);
const location = (0, router_1.useLocation)();
const pathname = location.pathname;
// Close dropdown on route change
(0, react_1.useEffect)(() => {
setIsOpen(false);
}, [pathname]);
// Close dropdown when clicking outside
(0, react_1.useEffect)(() => {
function handleClickOutside(event) {
if (dropdownRef.current &&
!dropdownRef.current.contains(event.target)) {
setIsOpen(false);
}
}
if (isOpen) {
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}
return undefined;
}, [isOpen]);
const toggleDropdown = () => {
setIsOpen(!isOpen);
};
return {
isOpen,
setIsOpen,
toggleDropdown,
dropdownRef,
};
}