kithkin/resources/views/components/button/index.blade.php

52 lines
1.4 KiB
PHP

@props([
'variant' => '', // e.g. "primary danger"
'size' => 'default', // sm | default | lg
'type' => 'button', // anchor | button | submit
'class' => '',
'label' => '',
'href' => null,
])
@php
// allow "primary danger" (space-delimited), or even "primary,danger"
$variantTokens = preg_split('/[\s,]+/', trim($variant)) ?: [];
$variantMap = [
'primary' => 'button--primary',
'secondary' => 'button--secondary',
'tertiary' => 'button--tertiary',
'danger' => 'button--danger',
];
$variantClass = collect($variantTokens)
->map(fn ($v) => $variantMap[$v] ?? null)
->filter()
->implode(' ');
$sizeClass = match ($size) {
'sm' => 'button--sm',
'lg' => 'button--lg',
default => '',
};
$isAnchor = $type === 'anchor';
$tag = $isAnchor ? 'a' : 'button';
$buttonType = $isAnchor ? null : ($type === 'submit' ? 'submit' : 'button');
$classes = trim("button {$variantClass} {$sizeClass} {$class}");
@endphp
@if($isAnchor)
<a href="{{ $href }}"
aria-label="{{ $label }}"
{{ $attributes->merge(['class' => $classes]) }}>
{{ $slot }}
</a>
@else
<button type="{{ $buttonType }}"
aria-label="{{ $label }}"
{{ $attributes->merge(['class' => $classes]) }}>
{{ $slot }}
</button>
@endif