link.blade.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. @php
  2. use Filament\Support\Enums\FontWeight;
  3. use Filament\Support\Enums\IconPosition;
  4. use Filament\Support\Enums\IconSize;
  5. use Filament\Support\Enums\Size;
  6. use Filament\Support\View\Components\BadgeComponent;
  7. use Filament\Support\View\Components\LinkComponent;
  8. use Illuminate\View\ComponentAttributeBag;
  9. @endphp
  10. @props([
  11. 'badge' => null,
  12. 'badgeColor' => 'primary',
  13. 'badgeSize' => Size::ExtraSmall,
  14. 'color' => 'primary',
  15. 'disabled' => false,
  16. 'form' => null,
  17. 'formId' => null,
  18. 'href' => null,
  19. 'icon' => null,
  20. 'iconAlias' => null,
  21. 'iconPosition' => IconPosition::Before,
  22. 'iconSize' => null,
  23. 'keyBindings' => null,
  24. 'labelSrOnly' => false,
  25. 'loadingIndicator' => true,
  26. 'size' => Size::Medium,
  27. 'spaMode' => null,
  28. 'tag' => 'a',
  29. 'target' => null,
  30. 'tooltip' => null,
  31. 'type' => 'button',
  32. 'weight' => null,
  33. ])
  34. @php
  35. if (! $iconPosition instanceof IconPosition) {
  36. $iconPosition = filled($iconPosition) ? (IconPosition::tryFrom($iconPosition) ?? $iconPosition) : null;
  37. }
  38. if (! $badgeSize instanceof Size) {
  39. $badgeSize = filled($badgeSize) ? (Size::tryFrom($badgeSize) ?? $badgeSize) : null;
  40. }
  41. if (! $size instanceof Size) {
  42. $size = filled($size) ? (Size::tryFrom($size) ?? $size) : null;
  43. }
  44. if (filled($iconSize) && (! $iconSize instanceof IconSize)) {
  45. $iconSize = IconSize::tryFrom($iconSize) ?? $iconSize;
  46. }
  47. $iconSize ??= match ($size) {
  48. Size::ExtraSmall, Size::Small => IconSize::Small,
  49. default => null,
  50. };
  51. if (! $weight instanceof FontWeight) {
  52. $weight = filled($weight) ? (FontWeight::tryFrom($weight) ?? $weight) : null;
  53. }
  54. $wireTarget = $loadingIndicator ? $attributes->whereStartsWith(['wire:target', 'wire:click'])->filter(fn ($value): bool => filled($value))->first() : null;
  55. $hasLoadingIndicator = filled($wireTarget) || ($type === 'submit' && filled($form));
  56. if ($hasLoadingIndicator) {
  57. $loadingIndicatorTarget = html_entity_decode($wireTarget ?: $form, ENT_QUOTES);
  58. }
  59. $hasTooltip = filled($tooltip);
  60. @endphp
  61. <{{ $tag }}
  62. @if (($tag === 'a') && (! ($disabled && $hasTooltip)))
  63. {{ \Filament\Support\generate_href_html($href, $target === '_blank', $spaMode) }}
  64. @endif
  65. @if ($keyBindings)
  66. x-bind:id="$id('key-bindings')"
  67. x-mousetrap.global.{{ collect($keyBindings)->map(fn (string $keyBinding): string => str_replace('+', '-', $keyBinding))->implode('.') }}="document.getElementById($el.id)?.click()"
  68. @endif
  69. @if ($hasTooltip)
  70. x-tooltip="{
  71. content: @js($tooltip),
  72. theme: $store.theme,
  73. allowHTML: @js($tooltip instanceof \Illuminate\Contracts\Support\Htmlable),
  74. }"
  75. @endif
  76. {{
  77. $attributes
  78. ->merge([
  79. 'aria-disabled' => $disabled ? 'true' : null,
  80. 'disabled' => $disabled && blank($tooltip),
  81. 'form' => $formId,
  82. 'type' => $tag === 'button' ? $type : null,
  83. 'wire:loading.attr' => $tag === 'button' ? 'disabled' : null,
  84. 'wire:target' => ($hasLoadingIndicator && $loadingIndicatorTarget) ? $loadingIndicatorTarget : null,
  85. ], escape: false)
  86. ->when(
  87. $disabled && $hasTooltip,
  88. fn (ComponentAttributeBag $attributes) => $attributes->filter(
  89. fn (mixed $value, string $key): bool => ! str($key)->startsWith(['href', 'x-on:', 'wire:click']),
  90. ),
  91. )
  92. ->class([
  93. 'fi-link',
  94. 'fi-disabled' => $disabled,
  95. ($size instanceof Size) ? "fi-size-{$size->value}" : (is_string($size) ? $size : ''),
  96. ($weight instanceof FontWeight) ? "fi-font-{$weight->value}" : (is_string($weight) ? $weight : ''),
  97. ])
  98. ->color(LinkComponent::class, $color)
  99. }}
  100. >
  101. @if ($iconPosition === IconPosition::Before)
  102. @if ($icon)
  103. {{
  104. \Filament\Support\generate_icon_html($icon, $iconAlias, (new \Illuminate\View\ComponentAttributeBag([
  105. 'wire:loading.remove.delay.' . config('filament.livewire_loading_delay', 'default') => $hasLoadingIndicator,
  106. 'wire:target' => $hasLoadingIndicator ? $loadingIndicatorTarget : false,
  107. ])), size: $iconSize)
  108. }}
  109. @endif
  110. @if ($hasLoadingIndicator)
  111. {{
  112. \Filament\Support\generate_loading_indicator_html((new \Illuminate\View\ComponentAttributeBag([
  113. 'wire:loading.delay.' . config('filament.livewire_loading_delay', 'default') => '',
  114. 'wire:target' => $loadingIndicatorTarget,
  115. ])), size: $iconSize)
  116. }}
  117. @endif
  118. @endif
  119. @if (! $labelSrOnly)
  120. {{ $slot }}
  121. @endif
  122. @if ($iconPosition === IconPosition::After)
  123. @if ($icon)
  124. {{
  125. \Filament\Support\generate_icon_html($icon, $iconAlias, (new \Illuminate\View\ComponentAttributeBag([
  126. 'wire:loading.remove.delay.' . config('filament.livewire_loading_delay', 'default') => $hasLoadingIndicator,
  127. 'wire:target' => $hasLoadingIndicator ? $loadingIndicatorTarget : false,
  128. ])), size: $iconSize)
  129. }}
  130. @endif
  131. @if ($hasLoadingIndicator)
  132. {{
  133. \Filament\Support\generate_loading_indicator_html((new \Illuminate\View\ComponentAttributeBag([
  134. 'wire:loading.delay.' . config('filament.livewire_loading_delay', 'default') => '',
  135. 'wire:target' => $loadingIndicatorTarget,
  136. ])), size: $iconSize)
  137. }}
  138. @endif
  139. @endif
  140. @if (filled($badge))
  141. <div class="fi-link-badge-ctn">
  142. @if ($badge instanceof \Illuminate\View\ComponentSlot)
  143. {{ $badge }}
  144. @else
  145. <span
  146. {{
  147. (new ComponentAttributeBag)->color(BadgeComponent::class, $badgeColor)->class([
  148. 'fi-badge',
  149. ($badgeSize instanceof Size) ? "fi-size-{$badgeSize->value}" : (is_string($badgeSize) ? $badgeSize : ''),
  150. ])
  151. }}
  152. >
  153. {{ $badge }}
  154. </span>
  155. @endif
  156. </div>
  157. @endif
  158. </{{ $tag }}>
  159. @trim