npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phoenix_typed_hook

v0.18.2

Published

Write your Phoenix LiveView client hooks using typed classes

Downloads

1,552

Readme

Phoenix Typed Hook

Write your Phoenix LiveView client hooks using typed classes, with or without TypeScript.

See autocomplete available in VS Code by using it:

Demo of code editor autocomplete using phoenix_typed_hook

Reasoning

Custom LiveView client hooks are defined as plain objects implementing callbacks such as mounted, updated, etc.

Since they're plain objects, there is no typing involved, so code editors can't provide code completion for attributes available, and also can't do compile-time checks (this last one, when TypeScript is used).

This package allows those features, by introducing a typed base hook class you can extend from, and a function to convert this class to a format that Phoenix LiveView understands.

How to use

Once installed, define your hook as a class that extends Hook, and convert it using makeHook:

import { Hook, makeHook } from "phoenix_typed_hook";

class MyHook extends Hook {
  mounted() {
    this.el.style.color = "red";
    this.handleEvent("foo", (payload) => {});
  }
}

export default makeHook(MyHook);

Then, pass the hook as usual to the LiveSocket constructor options.

import MyHook from "./hooks/my_hook";

const hooks = { MyHook };

const liveSocket = new LiveSocket("/live", Socket, { hooks, ... });

Installation

Phoenix Typed Hook can be installed via NPM or Mix.

Installing with NPM

In your project directory, go to the assets folder (cd assets) and run npm install phoenix_typed_hook --save

Installing with Hex

Using Hex to install Phoenix Typed Hook requires more steps compared to NPM, but allows you to manage its version on mix.exs, closer to where the Phoenix LiveView version is also declared. This makes it easier to remember to upgrade them together.

Add phoenix_typed_hook to the list of dependencies in mix.exs:

def deps do
  [
    {:phoenix_typed_hook, "~> 0.18.2"}
  ]
end

And run mix deps.get.

After that, open assets/package.json of your project (create it if it doesn't exist), and add phoenix_typed_hook to the list of dependencies:

{
  "dependencies": {
    "phoenix_typed_hook": "file:../deps/phoenix_typed_hook"
  }
}

And run npm install inside the assets folder.