Skip to content

[Site] Use ux-icons (1/2) #1936

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

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ux.symfony.com/assets/icons/symfony.svg
1 change: 1 addition & 0 deletions ux.symfony.com/assets/styles/components/_DocsLink.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
color: gray;
font-size: 1.5rem;
opacity: .5;
margin-inline-start: auto;
transition: opacity 600ms ease-in-out;
.DocsLink:hover & {
transition: opacity 250ms ease-in-out;
Expand Down
12 changes: 6 additions & 6 deletions ux.symfony.com/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 45 additions & 2 deletions ux.symfony.com/src/LiveMemory/Component/Icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace App\LiveMemory\Component;

use App\Twig\Icon as BaseIcon;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;

use function Symfony\Component\String\u;

/**
* @demo LiveMemory
Expand All @@ -24,11 +26,52 @@
name: 'LiveMemory:Icon',
template: 'demos/live_memory/components/LiveMemory/Icon.html.twig',
)]
class Icon extends BaseIcon
class Icon
{
/**
* Name of the icon file without extension (ex: `symfony-ux`).
*/
public string $name;

/**
* Accessibility label of the icon. Defaults to `name|title Icon` (ex: `Symfony Ux Icon`).
*/
public ?string $label = null;

protected string $iconDirectory;

public function __construct(
#[Autowire('%kernel.project_dir%')] string $projectDir,
) {
$this->iconDirectory = $projectDir.'/assets/images/demos/live-memory/icons/';
}

public function mount(string $name): void
{
if (!preg_match('/[A-Za-z0-9-]+/', $name)) {
throw new \InvalidArgumentException(\sprintf('Icon name can only contain letters, digits or dashes, "%s" provided.', $this->name));
}

$this->name = $name;
}

#[ExposeInTemplate]
public function getLabel(): string
{
return $this->label ??= u($this->name)->title(true)->append(' Icon')->toString();
}

#[ExposeInTemplate]
public function getRawSvg(): string
{
$path = $this->iconDirectory.'/'.$this->name.'.svg';
if (!file_exists($path)) {
throw new \InvalidArgumentException(\sprintf('File "%s" does not exist.', $path));
}
$svg = file_get_contents($path);

$svg = u($svg)->collapseWhitespace();

return $svg->toString();
}
}
84 changes: 0 additions & 84 deletions ux.symfony.com/src/Twig/Icon.php
Original file line number Diff line number Diff line change
@@ -1,84 +0,0 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Twig;

use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;

use function Symfony\Component\String\u;

#[AsTwigComponent]
class Icon
{
/**
* Name of the icon file without extension (ex: `symfony-ux`).
*/
public string $name;

/**
* Accessibility label of the icon. Defaults to `name|title Icon` (ex: `Symfony Ux Icon`).
*/
public ?string $label = null;

/**
* Remove XMLNS attribute on SVG tag (default).
*
* This attribute is required when the file is used as a distant asset (ex: an image src)
* but is unnecessary when used directly in HTML code
*
* @see https://www.w3.org/TR/SVG/struct.html#Namespace
*/
public bool $stripXmlns = true;

protected string $iconDirectory;

public function __construct(
#[Autowire('%kernel.project_dir%')] string $projectDir,
) {
$this->iconDirectory = $projectDir.'/assets/images/icons';
}

public function mount(string $name): void
{
if (!preg_match('/[A-Za-z0-9-]+/', $name)) {
throw new \InvalidArgumentException(\sprintf('Icon name can only contain letters, digits or dashes, "%s" provided.', $this->name));
}

$this->name = $name;
}

#[ExposeInTemplate]
public function getRawSvg(): string
{
$path = $this->iconDirectory.'/'.$this->name.'.svg';
if (!file_exists($path)) {
throw new \InvalidArgumentException(\sprintf('File "%s" does not exist.', $path));
}
$svg = file_get_contents($path);

$svg = u($svg)->collapseWhitespace();

if ($this->stripXmlns) {
$svg = $svg->replace('xmlns="http://www.w3.org/2000/svg"', '')
->replace('xmlns:xlink="http://www.w3.org/1999/xlink"', '');
}

return $svg->toString();
}

#[ExposeInTemplate]
public function getLabel(): string
{
return $this->label ??= u($this->name)->title(true)->append(' Icon')->toString();
}
}
6 changes: 2 additions & 4 deletions ux.symfony.com/templates/components/Icon.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
class: 'Icon Icon--' ~ name,
role: 'img',
'aria-hidden': 'true',
'aria-label': label,
'aria-label': label ?? name|humanize,
} %}
<i {{ attributes.defaults(default_attributes) }}>
{{- rawSvg|raw -}}
</i>
<twig:ux:icon name="{{ name }}" {{ ...attributes.defaults(default_attributes) }} />
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ rawSvg|raw }}
{{ this.getRawSvg()|raw }}