Merge pull request #15 from TiE23/ISSUE-013
ISSUE-013 - Refactored the method that isActive and isHovered work fo…
This commit is contained in:
2 files changed
+53
-42
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import React, { useState } from "react";
|
||||
import { NavLink } from "react-router-dom";
|
||||
import { Link, NavLink } from "react-router-dom";
|
||||
import { InlineCluster } from "@bedrock-layout/inline-cluster";
|
||||
import useForwardedRef from "@bedrock-layout/use-forwarded-ref";
|
||||
|
||||
@@ -9,54 +9,44 @@ import f1Logo from "../../public/images/logos/f1-logo-red.svg";
|
||||
|
||||
export const MainMenu = React.forwardRef<HTMLDivElement>(
|
||||
(_, ref) => (
|
||||
<Menu ref={ref} title="F1 World Broadcast Emulator Project">
|
||||
<NavLink to="/">Home</NavLink>
|
||||
<NavLink to="/broadcast">Broadcast Page</NavLink>
|
||||
</Menu>
|
||||
<Menu
|
||||
ref={ref}
|
||||
title="F1 World Broadcast Emulator Project"
|
||||
links={[
|
||||
{ label: "Home", to: "/" },
|
||||
{ label: "Broadcast Page", to: "/broadcast" },
|
||||
]}
|
||||
/>
|
||||
),
|
||||
);
|
||||
MainMenu.displayName = "MainMenu";
|
||||
|
||||
type MenuProps = {
|
||||
children: React.ReactNode,
|
||||
links: Array<{label: string, to: string}>,
|
||||
title: string,
|
||||
};
|
||||
export const Menu = React.forwardRef<HTMLDivElement, MenuProps>(
|
||||
const Menu = React.forwardRef<HTMLDivElement, MenuProps>(
|
||||
(props, ref) => {
|
||||
const innerRef = useForwardedRef(ref);
|
||||
const childrenArray = React.Children.toArray(props.children);
|
||||
const [hovered, setHovered] = useState(-1);
|
||||
const [active, setActive] = useState(-1);
|
||||
|
||||
const hackIsActive = (i: number) => ({ isActive }: { isActive: boolean}) => {
|
||||
if (isActive) setActive(i);
|
||||
return "";
|
||||
};
|
||||
|
||||
// Insert mouse event props to these children.
|
||||
const newChildren = childrenArray.map((child, i) => {
|
||||
if (React.isValidElement(child)) {
|
||||
return React.cloneElement(child, {
|
||||
onMouseEnter: () => setHovered(i),
|
||||
onMouseLeave: () => setHovered(-1),
|
||||
className: hackIsActive(i),
|
||||
});
|
||||
}
|
||||
return child;
|
||||
});
|
||||
const [hoveredIndex, setHoveredIndex] = useState(-1);
|
||||
|
||||
return (
|
||||
<MenuBar ref={innerRef}>
|
||||
<Link to="/">
|
||||
<img src={f1Logo} alt="Formula One logo" height="20px" />
|
||||
</Link>
|
||||
<Title>{props.title}</Title>
|
||||
<nav>
|
||||
<InlineCluster as="ul" gutter="sm" justify="end">
|
||||
{newChildren.map((child, i) => (
|
||||
<MenuItem
|
||||
{props.links.map((link, i) => (
|
||||
<MenuNavLink
|
||||
key={i}
|
||||
hovered={i === hovered}
|
||||
active={i === active}
|
||||
>{child}</MenuItem>
|
||||
to={link.to}
|
||||
label={link.label}
|
||||
isHovered={i === hoveredIndex}
|
||||
onMouseLeave={() => setHoveredIndex(-1)}
|
||||
onMouseEnter={() => setHoveredIndex(i)}
|
||||
/>
|
||||
))}
|
||||
</InlineCluster>
|
||||
</nav>
|
||||
@@ -65,3 +55,30 @@ export const Menu = React.forwardRef<HTMLDivElement, MenuProps>(
|
||||
},
|
||||
);
|
||||
Menu.displayName = "Menu";
|
||||
|
||||
type MenuNavLinkProps = {
|
||||
to: string,
|
||||
label: string,
|
||||
isHovered: boolean,
|
||||
onMouseLeave?: () => void,
|
||||
onMouseEnter?: () => void,
|
||||
};
|
||||
const MenuNavLink = ({ to, label, isHovered, onMouseLeave, onMouseEnter }: MenuNavLinkProps) => (
|
||||
<li>
|
||||
<NavLink
|
||||
to={to}
|
||||
style={{ textDecoration: "none" }}
|
||||
>
|
||||
{({ isActive }) => (
|
||||
<MenuItem
|
||||
isHovered={isHovered}
|
||||
isActive={isActive}
|
||||
onMouseLeave={onMouseLeave}
|
||||
onMouseEnter={onMouseEnter}
|
||||
>
|
||||
{label}
|
||||
</MenuItem>
|
||||
)}
|
||||
</NavLink>
|
||||
</li>
|
||||
);
|
||||
@@ -18,29 +18,23 @@ export const MenuBar = styled(InlineCluster).attrs(() => ({
|
||||
`;
|
||||
|
||||
type MenuItemProps = {
|
||||
hovered?: boolean,
|
||||
active?: boolean,
|
||||
isHovered?: boolean,
|
||||
isActive?: boolean,
|
||||
};
|
||||
export const MenuItem = styled(PadBox).attrs(() => ({
|
||||
as: "li",
|
||||
padding: ["sm", "lg"],
|
||||
}))<MenuItemProps>`
|
||||
border-radius: 0.25rem;
|
||||
> a {
|
||||
color: ${p => p.theme.colors.darkGrey};
|
||||
text-decoration: none;
|
||||
font-family: ${p => p.theme.fonts.f1Regular};
|
||||
font-size: 0.8em;
|
||||
}
|
||||
|
||||
${({ active }) => active && css`
|
||||
${({ isActive }) => isActive && css`
|
||||
background: ${p => p.theme.colors.lightGrey};
|
||||
`}
|
||||
|
||||
${({ hovered }) => hovered && css`
|
||||
${({ isHovered }) => isHovered && css`
|
||||
background: ${p => p.theme.colors.darkGrey};
|
||||
> a {
|
||||
color: white;
|
||||
}
|
||||
`}
|
||||
`;
|
||||
Reference in new issue
Block a user