Skip to content

Commit ad2bd96

Browse files
authored
feat(Alert): enhance component with elevation validation and preprocess attributes (#54)
1 parent 98ad3e4 commit ad2bd96

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

src/Components/Alert.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MaterialBlade\Components;
46

57
use Illuminate\View\Component;
68
use Illuminate\View\ComponentAttributeBag;
9+
use InvalidArgumentException;
710
use MaterialBlade\Components\Alert\Properties\Severity;
811
use MaterialBlade\Components\Alert\Properties\Variant;
912
use MaterialBlade\Helper;
1013

1114
/**
15+
* Alert Component
16+
*
17+
* Displays an alert message with optional icon, title, and customizable styling.
18+
* Supports different severity levels, variants, and Material Design elevation.
19+
*
20+
* @property Severity $severity The severity level of the alert (success, info, warning, error)
21+
* @property Variant $variant The visual variant (standard, filled, outlined)
22+
* @property string $cssColor The computed CSS color value
23+
* @property string|array|null $icon Custom icon or auto-selected based on severity
24+
* @property string|null $title Optional title text
25+
* @property int $elevation Material Design elevation level (0-24)
26+
*
1227
* @see https://mui.com/material-ui/react-alert/
28+
* @see https://m2.material.io/design/components/elevation.html
1329
*/
1430
class Alert extends Component
1531
{
@@ -22,7 +38,15 @@ class Alert extends Component
2238
/**
2339
* Create a new component instance.
2440
*
41+
* @param string|null $severity The severity level: 'success', 'info', 'warning', or 'error'
42+
* @param string|null $color Custom color (theme color or CSS color value)
43+
* @param string|null $variant Display variant: 'standard', 'filled', or 'outlined'
44+
* @param string|array|null $icon Custom icon name or [name, variant] array
45+
* @param string|null $title Optional title text displayed above the message
46+
* @param int $elevation Material Design elevation level (0-24)
2547
* @return void
48+
*
49+
* @throws InvalidArgumentException If elevation is out of range
2650
*/
2751
public function __construct(
2852
?string $severity = null,
@@ -31,7 +55,16 @@ public function __construct(
3155

3256
private string|array|null $icon = null,
3357
public ?string $title = null,
58+
private int $elevation = 0,
3459
) {
60+
// Validate elevation range
61+
if ($this->elevation < 0 || $this->elevation > 24) {
62+
throw new InvalidArgumentException(
63+
"Elevation must be between 0 and 24, got {$this->elevation}. ".
64+
'See Material Design elevation specification: https://m2.material.io/design/environment/elevation.html'
65+
);
66+
}
67+
3568
if ($variant) {
3669
$this->variant = Variant::fromString($variant);
3770
}
@@ -53,8 +86,21 @@ public function render()
5386
return 'mbv::alert';
5487
}
5588

89+
/**
90+
* Preprocess component attributes to apply styling and classes.
91+
*
92+
* Applies appropriate styling based on variant (standard, filled, outlined),
93+
* adds Material Design elevation classes, and sets typography.
94+
*
95+
* @param ComponentAttributeBag $attributes Component attributes
96+
* @return ComponentAttributeBag Processed attributes with classes and styles
97+
*/
5698
public function attributesPreprocess(ComponentAttributeBag $attributes): ComponentAttributeBag
5799
{
100+
$attributes = $attributes->class([
101+
"mdc-elevation--z{$this->elevation}" => $this->elevation > 0 && $this->elevation <= 24,
102+
]);
103+
58104
if ($this->variant === Variant::FILLED) {
59105
return $attributes->style([
60106
"background-color: {$this->cssColor}",
@@ -78,6 +124,14 @@ public function attributesPreprocess(ComponentAttributeBag $attributes): Compone
78124
])->class('mdc-typography--body1');
79125
}
80126

127+
/**
128+
* Get the icon to display in the alert.
129+
*
130+
* Returns custom icon if provided, otherwise returns default icon
131+
* based on severity level with 'outlined' variant.
132+
*
133+
* @return string|array Icon name as string or [name, variant] array
134+
*/
81135
public function getIcon(): string|array
82136
{
83137
return $this->icon ?? match ($this->severity) {

0 commit comments

Comments
 (0)