diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index 94f2988..2dae226 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -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( (_, ref) => ( - Home - Broadcast Page + Home + Broadcast Page ), ); @@ -24,15 +24,39 @@ type MenuProps = { export const Menu = React.forwardRef( (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 ( Formula One logo

{props.title}

diff --git a/src/components/Menu/styles.ts b/src/components/Menu/styles.ts index 722c1e7..0f36e38 100644 --- a/src/components/Menu/styles.ts +++ b/src/components/Menu/styles.ts @@ -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,22 +14,28 @@ export const MenuBar = styled(InlineCluster).attrs(() => ({ `; type MenuItemProps = { + hovered?: boolean, active?: boolean, }; export const MenuItem = styled(PadBox).attrs(() => ({ as: "li", padding: ["sm", "lg"], -})) ` +}))` border-radius: 0.25rem; > a { color: ${p => p.theme.colors.darkGrey}; 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; } - } + `} `; diff --git a/src/shared/theme.ts b/src/shared/theme.ts index 0017c16..206b3f9 100644 --- a/src/shared/theme.ts +++ b/src/shared/theme.ts @@ -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` *::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` padding: 70px 0 20px; min-height: calc(100vh - 50px); } + .activeNavLink { + border-color: ${({ theme }) => theme.colors.darkGrey}; + font-style: italic; + text-decoration: underline; + } `;