All files FzLink.vue

99.49% Statements 198/199
93.75% Branches 30/32
100% Functions 6/6
99.49% Lines 198/199

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 189 190 191 192 193 194 195 196 197 198 1991x 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 37x 1x 1x 1x 1x 1x 1x 36x 37x 1x 1x 1x 1x 1x 1x 35x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 2x 2x 29x 29x 24x 24x 29x 29x 2x 2x 29x 29x 1x 1x 29x 29x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 6x 6x 6x 8x 8x 2x 2x 2x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x   1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 2x 2x 7x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 1x 1x 1x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
<script setup lang="ts">
/**
 * FzLink Component
 *
 * Flexible link component supporting internal routing (vue-router) and external navigation.
 * Automatically renders as router-link for internal routes, anchor tag for external URLs,
 * or non-interactive span when disabled. Provides consistent styling and accessibility.
 *
 * TypeScript enforces correct prop types:
 * - Internal links (external=false or undefined): to accepts RouteLocationRaw (string or object)
 * - External links (external=true): to must be a string URL
 *
 * @component
 * @example
 * <FzLink to="/dashboard" size="md">Go to Dashboard</FzLink>
 * 
 * @example
 * <FzLink :to="{ name: 'user', params: { id: 123 }}">User Profile</FzLink>
 * 
 * @example
 * <FzLink to="https://example.com" external target="_blank">External Site</FzLink>
 */
import { computed } from 'vue'
import { FzLinkProps } from './types'
 
const props = withDefaults(defineProps<FzLinkProps>(), {
  type: 'default',
  linkStyle: 'default',
  size: 'md',
  disabled: false,
  replace: false,
  external: false
})
 
/**
 * Normalizes deprecated size values and shows deprecation warnings.
 * 
 * Maps deprecated sizes to their replacements:
 * - 'xs' → 'sm' (with warning)
 * - 'lg' → 'md' (with warning)
 * 
 * This ensures backward compatibility while encouraging migration to new values.
 */
const normalizedSize = computed(() => {
  if (props.size === 'xs') {
    console.warn(
      '[FzLink] The size prop value "xs" is deprecated and will be removed in a future version. ' +
      'Please use "sm" instead. The component will automatically map "xs" to "sm" for now.'
    )
    return 'sm'
  }
  
  if (props.size === 'lg') {
    console.warn(
      '[FzLink] The size prop value "lg" is deprecated and will be removed in a future version. ' +
      'Please use "md" instead. The component will automatically map "lg" to "md" for now.'
    )
    return 'md'
  }
  
  return props.size as 'sm' | 'md'
})
 
/**
 * Base classes shared between link and disabled span states.
 * 
 * Includes size-based text classes and conditional underline styling.
 * Border classes provide consistent spacing for focus indicators.
 */
const commonClass = computed(() => [
  'border-1 border-transparent inline-block',
  {
    'text-sm leading-xs': normalizedSize.value === 'sm',
    'text-base leading-base': normalizedSize.value === 'md',
    underline: props.linkStyle === 'underline'
  }
])
 
/**
 * Helper functions to identify UI states.
 * 
 * These functions explicitly describe when each UI representation should be applied,
 * making the component logic more declarative and maintainable.
 */
const isDefaultUnderline = (p: typeof props) => p.type === 'default' && p.linkStyle === 'underline'
const isDefaultNoUnderline = (p: typeof props) => p.type === 'default' && p.linkStyle !== 'underline'
const isDangerUnderline = (p: typeof props) => p.type === 'danger' && p.linkStyle === 'underline'
const isDangerNoUnderline = (p: typeof props) => p.type === 'danger' && p.linkStyle !== 'underline'
const isDefaultDisabled = (p: typeof props) => p.type === 'default' && p.disabled
const isDangerDisabled = (p: typeof props) => p.type === 'danger' && p.disabled
 
/**
 * CSS classes for interactive link states (router-link and anchor).
 */
const linkClass = computed(() => {
  const baseClasses = [...commonClass.value]
  
  switch (true) {
    case isDefaultUnderline(props):
      baseClasses.push('text-blue-500', 'hover:text-blue-600', 'focus:text-blue-600')
      break
      
    case isDefaultNoUnderline(props):
      baseClasses.push('text-blue-500', 'hover:text-blue-600', 'hover:underline', 'focus:text-blue-600')
      break
      
    case isDangerUnderline(props):
      baseClasses.push('text-semantic-error-200', 'hover:text-semantic-error-300', 'focus:text-semantic-error-300')
      break
      
    case isDangerNoUnderline(props):
      baseClasses.push('text-semantic-error-200', 'hover:text-semantic-error-300', 'hover:underline', 'focus:text-semantic-error-300')
      break
  }
  
  return baseClasses
})
 
/**
 * CSS classes for disabled link state (rendered as span).
 * 
 * Uses switch(true) pattern to explicitly map disabled UI states to their styling.
 * Each case represents a distinct visual representation of the disabled link.
 */
const spanClass = computed(() => {
  const baseClasses = [...commonClass.value, 'cursor-not-allowed']
  
  switch (true) {
    case isDefaultDisabled(props):
      // Default type disabled: blue-200, underline preserved if linkStyle is underline
      baseClasses.push('text-blue-200')
      break
      
    case isDangerDisabled(props):
      // Danger type disabled: semantic-error-100, underline preserved if linkStyle is underline
      baseClasses.push('text-semantic-error-100')
      break
  }
  
  return baseClasses
})
 
/**
 * Href value for external links.
 * 
 * When external is true, TypeScript guarantees to is a string.
 * This computed ensures type safety in the template.
 */
const externalHref = computed(() => {
  if (props.external) {
    // TypeScript narrows to to string when external is true
    return props.to as string
  }
  return ''
})
 
/**
 * Rel attribute for external links with target="_blank".
 * 
 * Adds security attributes (noopener noreferrer) when opening links in new tab
 * to prevent security vulnerabilities and improve privacy.
 */
const externalRel = computed(() => {
  if (props.external && props.target === '_blank') {
    return 'noopener noreferrer'
  }
  return undefined
})
</script>
 
<template>
  <span
    v-if="disabled"
    :class="spanClass"
    :aria-disabled="disabled ? 'true' : 'false'"
    role="link"
    aria-label="Link disabled"
  >
    <slot></slot>
  </span>
  <a
    v-else-if="external"
    :href="externalHref"
    :class="linkClass"
    :target="target"
    :rel="externalRel"
  >
    <slot></slot>
  </a>
  <router-link
    v-else
    :to="to"
    :replace="replace"
    :class="linkClass"
    :target="target"
  >
    <slot></slot>
  </router-link>
</template>