Skip to content

Commit

Permalink
adding focus and keyboard accessibility to the cookie consent buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Amberroseweeks committed Oct 9, 2024
1 parent 5cf0388 commit fed8c2e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
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";

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}
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

0 comments on commit fed8c2e

Please sign in to comment.