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

feat: Focus the primary action in 'ConfirmationDialog' #1471

Merged
merged 7 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/Dialog/ConfirmationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ export const ConfirmationDialog: React.FC<ConfirmationDialogProps> = props => {
}, [onClose])
const cancelButton: DialogButtonProps = {
content: cancelButtonContent,
onClick: onCancelButtonClick,
autoFocus: true
onClick: onCancelButtonClick
}
const confirmButton: DialogButtonProps = {
content: confirmButtonContent,
buttonType: confirmButtonType,
onClick: onConfirmButtonClick
onClick: onConfirmButtonClick,
autoFocus: true
}
const footerButtons = [cancelButton, confirmButton]
return (
Expand Down
23 changes: 18 additions & 5 deletions src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from 'styled-components'
import Button, {ButtonPrimary, ButtonDanger, ButtonProps} from '../Button'
import Box from '../Box'
import {get, SystemCommonProps, SystemPositionProps, COMMON, POSITION} from '../constants'
import {useOnEscapePress} from '../hooks'
import {useOnEscapePress, useProvidedRefOrCreate} from '../hooks'
import {useFocusTrap} from '../hooks/useFocusTrap'
import sx, {SxProp} from '../sx'
import StyledOcticon from '../StyledOcticon'
Expand Down Expand Up @@ -36,6 +36,12 @@ export type DialogButtonProps = ButtonProps & {
* focus this button automatically when the dialog appears.
*/
autoFocus?: boolean

/**
* A reference to the rendered Button’s DOM node, used together with
* `autoFocus` for `focusTrap`’s `initialFocus`.
*/
ref?: React.RefObject<HTMLButtonElement>
}

/**
Expand Down Expand Up @@ -250,16 +256,23 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
onClose,
role = 'dialog',
width = 'xlarge',
height = 'auto'
height = 'auto',
footerButtons = []
} = props
const dialogLabelId = useSSRSafeId()
const dialogDescriptionId = useSSRSafeId()
const autoFocusedFooterButtonRef = useRef<HTMLButtonElement>(null)
for (const footerButton of footerButtons) {
if (footerButton.autoFocus) {
footerButton.ref = autoFocusedFooterButtonRef
}
}
const defaultedProps = {...props, title, subtitle, role, dialogLabelId, dialogDescriptionId}

const dialogRef = useRef<HTMLDivElement>(null)
const combinedRef = useCombinedRefs(dialogRef, forwardedRef)
const backdropRef = useRef<HTMLDivElement>(null)
useFocusTrap({containerRef: dialogRef, restoreFocusOnCleanUp: true})
useFocusTrap({containerRef: dialogRef, restoreFocusOnCleanUp: true, initialFocusRef: autoFocusedFooterButtonRef})

useOnEscapePress(
(event: KeyboardEvent) => {
Expand Down Expand Up @@ -338,7 +351,7 @@ const buttonTypes = {
danger: ButtonDanger
}
const Buttons: React.FC<{buttons: DialogButtonProps[]}> = ({buttons}) => {
const autoFocusRef = useRef<HTMLButtonElement>(null)
const autoFocusRef = useProvidedRefOrCreate<HTMLButtonElement>(buttons.find(button => button.autoFocus)?.ref)
let autoFocusCount = 0
const [hasRendered, setHasRendered] = useState(0)
useEffect(() => {
Expand All @@ -348,7 +361,7 @@ const Buttons: React.FC<{buttons: DialogButtonProps[]}> = ({buttons}) => {
} else {
setHasRendered(hasRendered + 1)
}
}, [hasRendered])
}, [autoFocusRef, hasRendered])

return (
<>
Expand Down