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