Last active
September 25, 2021 20:07
-
-
Save stuart-haas/38cc1288f0945a8a974f3e3cdc259d21 to your computer and use it in GitHub Desktop.
Nuxt Dialog Modal
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
<template> | |
<transition name="fade"> | |
<div v-if="open" class="modal-backdrop"> | |
<div class="modal"> | |
<div class="modal-body"> | |
<p>{{ message }}</p> | |
</div> | |
<div class="modal-footer"> | |
<button class="button button--confirm" @click.stop="confirm">Confirm</button> | |
<button class="button button--cancel" @click.stop="cancel">Cancel</button> | |
</div> | |
</div> | |
</div> | |
</transition> | |
</template> | |
<script> | |
export default { | |
data() { | |
return { | |
open: false, | |
message: '', | |
resolver: () => null, | |
}; | |
}, | |
mounted() { | |
this.$nuxt.$on('dialog', this.dialog); | |
}, | |
methods: { | |
confirm() { | |
this.resolver(new Promise((resolve) => resolve('confirmed'))); | |
this.close(); | |
}, | |
cancel() { | |
this.resolver(new Promise((resolve, reject) => reject(new Error('cancelled')))); | |
this.close(); | |
}, | |
close() { | |
this.open = false; | |
}, | |
dialog({ message, open, resolver }) { | |
this.message = message; | |
this.open = open; | |
this.resolver = resolver; | |
} | |
} | |
}; | |
</script> | |
<style lang="scss" scoped> | |
.modal-backdrop { | |
position: fixed; | |
width: 100%; | |
height: 100%; | |
background-color: rgba(black, 0.75); | |
} | |
.modal { | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
padding: 16px; | |
background-color: white; | |
border-radius: 4px; | |
transform: translate(-50%, -50%); | |
} | |
.modal-body { | |
padding: 16px 0; | |
} | |
.modal-footer { | |
padding-top: 16px; | |
border-top: solid 2px gray; | |
text-align: right; | |
button { | |
margin-left: 16px; | |
} | |
} | |
</style> |
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
<template> | |
<div> | |
<button class="button button--confirm" @click="openDialog">Open Dialog</button> | |
<DialogModal /> | |
</div> | |
</template> | |
<script> | |
export default { | |
methods: { | |
openDialog() { | |
this.$dialog.open({ | |
message: 'Are you sure you want to do this?', | |
resolver: (async (result) => { | |
try { | |
const res = await result; | |
/* eslint-disable no-console */ | |
console.log(res); | |
} catch (error) { | |
console.warn(error); | |
} | |
}), | |
}); | |
} | |
} | |
}; | |
</script> | |
<style lang="scss" scoped> | |
.button { | |
position: fixed; | |
top: 50%; | |
left: 50%; | |
transform: translate(-50%, -50%); | |
cursor: pointer; | |
} | |
</style> |
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
.button { | |
padding: 2px; | |
background: none; | |
border-radius: 2px; | |
cursor: pointer; | |
text-align: center; | |
&--confirm { | |
padding: 8px 8px 6px 8px; | |
border: none; | |
background-color: green; | |
color: white; | |
} | |
&--cancel { | |
padding: 8px 8px 6px 8px; | |
border: none; | |
background-color: red; | |
color: white; | |
} | |
} |
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
export default (context, inject) => { | |
const dialog = { | |
open: (options) => { | |
window.$nuxt.$emit('dialog', { ...options, open: true }); | |
}, | |
close: (options) => { | |
window.$nuxt.$emit('dialog', { ...options, open: false }); | |
} | |
}; | |
inject('dialog', dialog); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment