victor23k

victor23k

Embed LiveView inside ShadowDOM

Hi all! I come with a proposal to create LiveViews inside the shadow DOM. At Doofinder, we have experience building embeddable LiveView apps, and we think it is a valuable addition to the ecosystem, as it allows for fast interaction widgets.

Why

The main problem with multiple LiveViews from different origins coexisting in the same DOM is duplication of events and elements. If we leverage the shadow DOM, we could bypass these limitations while also allowing for easier communication between the host page and the LiveView widgets.

Why use shadowDOM and not <iframe>

While there has already been discussion about doing this with <iframe>, We think the Shadow DOM is more adecuate for the following reasons:

  • SEO. If the LiveView widget is loaded with the page, the content would count to the host page, not the origin of the LiveView as it would happen with <iframe>.
  • Performance. No need to load a full DOM.
  • Session cookie: Safari conflicts with <iframe> and cookies. ref
  • Better comunication with host page and with other apps. No need for postMessage().

It is worth noting that I haven’t mentioned anything about WebComponents, because the goal of this is not adding support for them. The approach we present would only mean encapsulating the DOM where the LiveView lives. Here there was a related discussion, and I think some of the problems exposed there could be solved.

What would change

This feature would be a new way to use LiveView and it would not affect regular LiveViews

Doing a quick prototype I identified some areas of work:

  1. Add a way to configure a LiveView to create a shadow DOM.
  2. Places where document is used to find a DOM element should support using the shadowRoot.
  3. Event listeners need to be added to the shadowRoot instead of window.

We volunteer to work on this and implement it.


So what are your thoughts on this? Would you benefit from this feature? Are the core maintainers open to us working on this?

First Post!

rhcarvalho

rhcarvalho

Interesting idea! It does mean the “host page” must be a Phoenix LiveView app, right? Unlike an iframe, I’m understanding you’re not interested in embedding LiveViews into third-party pages but pages under your control, is that right?

:light_bulb:Reminds me of the idea of “islands” in the JS ecosystem.

Most Liked

rhcarvalho

rhcarvalho

Searching for something unrelated on the forum I accidentally found Running LiveView inside a web component with shadow DOM and thought it could be a reference point for this conversation.

victor23k

victor23k

Thanks! I’ll work on that. We had planned to open source a library with the functionality described that we use internally. The goal was to see if more people are interested in it. It can serve as a preview until the PR hopefully gets merged to LiveView.

Werner

Werner

Hi, have you seen this LiveViewPortal from Doofinder?

Last Post!

victor23k

victor23k

I forgot to add the link here, but @Werner got me covered :wink:. Check it out and ask any questions in the library thread!

Where Next?

Popular in Proposals: Ideas Top

alaadahmed
Hi folks, I tried Phoenix 1.7 and it is awesome, but I have small suggestion, which in my opinion will organize files in a better way. ...
New
mxgrn
As one edits a Phoenix LiveView form in a modal, it’s very easy to accidentally press ‘Esc’ and lose all the edits. Similar to data-conf...
New
bartblast
This could resolve to {[a: 1, b: 2]}. Was it ever considered to allow such syntax? Notice this: {:abc, a: 1, b: 2} and this: my_fun(:abc,...
New
pelopo
Hi, Oficial docs got this wonderful feature to download the ePub version that we can chuck to our kindles and read it. Now we are in the...
New
GregPhx
Greetings Everyone!!! A little bit of my background so it could be easier to understand where my comments are coming from, and to take t...
New
sezaru
When writing my code, I always find __MODULE__ very useful to use as alias of that module “inner dependencies”, ex: alias __MODULE__.{Im...
New
moacirbishopcamata
Good morning to the Elixir team. Please excuse me if I’m commenting in the wrong place; please indicate where I should discuss this. I ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement