import './navbar.less'; import React, { useState, useRef, useEffect } from 'react'; import cx from 'classnames'; import NaturalCritIcon from '@components/svg/naturalcrit-d20.svg.jsx'; const Nav = { base : ({ children, className, ...props })=>{ return ; }, logo : ()=>{ return NaturalCrit ; }, section : ({ children, className, ...props })=>{ return
{children}
; }, item : ({ icon, href, newTab, onClick, color, children, className, ...props })=>{ const classes = cx('navItem', color, className); if(href){ return {children} {icon && } ; } else { return ; } }, 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}
}
); } }; export default Nav;