Skip to content

Valian/live_vue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Hex.pm Hexdocs.pm GitHub

LiveVue

Vue inside Phoenix LiveView with seamless end-to-end reactivity.

logo

Resources

Features

  • ⚑ End-To-End Reactivity with LiveView
  • πŸ”‹ Server-Side Rendered (SSR) Vue
  • 🐌 Lazy-loading Vue Components
  • πŸͺ„ Sigil as an Alternative LiveView DSL
  • πŸ¦„ Tailwind Support
  • πŸ’€ Dead View Support
  • πŸ¦₯ Slot Interoperability
  • πŸš€ Amazing DX with Vite

Example

After installation, you can use Vue components in the same way as you'd use functional LiveView components. You can even handle Vue events with JS hooks! All the phx-click, phx-change attributes works inside Vue components as well.

<script setup lang="ts">
import {ref} from "vue"

// props are passed from LiveView
const props = defineProps<{count: number}>()

// local state
const diff = ref(1)
</script>

<template>
  Current count: {{ props.count }}
  <label>Diff: </label>
  <input v-model.number="diff" type="range" min="1" max="10" />

  <button phx-click="inc" :phx-value-diff="diff">
    Increase counter by {{ diff }}
  </button>
</template>
defmodule MyAppWeb.CounterLive do
  use MyAppWeb, :live_view

  def render(assigns) do
    ~H"""
    <.vue count={@count} v-component="Counter" v-socket={@socket} />
    """
  end

  def mount(_params, _session, socket) do
    {:ok, assign(socket, count: 0)}
  end

  def handle_event("inc", %{"diff" => value}, socket) do
    {:noreply, update(socket, :count, &(&1 + value))}
  end
end

Why?

Phoenix Live View makes it possible to create rich, interactive web apps without writing JS.

But once you'll need to do anything even slightly complex on the client-side, you'll end up writing lots of imperative, hard-to-maintain hooks.

LiveVue allows to create hybrid apps, where part of the session state is on the server and part on the client.

Reasons why you'd like to use LiveVue

  • Your hooks are starting to look like jQuery
  • You have a complex local state
  • You'd like to use a massive Vue ecosystem
  • You want transitions, graphs etc.
  • You simply like Vue πŸ˜‰

Installation

See Installation.

Guides

Getting Started

Core Usage

  • Basic Usage - Fundamental patterns, ~V sigil, and common examples
  • Configuration - Advanced setup, SSR, and customization options

Reference

Advanced Topics

Help & Troubleshooting

Relation to LiveSvelte

This project is heavily inspired by ✨ LiveSvelte ✨. Both projects try to solve the same problem. LiveVue was started as a fork of LiveSvelte with adjusted ESbuild settings, and evolved to use Vite and a slightly different syntax. I strongly believe more options are always better, and since I love Vue and it's ecosystem I've decided to give it a go πŸ˜‰

You can read more about differences between Vue and Svelte in FAQ.

LiveVue Development

Local Setup

Ensure you have node installed. Clone the repo and run mix setup. You can then run mix assets.watch to start a watcher for the assets.

Example Project

You can use /example_project as a way to test live_vue locally.

  • Clone this repo
  • Go to example_project dir
  • Run mix setup
  • Run mix phx.server to start the server
  • Open http://localhost:4000 in your browser

Building Static Files

LiveVue is written in TypeScript. To see the changes you made, you have to recompile the assets. You can do it with:

mix assets.build

Or run the watcher:

mix assets.watch

Releasing

Release is done with expublish package.

  • Write version changelog in untracked RELEASE.md file
  • Update version in INSTALLATION.md

Run

git add INSTALLATION.md
git commit -m "INSTALLATION version bump"

# to ensure everything works fine
mix expublish.minor --dry-run --allow-untracked --branch=main

# to publish everything
mix do assets.build, expublish.minor --allow-untracked --branch=main

Roadmap 🎯

  • Add a default handler for Vue emits to eg. automatically push them to the server without explicit v-on handlers.
  • try to use Igniter as a way of installing LiveVue in a project
  • usePushEvent - an utility similar to useFetch making it easy to get data from &handle_event/3 -> {:reply, data, socket} responses
  • useLiveForm - an utility to efforlessly use Ecto changesets & server-side validation, similar to HEEX
  • useEventHandler - an utility automatically attaching & detaching handleEvent
  • optimize payload - send only json_patch diffs of updated props
  • VS code extension highlighting ~V sigil
  • Add support for Phoenix streams as props

Credits

LiveSvelte

Star History

Star History Chart