Skip to content

Commit 7bcf499

Browse files
authored
LocalLiveView: Integrate event handling with Phoenix (#494)
Changed custom JS handling to overwriting functions in View class objects. It allows to use both localLiveViews and phoenixLiveViews at the same page.
1 parent 286d04c commit 7bcf499

File tree

96 files changed

+4052
-367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+4052
-367
lines changed

examples/local-lv-compare/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ compare_live_views-*.tar
3535
# In case you use Node.js/npm, you want to ignore these.
3636
npm-debug.log
3737
/assets/node_modules/
38+
39+
/assets/vendor/local_live_view
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as esbuild from "esbuild";
2+
import { popcorn } from "local_live_view/esbuild";
3+
import { dirname, resolve } from "path";
4+
import { fileURLToPath } from "url";
5+
6+
const __dirname = dirname(fileURLToPath(import.meta.url));
7+
const isWatch = process.argv.includes("--watch");
8+
9+
const ctx = await esbuild.context({
10+
entryPoints: ["js/app.js"],
11+
bundle: true,
12+
target: "es2022",
13+
format: "esm",
14+
sourcemap: isWatch ? "inline" : true,
15+
outdir: "../priv/static/assets/js",
16+
external: ["/fonts/*", "/images/*"],
17+
nodePaths: [
18+
resolve(__dirname, "../deps"),
19+
process.env.MIX_BUILD_PATH ?? resolve(__dirname, "../_build/dev"),
20+
],
21+
plugins: [
22+
popcorn({
23+
bundlePath: resolve(__dirname, "../priv/static/assets/js/wasm/bundle.avm"),
24+
}),
25+
],
26+
});
27+
28+
if (isWatch) {
29+
await ctx.watch();
30+
console.log("[esbuild] watching...");
31+
} else {
32+
await ctx.rebuild();
33+
await ctx.dispose();
34+
}

examples/local-lv-compare/assets/js/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
3737
window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
3838
window.addEventListener("phx:page-loading-stop", _info => topbar.hide())
3939

40+
// setup local live views, which will override the default pushWithReply and join functions of the live view to instead call popcorn
41+
import { setup } from "local_live_view"
42+
setup(liveSocket, { bundlePath: "bundle.avm" });
43+
4044
// connect if there are any LiveViews on the page
4145
liveSocket.connect()
4246
liveSocket.enableLatencySim(500)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "local_lv_compare",
3+
"private": true,
4+
"dependencies": {
5+
"local_live_view": "workspace:*"
6+
},
7+
"scripts": {
8+
"build": "node build.mjs",
9+
"watch": "node build.mjs --watch"
10+
},
11+
"devDependencies": {
12+
"esbuild": "^0.25.0"
13+
}
14+
}

examples/local-lv-compare/config/config.exs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ config :compare_live_views, CompareLiveViewsWeb.Endpoint,
3030
# at the `config/runtime.exs`.
3131
config :compare_live_views, CompareLiveViews.Mailer, adapter: Swoosh.Adapters.Local
3232

33-
# Configure esbuild (the version is required)
34-
config :esbuild,
35-
version: "0.25.4",
36-
compare_live_views: [
37-
args:
38-
~w(js/app.js --bundle --target=es2022 --outdir=../priv/static/assets/js --format=esm --external:/fonts/* --external:/images/* --alias:@=.),
39-
cd: Path.expand("../assets", __DIR__),
40-
env: %{"NODE_PATH" => [Path.expand("../deps", __DIR__), Mix.Project.build_path()]}
41-
]
42-
4333
# Configure tailwind (the version is required)
4434
config :tailwind,
4535
version: "4.1.7",

examples/local-lv-compare/config/dev.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ config :compare_live_views, CompareLiveViewsWeb.Endpoint,
1515
debug_errors: true,
1616
secret_key_base: "kkoz3bWTFeKbaSGq3oqEQwL7bpFAR4f0ejreK+rg71pTgpW8WCFdz+rksVqa6MAE",
1717
watchers: [
18-
esbuild: {Esbuild, :install_and_run, [:compare_live_views, ~w(--sourcemap=inline --watch)]},
18+
node: ["build.mjs", "--watch",
19+
cd: Path.expand("../assets", __DIR__),
20+
env: %{"MIX_BUILD_PATH" => Mix.Project.build_path()}
21+
],
1922
tailwind: {Tailwind, :install_and_run, [:compare_live_views, ~w(--watch)]}
2023
]
2124

examples/local-lv-compare/lib/compare_live_views_web/components/layouts/root.html.heex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
{assigns[:page_title]}
99
</.live_title>
1010
<link phx-track-static rel="stylesheet" href={~p"/assets/css/app.css"} />
11-
<script defer phx-track-static type="text/javascript" src={~p"/assets/js/app.js"}>
12-
</script>
13-
<script type="module" src="./local_live_view/local_live_view.js" defer>
11+
<script defer phx-track-static type="module" src={~p"/assets/js/app.js"}>
1412
</script>
1513
<script>
1614
(() => {
@@ -27,7 +25,7 @@
2725
setTheme(localStorage.getItem("phx:theme") || "system");
2826
}
2927
window.addEventListener("storage", (e) => e.key === "phx:theme" && setTheme(e.newValue || "system"));
30-
28+
3129
window.addEventListener("phx:set-theme", (e) => setTheme(e.target.dataset.phxTheme));
3230
})();
3331
</script>

examples/local-lv-compare/lib/compare_live_views_web/controllers/page_html/home.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
</div>
55

66
<div class="half" id="right-half">
7-
<div data-pop-view="DemoModalOffline"></div>
7+
<div data-pop-view="DemoModalOffline" id="DemoModalOffline"></div>
88
</div>
99
</div>
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import Config
22

33
config :popcorn,
4-
out_dir: "../priv/static/local_live_view/wasm",
5-
add_tracing: false
6-
7-
config :local_live_view,
8-
out_dir: "../priv/static/local_live_view"
4+
out_dir: "../priv/static/assets/js/wasm"

examples/local-lv-compare/local/lib/demo_modal_offline.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ defmodule DemoModalOffline do
44
def render(assigns) do
55
~H"""
66
<p>LOCAL LIVE VIEW</p>
7-
<button class="show-modal-button" pop-click="show_modal">SHOW OFFLINE MODAL</button>
7+
<button class="show-modal-button" phx-click="show_modal">SHOW OFFLINE MODAL</button>
88
99
<%= if @show_modal do %>
1010
<div class="modal">
1111
<div class="modal-content">
12-
<span class="close" pop-click="close_modal">&times;</span>
12+
<span class="close" phx-click="close_modal">&times;</span>
1313
<p>OFFLINE MODAL</p>
1414
</div>
1515
</div>

0 commit comments

Comments
 (0)