<?php
namespace JanusHercules\Shared\Presentation\Component;
use Exception;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
#[AsTwigComponent(
'button',
'@janus_hercules.shared.presentation/common/button_twig_component.html.twig'
)]
class ButtonTwigComponent
{
public string $type = 'button'; // a, button
public string $text = 'Button';
public string $textAlignment = 'center'; // start, center, end
public string $href = '';
public string $variant = 'filled'; // filled, light, outline, link
public string $size = 'md';
public string $color = 'green'; // red, danger, green, gray, black, accent
public string $class = '';
public string $rounded = 'sm';
public bool $isNewTab = false;
/**
* @throws Exception
*/
public function getStyle(): string
{
// Special handling for navbar links - keep original Tailwind styling
if ($this->variant === 'link' && $this->color === 'white') {
$sizes = [
'sm' => 'tw-text-sm tw-h-8 tw-px-3',
'md' => 'tw-text-base tw-h-9 tw-px-4',
'lg' => 'tw-text-lg tw-h-12 tw-px-5',
];
$size = $sizes[$this->size];
$textAlignment = "tw-justify-$this->textAlignment";
$base = "$size tw-no-underline tw-rounded-$this->rounded tw-whitespace-nowrap tw-cursor-pointer tw-flex $textAlignment tw-items-center";
return "$base tw-shadow-none tw-border-none tw-bg-transparent tw-text-black hover:tw-text-gray-dark $this->class";
}
// Determine if we should use link-as-button or button classes
$isLink = $this->type === 'a';
$prefix = $isLink ? 'jlls-link-as-button-' : 'jlls-button-';
// Map button variants and colors to Living Styleguide classes
$livingStyleguideClasses = [
'filled' => [
'accent' => $prefix . 'default', // Primary button/link
'green' => $prefix . 'tertiary', // Tertiary button/link (green)
'gray' => $prefix . 'secondary', // Secondary button/link
'red' => $prefix . 'danger', // Danger button/link
'danger' => $prefix . 'danger', // Danger button/link
],
'outline' => [
'accent' => $prefix . 'secondary', // Secondary button/link (outline with accent)
'green' => $prefix . 'tertiary', // Tertiary button/link (outline with green)
'gray' => $prefix . 'secondary', // Secondary button/link (outline with gray)
'red' => $prefix . 'danger', // Danger button/link (outline)
'danger' => $prefix . 'danger', // Danger button/link (outline)
],
'light' => [
'accent' => $prefix . 'default',
'green' => $prefix . 'tertiary',
'gray' => $prefix . 'secondary',
'red' => $prefix . 'danger',
'danger' => $prefix . 'danger',
],
'link' => [
'accent' => 'jlls-button-as-link-default',
'green' => 'jlls-button-as-link-tertiary',
'gray' => 'jlls-button-as-link-secondary',
'red' => 'jlls-button-as-link-danger',
'danger' => 'jlls-button-as-link-danger',
],
];
// Check if the variant and color combination exists in Living Styleguide
if (isset($livingStyleguideClasses[$this->variant][$this->color])) {
$styleClass = $livingStyleguideClasses[$this->variant][$this->color];
return trim("$styleClass $this->class");
}
// Fallback: throw exception for unsupported combinations
throw new Exception("
Invalid variant or color: $this->variant, $this->color
- Valid variants: filled, light, outline, link
- Valid colors: green, red, danger, gray, white, accent
");
}
}