-
-
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.
feature #6642 Add a Twig component to display flash messages (javiere…
…guiluz) This PR was squashed before being merged into the 4.x branch. Discussion ---------- Add a Twig component to display flash messages We're going to transform the entire UI using Twig Components. We've already have 2 components and this adds the third one. It's called `Banner` and it can render flash messages / notifications. Some comments: * I called it `Banner` to follow GitHub's Primer component naming (https://primer.style/components/banner) I like GitHub components and I'm getting many ideas from them. * The prop names `withDismissButton` and `variant` are also inspired/copied from GtiHub components * I created an `Enum` (the first one in this bundle!; this is fine because we now require PHP 8.1 and enums are supported there) to define the possible values of `variant`. I opted for the namespace `Config\Enum\...` because we already have `Config\Option\...` for the constant-based classes. The idea is to turn (in the future) the constants into enums, so we need a different namespace. Do you like `Enum\` or do you prefer a different name? Thanks! Commits ------- 0e878ab Add a Twig component to display flash messages
- Loading branch information
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