llaisdy

llaisdy

Dear All

nb a minimal working – or rather not working – example of my issue is here:

https://github.com/llaisdy/demo

The two relevant files are:

I am starting to implement a board game in elixir. Two pieces take turns to move around a square grid. I am using the HTML Drag and Drop API for moving the pieecs, and a phx-hook to identify the valid drop sites (i.e., on a given turn, which squares can a piece move to).

A moveable piece is a draggable div like this:

  <div class="piece" id={@piece_id} draggable="true" ondragstart="dragStart(event)"></div>

Game-board squares are table cells like this:

  <td id="cell-3-1" phx-hook="Drop">

(Using table for simplicity but will move to divs soon.)

The cells that have phx-hook="Drop" on initial page load are available drop sites and work as expected.

After each piece move, implicated cells have their phx-hook attribute updated (added or removed from the td). I can see the DOM is updated, but the changed cells are never mounted. The cells that have phx-hook="Drop" on initial page load remain the only available drop sites.

What am I missing or doing wrong?

Do I need to do something to force mount of updated elements?

Is there a simple LiveView board game I could consult for hints?

Many thanks

Ivan

Showing Posts 1 to 8

thiagomajesk

thiagomajesk

AFAIK, this is not possible, see this issue: phx-hook does not run in elements dynamically added to the DOM · Issue #2563 · phoenixframework/phoenix_live_view · GitHub.

I was doing something similar in the past but the idea of force-mounting hooks (to support dynamically added elements) was never implemented because of some security concerns. I’m hoping this will get revisited at some in the future though.

sodapopcan

sodapopcan

You are mixing “regular” embedded JS calls with hooks here which I can’t imagine is the intended use.

Can you have something more like this where everything is being managed by one hook?

<table id="board" phx-hook="Board">
  <tr>
    <td data-drop-target-id={target_id}>
      <div draggable>...</div>
    </td>
  </tr>
</table>

This is more or less how I’ve done drag-and-drop in the past complete with dynamically added elements (though I was using a library).

llaisdy

llaisdy OP

Hi, thanks for your comment! I link to that issue from my github README :smiley: especially Chris McCord’s & Jose Valim’s comments from here on, eg:

hooks are mounted any time their element first appears in the DOM

Every time the server sends something, if there are new hooks, they should be mounted.

The DOM nodes that liveview adds are mounted properly.

I am adding these hooks on the server – in a LiveView – I understood your issue to be adding hooks client-side.

llaisdy

llaisdy OP

:eyes: Interesting! So the hook is at board level … would it be alerted to updated in child nodes? Would something like this work?

[1]

<table id="board" phx-hook="Board">
  <tr>
    <td id="cell-3-1" >
      <div class="piece" draggable>...</div>
    </td>
    <td id="cell-3-2" data-drop-target=true>
    </td>
  </tr>
</table>

The piece is moved from (3,1) to (3,2), and the data-drop-target attribute is updated on both tds.

[2]

<table id="board" phx-hook="Board">
  <tr>
    <td id="cell-3-1" data-drop-target=true>
    </td>
    <td id="cell-3-2" >
      <div class="piece" draggable>...</div>
    </td>
  </tr>
</table>

Could the Board hook pick that up?

sodapopcan

sodapopcan

Ya, you essentially delegate to JS here, which maybe you are trying not to do? Again, I’ve only ever used a library, so it’s a pretty simple set up of setting up the library hooks in the Phoenix JS Hooks (ugh, so many hooks!) and pushing updates to the server

For example, here my entire phx hook using SortableJS for a fairly old project (but is the only one top of mind):

Hooks.Sortable = {
  mounted() {
    Sortable.create(this.el, {
      draggable: ".sticky",
      animation: 500,
      emptyInsertThreshold: 10,
      group: "sticky-lanes",
      forceFallback: true,
      invertSwap: true,
      onEnd: (e) => {
        const { id, phxTarget } = e.item.dataset
        this.pushEventTo(phxTarget, "move", {
          sticky_id: id,
          from_lane_id: e.from.dataset.id,
          to_lane_id: e.to.dataset.id,
          new_position: e.newIndex + 1,
        })
      },
    })
  },
}

The point being that the JS takes care of watching for new items. In my example, that’s all hidden away in Sortable but that’s the idea.

Otherwise, ya, creating an element with a hook server side should call mount, I think your issue was just mixing hooks with non-hooks (I think?) but it looks like you have more insight now!

codeanpeace

codeanpeace

To add on to this, the TodoTrek showcase repo has a working example of LiveView and Sortable that supports Trello/kanban-style dragging and dropping cards between lists.

llaisdy

llaisdy OP

Excellent info, thanks. Yes – trying to avoid javascript :sweat_smile: Will try it out and report back soon.

llaisdy

llaisdy OP

Thanks v much, I’ll give it a look. I have used Sortable with a django app. Will report back with results/failures soon.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews