-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[RFC] Introduce custom BadgeStyle #7007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VincentLanglet
wants to merge
11
commits into
EasyCorp:4.x
Choose a base branch
from
VincentLanglet:badgeColor
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
39036e9
WIP
VincentLanglet 423ef1f
Rework
VincentLanglet 581a282
Rework
VincentLanglet 32a0bb2
Change constructor
VincentLanglet 0312237
Redesign class
VincentLanglet fc8e1ea
Simplify
VincentLanglet 2cda7cf
Fix
VincentLanglet fe583a5
Rework
VincentLanglet 589c1e2
Simplify
VincentLanglet 3d0c582
Open
VincentLanglet c43b4cc
Renaming ?
VincentLanglet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
| <?php | ||
|
|
||
| namespace EasyCorp\Bundle\EasyAdminBundle\Field\Style; | ||
|
|
||
| final class BadgeStyle | ||
| { | ||
| public const VALID_BADGE_TYPES = ['success', 'warning', 'danger', 'info', 'primary', 'secondary', 'light', 'dark']; | ||
|
|
||
| /** | ||
| * @param array<string> $cssClasses | ||
| * @param array<string, string> $style | ||
| */ | ||
| private function __construct(private array $cssClasses, private array $style) | ||
| { | ||
| } | ||
|
|
||
| public function addCssClass(string $cssClass): self | ||
| { | ||
| $this->cssClasses[] = $cssClass; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function addStyle(string $key, string $value): self | ||
| { | ||
| $this->style[$key] = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public static function new(): self | ||
| { | ||
| return new self(['badge'], []); | ||
| } | ||
|
|
||
| public function withBgColor(string $backgroundColor, bool $autoTextContrast = true): self | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if
|
||
| { | ||
| if ($autoTextContrast) { | ||
| $this->addCssClass(self::generateTextClassFromBackgroundColor($backgroundColor)); | ||
| } | ||
|
|
||
| return $this->addStyle('background-color', $backgroundColor); | ||
| } | ||
|
|
||
| public function withTextColor(string $textColor): self | ||
| { | ||
| return $this->addStyle('color', $textColor); | ||
| } | ||
|
|
||
| /** | ||
| * @param value-of<self::VALID_BADGE_TYPES> $type | ||
| */ | ||
| public function withType(string $type): self | ||
| { | ||
| if (!\in_array($type, self::VALID_BADGE_TYPES, true)) { | ||
| throw new \InvalidArgumentException(sprintf('Invalid badge type "%s". Allowed types are: "%s".', $type, implode(', ', self::VALID_BADGE_TYPES))); | ||
| } | ||
|
|
||
| return $this->addCssClass('badge-'.$type); | ||
| } | ||
|
|
||
| public function asPill(): self | ||
| { | ||
| return $this->addCssClass('badge-pill'); | ||
| } | ||
|
|
||
| public function getCssClasses(): ?string | ||
| { | ||
| if ([] === $this->cssClasses) { | ||
| return null; | ||
| } | ||
|
|
||
| return implode(' ', $this->cssClasses); | ||
| } | ||
|
|
||
| public function getStyle(): ?string | ||
| { | ||
| if ([] === $this->style) { | ||
| return null; | ||
| } | ||
|
|
||
| $style = []; | ||
| foreach ($this->style as $key => $value) { | ||
| $style[] = sprintf('%s:%s;', $key, $value); | ||
| } | ||
|
|
||
| return implode(' ', $style); | ||
| } | ||
|
|
||
| private static function generateTextClassFromBackgroundColor(string $backgroundColor): string | ||
| { | ||
| if (1 !== preg_match('/^#[0-9a-f]{6}$/iD', $backgroundColor)) { | ||
| throw new \InvalidArgumentException(sprintf('Only full 6-digit hexadecimal color are supported to generate the appropriate text color ("%s" given).', $backgroundColor)); | ||
| } | ||
|
|
||
| [$r, $g, $b] = [ | ||
| hexdec(substr($backgroundColor, 1, 2)), | ||
| hexdec(substr($backgroundColor, 3, 2)), | ||
| hexdec(substr($backgroundColor, 5, 2)), | ||
| ]; | ||
|
|
||
| $luminance = (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255; | ||
|
|
||
| return $luminance > 0.5 ? 'text-dark' : 'text-light'; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure in which namespace I should declare this class