Skip to content

Commit b6e4d16

Browse files
committed
minor #1936 [Site] Use ux-icons (1/2) (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Site] Use ux-icons (1/2) First pass to switch from the old custom Icon component to ux-icons Next step (once feature available on ux-icons) * configure class & data-attributes auto-attributes * use <twig:ux:icon instead of <twig:Icon Commits ------- 314723c [Site] Use ux-icons (1/2)
2 parents bbf0a18 + 314723c commit b6e4d16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+60
-98
lines changed
Lines changed: 5 additions & 1 deletion

ux.symfony.com/assets/styles/components/_DocsLink.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
color: gray;
4444
font-size: 1.5rem;
4545
opacity: .5;
46+
margin-inline-start: auto;
4647
transition: opacity 600ms ease-in-out;
4748
.DocsLink:hover & {
4849
transition: opacity 250ms ease-in-out;

ux.symfony.com/composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ux.symfony.com/src/LiveMemory/Component/Icon.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace App\LiveMemory\Component;
1313

14-
use App\Twig\Icon as BaseIcon;
1514
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1615
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
16+
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
17+
18+
use function Symfony\Component\String\u;
1719

1820
/**
1921
* @demo LiveMemory
@@ -24,11 +26,52 @@
2426
name: 'LiveMemory:Icon',
2527
template: 'demos/live_memory/components/LiveMemory/Icon.html.twig',
2628
)]
27-
class Icon extends BaseIcon
29+
class Icon
2830
{
31+
/**
32+
* Name of the icon file without extension (ex: `symfony-ux`).
33+
*/
34+
public string $name;
35+
36+
/**
37+
* Accessibility label of the icon. Defaults to `name|title Icon` (ex: `Symfony Ux Icon`).
38+
*/
39+
public ?string $label = null;
40+
41+
protected string $iconDirectory;
42+
2943
public function __construct(
3044
#[Autowire('%kernel.project_dir%')] string $projectDir,
3145
) {
3246
$this->iconDirectory = $projectDir.'/assets/images/demos/live-memory/icons/';
3347
}
48+
49+
public function mount(string $name): void
50+
{
51+
if (!preg_match('/[A-Za-z0-9-]+/', $name)) {
52+
throw new \InvalidArgumentException(\sprintf('Icon name can only contain letters, digits or dashes, "%s" provided.', $this->name));
53+
}
54+
55+
$this->name = $name;
56+
}
57+
58+
#[ExposeInTemplate]
59+
public function getLabel(): string
60+
{
61+
return $this->label ??= u($this->name)->title(true)->append(' Icon')->toString();
62+
}
63+
64+
#[ExposeInTemplate]
65+
public function getRawSvg(): string
66+
{
67+
$path = $this->iconDirectory.'/'.$this->name.'.svg';
68+
if (!file_exists($path)) {
69+
throw new \InvalidArgumentException(\sprintf('File "%s" does not exist.', $path));
70+
}
71+
$svg = file_get_contents($path);
72+
73+
$svg = u($svg)->collapseWhitespace();
74+
75+
return $svg->toString();
76+
}
3477
}

ux.symfony.com/src/Twig/Icon.php

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +0,0 @@
1-
<?php
2-
3-
/*
4-
* This file is part of the Symfony package.
5-
*
6-
* (c) Fabien Potencier <[email protected]>
7-
*
8-
* For the full copyright and license information, please view the LICENSE
9-
* file that was distributed with this source code.
10-
*/
11-
12-
namespace App\Twig;
13-
14-
use Symfony\Component\DependencyInjection\Attribute\Autowire;
15-
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
16-
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
17-
18-
use function Symfony\Component\String\u;
19-
20-
#[AsTwigComponent]
21-
class Icon
22-
{
23-
/**
24-
* Name of the icon file without extension (ex: `symfony-ux`).
25-
*/
26-
public string $name;
27-
28-
/**
29-
* Accessibility label of the icon. Defaults to `name|title Icon` (ex: `Symfony Ux Icon`).
30-
*/
31-
public ?string $label = null;
32-
33-
/**
34-
* Remove XMLNS attribute on SVG tag (default).
35-
*
36-
* This attribute is required when the file is used as a distant asset (ex: an image src)
37-
* but is unnecessary when used directly in HTML code
38-
*
39-
* @see https://www.w3.org/TR/SVG/struct.html#Namespace
40-
*/
41-
public bool $stripXmlns = true;
42-
43-
protected string $iconDirectory;
44-
45-
public function __construct(
46-
#[Autowire('%kernel.project_dir%')] string $projectDir,
47-
) {
48-
$this->iconDirectory = $projectDir.'/assets/images/icons';
49-
}
50-
51-
public function mount(string $name): void
52-
{
53-
if (!preg_match('/[A-Za-z0-9-]+/', $name)) {
54-
throw new \InvalidArgumentException(\sprintf('Icon name can only contain letters, digits or dashes, "%s" provided.', $this->name));
55-
}
56-
57-
$this->name = $name;
58-
}
59-
60-
#[ExposeInTemplate]
61-
public function getRawSvg(): string
62-
{
63-
$path = $this->iconDirectory.'/'.$this->name.'.svg';
64-
if (!file_exists($path)) {
65-
throw new \InvalidArgumentException(\sprintf('File "%s" does not exist.', $path));
66-
}
67-
$svg = file_get_contents($path);
68-
69-
$svg = u($svg)->collapseWhitespace();
70-
71-
if ($this->stripXmlns) {
72-
$svg = $svg->replace('xmlns="http://www.w3.org/2000/svg"', '')
73-
->replace('xmlns:xlink="http://www.w3.org/1999/xlink"', '');
74-
}
75-
76-
return $svg->toString();
77-
}
78-
79-
#[ExposeInTemplate]
80-
public function getLabel(): string
81-
{
82-
return $this->label ??= u($this->name)->title(true)->append(' Icon')->toString();
83-
}
84-
}

ux.symfony.com/templates/components/Icon.html.twig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
class: 'Icon Icon--' ~ name,
33
role: 'img',
44
'aria-hidden': 'true',
5-
'aria-label': label,
5+
'aria-label': label ?? name|humanize,
66
} %}
7-
<i {{ attributes.defaults(default_attributes) }}>
8-
{{- rawSvg|raw -}}
9-
</i>
7+
<twig:ux:icon name="{{ name }}" {{ ...attributes.defaults(default_attributes) }} />
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{ rawSvg|raw }}
1+
{{ this.getRawSvg()|raw }}

0 commit comments

Comments
 (0)