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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

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
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews