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

Prevent popper closing when document is clicked #120

Open
garethellis36 opened this issue Nov 6, 2019 · 2 comments
Open

Prevent popper closing when document is clicked #120

garethellis36 opened this issue Nov 6, 2019 · 2 comments

Comments

@garethellis36
Copy link

I'd like to put a form inside a popper which is opened when a button is clicked. That is fine and easy to achieve with trigger="clickToOpen". However, whenever someone clicks outside the popper, the popper closes, which could be annoying for users. I want to control when the popper closes via a specific 'close' button and other events. Is this possible?

@cristianpoleyJS
Copy link

Hello, you can do it by placing the ref="<your-name>" in your component <Popper>. Then, captures when you click on an element and launch this.$refs.<your-name>.click() and it should close.

@etino
Copy link

etino commented May 28, 2021

This is an old issue, but the solution to achieve the behavior requested by @garethellis36 is the following

<template>
    <div>
        <popper trigger="clickToOpen" :force-show="show">
            <div class="popper">
                <button type="button" @click="close">Close Popover</button>
            </div>
        </popper>    
        
        <a @click="open" slot="reference" style="cursor: pointer"> 
            // @click required to set force-show to true
            Open Popover
        </a>
    </div>
</template>
<script>
import Popper from 'vue-popperjs'

export default {
    components: {
        Popper
    },
    methods: {
        open(){
	    this.show= true;
        }
        close() {
            this.show = false;
        }
    },
    data(){
        return {
            show: false
        }
    }
}
</script>

Reviewing the source code, you can see that if force-show is set to true, the handleDocumentClick method when is fired by the Document.click listener, returns before setting the showPopper to false, so the Popover remains open.

if (this.forceShow) {

Pay attention to manage the open data property bound with force-show when you click in the reference slot otherwise it doesn't work.

I don't know if this project is still active, but could be useful to improve the documentation for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants