-
Notifications
You must be signed in to change notification settings - Fork 71
feat(drawer): add focus management to embedded drawers #3139
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
base: main
Are you sure you want to change the base?
Conversation
…ore focusing previously focused element
… for improved focus handling
🦋 Changeset detectedLatest commit: 3192ddf The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds comprehensive focus management to embedded drawers, enhancing accessibility and keyboard navigation. When an embedded drawer opens, focus automatically moves to the first focusable element within the drawer, and when closed, focus is restored to the previously focused element.
Key changes:
- Implements focus management logic in
PanelGrid
component for embedded drawers - Removes redundant focus handling and ARIA attributes from existing components
- Adds interaction stories to test focus behavior for both embedded and overlay drawer modes
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
packages/drawer/src/LayoutComponent/PanelGrid/PanelGrid.tsx |
Adds focus management logic with useIsomorphicLayoutEffect to handle focus transitions |
packages/drawer/src/DrawerToolbarLayout/DrawerToolbarLayout/DrawerToolbarLayoutContent.tsx |
Removes redundant aria-live attributes that are now handled elsewhere |
packages/drawer/src/DrawerToolbarLayout/DrawerToolbarLayout/DrawerToolbarLayout.interactions.stories.tsx |
Adds comprehensive interaction stories to test focus management behavior |
packages/drawer/src/Drawer/Drawer.tsx |
Removes old focus handling logic and adds VisuallyHidden live region for screen reader announcements |
.changeset/beige-lights-kick.md |
Documents the changes for release notes |
import { useIsomorphicLayoutEffect } from '@leafygreen-ui/hooks'; | ||
|
||
import { useDrawerLayoutContext } from '../../DrawerLayout'; | ||
|
||
import { getPanelGridStyles } from './PanelGrid.styles'; | ||
import { PanelGridProps } from './PanelGrid.types'; | ||
import { useForwardedRef } from '@leafygreen-ui/hooks'; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import statements should be grouped and ordered consistently. The useForwardedRef
import on line 10 should be combined with the other hooks import on line 4.
import { useIsomorphicLayoutEffect } from '@leafygreen-ui/hooks'; | |
import { useDrawerLayoutContext } from '../../DrawerLayout'; | |
import { getPanelGridStyles } from './PanelGrid.styles'; | |
import { PanelGridProps } from './PanelGrid.types'; | |
import { useForwardedRef } from '@leafygreen-ui/hooks'; | |
import { useIsomorphicLayoutEffect, useForwardedRef } from '@leafygreen-ui/hooks'; | |
import { useDrawerLayoutContext } from '../../DrawerLayout'; | |
import { getPanelGridStyles } from './PanelGrid.styles'; | |
import { PanelGridProps } from './PanelGrid.types'; |
Copilot uses AI. Check for mistakes.
const firstFocusable = layoutRef.current?.querySelector( | ||
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])', | ||
) as HTMLElement; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CSS selector for focusable elements should be extracted to a constant or utility function to avoid duplication and improve maintainability. This same selector appears to be used elsewhere in the codebase.
Copilot uses AI. Check for mistakes.
if (document.contains(previouslyFocusedRef.current)) { | ||
previouslyFocusedRef.current.focus(); | ||
} else { | ||
// If the previously focused element is no longer in the DOM, focus the body | ||
document.body.focus(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Focusing document.body
may not provide the best user experience for keyboard navigation. Consider focusing a more appropriate fallback element or using a focus management library that can find the next logical focus target.
Copilot uses AI. Check for mistakes.
Size Change: +311 B (+0.02%) Total Size: 1.55 MB
ℹ️ View Unchanged
|
✍️ Proposed changes
This PR enhances the drawer component by adding comprehensive focus management for embedded drawers. The changes ensure that when an embedded drawer is opened, focus automatically moves to the first focusable element within the drawer, and when closed, focus is restored to the previously focused element (typically the trigger button). This improves keyboard navigation and accessibility compliance.
The implementation also fixes a bug that prevented the toolbar from being focused when the drawer is opened, and adds a live region for screen reader announcements when the drawer state changes.
🎟️ Jira ticket: No Jira ticket found
✅ Checklist
pnpm changeset
and documented my changes🧪 How to test changes
Test embedded drawer focus management:
Test overlay drawer behavior:
Test accessibility with screen readers:
Run the new interaction stories:
EmbeddedToolbarIsFocusedOnClose
andEmbeddedButtonIsFocusedOnClose
OverlayToolbarIsFocusedOnClose
andOverlayButtonIsFocusedOnClose