Created
September 8, 2023 11:17
-
-
Save martinbean/a99f7494c42a7a761d211e25e23a342b to your computer and use it in GitHub Desktop.
Laravel stack Blade component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div {{ $attributes->class(['hstack' => $horizontal, sprintf('gap-%d', $gap), 'vstack' => $vertical]) }}> | |
{{ $slot }} | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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