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

Turbo frames events #88

Closed
adrianthedev opened this issue Jan 8, 2021 · 5 comments
Closed

Turbo frames events #88

adrianthedev opened this issue Jan 8, 2021 · 5 comments

Comments

@adrianthedev
Copy link

Would be really helpful to add turbo frames events just like turbo drive has with the frame_id as a payload for that event.

document.addEventListener('turbo-frame:load', (event) => console.log('loaded frame ->', event.turboFrameId))

Would this be something helpful to explore?

@seanpdoyle
Copy link
Contributor

Would #59 address this? It doesn't introduce turbo-frame:: prefixed events or include the turboFrameId specifically, but it does include the frame as the target, so you could read it from there.

@Sub-Xaero
Copy link

Sub-Xaero commented Jan 13, 2021

I'd also go so far as to say that it would also be useful to have events that notify when Turbo streams modify a frame, even just something like a turbo-stream:received event so that a Stimulus controller can react to it (I.e. like incrementing the counter of replies on a comment).

I'm currently using MutationObservers to achieve this, but I wonder whether it would be more lightweight/performant to have the event fired directly by Turbo.

@seanpdoyle
Copy link
Contributor

@Sub-Xaero there is a turbo:before-stream-render that fires before rendering a <turbo-stream> element. According to past discussions, it is unlikely that there will be any other Stream events added in the future.

I wonder whether it would be more lightweight/performant to have the event fired directly by Turbo.

While the MutationObserver interface can be awkward to use at times, I'm not aware of any performance risks involved in using them. As I understand it, Stimulus is powered by tens (or hundreds?) of concurrently attached MutationObservers.

@Sub-Xaero
Copy link

@seanpdoyle Indeed, thank you, I had missed that. My only issue with that is that it fires at the document level, and the srcElement, target and path attributes all point to an element of type <turbo-stream> that I can only assume is either in the closed-mode ShadowDom of the <turbo-cable-stream-source> element or a dead reference to the <turbo-stream> that the <turbo-cable-stream-source> replaces as according to Chrome, Node cannot be found in the current page., so you cannot tell where the event originated.

I had imagined an event being fired on the frame itself and bubbling. But, following the precedent set in https://discuss.hotwire.dev/t/event-to-know-a-turbo-stream-has-been-rendered/1554/6 , it is probably a better convention to use MutationObservers as then it doesn't matter where the elements in the DOM come from.

Thanks for your help, I'll carry on my merry way.

@dhh
Copy link
Member

dhh commented Sep 24, 2021

Closed via #59

@dhh dhh closed this as completed Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants