@voidable/ui-liveview 0.1.2 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # Voidable UI for Phoenix LiveView
2
+
3
+ Two packages work together:
4
+
5
+ - **Hex** `live_voidable` (`hex/`): HEEx function components that render the
6
+ `<void-*>` custom elements (`use VoidableUI`).
7
+ - **npm** `@voidable/ui-liveview` (`src/`): `VoidHooks` (bridges Voidable
8
+ custom events to `pushEvent`) and `VoidDOM` (keeps client state across
9
+ LiveView patches).
10
+
11
+ Load `@voidable/ui` and a theme such as `@voidable/theme` in your asset
12
+ pipeline as usual; the components have no styling without a theme. To load each
13
+ component only when its tag first appears (in the page or in a LiveView patch),
14
+ import `@voidable/ui/autoloader` instead of `@voidable/ui`; see
15
+ [Loading components](https://voidable.dev/getting-started/loading/).
16
+
17
+ ## Setup
18
+
19
+ ```js
20
+ import { Socket } from "phoenix";
21
+ import { LiveSocket } from "phoenix_live_view";
22
+ import { VoidHooks, VoidDOM } from "@voidable/ui-liveview";
23
+
24
+ const liveSocket = new LiveSocket("/live", Socket, {
25
+ hooks: { ...VoidHooks },
26
+ dom: VoidDOM, // onBeforeElUpdated
27
+ });
28
+ ```
29
+
30
+ If you use esbuild without `node_modules`, import the source directly (the demo
31
+ does: `import {VoidHooks, VoidDOM} from "../../../src/index.ts"`).
32
+
33
+ ## Development warnings
34
+
35
+ Render `VoidableUI.Dev.warnings` once in the root layout, after your app script, to have misused `<void-*>` markup (a wrong attribute, a value outside the allowed set, a child in the wrong parent) reported in the browser console, including what LiveView patches in later:
36
+
37
+ ```heex
38
+ <script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}></script>
39
+ <VoidableUI.Dev.warnings />
40
+ ```
41
+
42
+ It renders nothing outside the `:dev` environment. `config :live_voidable, dev_warnings: false | true | :throw` changes that.
43
+
44
+ ## Events
45
+
46
+ Use LiveView's own bindings first: `phx-click` works on `button`, `tag`,
47
+ `card`, `dropdown_menu_item`, `menu_item` and a `<void-tabs-trigger>`,
48
+ and `phx-change` / `phx-submit` on the form around the controls, which all
49
+ submit under their `name`.
50
+
51
+ For a Voidable event no binding covers, opt in per element with `events`:
52
+ `events` bridges all of the component's events, `events={["void-close"]}` only
53
+ those. The component then renders `phx-hook="VoidEvent"` and
54
+ `data-void-events`; without `events` it renders neither. Each bridged event is
55
+ pushed to the server as a LiveView event of the same name, after the browser
56
+ has handled it (so a `phx-click` on the same element runs first):
57
+
58
+ ```heex
59
+ <.dialog id="confirm" events={["void-close"]}><void-dialog-title>Delete?</void-dialog-title>...</.dialog>
60
+ ```
61
+
62
+
63
+ ```elixir
64
+ def handle_event("void-change", %{"id" => id, "name" => name, "detail" => detail}, socket) do
65
+ ...
66
+ end
67
+ ```
68
+
69
+ The payload is `%{"id", "name", "event", "detail"}`; the fields of `detail`
70
+ (for example `"value"`) are also available at the top level. Set `phx-target`
71
+ on the component (a `@myself` cid or a selector) to route the event to a
72
+ LiveComponent.
73
+
74
+ ## State across patches
75
+
76
+ `VoidDOM` calls each component's pre-morph hook during morphdom patches:
77
+ client state of every reflected property (`open`, `checked`, `pressed`,
78
+ `collapsed`, `value`, ...) is kept only while the server keeps rendering the
79
+ same value it rendered last time. When the server renders a different value,
80
+ the server wins, so `open={false}` really closes a dialog the client opened.
81
+ The rule lives in `@voidable/ui` and is the same for the Turbo and Livewire
82
+ adapters.
83
+
84
+ ## Ids and `phx-update="ignore"`
85
+
86
+ Components that need an id derive a stable one from their kind, `name` and
87
+ `phx-*` attributes; pass `id` explicitly for several identical anonymous
88
+ components. `phx-update="ignore"` protects the children Voidable components
89
+ relocate, but also blocks all server attribute updates, so it is opt-in per
90
+ element. See the `VoidableUI.Components` moduledoc for guidance.
91
+
92
+ ## Typing through patches
93
+
94
+ A patch that arrives while the user types (the echo of an earlier
95
+ `phx-change`, a few keys behind) does not rewrite the field with focus in it,
96
+ and the clone LiveView patches while it waits for a reply is put back without
97
+ losing a key: nothing typed is lost and focus stays in the field.
98
+
99
+ ## Booleans
100
+
101
+ Every boolean is off when it is absent. Something drawn by default is
102
+ turned off with a `hide_*` attribute: `hide_close` on `dialog` and `drawer`,
103
+ `hide_border` on `nav_bar`, `hide_input` on `color_picker`, `hide_icon` on
104
+ `error_fallback`, and `hide_value`, `hide_tick_min` and `hide_tick_max` on
105
+ `gauge`. For example `<.error_fallback hide_icon />`.
106
+
107
+ ## Native input attributes
108
+
109
+ `input`, `textarea` and `action_input` declare `autocomplete`, `minlength`
110
+ and `maxlength` (and `pattern` on the single-line inputs); `inputmode`,
111
+ `enterkeyhint`, `autocapitalize` and `spellcheck` pass through as global
112
+ attributes. `number_input` accepts `step="any"` and decimal `value`, `min`
113
+ and `max`, and `stat` takes `description`.
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ declare type ListenerEntry = [event: string, handler: (e: Event) => void];
15
15
  declare interface ViewHookInstance {
16
16
  el: HTMLElement;
17
17
  pushEvent(event: string, payload: object): void;
18
+ pushEventTo(target: unknown, event: string, payload: object): void;
18
19
  handleEvent(event: string, callback: (payload: object) => void): void;
19
20
  mounted?(): void;
20
21
  updated?(): void;
@@ -26,29 +27,34 @@ declare interface ViewHookInstance {
26
27
  }
27
28
 
28
29
  /**
29
- * DOM-patching callbacks that preserve web component client state during
30
- * LiveView's morphdom updates.
30
+ * DOM-patching callbacks for `void-*` elements during LiveView's morphdom
31
+ * updates.
32
+ *
33
+ * `onBeforeElUpdated` calls each `void-*` element's `voidBeforeMorph(toEl)`
34
+ * pre-morph hook, which applies Void UI's rule for state: a reflected attribute
35
+ * the server changed since its last render wins (so `open={false}` on the
36
+ * server really closes a dialog the client opened); every other one keeps the
37
+ * client's value, and attributes the component manages on its host (`role`,
38
+ * `tabindex`, `aria-*`) are copied onto `toEl`. The hook also parks the chrome
39
+ * the component rendered, so morphdom compares the element's own children with
40
+ * the server markup. morphdom visits parents before children, so outer
41
+ * elements are parked first; every element puts the same chrome back in the
42
+ * microtask after the patch. The rule itself lives in the base class, shared
43
+ * with the Turbo and Livewire adapters; this module only connects it to
44
+ * morphdom.
31
45
  *
32
46
  * Usage:
33
47
  * ```js
34
48
  * import { VoidDOM } from "@voidable/ui-liveview";
35
49
  * const liveSocket = new LiveSocket("/live", Socket, {
36
- * dom: {
37
- * onBeforeElUpdated(from, to) {
38
- * VoidDOM.onBeforeElUpdated(from, to);
39
- * },
40
- * },
50
+ * dom: VoidDOM, // onBeforeElUpdated
41
51
  * });
42
52
  * ```
43
53
  */
44
54
  export declare const VoidDOM: {
45
55
  /**
46
- * Call from `dom.onBeforeElUpdated` in your LiveSocket config.
47
- *
48
- * When LiveView patches the DOM via morphdom, client-managed attributes on
49
- * `void-*` custom elements can be lost if they weren't in the server-rendered
50
- * markup. This callback copies them from the outgoing element to the incoming
51
- * one so the component keeps its visual state.
56
+ * Call from `dom.onBeforeElUpdated`: `fromEl` is the live element, `toEl` the
57
+ * server-rendered one.
52
58
  */
53
59
  onBeforeElUpdated(fromEl: Element, toEl: Element): void;
54
60
  };
@@ -57,6 +63,7 @@ export declare const VoidDOM: {
57
63
  declare interface VoidEventInstance extends ViewHookInstance {
58
64
  _listeners: ListenerEntry[];
59
65
  _boundEvents: string[];
66
+ _destroyed?: boolean;
60
67
  }
61
68
 
62
69
  /**
package/dist/index.js CHANGED
@@ -2,47 +2,49 @@
2
2
  function e(e) {
3
3
  return (e.dataset.voidEvents || "").split(",").filter(Boolean);
4
4
  }
5
- function t(e, t) {
6
- for (let n of t) {
7
- let t = (t) => {
8
- e.pushEvent(n, t.detail || {});
5
+ function t(e, t, n) {
6
+ return {
7
+ ...n && typeof n == "object" && !Array.isArray(n) ? n : {},
8
+ id: e.id || null,
9
+ name: e.getAttribute("name") ?? Array.from(e.children).find((e) => e.localName === "select")?.getAttribute("name") ?? null,
10
+ event: t,
11
+ detail: n ?? {}
12
+ };
13
+ }
14
+ function n(e, t, n) {
15
+ if (e._destroyed) return;
16
+ let r = e.el.getAttribute("phx-target"), i = r ? e.pushEventTo(r, t, n) : e.pushEvent(t, n);
17
+ i && typeof i.catch == "function" && i.catch(() => {});
18
+ }
19
+ function r(e, r) {
20
+ for (let i of r) {
21
+ let r = (r) => {
22
+ let a = t(e.el, i, r.detail);
23
+ setTimeout(() => n(e, i, a), 0);
9
24
  };
10
- e.el.addEventListener(n, t), e._listeners.push([n, t]);
25
+ e.el.addEventListener(i, r), e._listeners.push([i, r]);
11
26
  }
12
- e._boundEvents = t;
27
+ e._boundEvents = r;
13
28
  }
14
- function n(e) {
29
+ function i(e) {
15
30
  for (let [t, n] of e._listeners) e.el.removeEventListener(t, n);
16
31
  e._listeners = [], e._boundEvents = [];
17
32
  }
18
- var r = { VoidEvent: {
33
+ var a = { VoidEvent: {
19
34
  mounted() {
20
- this._listeners = [], this._boundEvents = [];
21
- let n = e(this.el);
22
- t(this, n);
35
+ this._destroyed = !1, this._listeners = [], this._boundEvents = [];
36
+ let t = e(this.el);
37
+ r(this, t);
23
38
  },
24
39
  updated() {
25
- let r = e(this.el), i = this._boundEvents || [];
26
- r.length === i.length && r.every((e, t) => e === i[t]) || (n(this), t(this, r));
40
+ let t = e(this.el), n = this._boundEvents || [];
41
+ t.length === n.length && t.every((e, t) => e === n[t]) || (i(this), r(this, t));
27
42
  },
28
43
  destroyed() {
29
- n(this);
30
- }
31
- } }, i = [
32
- "aria-expanded",
33
- "aria-selected",
34
- "aria-checked",
35
- "aria-hidden"
36
- ], a = [
37
- "open",
38
- "active",
39
- "checked",
40
- "selected"
41
- ], o = { onBeforeElUpdated(e, t) {
42
- if (e.tagName.startsWith("VOID-")) {
43
- for (let n of i) e.hasAttribute(n) && !t.hasAttribute(n) && t.setAttribute(n, e.getAttribute(n));
44
- for (let n of a) e.hasAttribute(n) && !t.hasAttribute(n) && t.setAttribute(n, e.getAttribute(n));
44
+ this._destroyed = !0, i(this);
45
45
  }
46
+ } }, o = { onBeforeElUpdated(e, t) {
47
+ e.tagName?.startsWith("VOID-") && (t.hasAttribute?.("data-phx-skip") || e.voidBeforeMorph?.(t));
46
48
  } };
47
49
  //#endregion
48
- export { o as VoidDOM, r as VoidHooks };
50
+ export { o as VoidDOM, a as VoidHooks };
package/package.json CHANGED
@@ -1,7 +1,21 @@
1
1
  {
2
2
  "name": "@voidable/ui-liveview",
3
- "version": "0.1.2",
3
+ "version": "0.8.1",
4
+ "description": "Phoenix LiveView hooks for Voidable UI web components",
5
+ "keywords": [
6
+ "phoenix",
7
+ "liveview",
8
+ "elixir",
9
+ "web-components",
10
+ "custom-elements",
11
+ "void-ui"
12
+ ],
13
+ "homepage": "https://voidable.dev",
14
+ "bugs": {
15
+ "url": "https://github.com/VoidableUI/Voidable/issues"
16
+ },
4
17
  "type": "module",
18
+ "sideEffects": false,
5
19
  "license": "MIT",
6
20
  "exports": {
7
21
  ".": {
@@ -12,23 +26,30 @@
12
26
  "files": [
13
27
  "dist"
14
28
  ],
15
- "scripts": {
16
- "build": "vite build",
17
- "typecheck": "tsc --noEmit"
18
- },
19
29
  "peerDependencies": {
20
- "@voidable/ui": "^0.6.0",
21
- "phoenix_live_view": ">=1.0.0"
30
+ "phoenix_live_view": ">=1.0.0",
31
+ "@voidable/ui": "0.8.1"
22
32
  },
23
33
  "devDependencies": {
24
- "@voidable/ui": "^0.6.0",
25
34
  "typescript": "^6.0.0",
26
35
  "vite": "^8.0.0",
27
- "vite-plugin-dts": "^4.5.0"
36
+ "vite-plugin-dts": "^4.5.0",
37
+ "vitest": "^3.1.0",
38
+ "@vitest/coverage-v8": "3.2.4",
39
+ "happy-dom": "^17.0.0",
40
+ "@voidable/ui": "0.8.1"
28
41
  },
29
42
  "repository": {
30
43
  "type": "git",
31
- "url": "https://github.com/VoidableUI/Voidable",
44
+ "url": "git+https://github.com/VoidableUI/Voidable.git",
32
45
  "directory": "packages/liveview"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "scripts": {
51
+ "build": "vite build",
52
+ "test": "vitest run",
53
+ "typecheck": "tsc --noEmit"
33
54
  }
34
- }
55
+ }