Giter Site home page Giter Site logo

phoenix_typed_hook's Introduction

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.

phoenix_typed_hook's People

Contributors

guisehn avatar

Stargazers

Mathew Garland avatar norn avatar Kent Are avatar Mike Wilson avatar Marko Vukovic avatar Ramtin Khalatbari avatar Nicholas Moen avatar Eugene Cheltsov avatar Nicolas Talle avatar Sylvain Colignon avatar Igor Barchenkov avatar O.T. avatar Kacper Golinski avatar Éber Freitas Dias avatar Florian Kapfenberger avatar

Watchers

 avatar  avatar  avatar

Forkers

loics2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.