Skip to content
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

[BootstrapAdminUi] Add custom stimulus controller #200

Open
brambaud opened this issue Dec 31, 2024 · 3 comments
Open

[BootstrapAdminUi] Add custom stimulus controller #200

brambaud opened this issue Dec 31, 2024 · 3 comments

Comments

@brambaud
Copy link

Description

When using the BootstrapAdminUi bundle out of the box, it is a nice DX to not have to install and configure Symfony Stimulus Bundle and care that it exists at all.

At some point, when needed to add custom Stimulus controllers, we tried with what it seems a "standard Symfony way" by adding Symfony Stimulus Bundle.
For completeness, we chose AssetMapper to handle assets.

Consequences are there are two started Stimulus apps: one by BootstrapAdminUi and the other is the custom one with Stimulus Bundle.
So all requests made by live components are made twice.

Additional context

By overriding the template of the sylius_admin.base#javascripts hook, we may avoid the load of the asset bundles/syliusbootstrapadminui/app.js.
It avoids the start of the Stimulus app by BootstrapAdminUi but other features like the ones provided by https://github.com/Sylius/Stack/tree/8fcb13f517662e7aa0cb24453dcd2e721a4af0eb/src/BootstrapAdminUi/assets/scripts are broken.

How to reproduce

Possible solutions

Before proposing a patch, I'd like to have feedback on how to address this.
Maybe I missed something somewhere 🤔

  • BootstrapAdminUi may stop starting a Stimulus app.
    It will imply that users will have to do it themselves like with Stimulus Bundle.
    It seems fairly simple for a user to do it. In another hand it increases the number of dependencies a user have to handle.

  • BootstrapAdminUi may propose a way to allow users to chose if it should start a Stimulus app or not.
    It may increase the complexity of maintenance of BootstrapAdminUi.
    It will keep the nice DX allowing people to not care about the Stimulus part if they don't need to.

  • BootstrapAdminUi may propose to reuse it's started Stimulus app in order to register custom controllers with it.
    Users won't be able to use the default behavior of the Stimulus Bundle which register the controllers automagically.
    Others "admin UI theme" may chose to do the same to ease life of users.

@Prometee
Copy link
Contributor

Prometee commented Dec 31, 2024

@brambaud we were discussing this very topic last week with @loic425, I tried exactly what you did and have my own components running.

@loic425 and I want to support Symfony asset mapper and Webpack to be able to switch from one to another and allow sylius/sylius to also get a migration path.

To complete your work around, here what I'm currently doing:

Disable the #javascripts Sylius Twig hook at the bottom, replace the #stylesheets in the <head> by importmap block and function:

# config/packages/sylius_twig_hooks.yaml
sylius_twig_hooks:
    hooks:
        'sylius_admin.base#stylesheets':
            styles:
                template: 'shared/layout/admin/importmap.html.twig'
        'sylius_admin.base#javascripts':
            javascripts:
                enabled: false
# templates/shared/layout/admin/importmap.html.twig
{% block importmap %}{{ importmap('app') }}{% endblock %}

Then to get bootstrap working :

bin/console importmap:require bootstrap
bin/console importmap:require tom-select/dist/css/tom-select.bootstrap5.css

Tell Symfony UX autocomplete to use bootstrap5 theme:

# assets/controllers.json
{
    "controllers": {
        "@symfony/ux-autocomplete": {
            "autocomplete": {
                "enabled": true,
                "fetch": "eager",
                "autoimport": {
                    "tom-select/dist/css/tom-select.default.css": false,
                    "tom-select/dist/css/tom-select.bootstrap4.css": false,
                    "tom-select/dist/css/tom-select.bootstrap5.css": true
                }
            }
        },
        
        [...]
    },
    "entrypoints": []
}

Update assets/app.js with the css and js from Sylius Bootstrap Admin UI.
First add a new asset mapper path to use scripts from the bundle:

# config/packages/asset_mapper.yaml
framework:
    asset_mapper:
        paths:
            'assets/': ''
            'vendor/sylius/bootstrap-admin-ui/assets/scripts/': 'bundles/syliusbootstrapadminui-scripts'
# assets/app.js

import './bootstrap.js';
import './styles/app.css';

// @todo An other way is to use symfonycasts/sass-bundle to only add required CSS
import '../vendor/sylius/bootstrap-admin-ui/public/app.css';

import '../vendor/sylius/bootstrap-admin-ui/assets/scripts/bulk-delete.js';
import '../vendor/sylius/bootstrap-admin-ui/assets/scripts/check-all.js';
import '../vendor/sylius/bootstrap-admin-ui/assets/scripts/menu-search.js';
import '../vendor/sylius/bootstrap-admin-ui/assets/scripts/bootstrap.js';

@brambaud
Copy link
Author

brambaud commented Jan 1, 2025

Thanks for the completion of the workaround @Prometee
On my side I ended with patching (removing the start of the stimulus app on BootstrapAdminUi) the bundle for the moment.

If there is something I might do to help, I'm all ears 😁

@Prometee
Copy link
Contributor

Prometee commented Jan 2, 2025

@brambaud if you can start a PR to handle Symfony Asset Mapper it could be awesome 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants