bartblast
Creator of Hologram
It’s time to implement JavaScript interop for Hologram!
Eventually, there will be Hologram wrappers for most APIs, but sometimes we’ll need escape hatches - especially when working with legacy apps or integrating 3rd party libraries. Things like:
- Integrating charting libraries (Chart.js, D3, etc.)
- Using JavaScript-only APIs (Web APIs, browser-specific features)
- Calling into existing JavaScript codebases
- Leveraging the vast npm ecosystem
How would you envision a Hologram <> JavaScript interop API/DSL?
Some questions to spark discussion:
- Should it be declarative or imperative?
- How should data be marshaled between Elixir and JavaScript?
- What would the ideal developer experience look like?
- Are there specific JavaScript libraries you’re eager to use?
- How should errors be handled across the boundary?
I have some ideas brewing, but I don’t want to bias the discussion by sharing them upfront. I’m genuinely curious about your use cases and what would make the most sense from a developer ergonomics perspective.
Looking forward to hearing your thoughts!
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
Quite interesting article Google brought me. Didn’t find any mentions about it here.
What do you think in general? Would you use togethe...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
:warning: Security advisory: Decimal DoS vulnerability
A vulnerability has been published for decimal where very large exponents can cau...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
For libraries I would prefer to call
JScode without any changes. This way would make integrations simple and not depend on theHologramor any other package. The most what we can do here is to queryelementusingHologramand pass it toJScode (something like an assign), so anElixirterm would be translated toJSDOM element.For everything else I would like to have a
Hologramwrappers. This way we should be able to even re-writeJSlibrary inElixirwith a properDOMandWebAPIbindings. Depending on case some of those are short term (like simply read/write some client state) or long term (rewritten/new libraries that would be compiled to JS).It would be interesting to write a library in
Elixirand export it in a legacy or modernJS. One configuration thing would handle a lot of use cases in my opinion.bartblast
Thanks for the input @Eiji! Could you share some rough code snippets showing how you’d envision the API/DSL? Even pseudocode would help.
Eiji
Not sure how
Hologramtranslates things, but in very short something like:For JS we could have something like:
The point of above snippet is that we could call it in every
initoraction. This way we could for example initialise an editor in a tabbed only when switching to the right tab for example.jam
I was thinking it might be as simple as:
I’m not sure how calling Hologram
actionsandcommandsfrom the js code should work. I’m assuming that would be needed. Maybe some sort of shorthand that can be parsed when scanning theJSblock?Edit: I think we’d also need a way to execute JS outside of a template context. One example would be using the npm package
idbwhich makes indexeddb easier to work with.Maybe it could be as simple as dropping a .js file somewhere that Hologram would pick up and bundle and allow importing of its functions into Hologram. Maybe something like:
These thoughts are a bit off the cuff so take them for what you will
bartblast
Thanks Eiji. Can you also give some use case for the code?
For example, assuming you could do this:
element = Hologram.DOM.query_selector(document, "#element-id")what would you do with the
element?And in what cases you would need to use this:
Hologram.DOM.add_event_listener(element, "click", {:action, :action_name})outside of .holo templates?
Eiji
As in example code I was fetching the
data-*attributes usingdatasetAPI and adding an event listeners for it.https://caniuse.com/dataset
clickevent is rarely used also inLiveView, but it’s just a generic example that everyone would understand. There could be a custom events (especially when integrating with 3rd party JS libraries). Sometimes you want to temporarily add some event and the remove it. In some cases you want to stop event propagation based on some conditions.There are tons of examples for all those cases. One of them is a “click away” implementation in vanilla
JSthat is possible by using aevent.targetelement andcontainsAPI.Such event listener may be called once and removed to not cause any conflicts with the rest of code.
See also a guide for creating and dispatching custom events:
bartblast
I think I understand the technical approach you’re proposing, but I’m trying to dig deeper into the specific use cases to make sure we’re solving real problems.
Regarding your example with
<div id="element-id" data-user="123" data-role="admin"></div>- how would this work in practice since that div would already be managed by Hologram’s template system? Are you thinking about scenarios where you’re rendering some portions of the page outside of Hologram’s templates and then managing them through Hologram? Or are you envisioning querying elements that are already part of your.holotemplates?I’m trying to think through specific use cases and focus on the most common ones that are actually worth implementing. I don’t want to build features just for the sake of it.
The custom event listeners from 3rd party libraries - that makes total sense to me as a clear interop need. But for things like “click away” patterns or event propagation control, my thinking is that the whole goal of JS interop shouldn’t be to create a separate way of doing things in Hologram. The primary reason should be interoping with 3rd party libs and escape hatches.
For the rest, I’d rather cover it within Hologram’s way of thinking. For example - there’s no
$click_awayevent, but there definitely will be. Same with event bubbling - Hologram should probably allow you to manage that within its template system rather than requiring you to drop down to imperative DOM manipulation.Does that make sense? I’m trying to draw a line between “this needs JS interop because it’s inherently about external libraries and/or escape hatches” vs “this should be a first-class Hologram feature.”
What do you think?
olivermt
For me the usecase is very straight forward and I suggest you even use it both as a thing to prototype with and as a showcase of interop.
I want to do a
<div id=”map”/>and then initialize google maps on it.In regular liveview this would just mean setting phx-update=”ignore” on it and start mapping JS hook events.
In Hologram I have no idea what would be most idiomatic since I haven’t really dug in yet due to lack of said JS interop.
I propose three usecases:
Eiji
Well … since you are changing
Elixircode toJavaScriptpeople would expect allJSbindings or they would write their own scripts. It’s really hard to tell what API would be needed as each app requires completely different things.For example you may think that since
Hologramhandles all communication theXMLHTTPRequestis not needed any more. That’s true for requests send to same server, but what about 3rd party APIs? In many cases you want to move as much as possible to client, so the server focus on it’s core stuff.data-*attributes may not be a best example, but what aboutclasslistmethods? Do you prefer to just add/remove/toggle class or maintain a list of classes? Use cases? It’s rather completely or almost completely up to developer creativity and specific project needs.There are way to many cases to just mention them not even saying about describing or discussing each one.
However sure, you may be interested to focus on things that
Hologramtemplates does not cover (better or worse). Anyway you would have lots of work with bindings forCanvasAPI, push notifications, sounds and many, many more … Today in browser you can do literally everything including FPS games. Of course nobody expects APIs related to FPS games in that early stage, but you would have to support everything sooner or later.At the very end you can do a best practices to avoid working on attributes if the same could be done in templates. However I would not be surprised if one day someone would come with a huge template example and start to complain that all the logic has to be done in it as there are no
JSbindings available that would split templates and business logic. Again everything depends on case. There is no rule to cover all cases, so better to support as much as possible.If one day you would do so then people instead of complaining would rewrite the
JavaScriptlibraries intoHologramcomponents as with complete API, the migration would be very simple, easily work in legacy browsers and everything could be done with same code style (see the Elixir naming inHologram.DOMmentioned previously).kingdomcoder
Hi, @bartblast
Have you considered ideas from Elm?