Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/docs/src/examples/v-alert/prop-duration.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<div>
<v-btn
v-if="!visible"
@click="visible = true"
>
Show alert
</v-btn>

<v-alert
v-else
v-model="visible"
:duration="3000"
text="This alert will automatically dismiss after 3 seconds."
type="info"
closable
></v-alert>
</div>
</template>

<script setup>
import { ref } from 'vue'

const visible = ref(true)
</script>
6 changes: 6 additions & 0 deletions packages/docs/src/pages/en/components/alerts.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ The **closable** prop adds a [v-icon](/components/icons) on the far right, after

<ExamplesExample file="v-alert/prop-closable" />

#### Duration

The **duration** prop causes the alert to automatically dismiss itself after the specified number of milliseconds. Set to `-1` (the default) to disable auto-dismiss. Combine with **closable** to give users both auto-dismiss and manual control.

<ExamplesExample file="v-alert/prop-duration" />

The close icon automatically applies a default `aria-label` and is configurable by using the **close-label** prop or changing **close** value in your locale.

::: info
Expand Down
28 changes: 27 additions & 1 deletion packages/vuetify/src/components/VAlert/VAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { makeThemeProps, provideTheme } from '@/composables/theme'
import { genOverlays, makeVariantProps, useVariant } from '@/composables/variant'

// Utilities
import { toRef } from 'vue'
import { onMounted, onScopeDispose, toRef, watch } from 'vue'
import { genericComponent, propsFactory } from '@/util'

// Types
Expand Down Expand Up @@ -57,6 +57,10 @@ export const makeVAlertProps = propsFactory({
type: String,
default: '$vuetify.close',
},
duration: {
type: [Number, String],
default: -1,
},
icon: {
type: [Boolean, String, Function, Object] as PropType<false | IconValue>,
default: null,
Expand Down Expand Up @@ -107,6 +111,28 @@ export const VAlert = genericComponent<VAlertSlots>()({

setup (props, { emit, slots }) {
const isActive = useProxiedModel(props, 'modelValue')

let dismissTimer = -1
function clearDismissTimer () {
if (dismissTimer !== -1) {
window.clearTimeout(dismissTimer)
dismissTimer = -1
}
}
function scheduleDismiss () {
clearDismissTimer()
const ms = Number(props.duration)
if (ms > 0 && isActive.value) {
dismissTimer = window.setTimeout(() => {
isActive.value = false
dismissTimer = -1
}, ms)
}
}
onMounted(scheduleDismiss)
watch(() => [props.duration, isActive.value], scheduleDismiss)
onScopeDispose(clearDismissTimer)

const icon = toRef(() => {
if (props.icon === false) return undefined
if (!props.type) return props.icon
Expand Down
4 changes: 3 additions & 1 deletion packages/vuetify/src/components/VList/VListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ export const VListItem = genericComponent<VListItemSlots>()({
}
}

const clickHandler = computed(() => (isClickable.value || attrs.onClick) ? onClick : undefined)

useRender(() => {
const Tag = isLink.value ? 'a' : props.tag
const hasTitle = (slots.title || props.title != null)
Expand Down Expand Up @@ -317,7 +319,7 @@ export const VListItem = genericComponent<VListItemSlots>()({
tabindex={ props.tabindex ?? (isClickable.value ? (list ? -2 : 0) : undefined) }
aria-selected={ ariaSelected.value }
role={ role.value }
onClick={ onClick }
onClick={ clickHandler.value }
onKeydown={ isClickable.value && !isLink.value && onKeyDown }
v-ripple={ isClickable.value && rippleOptions.value }
>
Expand Down
Loading