All files / src/components PageIcon.vue

100% Statements 16/16
100% Branches 12/12
100% Functions 2/2
100% Lines 15/15

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                                              4162x                   4162x 4162x   4162x 4163x       4163x 4163x 4163x 304x 304x   3859x 3859x   3859x 3858x 3858x          
<template>
  <!-- While an icon loads, its fallback holds the place, invisible, so that
       nothing moves when it appears -->
  <font-awesome-icon
    :icon="definition ?? fallback"
    :fixed-width="fixedWidth"
    :style="loading ? { visibility: 'hidden' } : undefined"
    :data-icon-name="definition ? name : fallback"
  />
</template>
 
<script setup lang="ts">
// @implements UC-SPACE-015.1
/**
 * A space page's menu icon: any Font Awesome free solid icon, fetched on
 * demand (utils/pageIcons.ts); the fallback (a registered icon) for a name
 * Font Awesome does not have.
 */
import { ref, watch } from 'vue'
import type { IconDefinition } from '@fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { loadPageIcon, registeredPageIcon } from '@/utils/pageIcons'
 
const props = withDefaults(
  defineProps<{
    name: string
    /** A registered icon, shown when `name` is unknown */
    fallback?: string
    fixedWidth?: boolean
  }>(),
  { fallback: 'file-lines', fixedWidth: false }
)
 
const definition = ref<IconDefinition | null>(null)
const loading = ref(false)
 
watch(
  () => props.name,
  async (name) => {
    // Registered icons render at once, with no request; so does the fallback,
    // always registered (the default page icons, in main.ts)
    const registered = registeredPageIcon(name)
    definition.value = registered
    if (registered || name === props.fallback) {
      loading.value = false
      return
    }
    loading.value = true
    const loaded = await loadPageIcon(name)
    // Ignore a late answer for a name that changed meanwhile
    if (name !== props.name) return
    definition.value = loaded
    loading.value = false
  },
  { immediate: true }
)
</script>