-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Twig component to display flash messages
- Loading branch information
1 parent
150e6cb
commit 0e878ab
Showing
9 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Twig\Component; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Option\AlertVariant; | ||
|
||
/** | ||
* Highlights important messages that require the user's attention, such as notifications and flash messages. | ||
* Inspired by https://primer.style/components/banner. | ||
*/ | ||
class Alert | ||
{ | ||
public AlertVariant $variant = AlertVariant::Info; | ||
public bool $withDismissButton = false; | ||
|
||
public function mount(string|AlertVariant $variant): void | ||
{ | ||
try { | ||
$this->variant = \is_string($variant) ? AlertVariant::from($variant) : $variant; | ||
} catch (\ValueError) { | ||
throw new \InvalidArgumentException(sprintf('The alert variant "%s" is not valid. Valid values are: %s', $variant, implode(', ', array_map(static fn (AlertVariant $variant): string => $variant->value, AlertVariant::cases())))); | ||
} | ||
} | ||
|
||
public function getDefaultCssClass(): string | ||
{ | ||
$cssClass = sprintf('alert %s', $this->variant->asBootstrapCssClass()); | ||
if ($this->withDismissButton) { | ||
$cssClass .= ' alert-dismissible'; | ||
} | ||
|
||
return $cssClass; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Option; | ||
|
||
enum AlertVariant: string | ||
{ | ||
case Primary = 'primary'; | ||
case Secondary = 'secondary'; | ||
case Success = 'success'; | ||
case Danger = 'danger'; | ||
case Warning = 'warning'; | ||
case Info = 'info'; | ||
case Light = 'light'; | ||
case Dark = 'dark'; | ||
|
||
public function asBootstrapCssClass(): string | ||
{ | ||
return 'alert-'.$this->value; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div {{ attributes.defaults({class: this.defaultCssClass(), role: 'alert'}) }}> | ||
<twig:block name="content"></twig:block> | ||
|
||
{% if withDismissButton %} | ||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> | ||
{% endif %} | ||
</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