Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created September 8, 2023 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbean/a99f7494c42a7a761d211e25e23a342b to your computer and use it in GitHub Desktop.
Save martinbean/a99f7494c42a7a761d211e25e23a342b to your computer and use it in GitHub Desktop.
Laravel stack Blade component
<div {{ $attributes->class(['hstack' => $horizontal, sprintf('gap-%d', $gap), 'vstack' => $vertical]) }}>
{{ $slot }}
</div>
<?php
namespace App\View\Components;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use InvalidArgumentException;
class Stack extends Component
{
/**
* The stack gap value.
*/
public int $gap;
/**
* Indiciates if the stack should be horizontal.
*/
public bool $horizontal;
/**
* Indicates if the stack should be vertical.
*/
public bool $vertical;
/**
* Create a new component instance.
*
* @throws \InvalidArgumentException
*/
public function __construct(int $gap, bool $horizontal = false, bool $vertical = false)
{
$this->gap = $gap;
$this->horizontal = $horizontal;
$this->vertical = $vertical;
if ($this->gap < 0 || $this->gap > 5) {
throw new InvalidArgumentException('Gap should be an integer between 0 and 5');
}
if ($this->horizontal === $this->vertical) {
throw new InvalidArgumentException('Stack should be one of horizontal or vertical');
}
}
/**
* Get the view that represents the component.
*/
public function render(): View
{
return view('components.stack');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment