@voidable/ui-hotwire 0.4.1 → 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 CHANGED
@@ -12,35 +12,42 @@ npm install @voidable/ui @voidable/theme @voidable/ui-hotwire
12
12
 
13
13
  ```js
14
14
  import { Application } from '@hotwired/stimulus';
15
- import { VoidEventController, VoidTurbo } from '@voidable/ui-hotwire';
15
+ import { VoidTurbo } from '@voidable/ui-hotwire';
16
16
 
17
- const application = Application.start();
18
- application.register('void-event', VoidEventController);
19
- VoidTurbo.start();
17
+ Application.start();
18
+ VoidTurbo.start(); // idempotent; returns a function that detaches the listener
20
19
  ```
21
20
 
21
+ `VoidTurbo` calls each component's pre-morph hook during Turbo morphs: the
22
+ state of every reflected property (`open`, `checked`, `pressed`, `collapsed`,
23
+ `value`, ...) and the component's own `role` / `tabindex` / `aria-*` survive
24
+ while the server keeps rendering the same value, and the server wins as soon
25
+ as it renders a different one. The rule lives in `@voidable/ui` and is the
26
+ same for the Livewire and LiveView adapters. A component whose server markup
27
+ is identical to the previous render is skipped with its subtree.
28
+
29
+ To load each component only when its tag first appears (including tags that
30
+ arrive in Turbo visits, streams and frames), import `@voidable/ui/autoloader`
31
+ instead of `@voidable/ui`; see
32
+ [Loading components](https://voidable.dev/getting-started/loading/).
33
+
22
34
  ## Usage
23
35
 
36
+ Stimulus handles custom events natively, so wire Voidable events straight to
37
+ your controllers with `data-action`:
38
+
24
39
  ```erb
25
- <void-button
26
- data-controller="void-event"
27
- data-void-event-events-value="void-click"
28
- data-action="void-click->form#submit"
29
- >
30
- Save
31
- </void-button>
32
-
33
- <void-select
34
- data-controller="void-event"
35
- data-void-event-events-value="void-change"
36
- data-action="void-change->filter#update"
37
- >
38
- <option value="a">Option A</option>
39
- <option value="b">Option B</option>
40
+ <void-button data-action="void-click->form#submit">Save</void-button>
41
+
42
+ <void-select aria-label="Filter" data-action="void-change->filter#update">
43
+ <select>
44
+ <option value="a">Option A</option>
45
+ <option value="b">Option B</option>
46
+ </select>
40
47
  </void-select>
41
48
  ```
42
49
 
43
50
  ## Documentation
44
51
 
45
- - [Storybook](https://voidableui.github.io/Voidable/)
52
+ - [Storybook](https://voidable.dev/storybook/)
46
53
  - [GitHub](https://github.com/VoidableUI/Voidable)
package/dist/index.d.ts CHANGED
@@ -1,48 +1,30 @@
1
- import { Controller } from '@hotwired/stimulus';
2
-
3
1
  /**
4
- * Stimulus controller that bridges CustomEvents from Voidable web components
5
- * so parent controllers can handle them via `data-action`.
2
+ * Turbo morph integration for `void-*` elements.
6
3
  *
7
- * Usage:
8
- * ```html
9
- * <void-button
10
- * data-controller="void-event"
11
- * data-void-event-events-value="void-click"
12
- * data-action="void-click->form#submit"
13
- * >
14
- * Save
15
- * </void-button>
16
- * ```
17
- */
18
- export declare class VoidEventController extends Controller {
19
- static values: {
20
- events: {
21
- type: StringConstructor;
22
- default: string;
23
- };
24
- };
25
- eventsValue: string;
26
- private _bindings;
27
- connect(): void;
28
- eventsValueChanged(): void;
29
- disconnect(): void;
30
- private _bind;
31
- private _unbind;
32
- }
33
-
34
- /**
35
- * Turbo morph integration to preserve client-managed attributes on `VOID-*`
36
- * elements during page morphs.
4
+ * Before Turbo morphs a `void-*` element (`turbo:before-morph-element`) this
5
+ * calls the element's `voidBeforeMorph(newElement)` pre-morph hook, which
6
+ * applies Void UI's rule for state: a reflected attribute the server changed
7
+ * since its last render wins; every other one keeps the client's value (an
8
+ * opened dialog, a pressed toggle, a collapsed sidebar), and attributes the
9
+ * component manages on its host (`role`, `tabindex`, `aria-*`) are carried over.
10
+ * The hook also parks the chrome the component rendered, so the morph compares
11
+ * the element's own children with the server markup. Turbo morphs parents
12
+ * before children, so outer elements are parked first; every element puts the
13
+ * same chrome back (same nodes, state, typed text and focus) in the microtask
14
+ * after the morph. The rule itself lives in the base class, shared with the
15
+ * Livewire and LiveView adapters; this module only connects it to Turbo. An
16
+ * element whose server markup is identical to the server's previous render
17
+ * (`voidUnchanged`) is left out of the morph with its subtree.
37
18
  *
38
19
  * Usage:
39
20
  * ```js
40
21
  * import { VoidTurbo } from '@voidable/ui-hotwire';
41
- * VoidTurbo.start();
22
+ * const stop = VoidTurbo.start(); // call stop() to detach
42
23
  * ```
43
24
  */
44
25
  export declare const VoidTurbo: {
45
- start(): void;
26
+ /** Start listening. Returns a function that removes the listener. */
27
+ start(target?: Document): () => void;
46
28
  };
47
29
 
48
30
  export { }
package/dist/index.js CHANGED
@@ -1,52 +1,22 @@
1
- import { Controller as e } from "@hotwired/stimulus";
2
- //#region src/controller.ts
3
- var t = class extends e {
4
- static values = { events: {
5
- type: String,
6
- default: ""
7
- } };
8
- _bindings = [];
9
- connect() {
10
- this._bind();
11
- }
12
- eventsValueChanged() {
13
- this._unbind(), this._bind();
14
- }
15
- disconnect() {
16
- this._unbind();
17
- }
18
- _bind() {
19
- let e = this.eventsValue.split(",").filter(Boolean);
20
- for (let t of e) {
21
- let e = ((e) => {
22
- this.dispatch(t, {
23
- detail: e.detail,
24
- prefix: ""
25
- });
26
- });
27
- this.element.addEventListener(t, e), this._bindings.push({
28
- event: t,
29
- handler: e
30
- });
1
+ //#region src/turbo.ts
2
+ var e = /* @__PURE__ */ new WeakMap(), t = { start(t = document) {
3
+ let n = e.get(t);
4
+ if (n) return n;
5
+ let r = ((e) => {
6
+ let t = e.target, n = e.detail?.newElement;
7
+ if (!(!n || e.defaultPrevented || !t.tagName?.startsWith("VOID-"))) {
8
+ if (t.voidUnchanged?.(n)) {
9
+ e.preventDefault();
10
+ return;
11
+ }
12
+ t.voidBeforeMorph?.(n);
31
13
  }
32
- }
33
- _unbind() {
34
- for (let { event: e, handler: t } of this._bindings) this.element.removeEventListener(e, t);
35
- this._bindings = [];
36
- }
37
- }, n = [
38
- "aria-expanded",
39
- "aria-selected",
40
- "aria-checked",
41
- "aria-hidden",
42
- "open",
43
- "active",
44
- "checked",
45
- "selected"
46
- ], r = { start() {
47
- document.addEventListener("turbo:before-morph-attribute", ((e) => {
48
- e.target.tagName?.startsWith("VOID-") && n.includes(e.detail.attributeName) && e.preventDefault();
49
- }));
14
+ });
15
+ t.addEventListener("turbo:before-morph-element", r);
16
+ let i = () => {
17
+ t.removeEventListener("turbo:before-morph-element", r), e.delete(t);
18
+ };
19
+ return e.set(t, i), i;
50
20
  } };
51
21
  //#endregion
52
- export { t as VoidEventController, r as VoidTurbo };
22
+ export { t as VoidTurbo };
package/package.json CHANGED
@@ -1,7 +1,22 @@
1
1
  {
2
2
  "name": "@voidable/ui-hotwire",
3
- "version": "0.4.1",
3
+ "version": "0.8.1",
4
+ "description": "Hotwire (Turbo and Stimulus) integration for Voidable UI web components",
5
+ "keywords": [
6
+ "hotwire",
7
+ "turbo",
8
+ "stimulus",
9
+ "rails",
10
+ "web-components",
11
+ "custom-elements",
12
+ "void-ui"
13
+ ],
14
+ "homepage": "https://voidable.dev",
15
+ "bugs": {
16
+ "url": "https://github.com/VoidableUI/Voidable/issues"
17
+ },
4
18
  "type": "module",
19
+ "sideEffects": false,
5
20
  "license": "MIT",
6
21
  "exports": {
7
22
  ".": {
@@ -12,26 +27,31 @@
12
27
  "files": [
13
28
  "dist"
14
29
  ],
15
- "scripts": {
16
- "build": "vite build",
17
- "typecheck": "tsc --noEmit"
18
- },
19
30
  "peerDependencies": {
20
- "@hotwired/stimulus": "^3.2.0",
21
31
  "@hotwired/turbo": "^8.0.0",
22
- "@voidable/ui": "^0.6.0"
32
+ "@voidable/ui": "0.8.1"
23
33
  },
24
34
  "devDependencies": {
25
- "@hotwired/stimulus": "^3.2.0",
26
35
  "@hotwired/turbo": "^8.0.0",
27
- "@voidable/ui": "^0.6.0",
28
36
  "typescript": "^6.0.0",
29
37
  "vite": "^8.0.0",
30
- "vite-plugin-dts": "^4.5.0"
38
+ "vite-plugin-dts": "^4.5.0",
39
+ "vitest": "^3.1.0",
40
+ "@vitest/coverage-v8": "3.2.4",
41
+ "happy-dom": "^17.0.0",
42
+ "@voidable/ui": "0.8.1"
31
43
  },
32
44
  "repository": {
33
45
  "type": "git",
34
- "url": "https://github.com/VoidableUI/Voidable",
46
+ "url": "git+https://github.com/VoidableUI/Voidable.git",
35
47
  "directory": "packages/hotwire"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "scripts": {
53
+ "build": "vite build",
54
+ "test": "vitest run",
55
+ "typecheck": "tsc --noEmit"
36
56
  }
37
- }
57
+ }