src/JanusHercules/Shared/Presentation/Component/ButtonTwigComponent.php line 12

Open in your IDE?
  1. <?php
  2. namespace JanusHercules\Shared\Presentation\Component;
  3. use Exception;
  4. use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
  5. #[AsTwigComponent(
  6. 'button',
  7. '@janus_hercules.shared.presentation/common/button_twig_component.html.twig'
  8. )]
  9. class ButtonTwigComponent
  10. {
  11. public string $type = 'button'; // a, button
  12. public string $text = 'Button';
  13. public string $textAlignment = 'center'; // start, center, end
  14. public string $href = '';
  15. public string $variant = 'filled'; // filled, light, outline, link
  16. public string $size = 'md';
  17. public string $color = 'green'; // red, danger, green, gray, black, accent
  18. public string $class = '';
  19. public string $rounded = 'sm';
  20. public bool $isNewTab = false;
  21. /**
  22. * @throws Exception
  23. */
  24. public function getStyle(): string
  25. {
  26. // Special handling for navbar links - keep original Tailwind styling
  27. if ($this->variant === 'link' && $this->color === 'white') {
  28. $sizes = [
  29. 'sm' => 'tw-text-sm tw-h-8 tw-px-3',
  30. 'md' => 'tw-text-base tw-h-9 tw-px-4',
  31. 'lg' => 'tw-text-lg tw-h-12 tw-px-5',
  32. ];
  33. $size = $sizes[$this->size];
  34. $textAlignment = "tw-justify-$this->textAlignment";
  35. $base = "$size tw-no-underline tw-rounded-$this->rounded tw-whitespace-nowrap tw-cursor-pointer tw-flex $textAlignment tw-items-center";
  36. return "$base tw-shadow-none tw-border-none tw-bg-transparent tw-text-black hover:tw-text-gray-dark $this->class";
  37. }
  38. // Determine if we should use link-as-button or button classes
  39. $isLink = $this->type === 'a';
  40. $prefix = $isLink ? 'jlls-link-as-button-' : 'jlls-button-';
  41. // Map button variants and colors to Living Styleguide classes
  42. $livingStyleguideClasses = [
  43. 'filled' => [
  44. 'accent' => $prefix . 'default', // Primary button/link
  45. 'green' => $prefix . 'tertiary', // Tertiary button/link (green)
  46. 'gray' => $prefix . 'secondary', // Secondary button/link
  47. 'red' => $prefix . 'danger', // Danger button/link
  48. 'danger' => $prefix . 'danger', // Danger button/link
  49. ],
  50. 'outline' => [
  51. 'accent' => $prefix . 'secondary', // Secondary button/link (outline with accent)
  52. 'green' => $prefix . 'tertiary', // Tertiary button/link (outline with green)
  53. 'gray' => $prefix . 'secondary', // Secondary button/link (outline with gray)
  54. 'red' => $prefix . 'danger', // Danger button/link (outline)
  55. 'danger' => $prefix . 'danger', // Danger button/link (outline)
  56. ],
  57. 'light' => [
  58. 'accent' => $prefix . 'default',
  59. 'green' => $prefix . 'tertiary',
  60. 'gray' => $prefix . 'secondary',
  61. 'red' => $prefix . 'danger',
  62. 'danger' => $prefix . 'danger',
  63. ],
  64. 'link' => [
  65. 'accent' => 'jlls-button-as-link-default',
  66. 'green' => 'jlls-button-as-link-tertiary',
  67. 'gray' => 'jlls-button-as-link-secondary',
  68. 'red' => 'jlls-button-as-link-danger',
  69. 'danger' => 'jlls-button-as-link-danger',
  70. ],
  71. ];
  72. // Check if the variant and color combination exists in Living Styleguide
  73. if (isset($livingStyleguideClasses[$this->variant][$this->color])) {
  74. $styleClass = $livingStyleguideClasses[$this->variant][$this->color];
  75. return trim("$styleClass $this->class");
  76. }
  77. // Fallback: throw exception for unsupported combinations
  78. throw new Exception("
  79. Invalid variant or color: $this->variant, $this->color
  80. - Valid variants: filled, light, outline, link
  81. - Valid colors: green, red, danger, gray, white, accent
  82. ");
  83. }
  84. }