pedromvieira

pedromvieira

What’s the best approach to allow an external javascript library in LiveView after initial mount?
Currently it only works on first HTTP mount. And after any socket update, it’s stop.
The idea it’s to enable side menu or dropdown features like those in Flowbite.

Currently our side menu is a Phoenix.Component with a child Phoenix.Component per section.

We tried phx-update="ignore".

DROPDOWN

For dropdown, we did via phx-hook and it’s working.

function changeDom(obj) {
  let dropdownId = obj.getAttribute("data-dropdown-toggle");
  let dropdownObj = document.getElementById(dropdownId);
  let options = {
    placement: "bottom",
    onHide: () => {
      // console.log("dropdown has been hidden");
    },
    onShow: () => {
      // console.log("dropdown has been shown");
    },
  };
  let newDropdown = new Dropdown(dropdownObj, obj, options);
}

const navDropdown = {
  mounted() {
    let obj = this.el;
    changeDom(obj);
  },

  updated() {
    let obj = this.el;
    changeDom(obj);
  },

  destroyed() {},
};

export default navDropdown;

SIDE MENU

For side menu dropdown, we did via phx-hook and it’s working.

HEEX

    <ComponentsCompany.side_menu
      locale={@locale}
      token={@token}
      socket={@socket}
      section="users"
      section_item="users-profile"
    />
    <button
      id="myId"
      ...
      phx-hook="sidemenu"
      ...
    </button>

SELECT

For multiple select, we did via phx-hook and it’s working.

HEEX

            <%= select(f, :data_gender, Locale.choose_gender(@locale),
              value: CommonUI.data(@changeset, [:data, "gender"]),
              name: "user[data][gender]",
              class: "form-live-select-input",
              "phx-hook": "select",
              autocomplete: "new-gender"
            ) %>

HOOK

const slimSelect = {
  mounted() {
    let opts = finalOpts(this);
    new SlimSelect(opts);
  },

  updated() {
    let opts = finalOpts(this);
    new SlimSelect(opts);
  },

  destroyed() {},
};

export default slimSelect;

Showing Posts 1 to 4

cmo

cmo

So what’s the code that’s not working?

ityonemo

ityonemo

Oops I missed the part where you explained the ignore thing

pedromvieira

pedromvieira OP

It’s not working without hooks and recreating the click/show behaviour of the component.
Is there a simpler solution to rely on traditional Javascript libraries for small parts of the LiveView?

cmo

cmo

Hooks are the simple solution. Another one is using Phoenix.LiveView.JS or simple things.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
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
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews