Skip to content

[LiveComponent] Add informations for AssetMapper and Stimulus #1276

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

Closed
wants to merge 2 commits into from
Closed

[LiveComponent] Add informations for AssetMapper and Stimulus #1276

wants to merge 2 commits into from

Conversation

TBoileau
Copy link
Contributor

I tried to reproduce this part of the documentation, without success.

There's no bug, just some missing information in the documentation.

How to reproduce this issue :

  • Create a webapp project

  • Install AssetMapper : composer require symfony/asset-mapper symfony/asset symfony/twig-pack

  • Install LiveComponent : composer require symfony/ux-live-component

  • Install Stimulus : composer require symfony/ux-live-component

  • Create a Live component :

<?php

declare(strict_types=1);

namespace App\Component;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent('foo')]
class FooComponent
{
    use DefaultActionTrait;


    #[LiveProp]
    public int $count = 0;

    #[LiveAction]
    public function increase(): void
    {
        $this->count++;
    }
}
  • Create the Twig file :
<div {{ attributes.defaults(stimulus_controller('foo')) }}>
    {{ count }}
</div>
  • And create the stimulus controller foo_controller.js :
import { Controller } from '@hotwired/stimulus';
import { getComponent } from '@symfony/ux-live-component';

export default class extends Controller {
    async initialize() {
        this.component = await getComponent(this.element);

        this.component.action('increase');
    }
}
  • First error in the browser console :
Uncaught TypeError: Failed to resolve module specifier "@symfony/ux-live-component". Relative references must start with either "/", "./", or "../".
  • To fix this error, I add a new entry in importmap.php :
<?php

return [
    //...
    '@symfony/ux-live-component' => [
        'path' => '@symfony/ux-live-component/live_controller.js',
    ],
];
  • New error in the browser console :
Uncaught (in promise) Error: Component not found for element <div data-controller="foo live" [...]
  • To fix this, I add a new entry in assets/controllers.json :
{
  "controllers": {
    "@symfony/ux-live-component": {
      "live": {
        "enabled": true,
        "fetch": "eager"
      }
    }
  },
  "entrypoints": []
}
  • And now, we can see the property count increase to 1 and re-render the component automatically.

So I've added a bit of documentation, but please excuse my poor English.

@@ -791,6 +791,22 @@ Want to change the value of a model or even trigger an action from your
own custom JavaScript? No problem, thanks to a JavaScript ``Component``
object, which is attached to each root component element.

Before going any further, check that the controller entry ``@symfony/ux-live-component`` has been added to ``assets/controllers.json``, like this::
Copy link
Contributor

@WebMamba WebMamba Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I know what goes wrong here. You installed the LiveComponent bundle before the StimulusBundle. This is explained in the doc here https://symfony.com/bundles/ux-live-component/current/index.html#installation :

Before you start, make sure you have [StimulusBundle configured in your app]

But like you, I made the mistake multiple times.
So I don't think we should talk about the assets/controller.json here, but maybe we can add a more detailed section on how to install the Stimulus Bundle directly into this page. Or maybe write something more visual that the StimulusBundle should be installed first. And I wonder if when installing the LiveComponentBundle we can execute the StimulusBundle recipe?

Copy link
Contributor Author

@TBoileau TBoileau Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be a good idea to give a little more highlight to the message you quote in the documentation.

With AssetMapper
~~~~~~~~~~~~~~~~

If you're using AssetMapper, you need to add one new entry to you ``importmap.php``::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here this is part of the StimulusBundle recipe

weaverryan added a commit that referenced this pull request Dec 12, 2023
…le (ker0x)

This PR was merged into the 2.x branch.

Discussion
----------

[Docs] Add caution alert when bundle require StimulusBundle

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| Issues        | To complement  #1276
| License       | MIT

This PR add a `caution` alert at the beginning of `Installation` section for all bundle requiring StimulusBundle

Commits
-------

743d44a [Docs] Add caution alert when bundle require StimulusBundle
@weaverryan
Copy link
Member

I've just merged a PR to add docs about installing StimulusBundle before LiveComponents (or any other package). Hopefully that will help :).

Cheers!

@weaverryan weaverryan closed this Dec 12, 2023
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

Successfully merging this pull request may close these issues.

3 participants