ISSUE-013 - Refactored the method that isActive and isHovered work for the MainMenu nav buttons.
This commit is contained in:
1 parent
e722ac9d7d
commit
977e433dd9
2 files changed
+58
-47
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
import React, { useState } from "react";
|
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 { InlineCluster } from "@bedrock-layout/inline-cluster";
|
||||||
import useForwardedRef from "@bedrock-layout/use-forwarded-ref";
|
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>(
|
export const MainMenu = React.forwardRef<HTMLDivElement>(
|
||||||
(_, ref) => (
|
(_, ref) => (
|
||||||
<Menu ref={ref} title="F1 World Broadcast Emulator Project">
|
<Menu
|
||||||
<NavLink to="/">Home</NavLink>
|
ref={ref}
|
||||||
<NavLink to="/broadcast">Broadcast Page</NavLink>
|
title="F1 World Broadcast Emulator Project"
|
||||||
</Menu>
|
links={[
|
||||||
|
{ label: "Home", to: "/" },
|
||||||
|
{ label: "Broadcast Page", to: "/broadcast" },
|
||||||
|
]}
|
||||||
|
/>
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
MainMenu.displayName = "MainMenu";
|
MainMenu.displayName = "MainMenu";
|
||||||
|
|
||||||
type MenuProps = {
|
type MenuProps = {
|
||||||
children: React.ReactNode,
|
links: Array<{label: string, to: string}>,
|
||||||
title: string,
|
title: string,
|
||||||
};
|
};
|
||||||
export const Menu = React.forwardRef<HTMLDivElement, MenuProps>(
|
const Menu = React.forwardRef<HTMLDivElement, MenuProps>(
|
||||||
(props, ref) => {
|
(props, ref) => {
|
||||||
const innerRef = useForwardedRef(ref);
|
const innerRef = useForwardedRef(ref);
|
||||||
const childrenArray = React.Children.toArray(props.children);
|
const [hoveredIndex, setHoveredIndex] = useState(-1);
|
||||||
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;
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MenuBar ref={innerRef}>
|
<MenuBar ref={innerRef}>
|
||||||
<img src={f1Logo} alt="Formula One logo" height="20px" />
|
<Link to="/">
|
||||||
|
<img src={f1Logo} alt="Formula One logo" height="20px" />
|
||||||
|
</Link>
|
||||||
<Title>{props.title}</Title>
|
<Title>{props.title}</Title>
|
||||||
<nav>
|
<nav>
|
||||||
<InlineCluster as="ul" gutter="sm" justify="end">
|
<InlineCluster as="ul" gutter="sm" justify="end">
|
||||||
{newChildren.map((child, i) => (
|
{props.links.map((link, i) => (
|
||||||
<MenuItem
|
<MenuNavLink
|
||||||
key={i}
|
key={i}
|
||||||
hovered={i === hovered}
|
to={link.to}
|
||||||
active={i === active}
|
label={link.label}
|
||||||
>{child}</MenuItem>
|
isHovered={i === hoveredIndex}
|
||||||
|
onMouseLeave={() => setHoveredIndex(-1)}
|
||||||
|
onMouseEnter={() => setHoveredIndex(i)}
|
||||||
|
/>
|
||||||
))}
|
))}
|
||||||
</InlineCluster>
|
</InlineCluster>
|
||||||
</nav>
|
</nav>
|
||||||
@@ -65,3 +55,30 @@ export const Menu = React.forwardRef<HTMLDivElement, MenuProps>(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
Menu.displayName = "Menu";
|
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 = {
|
type MenuItemProps = {
|
||||||
hovered?: boolean,
|
isHovered?: boolean,
|
||||||
active?: boolean,
|
isActive?: boolean,
|
||||||
};
|
};
|
||||||
export const MenuItem = styled(PadBox).attrs(() => ({
|
export const MenuItem = styled(PadBox).attrs(() => ({
|
||||||
as: "li",
|
|
||||||
padding: ["sm", "lg"],
|
padding: ["sm", "lg"],
|
||||||
}))<MenuItemProps>`
|
}))<MenuItemProps>`
|
||||||
border-radius: 0.25rem;
|
border-radius: 0.25rem;
|
||||||
> a {
|
color: ${p => p.theme.colors.darkGrey};
|
||||||
color: ${p => p.theme.colors.darkGrey};
|
font-family: ${p => p.theme.fonts.f1Regular};
|
||||||
text-decoration: none;
|
font-size: 0.8em;
|
||||||
font-family: ${p => p.theme.fonts.f1Regular};
|
|
||||||
font-size: 0.8em;
|
|
||||||
}
|
|
||||||
|
|
||||||
${({ active }) => active && css`
|
${({ isActive }) => isActive && css`
|
||||||
background: ${p => p.theme.colors.lightGrey};
|
background: ${p => p.theme.colors.lightGrey};
|
||||||
`}
|
`}
|
||||||
|
|
||||||
${({ hovered }) => hovered && css`
|
${({ isHovered }) => isHovered && css`
|
||||||
background: ${p => p.theme.colors.darkGrey};
|
background: ${p => p.theme.colors.darkGrey};
|
||||||
> a {
|
color: white;
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
`}
|
`}
|
||||||
`;
|
`;
|
||||||
Reference in new issue
Block a user