Skip to content

Commit 7ec0652

Browse files
Don't scroll lock when transition isn't showing (#2422)
* Add tests * Make transition initial state based on computed `show` prop * Update changelog
1 parent 2063132 commit 7ec0652

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

packages/@headlessui-react/src/components/dialog/dialog.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,28 @@ describe('Rendering', () => {
440440
expect(document.documentElement.style.overflow).toBe('')
441441
})
442442
)
443+
444+
it(
445+
'should not have a scroll lock when the transition marked as not shown',
446+
suppressConsoleLogs(async () => {
447+
function Example() {
448+
return (
449+
<Transition as={Fragment} show={false} unmount={false}>
450+
<Dialog as="div" onClose={() => {}}>
451+
<input type="text" />
452+
</Dialog>
453+
</Transition>
454+
)
455+
}
456+
457+
render(<Example />)
458+
459+
await nextFrame()
460+
461+
// The overflow should NOT be there
462+
expect(document.documentElement.style.overflow).toBe('')
463+
})
464+
)
443465
})
444466

445467
describe('Dialog.Overlay', () => {

packages/@headlessui-vue/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Fix focus styles showing up when using the mouse ([#2347](https://github.com/tailwindlabs/headlessui/pull/2347))
1313
- Disable `ComboboxInput` when its `Combobox` is disabled ([#2375](https://github.com/tailwindlabs/headlessui/pull/2375))
1414
- Add `FocusTrap` event listeners once document has loaded ([#2389](https://github.com/tailwindlabs/headlessui/pull/2389))
15+
- Don't scroll-lock `<Dialog>` when wrapping transition isn't showing ([#2422](https://github.com/tailwindlabs/headlessui/pull/2422))
1516

1617
### Added
1718

packages/@headlessui-vue/src/components/dialog/dialog.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,30 @@ describe('Rendering', () => {
615615
expect(document.documentElement.style.overflow).toBe('')
616616
})
617617
)
618+
619+
it(
620+
'should not have a scroll lock when the transition marked as not shown',
621+
suppressConsoleLogs(async () => {
622+
renderTemplate({
623+
components: {
624+
Dialog,
625+
TransitionRoot,
626+
},
627+
template: `
628+
<TransitionRoot as="template" :show="false" :unmount="false">
629+
<Dialog as="div">
630+
<input type="text" />
631+
</Dialog>
632+
</TransitionRoot>
633+
`,
634+
})
635+
636+
await nextFrame()
637+
638+
// The overflow should NOT be there
639+
expect(document.documentElement.style.overflow).toBe('')
640+
})
641+
)
618642
})
619643

620644
describe('DialogOverlay', () => {

packages/@headlessui-vue/src/components/transitions/transition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ export let TransitionChild = defineComponent({
193193
}
194194

195195
let container = ref<HTMLElement | null>(null)
196-
let state = ref(TreeStates.Visible)
197196
let strategy = computed(() => (props.unmount ? RenderStrategy.Unmount : RenderStrategy.Hidden))
198197

199198
expose({ el: container, $el: container })
200199

201200
let { show, appear } = useTransitionContext()
202201
let { register, unregister } = useParentNesting()
203202

203+
let state = ref(show.value ? TreeStates.Visible : TreeStates.Hidden)
204204
let initial = { value: true }
205205

206206
let id = useId()
@@ -228,7 +228,7 @@ export let TransitionChild = defineComponent({
228228
if (!id) return
229229

230230
// Make sure that we are visible
231-
if (show && state.value !== TreeStates.Visible) {
231+
if (show.value && state.value !== TreeStates.Visible) {
232232
state.value = TreeStates.Visible
233233
return
234234
}

0 commit comments

Comments
 (0)