Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding focus and keyboard accessibility to the cookie consent buttons #945

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/components/CookieConsentBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import { ThemeButton } from "./ThemeButton";
import { Check, X } from "@phosphor-icons/react";
import { useCookieContext } from "@/context/CookieContext";
import { useEffect, useState } from "react";
import { useEffect, useState, useRef } from "react";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you have a couple of small conflicts that need to be resolved with main.


const CookieConsentBanner = () => {
const { shouldShowBanner, setShouldAllowCookies, setShouldShowBanner } =
useCookieContext();

const bannerRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);

const [isClientSide, setIsClientSide] = useState(false);

useEffect(() => {
Expand All @@ -21,34 +24,54 @@ const CookieConsentBanner = () => {
setShouldShowBanner(false);
};

useEffect(() => {
// Focus on the banner when it becomes visible
if (shouldShowBanner && bannerRef.current) {
// Use setTimeout to ensure focus is applied after DOM updates
setTimeout(() => {
bannerRef.current?.focus();
}, 0);
}
}, [shouldShowBanner]); // Trigger focus when shouldShowBanner changes

if (!isClientSide) return null;

return (
<div
role="region"
tabIndex={-1} // Make the div focusable
ref={bannerRef} // Attach the ref to the banner
aria-labelledby="cookie_heading"
className={`${
shouldShowBanner
? "md:flex fixed inset-x-0 bottom-0 z-20 justify-between bg-blue-200 p-4 sm:p-6"
: "hidden"
}`}
>
<div>
<div className="heading-md">Can we store cookies to your browser?</div>
<h2 className="heading-md" id="cookie_heading">
Can we store cookies to your browser?
</h2>
<div className="body-sm">
This provides you a nice experience preserving your filtering, your
position on the map and your property saved list.
<p>
This provides you a nice experience preserving your filtering, your
position on the map and your property saved list.
</p>
</div>
</div>

<div className="flex justify-end gap-2 pt-4 sm:pt-0">
<div className="flex flex-none items-center gap-x-2">
<ThemeButton
tabIndex={1}
color="tertiary"
label="Decline"
startContent={<X />}
onPress={() => onClickButton(false)}
/>

<ThemeButton
tabIndex={2}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of those exceptions to the rule that I think is okay.
Although I have seen it said somewhere on the A11y Slack channel that by now screenreader users just expect that the cookie banner will be on the bottom and they will TAB backwards to get to it.

color="primary"
label="Accept"
startContent={<Check />}
Expand Down
13 changes: 7 additions & 6 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useCookieContext } from "@/context/CookieContext";
import CookieConsentBanner from "./CookieConsentBanner";
import { ThemeButton } from "./ThemeButton";
import Link from "next/link";

const Footer = () => {
Expand All @@ -25,12 +26,12 @@ const Footer = () => {
<span className="max-sm:hidden">—</span>

<li className="base-sm underline text-gray-600 mx-auto">
<a
className="hover:text-gray-800 cursor-pointer"
onClick={onClickCookieSettings}
>
Cookie Settings
</a>
<ThemeButton
color="tertiary"
label="Cookie Settings"
onPress={onClickCookieSettings}
className="hover:text-gray-800 cursor-pointer text-gray-600"
/>
</li>

<span className="max-sm:hidden">—</span>
Expand Down