require('client/homebrew/navbar/navbar.less');
const React = require('react');
const { useState, useRef, useEffect } = React;
const createClass = require('create-react-class');
const _ = require('lodash');
const cx = require('classnames');
const NaturalCritIcon = require('naturalcrit/svg/naturalcrit.svg.jsx');
const Nav = {
base : createClass({
displayName : 'Nav.base',
render : function(){
return ;
}
}),
logo : function(){
return
NaturalCrit
;
},
section : createClass({
displayName : 'Nav.section',
render : function(){
return
{this.props.children}
;
}
}),
item : createClass({
displayName : 'Nav.item',
getDefaultProps : function() {
return {
icon : null,
href : null,
newTab : false,
onClick : function(){},
color : null
};
},
handleClick : function(e){
this.props.onClick(e);
},
render : function(){
const classes = cx('navItem', this.props.color, this.props.className);
let icon;
if(this.props.icon) icon = ;
const props = _.omit(this.props, ['newTab']);
if(this.props.href){
return
{this.props.children}
{icon}
;
} else {
return
{this.props.children}
{icon}
;
}
}
}),
dropdown : function dropdown(props) {
props = Object.assign({}, props, {
trigger : 'hover click'
});
const myRef = useRef(null);
const [showDropdown, setShowDropdown] = useState(false);
useEffect(()=>{
document.addEventListener('click', handleClickOutside);
return ()=>{
document.removeEventListener('click', handleClickOutside);
};
}, []);
function handleClickOutside(e) {
// Close dropdown when clicked outside
if(!myRef.current?.contains(e.target)) {
handleDropdown(false);
}
}
function handleDropdown(show) {
setShowDropdown(show ?? !showDropdown);
}
const dropdownChildren = React.Children.map(props.children, (child, i)=>{
if(i < 1) return;
return child;
});
return (
handleDropdown(true) : undefined }
onMouseLeave = { props.trigger.includes('hover') ? ()=>handleDropdown(false) : undefined }
onClick = { props.trigger.includes('click') ? ()=>handleDropdown(true) : undefined }
>
{props.children[0] || props.children /*children is not an array when only one child*/}
{showDropdown &&
{dropdownChildren}
}
);
}
};
module.exports = Nav;