All files / src/components NotificationContainer.vue

96.55% Statements 28/29
100% Branches 7/7
87.5% Functions 7/8
96.55% Lines 28/29

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                                                        3x 3x     3x 3x   22x 22x     22x             22x 22x   22x 22x       24x 2x 2x       22x 2x 2x       21x     21x 1x       20x   20x 5x         22x     22x 21x   1x             22x                                                                              
<template>
  <div
    v-if="hasNotification"
    class="notification-container"
    role="region"
    aria-label="Notifications"
    aria-live="polite"
    aria-atomic="true"
  >
    <div class="grid-container">
      <div class="grid-x grid-padding-x">
        <div
          class="cell small-12 medium-10 medium-offset-1 large-8 large-offset-2"
        >
          <Notification
            :severity="notification.severity"
            :closable="notification.closable"
            @close="handleClose"
          >
            {{ notification.message }}
          </Notification>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup>
import Notification from '@atomic-ui/notification';
import {
  computed, watch, ref, onUnmounted,
} from 'vue';
import { useStore } from 'vuex';
import { NOTIF_FQ } from '../store/types';
 
const store = useStore();
const dismissTimer = ref(null);
 
// Default auto-dismiss durations (in ms)
const DEFAULT_DURATIONS = {
  success: 5000, // 5 seconds for success
  error: 8000, // 8 seconds for errors (more time to read)
  warning: 6000, // 6 seconds for warnings
  info: 5000, // 5 seconds for info
};
 
const hasNotification = computed(
  () => store.getters[NOTIF_FQ.getters.hasNotification],
);
const notification = computed(
  () => store.getters[NOTIF_FQ.getters.notification],
);
 
function clearDismissTimer() {
  if (dismissTimer.value) {
    clearTimeout(dismissTimer.value);
    dismissTimer.value = null;
  }
}
 
const handleClose = () => {
  clearDismissTimer();
  store.dispatch(NOTIF_FQ.actions.clear);
};
 
function startDismissTimer(notif) {
  clearDismissTimer();
 
  // Skip auto-dismiss if explicitly disabled
  if (notif.autoDismiss === false) {
    return;
  }
 
  // Use custom duration or default based on severity
  const duration = notif.duration || DEFAULT_DURATIONS[notif.severity] || 5000;
 
  dismissTimer.value = setTimeout(() => {
    store.dispatch(NOTIF_FQ.actions.clear);
  }, duration);
}
 
// Watch for notification changes to start/stop timer
watch(
  notification,
  (newNotif) => {
    if (newNotif) {
      startDismissTimer(newNotif);
    } else {
      clearDismissTimer();
    }
  },
  { immediate: true },
);
 
// Cleanup on unmount
onUnmounted(() => {
  clearDismissTimer();
});
</script>
 
<style lang="scss" scoped>
.notification-container {
  position: fixed;
  left: 0;
  right: 0;
  z-index: 9999;
  pointer-events: none;
  animation: slideDown 0.3s ease-out;
  top: rem-calc(8);
 
  @include breakpoint(medium) {
    top: rem-calc(16);
  }
 
  @include breakpoint(large) {
    top: rem-calc(24);
  }
 
  .grid-container {
    pointer-events: auto;
  }
}
 
@keyframes slideDown {
  from {
    transform: translateY(-100%);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>