Skip to content
Closed
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
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ jobs:
- name: Run PHPUnit (Shadcn Admin Ui)
run: (cd src/ShadcnAdminUi/ && vendor/bin/phpunit)

- name: Run JavaScript tests (Shadcn Admin UI)
if: matrix.php == '8.3' && matrix.symfony == '^7.4'
working-directory: src/ShadcnAdminUi
run: |
yarn install --frozen-lockfile --non-interactive
yarn test

- name: "Restrict packages' versions (Twig Hooks)"
run: |
(cd src/TwigHooks/ && composer config --no-plugins allow-plugins.symfony/runtime true)
Expand Down
51 changes: 14 additions & 37 deletions app/Grid/TalkGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
use Sylius\Bundle\GridBundle\Builder\Action\DeleteAction;
use Sylius\Bundle\GridBundle\Builder\Action\ShowAction;
use Sylius\Bundle\GridBundle\Builder\Action\UpdateAction;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
use Sylius\Bundle\GridBundle\Builder\Field\DateTimeField;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Field\TwigField;
Expand All @@ -42,69 +39,49 @@ public function __invoke(GridBuilderInterface $gridBuilder): void
$gridBuilder
->addOrderBy('startsAt')
->setLimits([10, 25, 50])
->addFilter(
->withFilters(
EntityFilter::create('conference', Conference::class)
->setLabel('app.ui.conference')
->addFormOption('choice_label', 'name'),
)
->addFilter(
Filter::create(name: 'speaker', type: SpeakerFilter::class)
->setLabel('app.ui.speaker')
->setOptions(['fields' => ['speakers.id']]),
)
->addFilter(
DateFilter::create('startsAt')
->setLabel('app.ui.starts_at'),
)
->addFilter(
SelectFilter::create('track', [
'app.ui.biz' => Track::BIZ->value,
'app.ui.tech_one' => Track::TECH_ONE->value,
'app.ui.tech_two' => Track::TECH_TWO->value,
])
->setLabel('app.ui.track'),
)
->addField(
->withFields(
TwigField::create('avatar', 'talk/grid/field/images.html.twig')
->setPath('speakers')
->setLabel('app.ui.avatar'),
)
->addField(
TwigField::create('speakers', 'talk/grid/field/speakers.html.twig')
->setLabel('app.ui.speakers'),
)
->addField(
StringField::create('title')
->setLabel('app.ui.title')
->setSortable(true),
)
->addField(
DateTimeField::create('startsAt', 'Y-m-d H:i')
->setLabel('app.ui.starts_at')
->setSortable(true),
)
->addActionGroup(
MainActionGroup::create(
CreateAction::create(),
),
->withMainActions(
CreateAction::create(),
)
->withItemActions(
ShowAction::create()
->setOptions(['link' => ['url' => 'resource.videoUrl']])
->setIcon('tabler:video'),
)
->addActionGroup(
ItemActionGroup::create(
ShowAction::create()
->setOptions([
'link' => [
'url' => 'resource.videoUrl',
],
])
->setIcon('tabler:video'),
UpdateAction::create(),
DeleteAction::create(),
),
->withSubItemActions(
UpdateAction::create(),
DeleteAction::create(),
)
->addActionGroup(
BulkActionGroup::create(
DeleteAction::create(),
),
->withBulkActions(
DeleteAction::create(),
)
;
}
Expand Down
1 change: 1 addition & 0 deletions src/ShadcnAdminUi/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
composer.lock
/node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Controller } from '@hotwired/stimulus';

/**
* Row "…" menu of the grid: the item actions are rendered by the grid action templates (icon
* buttons and links carrying a title), not by DropdownMenu:Item. Turn each of them into a menu
* item so the shadcn--dropdown-menu controller moves the focus with the arrow keys.
* Elements inside a <dialog> (the delete confirmation) are not menu items.
*/
export default class extends Controller {
connect() {
for (const action of this.element.querySelectorAll('[title]')) {
if (action.closest('dialog')) {
continue;
}

action.setAttribute('role', 'menuitem');
action.setAttribute('tabindex', '-1');
action.setAttribute('data-shadcn--dropdown-menu-target', 'item');
}
}
}
2 changes: 1 addition & 1 deletion src/ShadcnAdminUi/config/app/twig_hooks/layout/sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
'template' => '@SyliusShadcnAdminUi/shared/crud/common/sidebar/logo/image.html.twig',
'configuration' => [
'name' => 'Sylius',
'subtitle' => 'Admin panel',
'subtitle' => 'sylius.ui.admin_panel',
],
],
],
Expand Down
14 changes: 14 additions & 0 deletions src/ShadcnAdminUi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"devDependencies": {
"@hotwired/stimulus": "^3.2.2"
},
"engines": {
"node": "^24"
},
"license": "UNLICENSED",
"private": true,
"type": "module",
"scripts": {
"test": "node --test tests/JavaScript/*.test.mjs"
}
}
9 changes: 7 additions & 2 deletions src/ShadcnAdminUi/templates/shared/helper/button.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# templates only need the namespace to be changed.
#
# Extra params: variant ('default'|'secondary'|'outline'|'ghost'|'destructive'|'link')
# dropdown: true adds a chevron; an attribute map (the DropdownMenu trigger attributes) is also
# rendered, before attr, so these attributes win over a duplicate in attr.
#}
{% macro default(params = {}) %}
{% from '@SyliusShadcnAdminUi/shared/helper/ui.html.twig' import button_class, html_attributes %}
Expand Down Expand Up @@ -31,6 +33,7 @@
{% set icon_attributes = {class: params.icon_class|default(''), 'aria-hidden': 'true'}|merge(params.icon_only ? {} : {'data-icon': 'inline-start'}) %}
{% set icon = params.icon ? ux_icon(params.icon, icon_attributes) %}
{% set label = params.icon_only ? '<span class="sr-only">' ~ params.text|e ~ '</span>' : params.text|e %}
{% set chevron = params.dropdown and not params.icon_only ? ux_icon('lucide:chevron-down', {'data-icon': 'inline-end', class: 'opacity-60', 'aria-hidden': 'true'}) %}

{% if params.url %}
<a href="{{ params.url }}"
Expand All @@ -39,8 +42,9 @@
class="{{ class }}"
{%- if params.disabled %} aria-disabled="true" tabindex="-1"{% endif %}
{%- if params.icon_only %} title="{{ params.text }}"{% endif %}
{%- if params.dropdown is iterable %} {{ html_attr(params.dropdown) }}{% endif %}
{{- html_attributes(params.attr) }}>
{{- icon|raw }}{{ label|raw -}}
{{- icon|raw }}{{ label|raw }}{{ chevron|raw -}}
</a>
{% else %}
<button type="{{ params.type }}"
Expand All @@ -50,8 +54,9 @@
class="{{ class }}"
{%- if params.disabled %} disabled{% endif %}
{%- if params.icon_only %} title="{{ params.text }}"{% endif %}
{%- if params.dropdown is iterable %} {{ html_attr(params.dropdown) }}{% endif %}
{{- html_attributes(params.attr) }}>
{{- icon|raw }}{{ label|raw -}}
{{- icon|raw }}{{ label|raw }}{{ chevron|raw -}}
</button>
{% endif %}
{% endmacro %}
Expand Down
9 changes: 3 additions & 6 deletions src/ShadcnAdminUi/templates/shared/helper/dropdown.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
{% if params.customTrigger %}
<div role="button" tabindex="0" {{ html_attr(dropdown_menu_trigger_attrs, {'data-action': 'keydown.enter->shadcn--dropdown-menu#toggle keydown.space->shadcn--dropdown-menu#onTriggerKeydown:prevent keyup.space->shadcn--dropdown-menu#toggle'|html_attr_type('sst')}) }}>{{ params.customTrigger }}</div>
{% else %}
{% set button = {text: 'Menu', icon: null, class: null}|merge(params.button) %}
<twig:shadcn:Button variant="outline" class="{{ button.class }}" {{ ...dropdown_menu_trigger_attrs }}>
{% if button.icon %}{{ ux_icon(button.icon, {'data-icon': 'inline-start'}) }}{% endif %}
{{ button.text }}
{{ ux_icon('lucide:chevron-down', {'data-icon': 'inline-end', class: 'opacity-60'}) }}
</twig:shadcn:Button>
{# Every button.default() option works here (disabled, icon_only, size, variant...), as in BootstrapAdminUi. #}
{% import '@SyliusShadcnAdminUi/shared/helper/button.html.twig' as button %}
{{ button.default({text: 'Menu'}|merge(params.button)|merge({dropdown: dropdown_menu_trigger_attrs})) }}
{% endif %}
</twig:shadcn:DropdownMenu:Trigger>
<twig:shadcn:DropdownMenu:Content class="{{ params.class }}">
Expand Down
6 changes: 4 additions & 2 deletions src/ShadcnAdminUi/templates/shared/helper/table.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
{{ sylius_grid_render_action(grid, action, row) }}
{% endfor %}
{% endset %}
{# admin--grid-row-menu turns the actions into menu items, for the arrow keys #}
{% set subactions_menu %}<div class="flex flex-col *:w-full" {{ stimulus_controller('admin--grid-row-menu') }}>{{ subactions }}</div>{% endset %}
{% if subactions|trim|length > 0 %}
{% set more_button %}
<span class="{{ button_class('ghost', 'icon-sm') }}" title="{{ 'sylius_shadcn_admin_ui.ui.more'|trans }}">
Expand All @@ -94,8 +96,8 @@
</span>
{% endset %}
{# Action templates render icon-only buttons (label in .sr-only): in the menu, stretch each trigger (the element carrying title) into a labelled item. #}
{% set menu_class = 'flex flex-col *:w-full [&_[title]]:h-auto [&_[title]]:w-full [&_[title]]:justify-start [&_[title]]:gap-2 [&_[title]]:px-1.5 [&_[title]]:py-1 [&_[title]]:font-normal [&_[title].text-muted-foreground]:text-popover-foreground [&_[title]_.sr-only]:not-sr-only [&_[title]_.sr-only]:whitespace-nowrap' %}
{{ dropdown.default({id: 'row-' ~ row.id|default(random()), customTrigger: more_button, content: subactions, direction: 'down-end', class: menu_class}) }}
{% set menu_class = '[&_[title]]:h-auto [&_[title]]:w-full [&_[title]]:justify-start [&_[title]]:gap-2 [&_[title]]:px-1.5 [&_[title]]:py-1 [&_[title]]:font-normal [&_[title].text-muted-foreground]:text-popover-foreground [&_[title]_.sr-only]:not-sr-only [&_[title]_.sr-only]:whitespace-nowrap' %}
{{ dropdown.default({id: 'row-' ~ row.id|default(random()), customTrigger: more_button, content: subactions_menu, direction: 'down-end', class: menu_class}) }}
{% else %}
{# Keep the menu slot so the other action icons stay aligned from row to row #}
<span class="inline-block size-7 shrink-0" aria-hidden="true"></span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\SubItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\Field\DateTimeField;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Field\TwigField;
Expand Down Expand Up @@ -74,7 +75,8 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
)
->addField(DateTimeField::create('createdAt', 'd/m/Y')->setLabel('app.ui.created_at')->setSortable(true))
->addActionGroup(MainActionGroup::create(CreateAction::create()))
->addActionGroup(ItemActionGroup::create(ShowAction::create(), UpdateAction::create(), DeleteAction::create()))
->addActionGroup(ItemActionGroup::create(ShowAction::create()))
->addActionGroup(SubItemActionGroup::create(UpdateAction::create(), DeleteAction::create()))
->addActionGroup(BulkActionGroup::create(DeleteAction::create()))
;
}
Expand Down
1 change: 1 addition & 0 deletions src/ShadcnAdminUi/tests/Functional/DemoApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function testTheBookGridRendersFiltersAndBadges(): void
self::assertStringContainsString('data-controller="admin--grid-selection"', $html);
self::assertStringContainsString('data-slot="badge"', $html);
self::assertStringContainsString('Science-fiction', $html);
self::assertSelectorExists('[data-slot="dropdown-menu-content"] [data-controller="admin--grid-row-menu"] [title]');
}

public function testTheBookShowPageRendersTheDetailsHook(): void
Expand Down
33 changes: 33 additions & 0 deletions src/ShadcnAdminUi/tests/Functional/DropdownMacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ public function testTheButtonTriggerOpensTheMenuAndKeepsItsClass(): void
self::assertNotContains('h-8', $classes);
}

public function testTheButtonTriggerTakesTheButtonMacroOptions(): void
{
$crawler = $this->crawl(<<<'TWIG'
{% import '@SyliusShadcnAdminUi/shared/helper/dropdown.html.twig' as dropdown %}
{{ dropdown.default({id: 'opts', button: {text: 'Actions', icon_only: true, icon: 'lucide:ellipsis', size: 'small', variant: 'ghost', disabled: true, attr: {'data-test': 'opts', 'data-action': 'click->other#run'}}, content: ''}) }}
TWIG);

$trigger = $crawler->filter('button#dropdown-menu-opts-trigger');
self::assertCount(1, $trigger);
self::assertNotNull($trigger->attr('disabled'));
self::assertSame('Actions', $trigger->attr('title'));
self::assertSame('Actions', trim($trigger->filter('.sr-only')->text()));
self::assertSame('opts', $trigger->attr('data-test'));
self::assertSame('menu', $trigger->attr('aria-haspopup'));
// The trigger action is rendered first, so the browser keeps it over the duplicate from attr.
self::assertStringContainsString('shadcn--dropdown-menu#toggle', (string) $trigger->attr('data-action'));
$classes = explode(' ', (string) $trigger->attr('class'));
self::assertContains('size-7', $classes);
self::assertNotContains('bg-background', $classes);
// icon_only: no chevron, the button stays square.
self::assertCount(1, $trigger->filter('svg'));
}

public function testTheButtonTriggerShowsAChevron(): void
{
$crawler = $this->crawl(<<<'TWIG'
{% import '@SyliusShadcnAdminUi/shared/helper/dropdown.html.twig' as dropdown %}
{{ dropdown.default({id: 'chevron', button: {text: 'Menu'}, content: ''}) }}
TWIG);

self::assertCount(1, $crawler->filter('#dropdown-menu-chevron-trigger svg[data-icon="inline-end"]'));
}

public function testAListItemWithoutUrlIsAButton(): void
{
$crawler = $this->crawl(<<<'TWIG'
Expand Down
Loading
Loading