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

[skeleton] Close aside when Esc is pressed #2503

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Changes from 1 commit
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
25 changes: 24 additions & 1 deletion templates/skeleton/app/components/Aside.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import {createContext, type ReactNode, useContext, useState} from 'react';
import {
createContext,
type ReactNode,
useContext,
useEffect,
useState,
} from 'react';

type AsideType = 'search' | 'cart' | 'mobile' | 'closed';
type AsideContextValue = {
Expand Down Expand Up @@ -29,6 +35,23 @@ export function Aside({
const {type: activeType, close} = useAside();
const expanded = type === activeType;

useEffect(() => {
const abortController = new AbortController();

if (expanded) {
document.addEventListener(
'keypress',
function handler(event: KeyboardEvent) {
frontsideair marked this conversation as resolved.
Show resolved Hide resolved
if (event.key === 'Escape') {
close();
}
},
{signal: abortController.signal},
);
scottdixon marked this conversation as resolved.
Show resolved Hide resolved
}
return () => abortController.abort();
}, [close, expanded]);
scottdixon marked this conversation as resolved.
Show resolved Hide resolved

return (
<div
aria-modal
Expand Down
Loading