All files / components/base BaseAlert.vue

100% Statements 126/126
100% Branches 9/9
100% Functions 2/2
100% Lines 126/126

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188  1x                   1x           1x               1x   1x 23x 23x 23x 23x 23x 23x 23x 1x   1x 23x 23x 23x 23x 23x 23x 23x 1x   1x 23x 23x 23x 23x 23x 23x 23x 1x   1x 23x 23x 23x 23x 23x 23x 23x 1x   1x 1x 1x   1x 1x 1x       1x 1x       1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x         1x 1x 1x 1x 1x   1x 1x 1x             1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x               1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x    
<script setup lang="ts">
import { computed } from 'vue'
 
export interface BaseAlertProps {
  variant?: 'info' | 'success' | 'warning' | 'error'
  title?: string
  message: string
  dismissible?: boolean
  showRetry?: boolean
}
 
const props = withDefaults(defineProps<BaseAlertProps>(), {
  variant: 'info',
  dismissible: false,
  showRetry: false,
})
 
const emit = defineEmits<{
  dismiss: []
  retry: []
}>()
 
/**
 * i18n
 */
const { t } = useI18n()
 
const variantClasses = computed(() => {
  const classes = {
    info: 'bg-info-50 border-info-200 text-info-800',
    success: 'bg-success-50 border-success-200 text-success-800',
    warning: 'bg-warning-50 border-warning-200 text-warning-800',
    error: 'bg-error-50 border-error-200 text-error-800',
  }
  return classes[props.variant]
})
 
const iconClasses = computed(() => {
  const classes = {
    info: 'text-info-600',
    success: 'text-success-600',
    warning: 'text-warning-600',
    error: 'text-error-600',
  }
  return classes[props.variant]
})
 
const iconPath = computed(() => {
  const paths = {
    info: 'M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z',
    success: 'M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z',
    warning: 'M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z',
    error: 'M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z',
  }
  return paths[props.variant]
})
 
const variantLabel = computed(() => {
  const labels = {
    info: t('alerts.info'),
    success: t('alerts.success'),
    warning: t('alerts.warning'),
    error: t('alerts.error'),
  }
  return labels[props.variant]
})
 
function handleDismiss() {
  emit('dismiss')
}
 
function handleRetry() {
  emit('retry')
}
</script>
 
<template>
  <div
    :class="[
      'rounded-lg border p-4',
      variantClasses,
    ]"
    role="alert"
    :aria-live="variant === 'error' ? 'assertive' : 'polite'"
  >
    <div class="flex">
      <div class="flex-shrink-0">
        <svg
          :class="['h-5 w-5', iconClasses]"
          xmlns="http://www.w3.org/2000/svg"
          fill="none"
          viewBox="0 0 24 24"
          stroke="currentColor"
          :aria-label="variantLabel"
        >
          <path
            stroke-linecap="round"
            stroke-linejoin="round"
            stroke-width="2"
            :d="iconPath"
          />
        </svg>
      </div>
      <div class="ml-3 flex-1">
        <h3
          v-if="title"
          class="text-sm font-medium"
        >
          {{ title }}
        </h3>
        <div
          :class="[
            'text-sm',
            title ? 'mt-1' : '',
          ]"
        >
          {{ message }}
        </div>
        <div
          v-if="showRetry"
          class="mt-3"
        >
          <button
            type="button"
            :class="[
              'inline-flex items-center rounded-md px-3 py-2 text-sm font-medium transition-colors',
              variant === 'error'
                ? 'bg-error-100 text-error-800 hover:bg-error-200 focus:ring-error-600'
                : 'bg-info-100 text-info-800 hover:bg-info-200 focus:ring-info-600',
              'focus:outline-none focus:ring-2 focus:ring-offset-2',
            ]"
            @click="handleRetry"
          >
            <svg
              class="-ml-0.5 mr-2 h-4 w-4"
              xmlns="http://www.w3.org/2000/svg"
              fill="none"
              viewBox="0 0 24 24"
              stroke="currentColor"
            >
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
              />
            </svg>
            {{ $t('alerts.retry') }}
          </button>
        </div>
      </div>
      <div
        v-if="dismissible"
        class="ml-auto pl-3"
      >
        <button
          type="button"
          :class="[
            'inline-flex rounded-md p-1.5 transition-colors',
            'focus:outline-none focus:ring-2 focus:ring-offset-2',
            variant === 'info' && 'text-info-600 hover:bg-info-100 focus:ring-info-600',
            variant === 'success' && 'text-success-600 hover:bg-success-100 focus:ring-success-600',
            variant === 'warning' && 'text-warning-600 hover:bg-warning-100 focus:ring-warning-600',
            variant === 'error' && 'text-error-600 hover:bg-error-100 focus:ring-error-600',
          ]"
          :aria-label="$t('alerts.dismiss')"
          @click="handleDismiss"
        >
          <svg
            class="h-5 w-5"
            xmlns="http://www.w3.org/2000/svg"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              stroke-width="2"
              d="M6 18L18 6M6 6l12 12"
            />
          </svg>
        </button>
      </div>
    </div>
  </div>
</template>