Skip to content

Instantly share code, notes, and snippets.

@leemcd56
Last active May 18, 2021 23:35
Show Gist options
  • Save leemcd56/81d737bf8bb7053171540101f7521b62 to your computer and use it in GitHub Desktop.
Save leemcd56/81d737bf8bb7053171540101f7521b62 to your computer and use it in GitHub Desktop.
Vue 3 Delete Button
<template>
<button type="button" @click="confirmDelete">
{{ state.message }}
</button>
</template>
<script>
import { reactive } from 'vue'
export default {
setup(props, { emit }) {
const state = reactive({
isConfirming: false,
message: 'Delete',
timer: null,
})
const confirmDelete = () => {
if (state.isConfirming) {
clearTimeout(state.timer)
resetState()
emit('confirm')
} else {
state.isConfirming = true
state.message = 'Click to confirm'
state.timer = setTimeout(resetState, 5000)
}
}
const resetState = () => {
state.isConfirming = false
state.message = 'Delete'
state.timer = null
}
return {
confirmDelete,
resetState,
state,
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment