Skip to content

useAlert

A composable for programmatic show/hide of inline BvAlert. Use with v-if="isVisible" or :closed="!isVisible" on BvAlert.

Basic Usage

vue
<template>
  <div>
    <BvButton @click="showSuccess">Show success</BvButton>
    <BvAlert
      v-if="isVisible"
      :variant="variant"
      :caption="caption"
      :description="description"
      :closable="closable"
      @close="hide"
    />
  </div>
</template>

<script setup>
import { BvButton, BvAlert } from "@baklavue/ui";
import { useAlert } from "@baklavue/composables";

const { isVisible, variant, caption, description, closable, show, hide } = useAlert();

const showSuccess = () => {
  show({
    variant: "success",
    caption: "Saved",
    description: "Your changes have been saved.",
    closable: true,
  });
};
</script>

With Closable

vue
<script setup>
import { useAlert } from "@baklavue/composables";

const { isVisible, variant, caption, description, closable, show, hide } = useAlert();

show({
  variant: "warning",
  description: "Please fix the errors before submitting.",
  closable: true,
});
</script>

API

Return Value

PropertyTypeDescription
isVisibleRef<boolean>Whether alert is visible
variantRef<AlertVariant>info, success, warning, danger
captionRef<string>Alert title
descriptionRef<string>Alert message
closableRef<boolean>Whether close button is shown
show(options?) => voidShow alert with options
hide() => voidHide alert

Options

typescript
interface UseAlertOptions {
  variant?: "info" | "success" | "warning" | "danger";
  caption?: string;
  description?: string;
  closable?: boolean;
}

TypeScript Support

typescript
import { useAlert, type UseAlertOptions, type AlertVariant } from "@baklavue/composables";

const { show } = useAlert();
show({ variant: "success", description: "Done!" });