-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
50 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,45 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import React, { useEffect, useRef } from 'react'; | ||
import React from 'react'; | ||
|
||
import { Button } from '~components'; | ||
|
||
import styles from './styles.scss'; | ||
|
||
export default function FeedbackDialog({ | ||
children, | ||
footer, | ||
onDismiss, | ||
}: { | ||
children: React.ReactNode; | ||
footer: React.ReactNode; | ||
onDismiss: () => void; | ||
}) { | ||
const dialogRef = useRef<HTMLDivElement>(null); | ||
const triggerRef = useRef<HTMLElement | null>(null); | ||
|
||
useEffect(() => { | ||
// Store the element that had focus before dialog opened | ||
triggerRef.current = document.activeElement as HTMLElement; | ||
|
||
// Focus the dialog container when it opens | ||
if (dialogRef.current) { | ||
dialogRef.current.focus(); | ||
} | ||
// Cleanup: Return focus to the trigger element when dialog closes | ||
return () => { | ||
if (triggerRef.current) { | ||
triggerRef.current.focus(); | ||
} | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div | ||
ref={dialogRef} | ||
className={styles['feedback-dialog']} | ||
role="dialog" | ||
aria-label="Feedback dialog" | ||
aria-modal="false" // Maintains natural focus flow since it's an inline dialog | ||
tabIndex={-1} // This allows the dialog to receive focus | ||
> | ||
<div className={styles.content}> | ||
<div className={styles.dismiss}> | ||
<Button iconName="close" variant="icon" onClick={onDismiss} ariaLabel="Close dialog" /> | ||
const FeedbackDialog = React.forwardRef( | ||
( | ||
{ | ||
children, | ||
footer, | ||
onDismiss, | ||
}: { | ||
children: React.ReactNode; | ||
footer: React.ReactNode; | ||
onDismiss: () => void; | ||
}, | ||
ref: React.Ref<HTMLDivElement> | ||
) => { | ||
return ( | ||
<div | ||
ref={ref} | ||
className={styles['feedback-dialog']} | ||
role="dialog" | ||
aria-label="Feedback dialog" | ||
aria-modal="false" // Maintains natural focus flow since it's an inline dialog | ||
tabIndex={-1} // This allows the dialog to receive focus | ||
> | ||
<div className={styles.content}> | ||
<div className={styles.dismiss}> | ||
<Button iconName="close" variant="icon" onClick={onDismiss} ariaLabel="Close dialog" /> | ||
</div> | ||
|
||
<div className={styles['inner-content']}>{children}</div> | ||
</div> | ||
|
||
<div className={styles['inner-content']}>{children}</div> | ||
<div className={styles.footer}>{footer}</div> | ||
</div> | ||
); | ||
} | ||
); | ||
|
||
<div className={styles.footer}>{footer}</div> | ||
</div> | ||
); | ||
} | ||
export default FeedbackDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters