colus001

colus001

I create boilerplate to demonstrate how to setup Phoenix LiveView project with typescript, svelte and tailwind

I have struggled to setup phoenix liveview project with svelte and typescript, tailwind.

Most steps are not very difficult but it was difficult to how to properly load svelte component properly. Some of suggestions I found on elixir forum was not working with different situation. So I created a hook to load svelte component properly and it might work to other frontend library such as react or vue.js.

I’m not sure somebody already solved this issue but it might be good starting point for someone who don’t understand yet. You can find Phovelte in github.

It works so far in my use case, but still need to figure out how to change props dynamically from phoenix liveview. Please leave a comment if there’s better way.

Thanks for reading.

Most Liked

colus001

colus001

That’s good point.

I updated hook to support instance clearing by using SvelteComponent.$destroy.
It is probably internal API, but it works for now.

import type { SvelteComponent } from 'svelte';

const hooks = {
  'svelte-component': {
    mounted(this: { el: HTMLElement, _instance: SvelteComponent | null }) {
      const componentName = this.el.getAttribute('data-name');
      if (!componentName) {
        throw new Error('Component name must be provided');
      }

      const requiredApp = require(`./${componentName}.svelte`);
      if (!requiredApp) {
        throw new Error(`Unable to find ${componentName} component`);
      }

      const props = this.el.getAttribute('data-props');
      const parsedProps = props ? JSON.parse(props) : {};

      this._instance = new requiredApp.default({
        target: this.el,
        props: parsedProps,
      });
    },

    destroyed() {
      this._instance?.$destroy();
    }
  },
}

export default hooks;
Exadra37

Exadra37

Would be good if you could add to the README an example of using your project, otherwise it just looks like one more playground repo :wink:

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement