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

Add support for initial focus ref to Dialog 2 #4729

Merged
merged 3 commits into from
Jul 15, 2024
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
28 changes: 27 additions & 1 deletion packages/react/src/Dialog/Dialog.features.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useState, useRef, useCallback} from 'react'
import {Box, TextInput, Text, Button} from '..'
import {Box, TextInput, Text, Button, ActionList} from '..'
import type {DialogProps, DialogWidth, DialogHeight} from './Dialog'
import {Dialog} from './Dialog'

Expand Down Expand Up @@ -304,3 +304,29 @@ export const ReturnFocusRef = () => {
</React.Suspense>
)
}

export const NewIssues = () => {
const [isOpen, setIsOpen] = useState(false)
const onDialogClose = useCallback(() => setIsOpen(false), [])
const initialFocusRef = useRef(null)
return (
<>
<Button onClick={() => setIsOpen(true)}>Show dialog</Button>
{isOpen ? (
<Dialog
initialFocusRef={initialFocusRef}
onClose={onDialogClose}
title="New issue"
renderBody={() => (
<ActionList>
<ActionList.LinkItem ref={initialFocusRef} href="https://github.com">
Item 1
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com">Link</ActionList.LinkItem>
</ActionList>
)}
></Dialog>
) : null}
</>
)
}
8 changes: 7 additions & 1 deletion packages/react/src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export interface DialogProps extends SxProp {
* instead of the element that had focus immediately before the Dialog opened
*/
returnFocusRef?: React.RefObject<HTMLElement>

/**
* The element to focus when the Dialog opens
*/
initialFocusRef?: React.RefObject<HTMLElement>
}

/**
Expand Down Expand Up @@ -403,6 +408,7 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
footerButtons = [],
position = defaultPosition,
returnFocusRef,
initialFocusRef,
sx,
} = props
const dialogLabelId = useId()
Expand All @@ -429,7 +435,7 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP

useFocusTrap({
containerRef: dialogRef,
initialFocusRef: autoFocusedFooterButtonRef,
initialFocusRef: initialFocusRef ?? autoFocusedFooterButtonRef,
restoreFocusOnCleanUp: returnFocusRef?.current ? false : true,
returnFocusRef,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/__tests__/Dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {render as HTMLRender, fireEvent} from '@testing-library/react'
import axe from 'axe-core'
import {behavesAsComponent, checkExports} from '../utils/testing'

/* Dialog Version 2 */
/* Dialog Version 1*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you say so 😅😅😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting out of our hand now 🤣 I think version 2 tests are this : https://github.com/primer/react/blob/main/packages/react/src/Dialog/Dialog.test.tsx

Confirmed with looking at the APIs as well - I hope we get it right this time 😅 🤞🏻


const comp = (
<Dialog isOpen onDismiss={() => null} aria-labelledby="header">
Expand Down
Loading