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

Fix incorrect Transition boundary for Dialog component #3331

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
49 changes: 37 additions & 12 deletions packages/@headlessui-react/src/components/dialog/dialog.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { render } from '@testing-library/react'
import React, { createElement, Fragment, useCallback, useEffect, useRef, useState } from 'react'
import React, { Fragment, createElement, useCallback, useEffect, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { OpenClosedProvider, State } from '../../internal/open-closed'
import {
DialogState,
PopoverState,
assertActiveElement,
assertDialog,
assertDialogDescription,
assertDialogTitle,
assertPopoverPanel,
DialogState,
getByText,
getDialog,
getDialogs,
getPopoverButton,
PopoverState,
} from '../../test-utils/accessibility-assertions'
import { click, focus, Keys, mouseDrag, press, shift } from '../../test-utils/interactions'
import { Keys, click, focus, mouseDrag, press, shift } from '../../test-utils/interactions'
import { suppressConsoleLogs } from '../../test-utils/suppress-console-logs'
import type { PropsOf } from '../../types'
import { Popover } from '../popover/popover'
Expand Down Expand Up @@ -498,25 +497,51 @@ describe('Rendering', () => {
it(
'should remove the scroll lock when the open closed state is `Closing`',
suppressConsoleLogs(async () => {
function Example({ value = State.Open }) {
function Example({ open = true }) {
return (
<Dialog transition autoFocus={false} open={open} onClose={() => {}}>
<input id="a" type="text" />
<input id="b" type="text" />
<input id="c" type="text" />
</Dialog>
)
}

let { rerender } = render(<Example open={true} />)

// The overflow should be there
expect(document.documentElement.style.overflow).toBe('hidden')

// Re-render but with an exit transition
rerender(<Example open={false} />)

// The moment the dialog is closing, the overflow should be gone
expect(document.documentElement.style.overflow).toBe('')
})
)

it(
'should remove the scroll lock when the open closed state is `Closing` (using Transition wrapper)',
suppressConsoleLogs(async () => {
function Example({ open = true }) {
return (
<OpenClosedProvider value={value}>
<Dialog autoFocus={false} open={true} onClose={() => {}}>
<Transition show={open}>
<Dialog autoFocus={false} onClose={() => {}}>
<input id="a" type="text" />
<input id="b" type="text" />
<input id="c" type="text" />
</Dialog>
</OpenClosedProvider>
</Transition>
)
}

let { rerender } = render(<Example value={State.Open} />)
let { rerender } = render(<Example open={true} />)

// The overflow should be there
expect(document.documentElement.style.overflow).toBe('hidden')

// Re-render but with the `Closing` state
rerender(<Example value={State.Open | State.Closing} />)
// Re-render but with an exit transition
rerender(<Example open={false} />)

// The moment the dialog is closing, the overflow should be gone
expect(document.documentElement.style.overflow).toBe('')
Expand Down
3 changes: 1 addition & 2 deletions packages/@headlessui-react/src/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,7 @@ function DialogFn<TTag extends ElementType = typeof DEFAULT_DIALOG_TAG>(
)
}

let inTransitionComponent = usesOpenClosedState !== null
if (!inTransitionComponent && open !== undefined && !rest.static) {
Comment on lines -393 to -394
Copy link
Member Author

Choose a reason for hiding this comment

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

Now we are not relying on the OpenClosed state from a parent to know whether we should wrap the Dialog in a Transition

Instead, we will always do that. If you happen to already wrap your Dialog in a Transition on the outside, then the props will be forwarded to the underlying element because Transition renders as Fragment by default.

if ((open !== undefined || transition) && !rest.static) {
return (
<Transition show={open} transition={transition} unmount={rest.unmount}>
<InternalDialog ref={ref} {...rest} />
Expand Down
Loading