Skip to content

Commit 81e468d

Browse files
committedDec 15, 2024·
feature #6642 Add a Twig component to display flash messages (javiereguiluz)
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
2 parents 28dc420 + 0e878ab commit 81e468d

File tree

9 files changed

+85
-6
lines changed

9 files changed

+85
-6
lines changed
 

‎assets/css/easyadmin-theme/base.css

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
:root {
2+
color-scheme: light dark;
3+
}
4+
15
/* Reset styles
26
========================================================================= */
37
html, body {
@@ -869,6 +873,14 @@ a.user-menu-wrapper .user-details:hover {
869873
box-shadow: none;
870874
outline: none;
871875
background-size: .75em;
876+
padding: .5em;
877+
top: .75em;
878+
right: .5em;
879+
}
880+
.alert .btn-close:focus,
881+
.alert .btn-close:hover {
882+
opacity: 1;
883+
background-color: light-dark(rgba(128, 128, 128, 0.2), rgba(128, 128, 128, 0.5));
872884
}
873885

874886
.alert:last-of-type {
@@ -920,6 +932,9 @@ a.user-menu-wrapper .user-details:hover {
920932
.ea-dark-scheme .modal-header .btn-close {
921933
filter: invert(1);
922934
}
935+
[data-bs-theme="dark"] .btn-close {
936+
filter: none;
937+
}
923938

924939
/* Utilities */
925940
.text-primary { color: var(--text-primary-color) !important; }

‎config/services.php

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
use EasyCorp\Bundle\EasyAdminBundle\Router\UrlSigner;
8383
use EasyCorp\Bundle\EasyAdminBundle\Security\AuthorizationChecker;
8484
use EasyCorp\Bundle\EasyAdminBundle\Security\SecurityVoter;
85+
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Alert;
8586
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Flag;
8687
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Icon;
8788
use EasyCorp\Bundle\EasyAdminBundle\Twig\EasyAdminTwigExtension;
@@ -403,5 +404,8 @@
403404

404405
->set(Flag::class)
405406
->tag('twig.component')
407+
408+
->set(Alert::class)
409+
->tag('twig.component')
406410
;
407411
};

‎public/app.b1430af9.css ‎public/app.fc5841f9.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎public/entrypoints.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"entrypoints": {
33
"app": {
44
"css": [
5-
"/app.b1430af9.css"
5+
"/app.fc5841f9.css"
66
],
77
"js": [
88
"/app.1ecd6d7a.js"

‎public/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"app.css": "app.b1430af9.css",
2+
"app.css": "app.fc5841f9.css",
33
"app.js": "app.1ecd6d7a.js",
44
"form.js": "form.bcec6c2a.js",
55
"page-layout.js": "page-layout.3347892e.js",

‎src/Twig/Component/Alert.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Twig\Component;
4+
5+
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Option\AlertVariant;
6+
7+
/**
8+
* Highlights important messages that require the user's attention, such as notifications and flash messages.
9+
* Inspired by https://primer.style/components/banner.
10+
*/
11+
class Alert
12+
{
13+
public AlertVariant $variant = AlertVariant::Info;
14+
public bool $withDismissButton = false;
15+
16+
public function mount(string|AlertVariant $variant): void
17+
{
18+
try {
19+
$this->variant = \is_string($variant) ? AlertVariant::from($variant) : $variant;
20+
} catch (\ValueError) {
21+
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()))));
22+
}
23+
}
24+
25+
public function getDefaultCssClass(): string
26+
{
27+
$cssClass = sprintf('alert %s', $this->variant->asBootstrapCssClass());
28+
if ($this->withDismissButton) {
29+
$cssClass .= ' alert-dismissible';
30+
}
31+
32+
return $cssClass;
33+
}
34+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Option;
4+
5+
enum AlertVariant: string
6+
{
7+
case Primary = 'primary';
8+
case Secondary = 'secondary';
9+
case Success = 'success';
10+
case Danger = 'danger';
11+
case Warning = 'warning';
12+
case Info = 'info';
13+
case Light = 'light';
14+
case Dark = 'dark';
15+
16+
public function asBootstrapCssClass(): string
17+
{
18+
return 'alert-'.$this->value;
19+
}
20+
}

‎templates/components/Alert.html.twig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div {{ attributes.defaults({class: this.defaultCssClass(), role: 'alert'}) }}>
2+
<twig:block name="content"></twig:block>
3+
4+
{% if withDismissButton %}
5+
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
6+
{% endif %}
7+
</div>

‎templates/flash_messages.html.twig

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
<div id="flash-messages">
88
{% for label, messages in flash_messages %}
99
{% for message in messages %}
10-
<div class="alert alert-{{ label }} alert-dismissible fade show" role="alert">
11-
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
10+
<twig:ea:Alert variant="{{ label }}" withDismissButton>
1211
{{ message|trans|raw }}
13-
</div>
12+
</twig:ea:Alert>
1413
{% endfor %}
1514
{% endfor %}
1615
</div>

0 commit comments

Comments
 (0)
Please sign in to comment.