Skip to content

Commit

Permalink
Modals: Prevent background scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
benpate committed Nov 15, 2023
1 parent 0d72e85 commit f265ec7
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions _embed/templates/theme-global/hyperscript/modal._hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
behavior Modal

init
measure #modal-window
set delta to Math.floor(it's top / 3)
-- set #modal-window's *transform to `translateY(-${delta}px)`

add [@role="dialog"]
set title to the first <h1,h2,h3/> in me

Expand All @@ -17,23 +13,38 @@ behavior Modal
set the @aria-labelledby to the title's id
end

-- prevent window from scrolling underneath the modal
set document.body.style.position to "fixed"
set document.body.style.top to `-${window.scrollY}px`

wait a tick then
add .ready to #modal
focus() the first <[tabindex]/> in me
end

on closeModal(nextPage) from window

if #modal is not empty then
remove .ready from #modal
add .closing to #modal
settle
remove #modal
if #modal is empty then
exit
end

if nextPage is not empty then
set the window's location to nextPage
-- reset scroll position
set scrollY to parseInt(document.body.style.top)
set document.body.style.position to ""
set document.body.style.top to ""

if scrollY is not empty then
window.scrollTo(0, scrollY * -1)
end

-- animate the modal closing
remove .ready from #modal
add .closing to #modal
settle

-- done
remove #modal

end

on click (target)
Expand Down

0 comments on commit f265ec7

Please sign in to comment.