@makeen.io/material-ui-kit
Version:
Makeen UI components kit. Based on material-ui.
42 lines • 2.41 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; /* eslint-disable @typescript-eslint/no-empty-function */
import React, { useContext } from "react";
import ClickAwayListener from "@material-ui/core/ClickAwayListener";
import { useAfterFirstRender, useStateWithHistory } from "../../hooks";
var ToggleStateContext = /*#__PURE__*/React.createContext(false);
var ToggleDispatchContext = /*#__PURE__*/React.createContext(function () {});
var ToggleButton = function ToggleButton(_ref) {var children = _ref.children;
var visible = useContext(ToggleStateContext);
var setVisibility = useContext(ToggleDispatchContext);
if (typeof children === "function") {
return children(visible, setVisibility);
}
return React.Children.map(children, function (elem) {return /*#__PURE__*/React.cloneElement(elem, { onClick: function onClick() {return setVisibility(function (prev) {return !prev;});} });});
};
var Content = function Content(_ref2) {var children = _ref2.children;
var visible = useContext(ToggleStateContext);
var setVisibility = useContext(ToggleDispatchContext);
if (typeof children === "function") {
return children(visible, setVisibility);
}
return visible ? children : null;
};
var Toggle = function Toggle(_ref3) {var children = _ref3.children,onOpen = _ref3.onOpen,onClose = _ref3.onClose;var _useStateWithHistory =
useStateWithHistory(false),_useStateWithHistory2 = _slicedToArray(_useStateWithHistory, 3),visible = _useStateWithHistory2[0],prevVisiblity = _useStateWithHistory2[1],setVisibility = _useStateWithHistory2[2];
useAfterFirstRender(function () {
if (visible !== prevVisiblity) {
if (visible) {
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
} else
{
onClose === null || onClose === void 0 ? void 0 : onClose();
}
}
}, [visible, onOpen, onClose]);
return /*#__PURE__*/React.createElement(ToggleDispatchContext.Provider, { value: setVisibility }, /*#__PURE__*/
React.createElement(ToggleStateContext.Provider, { value: visible }, /*#__PURE__*/
React.createElement(ClickAwayListener, { onClickAway: function onClickAway() {return setVisibility(false);} }, React.Children.count(children) !== 1 ? /*#__PURE__*/React.createElement("span", null, children) : children)));
};
Toggle.Button = ToggleButton;
Toggle.Content = Content;
export default Toggle;
//# sourceMappingURL=index.js.map