-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare a PoC of Twig Components for the Sylius UI
- Loading branch information
1 parent
2a35ebc
commit dfa35f5
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
57 changes: 57 additions & 0 deletions
57
src/Sylius/Bundle/UiBundle/Resources/views/component/button.html.twig
This file contains 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,57 @@ | ||
{% from '@SyliusAdmin/shared/helper/icon.html.twig' import icon as i %} | ||
|
||
{% props | ||
text = '', | ||
style = null, | ||
class = '', | ||
dropdown = false, | ||
disabled = false, | ||
size = null, | ||
icon = null, | ||
iconOnly = false, | ||
href = null, | ||
hookableMetadata = null, | ||
testAttribute = null, | ||
%} | ||
|
||
{% set button_class = 'btn' ~ | ||
(class ? ' ' ~ class : '') ~ | ||
(style ? ' btn-' ~ style : '') ~ | ||
(dropdown ? ' dropdown-toggle' : '') ~ | ||
(iconOnly ? ' btn-icon' : '') ~ | ||
(size == 'large' ? ' btn-lg' : '') ~ | ||
(size == 'small' ? ' btn-sm' : '') %} | ||
|
||
{% set content %} | ||
{% if text is not empty %} | ||
{{ text }} | ||
{% else %} | ||
{{ block('content') }} | ||
{% endif %} | ||
{% endset %} | ||
|
||
{% if href is not null %} | ||
<a {{ attributes }} | ||
class='{{ button_class|trim }} {{ class|trim }}' | ||
href='{{ href }}' | ||
{% if disabled %}aria-disabled='true'{% endif %} | ||
{% if dropdown %}data-bs-toggle='dropdown'{% endif %} | ||
{% if testAttribute %}{{ sylius_test_html_attribute(testAttribute) }}{% endif %} | ||
> | ||
{{ icon ? i({ icon }) }} | ||
{% if iconOnly == false %} | ||
{{ content }} | ||
{% endif %} | ||
</a> | ||
{% else %} | ||
<button {{ attributes }} | ||
class='{{ button_class|trim }} {{ class|trim }}' | ||
{% if disabled %}disabled{% endif %} | ||
{% if dropdown %}data-bs-toggle='dropdown'{% endif %} | ||
> | ||
{{ icon ? i({ icon }) }} | ||
{% if iconOnly == false %} | ||
{{ content }} | ||
{% endif %} | ||
</button> | ||
{% endif %} |
16 changes: 16 additions & 0 deletions
16
src/Sylius/Bundle/UiBundle/Resources/views/component/button/delete.html.twig
This file contains 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,16 @@ | ||
{% extends '@SyliusUi/component/button/submit.html.twig' %} | ||
|
||
{% props | ||
text = '', | ||
style = 'danger', | ||
class = '', | ||
dropdown = false, | ||
disabled = false, | ||
size = null, | ||
icon = null, | ||
iconOnly = false, | ||
href = null, | ||
hookableMetadata = null, | ||
%} | ||
|
||
{% set attributes = attributes.defaults({ type: 'submit' }) %} |
16 changes: 16 additions & 0 deletions
16
src/Sylius/Bundle/UiBundle/Resources/views/component/button/submit.html.twig
This file contains 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,16 @@ | ||
{% extends '@SyliusUi/component/button.html.twig' %} | ||
|
||
{% props | ||
text = '', | ||
style = 'primary', | ||
class = '', | ||
dropdown = false, | ||
disabled = false, | ||
size = null, | ||
icon = null, | ||
iconOnly = false, | ||
href = null, | ||
hookableMetadata = null, | ||
%} | ||
|
||
{% set attributes = attributes.defaults({ type: 'submit' }) %} |
50 changes: 50 additions & 0 deletions
50
src/Sylius/Bundle/UiBundle/Twig/Ux/ComponentTemplateFinder.php
This file contains 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,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\UiBundle\Twig\Ux; | ||
|
||
use Symfony\UX\TwigComponent\ComponentTemplateFinderInterface; | ||
use Twig\Environment; | ||
use Twig\Loader\LoaderInterface; | ||
|
||
final readonly class ComponentTemplateFinder implements ComponentTemplateFinderInterface | ||
{ | ||
public function __construct( | ||
private ComponentTemplateFinderInterface $decorated, | ||
private Environment|LoaderInterface $loader, | ||
) { | ||
} | ||
|
||
public function findAnonymousComponentTemplate(string $name): ?string | ||
{ | ||
if (!str_starts_with($name, 'sylius_ui')) { | ||
return $this->decorated->findAnonymousComponentTemplate($name); | ||
} | ||
|
||
$template = $this->guessTemplatePath($name); | ||
|
||
if ($this->loader->exists($template)) { | ||
return $template; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function guessTemplatePath(string $name): string | ||
{ | ||
$normalizedName = str_replace('sylius_ui:', '', $name); | ||
$normalizedName = str_replace(':', '/', $normalizedName); | ||
|
||
return sprintf('@SyliusUi/component/%s.html.twig', $normalizedName); | ||
} | ||
} |