HubSpot / vex

A modern dialog library which is highly configurable and easy to style. #hubspot-open-source
http://github.hubspot.com/vex/docs/welcome
MIT License
6.92k stars 493 forks source link

Dialog closes instantly upon initialization #207

Closed Janskiproducer closed 6 years ago

Janskiproducer commented 7 years ago

When trying to make Vex appear on the page, I first tested the initialization as a part of the jQuery document ready section. When the page loads, the dialog loads OK.

I then moved the opening of an alert out into it's own function, and tried to call that function from a href tag. The dialog loads, along with the overlay, but as soon as it's loaded it disappears again instantly.

This is the function I call from the href: function kjorpo(){ vex.dialog.alert({ message: 'This is a test', overlayClosesOnClick: false, showCloseButton: true }); }

Tested in Chrome v. 56

bbatliner commented 7 years ago

I'm assuming that when you say "call that function from a href tag" you mean, an anchor tag like this:

<a href="#" onclick="kjorpo()">Link</a>

The problem is that clicking on the link triggers a popstate event - technically, the page is navigating to #. Vex detects this and by default closes all dialogs.

I would recommend using another element, like a button, and your code will work fine.

bbatliner commented 6 years ago

You could also set the option vex.defaultOptions.closeAllOnPopState to false to disable that behavior... Closing to clean up!

aderegil commented 4 years ago

I am using Vuejs and ran into the same with

<a href="#" v-on:click="confirmDelete(id)"></a>

I cannot prevent the return, but @bbatliner suggestion solved the problem:

vex.defaultOptions.closeAllOnPopState = false;

Thanks.