File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed
@headlessui-react/src/components/dialog
@headlessui-vue/src/components/dialog Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -145,7 +145,23 @@ function useScrollLock(
145145 )
146146
147147 // Restore scroll position
148- d . add ( ( ) => window . scrollTo ( 0 , scrollPosition ) )
148+ d . add ( ( ) => {
149+ // Before opening the Dialog, we capture the current pageYOffset, and offset the page with
150+ // this value so that we can also scroll to `(0, 0)`.
151+ //
152+ // If we want to restore a few things can happen:
153+ //
154+ // 1. The window.pageYOffset is still at 0, this means nothing happened, and we can safely
155+ // restore to the captured value earlier.
156+ // 2. The window.pageYOffset is **not** at 0. This means that something happened (e.g.: a
157+ // link was scrolled into view in the background). Ideally we want to restore to this _new_
158+ // position. To do this, we can take the new value into account with the captured value from
159+ // before.
160+ //
161+ // (Since the value of window.pageYOffset is 0 in the first case, we should be able to
162+ // always sum these values)
163+ window . scrollTo ( 0 , window . pageYOffset + scrollPosition )
164+ } )
149165 }
150166
151167 return d . dispose
Original file line number Diff line number Diff line change @@ -272,7 +272,23 @@ export let Dialog = defineComponent({
272272 )
273273
274274 // Restore scroll position
275- d . add ( ( ) => window . scrollTo ( 0 , scrollPosition ) )
275+ d . add ( ( ) => {
276+ // Before opening the Dialog, we capture the current pageYOffset, and offset the page with
277+ // this value so that we can also scroll to `(0, 0)`.
278+ //
279+ // If we want to restore a few things can happen:
280+ //
281+ // 1. The window.pageYOffset is still at 0, this means nothing happened, and we can safely
282+ // restore to the captured value earlier.
283+ // 2. The window.pageYOffset is **not** at 0. This means that something happened (e.g.: a
284+ // link was scrolled into view in the background). Ideally we want to restore to this _new_
285+ // position. To do this, we can take the new value into account with the captured value from
286+ // before.
287+ //
288+ // (Since the value of window.pageYOffset is 0 in the first case, we should be able to
289+ // always sum these values)
290+ window . scrollTo ( 0 , window . pageYOffset + scrollPosition )
291+ } )
276292 }
277293
278294 onInvalidate ( d . dispose )
You can’t perform that action at this time.
0 commit comments