Skip to content

Commit

Permalink
Add a Twig component to display flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Dec 12, 2024
1 parent 150e6cb commit f0c9bd2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Router\UrlSigner;
use EasyCorp\Bundle\EasyAdminBundle\Security\AuthorizationChecker;
use EasyCorp\Bundle\EasyAdminBundle\Security\SecurityVoter;
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Banner;
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Flag;
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Icon;
use EasyCorp\Bundle\EasyAdminBundle\Twig\EasyAdminTwigExtension;
Expand Down Expand Up @@ -403,5 +404,8 @@

->set(Flag::class)
->tag('twig.component')

->set(Banner::class)
->tag('twig.component')
;
};
21 changes: 21 additions & 0 deletions src/Config/Enum/BannerVariant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Config\Enum;

enum BannerVariant: 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 asCssClass(): string
{
return $this->value;
}
}

16 changes: 16 additions & 0 deletions src/Twig/Component/Banner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Twig\Component;

use EasyCorp\Bundle\EasyAdminBundle\Config\Enum\BannerVariant;

class Banner
{
public BannerVariant|string $variant = BannerVariant::Info;
public bool $withDismissButton = false;

public function getVariantCssClass(): string
{
return \is_string($this->variant) ? $this->variant : $this->variant->asCssClass();
}
}
13 changes: 13 additions & 0 deletions templates/components/Banner.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% set css_class = html_classes({
'alert': true,
'alert-dismissible': withDismissButton,
}) %}
{% set css_class = css_class ~ ' alert-' ~ this.getVariantCssClass() %}

<div {{ attributes.defaults({class: css_class, 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>
5 changes: 2 additions & 3 deletions templates/flash_messages.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
<div id="flash-messages">
{% for label, messages in flash_messages %}
{% for message in messages %}
<div class="alert alert-{{ label }} alert-dismissible fade show" role="alert">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
<twig:ea:Banner :variant="label" :withDismissButton="true">
{{ message|trans|raw }}
</div>
</twig:ea:Banner>
{% endfor %}
{% endfor %}
</div>
Expand Down

0 comments on commit f0c9bd2

Please sign in to comment.