Skip to content

Commit

Permalink
Changes related to focus management
Browse files Browse the repository at this point in the history
  • Loading branch information
laveshv committed Oct 29, 2024
1 parent e0ed2b5 commit 277560b
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions pages/bluedialog/bluedialog.page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import React, { useEffect, useRef } from 'react';

import {
Box,
Expand All @@ -19,26 +19,43 @@ import {
import styles from './styles.scss';

export default function Bluedialogbox() {
// Inputs
const dialogRef = useRef<HTMLDivElement>(null);
const triggerRef = useRef<HTMLElement | null>(null);
// Inputs (these following can be clubbed into one state object)
const [dateValue, setDateValue] = React.useState('');
const [amountValue, setAmountValue] = React.useState('0');
const [descriptionValue, setDescriptionValue] = React.useState('');
const [selectedService, setSelectedService] = React.useState({ label: 'Cloudwatch', value: 'Cloudwatch' });

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();
}
};
}, []);
function submitData() {
console.log({ dateValue, amountValue, descriptionValue, selectedService });
console.log({ dateValue, amountValue, descriptionValue, selectedService }); // submit the form with these values to determine next action
}
function skipDialog() {
console.log('skip dialog');
console.log('skip dialog'); // can be used by the parent component to dismiss/ hide this dialog box
}

return (
<div
className={styles['blue-dialog-box']}
role={'dialog'}
aria-labelledby="dialog-box"
aria-modal="true"
tabIndex={-1}
aria-labelledby="dialog-title"
aria-modal="false" // Maintains natural focus flow since it's an inline dialog
tabIndex={-1} // This allows the dialog to receive focus
>
<Form
header={
Expand Down

0 comments on commit 277560b

Please sign in to comment.