DEV Community

loading...
Cover image for useDialog - Vue.js snippets

useDialog - Vue.js snippets

neophen profile image Mykolas Mankevicius ・1 min read

TL;DR;

Here's the link to the gist use/change/modify it.

Have you ever wanted a simple dialog, which is completely custom, but you didn't want to pull in a package.

But it always looks like a mess to make?

Well worry no more! In todays Vue.js snippets post we'll make just that! Don't believe me? Have a look at the finished result!

And this is the API for it:
the api of using dialogs

If you like it here's the explanation on how to write one yourself.

The useDialog.js

The basic premise is, we want a nice api to use dialogs right?

Something that has an expresive api with autocompletion if possible. Something like:

Dialog.myCustomType()
.title('hello')
.onConfirm(() => { //do something })
Enter fullscreen mode Exit fullscreen mode

This is a bit of a whopper of a class, but it's easy to write test for. I've written the basic api. You can change/modify this to your liking.

Thing to improve?

  • Allow multiple onConfirm/onCancel callbacks Quite simple just change the onConfirmCallback to onConfirmCallbacks = [] And then on each call of onConfirm, just push the callback into the stack, and onResolve await all of the callbacks

Discussion (0)

Forem Open with the Forem app