ISSUE-008 - Hacked together a solution that can softly highlight the NavLink on the current route. And can handle the hover without relying on :hover.

This commit is contained in:
Kyle Geib committed 2021-12-22 17:50:17 -05:00
1 parent f4ccb9b3dd
commit b44e3ff42c
3 files changed
+47 -13

No files matched your search

+31 -7
View File
@@ -1,5 +1,5 @@
import React, { Children } from "react"; import React, { useState } from "react";
import { Link } from "react-router-dom"; import { 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";
@@ -10,8 +10,8 @@ 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 ref={ref} title="F1 World Broadcast Emulator Project">
<Link to="/">Home</Link> <NavLink to="/">Home</NavLink>
<Link to="/broadcast">Broadcast Page</Link> <NavLink to="/broadcast">Broadcast Page</NavLink>
</Menu> </Menu>
), ),
); );
@@ -24,15 +24,39 @@ type MenuProps = {
export const Menu = React.forwardRef<HTMLDivElement, MenuProps>( export const Menu = React.forwardRef<HTMLDivElement, MenuProps>(
(props, ref) => { (props, ref) => {
const innerRef = useForwardedRef(ref); const innerRef = useForwardedRef(ref);
const childrenArray = Children.toArray(props.children); 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;
});
return ( return (
<MenuBar ref={innerRef}> <MenuBar ref={innerRef}>
<img src={f1Logo} alt="Formula One logo" height="20px" /> <img src={f1Logo} alt="Formula One logo" height="20px" />
<h3>{props.title}</h3> <h3>{props.title}</h3>
<nav> <nav>
<InlineCluster as="ul" gutter="sm" justify="end"> <InlineCluster as="ul" gutter="sm" justify="end">
{childrenArray.map((child, i) => ( {newChildren.map((child, i) => (
<MenuItem key={i}>{child}</MenuItem> <MenuItem
key={i}
hovered={i === hovered}
active={i === active}
>{child}</MenuItem>
))} ))}
</InlineCluster> </InlineCluster>
</nav> </nav>
+10 -4
View File
@@ -1,6 +1,6 @@
import { InlineCluster } from "@bedrock-layout/inline-cluster"; import { InlineCluster } from "@bedrock-layout/inline-cluster";
import { PadBox } from "@bedrock-layout/padbox"; import { PadBox } from "@bedrock-layout/padbox";
import styled from "styled-components"; import styled, { css } from "styled-components";
export const MenuBar = styled(InlineCluster).attrs(() => ({ export const MenuBar = styled(InlineCluster).attrs(() => ({
as: PadBox, as: PadBox,
@@ -14,22 +14,28 @@ export const MenuBar = styled(InlineCluster).attrs(() => ({
`; `;
type MenuItemProps = { type MenuItemProps = {
hovered?: boolean,
active?: boolean, active?: boolean,
}; };
export const MenuItem = styled(PadBox).attrs(() => ({ export const MenuItem = styled(PadBox).attrs(() => ({
as: "li", as: "li",
padding: ["sm", "lg"], padding: ["sm", "lg"],
})) <MenuItemProps>` }))<MenuItemProps>`
border-radius: 0.25rem; border-radius: 0.25rem;
> a { > a {
color: ${p => p.theme.colors.darkGrey}; color: ${p => p.theme.colors.darkGrey};
text-decoration: none; text-decoration: none;
font-weight: 900; font-weight: 900;
} }
:hover {
${({ active }) => active && css`
background: ${p => p.theme.colors.lightGrey};
`}
${({ hovered }) => hovered && css`
background: ${p => p.theme.colors.darkGrey}; background: ${p => p.theme.colors.darkGrey};
> a { > a {
color: white; color: white;
} }
} `}
`; `;
+6 -2
View File
@@ -10,6 +10,7 @@ export const theme = {
blue: "#387af5", blue: "#387af5",
pink: "#eb57a3", pink: "#eb57a3",
grey: "#aaa", grey: "#aaa",
lightGrey: "#ddd",
darkGrey: "#303030", darkGrey: "#303030",
videoFrameBG: "#333", videoFrameBG: "#333",
}, },
@@ -29,8 +30,6 @@ export const GlobalStyle = createGlobalStyle<MainThemeProps>`
*::before { box-sizing: border-box; } *::before { box-sizing: border-box; }
h1, h2, h3, h4, h5, h6 { margin: 0; } h1, h2, h3, h4, h5, h6 { margin: 0; }
a { color: ${({ theme }) => theme.colors.blue} }
a:hover { color: ${({ theme }) => theme.colors.pink} }
input, input,
textarea, textarea,
@@ -42,4 +41,9 @@ export const GlobalStyle = createGlobalStyle<MainThemeProps>`
padding: 70px 0 20px; padding: 70px 0 20px;
min-height: calc(100vh - 50px); min-height: calc(100vh - 50px);
} }
.activeNavLink {
border-color: ${({ theme }) => theme.colors.darkGrey};
font-style: italic;
text-decoration: underline;
}
`; `;