guisehn

guisehn

The combination of Elixir typespecs with @doc allows Phoenix to provide great documentation and autocompletion inside the code editor. But I missed the same level of good experience when playing with LiveView client hooks via phx-hook.

Since LiveView hooks are defined as plain JS objects, there is no way for code editors to provide documentation and autocomplete around attributes and callbacks available, nor compile-time checks (this last one, when TypeScript is used in the project).

To solve that, I created a tiny library called phoenix_typed_hook, that allows you to define a LiveView client hook by extending a base typed Hook class. By using it, editors like VS Code will provide all those documentation and autocomplete features automatically. And there is no need to use TypeScript in the project to take advantage of it, if it’s not your thing.

One GIF says more than a thousand words. :slight_smile:

It can be installed through Hex or NPM.

Hope you find it useful!

Links:

Showing Posts 1 to 3

zachallaun

zachallaun

This is awesome! I see that you’ve released it as v0.18 as well, which is smart. Should make upgrades nice and easy!

One suggestion, if I may: I believe it should be possible to distribute this through Hex so that it can be added to your mix.exs dependencies. This would also make it very easy to remember to upgrade down the line. Here’s an example of a project that bundles some JS. It looks like the minified JS is included in the list of files in package. I don’t think there’s any particular reason that

guisehn

guisehn OP

Nice suggestion! I’ve released it to Hex as well and updated the initial post. :slight_smile:

Note: when installing it through Hex, it’s still necessary to add it to package.json but pointing to the Hex-downloaded code inside deps, to make the code editor detect the typing to provide autocomplete.

RodolfoSilva

RodolfoSilva

Use JS Docs, check my PR:
https://github.com/phoenixframework/phoenix_live_view/pull/3022

Usage example:

/**
 * @type {import("phoenix_live_view").ViewHook}
 */
Hooks.InfiniteScroll = {
  page() { return this.el.dataset.page },
  mounted(){
    this.pending = this.page()
    window.addEventListener("scroll", e => {
      if(this.pending == this.page() && scrollAt() > 90){
        this.pending = this.page() + 1
        this.pushEvent("load-more", {})
      }
    })
  },
  updated(){ this.pending = this.page() }
}

— All posts loaded —

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
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
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews