Skip to content

Close the modal when a user click outside the modal #131

Discussion options

You must be logged in to vote

This is one of those features I expect to be way more involved than it was. Here's what it took:

// Clicking outside the dialog should close it...
this.el.addEventListener('click', e => {
    if (e.target !== this.el) return

    if (clickHappenedOutside(this.el, e)) {
        this.hide()

        e.preventDefault(); e.stopPropagation()
    }
})

function clickHappenedOutside(el, event) {
    let rect = el.getBoundingClientRect()

    let x = event.clientX
    let y = event.clientY

    let isInside =
        x >= rect.left && x <= rect.right &&
        y >= rect.top  && y <= rect.bottom

    return ! isInside
}

This will ship in the next Flux release. Thanks for the input everyone!

Replies: 11 comments 15 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@idealerror
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@joffreypersia
Comment options

@mauricekindermann
Comment options

Comment options

You must be logged in to vote
3 replies
@mauricekindermann
Comment options

@dayemsiddiqui
Comment options

@mauricekindermann
Comment options

Comment options

You must be logged in to vote
9 replies
@joffreypersia
Comment options

@danielrona
Comment options

@jeffchown
Comment options

@ericjamesturner
Comment options

@calebporzio
Comment options

Answer selected by calebporzio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Converted from issue

This discussion was converted from issue #54 on September 27, 2024 13:03.