sailboat-design
Version:
A simple sailboat simulator
81 lines (80 loc) • 3.53 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useContext, useState } from 'react';
import classNames from 'classnames';
import { MenuContext } from './menu';
import { menuItemDisplayName } from './menuItem';
import Icon from '../Icon/icon';
import Transition from '../Transition';
export var subMenuDisplayName = 'SubMenu';
export var SubMenu = function (props) {
var context = useContext(MenuContext);
var className = props.className, index = props.index, title = props.title, children = props.children;
var _a = useState(context.mode === 'vertical' ? props.open : false), open = _a[0], setOpen = _a[1];
var classes = classNames('menu-item submenu-item', className, {
'is-active': context.activeIndex.split('-')[0] === (index === null || index === void 0 ? void 0 : index.toString()),
'is-vertical': context.mode === 'vertical',
'is-opened': open
});
var handleClick = function (e) {
e.preventDefault();
setOpen(!open);
setTimeout(function () {
context.forceRenderCallback();
}, 400);
};
var timer;
var handleMouse = function (e, toggle) {
e.preventDefault();
clearTimeout(timer);
timer = setTimeout(function () {
setOpen(toggle);
}, 100);
};
var clickEvents = context.mode === 'vertical' ? { onClick: handleClick } : {};
var hoverEvents = context.mode === 'horizontal'
? {
onMouseEnter: function (e) {
handleMouse(e, true);
},
onMouseLeave: function (e) {
handleMouse(e, false);
}
}
: {};
var renderChildren = function () {
var classes = classNames('submenu', {
'is-opened': open
});
var childrenComponent = React.Children.map(children, function (child, _index) {
var childElement = child;
if (childElement.type.displayName === menuItemDisplayName) {
return React.cloneElement(childElement, {
index: "".concat(index, "-").concat(_index)
});
}
else {
console.error('Warning: SubMenu has a child which is not a menu item');
}
});
return (_jsx(Transition, __assign({ animation: "zoom-in-top", timeout: 300, in: open }, { children: _jsx("div", __assign({ className: classes }, { children: childrenComponent })) })));
};
return (_jsx(_Fragment, { children: _jsxs("ul", __assign({ className: classes }, hoverEvents, { children: [_jsxs("div", __assign({ className: "submenu-title" }, clickEvents, { children: [title, _jsx("div", __assign({ className: "arrow-icon" }, { children: _jsx(Icon, { icon: "angle-down" }) }))] })), renderChildren()] }), index) }));
};
SubMenu.defaultProps = {
open: false
};
SubMenu.displayName = subMenuDisplayName;
var exportMenu = React.memo(SubMenu);
exportMenu.displayName = subMenuDisplayName;
export default exportMenu;