pedromvieira

pedromvieira

LiveView external JS after socket mounted?

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;

First Post!

cmo

cmo

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

Last Post!

cmo

cmo

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

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement