Last active
May 22, 2020 20:00
-
-
Save padcom/3bdfe697b11e38dea7692f6401e7cc00 to your computer and use it in GitHub Desktop.
Coding with Padcom - dialogs with Vue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Dialogs</title> | |
<script src="https://unpkg.com/vue"></script> | |
</head> | |
<body> | |
<div id="app"> | |
<button @click="open">Show dialog</button> | |
<notification-dialog ref="dialog"> | |
<p>{{ message }}</p> | |
<template #buttons="{ close }"> | |
<button @click="close('ok')">OK</button> | |
<button @click="close('cancel')">Cancel</button> | |
</template> | |
</notification-dialog> | |
</div> | |
<template id="notification-dialog"> | |
<dialog ref="dialog" @close="close(null)"> | |
<h2>{{ title }}</h2> | |
<div class="dialog__body"> | |
<slot></slot> | |
</div> | |
<div class="dialog__buttons"> | |
<slot name="buttons" :close="close"></slot> | |
</div> | |
</dialog> | |
</template> | |
<script> | |
Vue.component('notification-dialog', { | |
template: '#notification-dialog', | |
props: { | |
title: { type: String, default: 'Notification' } | |
}, | |
methods: { | |
open() { | |
return new Promise(resolve => { | |
this.resolve = resolve | |
this.$refs.dialog.showModal() | |
}) | |
}, | |
close(result) { | |
this.$refs.dialog.close() | |
this.resolve && this.resolve(result) | |
this.resolve = null | |
} | |
} | |
}) | |
</script> | |
<script> | |
new Vue({ | |
el: '#app', | |
data: { | |
message: 'Hello, world!' | |
}, | |
methods: { | |
async open() { | |
const result = await this.$refs.dialog.open() | |
console.log('Dialog result:', result) | |
} | |
} | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment