Skip to content

Commit

Permalink
feat: improve header's a11y (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
honeymaro authored Feb 25, 2024
1 parent efc6e69 commit e6a281b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 67 deletions.
24 changes: 15 additions & 9 deletions src/components/NavBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { css } from "@emotion/react";
import styled from "@emotion/styled";
import Tippy, { TippyProps } from "@tippyjs/react/headless";
import { NavLink } from "react-router-dom";

const Wrapper = styled.div`
Expand Down Expand Up @@ -30,7 +31,7 @@ const NavItem = styled(NavLink, {
flex: 1;
`}
&.active:not(.disabled), &:hover:not(.disabled) {
&.active:not(.disabled), &:hover:not(.disabled), &[aria-expanded="true"] {
opacity: 1;
}
Expand All @@ -43,21 +44,26 @@ interface NavBarProps {
items: (React.ComponentProps<typeof NavItem> & {
key?: React.Key;
disabled?: boolean;
tippyProps?: TippyProps;
})[];
flex?: boolean;
}

function NavBar({ items, flex = true }: NavBarProps) {
return (
<Wrapper>
{items.map(({ key, disabled, to, ...navLink }) => (
<NavItem
className={`${navLink.className ?? ""} ${disabled ? "disabled" : ""}`}
key={key ?? `${to.toString()}`}
to={disabled ? "#" : to}
{...navLink}
flex={flex}
/>
{items.map(({ key, disabled, to, tippyProps, ...navItemProps }) => (
<Tippy {...tippyProps}>
<NavItem
className={`${navItemProps.className ?? ""} ${
disabled ? "disabled" : ""
}`}
key={key ?? `${to.toString()}`}
to={disabled ? "#" : to}
{...navItemProps}
flex={flex}
/>
</Tippy>
))}
</Wrapper>
);
Expand Down
102 changes: 44 additions & 58 deletions src/layout/Main/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ const SubLink = styled(Link)`
background-color: ${({ theme }) => theme.colors.text.background};
}
&:first-child {
&:first-of-type {
border-top-left-radius: 12px;
border-top-right-radius: 12px;
}
&:last-child {
&:last-of-type {
border-bottom-left-radius: 12px;
border-bottom-right-radius: 12px;
}
Expand Down Expand Up @@ -377,68 +377,54 @@ function Header() {
items={navLinks.map((item) => ({
key: item.path,
to: item.path,
css: css`
.sub-menu {
display: none;
}
&:hover {
.sub-menu {
display: block;
}
}
`,
children: (
<Typography size={18} weight={900} color="primary">
{item.label}
{item.children && (
<div
className="sub-menu"
tippyProps: {
disabled: !item.children,
placement: "bottom",
interactive: true,
offset: [0, 3],
render: (attrs) => (
<div {...attrs}>
<Panel
border
noPadding
css={css`
position: absolute;
padding-top: 3px;
left: 50%;
z-index: 6000;
transform: translateX(-50%);
top: 100%;
min-width: 138px;
`}
>
<Panel
border
noPadding
css={css`
min-width: 138px;
`}
>
{item.children.map((child) => (
<Tooltip
{item?.children?.map((child) => (
<Tooltip
key={child.path}
disabled={!child.disabled}
content="Coming soon"
>
<SubLink
key={child.path}
disabled={!child.disabled}
content="Coming soon"
to={!child.disabled ? child.path : "#"}
css={
child.disabled
? css`
cursor: default;
`
: undefined
}
>
<SubLink
key={child.path}
to={!child.disabled ? child.path : "#"}
css={
child.disabled
? css`
cursor: default;
`
: undefined
}
<Typography
size={16}
weight={900}
color="primary"
>
<Typography
size={16}
weight={900}
color="primary"
>
{child.label}
</Typography>
</SubLink>
</Tooltip>
))}
</Panel>
</div>
)}
{child.label}
</Typography>
</SubLink>
</Tooltip>
))}
</Panel>
</div>
),
},
children: (
<Typography size={18} weight={900} color="primary">
{item.label}
</Typography>
),
}))}
Expand Down

0 comments on commit e6a281b

Please sign in to comment.