Improve a11y on the cart

This commit is contained in:
Federico Orlandau 2021-07-08 12:55:42 +02:00
parent 166bb037e4
commit f5c218193b
5 changed files with 39 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import useCustomer from '@framework/customer/use-customer'
import { Avatar } from '@components/common' import { Avatar } from '@components/common'
import { Heart, Bag } from '@components/icons' import { Heart, Bag } from '@components/icons'
import { useUI } from '@components/ui/context' import { useUI } from '@components/ui/context'
import Button from '@components/ui/Button'
import DropdownMenu from './DropdownMenu' import DropdownMenu from './DropdownMenu'
import s from './UserNav.module.css' import s from './UserNav.module.css'
@ -26,8 +27,10 @@ const UserNav: FC<Props> = ({ className }) => {
<nav className={cn(s.root, className)}> <nav className={cn(s.root, className)}>
<ul className={s.list}> <ul className={s.list}>
{process.env.COMMERCE_CART_ENABLED && ( {process.env.COMMERCE_CART_ENABLED && (
<li className={s.item} onClick={toggleSidebar}> <li className={s.item}>
<Bag /> <Button variant="naked" onClick={toggleSidebar} aria-label="Cart">
<Bag />
</Button>
{itemsCount > 0 && <span className={s.bagCount}>{itemsCount}</span>} {itemsCount > 0 && <span className={s.bagCount}>{itemsCount}</span>}
</li> </li>
)} )}

View File

@ -35,6 +35,15 @@
@apply border-accent-9 bg-accent-9 text-accent-0; @apply border-accent-9 bg-accent-9 text-accent-0;
} }
.naked {
@apply bg-white text-gray-800 font-semibold border-none shadow-none outline-none py-0 px-0;
}
.naked:hover,
.naked:focus {
@apply bg-white border-none;
}
.disabled, .disabled,
.disabled:hover { .disabled:hover {
@apply text-accent-4 border-accent-2 bg-accent-1 cursor-not-allowed; @apply text-accent-4 border-accent-2 bg-accent-1 cursor-not-allowed;

View File

@ -12,7 +12,7 @@ import { LoadingDots } from '@components/ui'
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> { export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
href?: string href?: string
className?: string className?: string
variant?: 'flat' | 'slim' | 'ghost' variant?: 'flat' | 'slim' | 'ghost' | 'naked'
active?: boolean active?: boolean
type?: 'submit' | 'reset' | 'button' type?: 'submit' | 'reset' | 'button'
Component?: string | JSXElementConstructor<any> Component?: string | JSXElementConstructor<any>
@ -41,6 +41,7 @@ const Button: React.FC<ButtonProps> = forwardRef((props, buttonRef) => {
{ {
[s.ghost]: variant === 'ghost', [s.ghost]: variant === 'ghost',
[s.slim]: variant === 'slim', [s.slim]: variant === 'slim',
[s.naked]: variant === 'naked',
[s.loading]: loading, [s.loading]: loading,
[s.disabled]: disabled, [s.disabled]: disabled,
}, },

View File

@ -1,5 +1,5 @@
.root { .root {
@apply fixed inset-0 h-full z-50 box-border; @apply fixed inset-0 h-full z-50 box-border outline-none;
} }
.sidebar { .sidebar {

View File

@ -13,27 +13,44 @@ interface SidebarProps {
} }
const Sidebar: FC<SidebarProps> = ({ children, onClose }) => { const Sidebar: FC<SidebarProps> = ({ children, onClose }) => {
const innerRef = useRef() as React.MutableRefObject<HTMLDivElement>
const ref = useRef() as React.MutableRefObject<HTMLDivElement> const ref = useRef() as React.MutableRefObject<HTMLDivElement>
const onKeyDownSidebar = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.code === 'Escape') {
onClose()
}
}
useEffect(() => { useEffect(() => {
if (ref.current) { if (ref.current) {
disableBodyScroll(ref.current, { reserveScrollBarGap: true }) ref.current.focus()
} }
if (innerRef.current) {
disableBodyScroll(innerRef.current, { reserveScrollBarGap: true })
}
return () => { return () => {
if (ref && ref.current) { if (innerRef && innerRef.current) {
enableBodyScroll(ref.current) enableBodyScroll(innerRef.current)
} }
clearAllBodyScrollLocks() clearAllBodyScrollLocks()
} }
}, []) }, [])
return ( return (
<div className={cn(s.root)}> <div
className={cn(s.root)}
ref={ref}
onKeyDown={onKeyDownSidebar}
tabIndex={1}
>
<div className="absolute inset-0 overflow-hidden"> <div className="absolute inset-0 overflow-hidden">
<div className={s.backdrop} onClick={onClose} /> <div className={s.backdrop} onClick={onClose} />
<section className="absolute inset-y-0 right-0 max-w-full flex outline-none pl-10"> <section className="absolute inset-y-0 right-0 max-w-full flex outline-none pl-10">
<div className="h-full w-full md:w-screen md:max-w-md"> <div className="h-full w-full md:w-screen md:max-w-md">
<div className={s.sidebar} ref={ref}> <div className={s.sidebar} ref={innerRef}>
{children} {children}
</div> </div>
</div> </div>