@threadlabs/looma 0.1.5 → 0.1.6

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.
@@ -1,84 +0,0 @@
1
- // src/adapter.ts
2
- import {
3
- cloneVNode,
4
- defineComponent,
5
- h,
6
- isVNode,
7
- shallowRef,
8
- watchEffect
9
- } from "vue";
10
- var BASE_EVENT_BINDINGS = [
11
- ["open", "onOpen"],
12
- ["close", "onClose"],
13
- ["select", "onSelect"],
14
- ["change", "onChange"],
15
- ["input", "onInput"],
16
- ["dismiss", "onDismiss"]
17
- ];
18
- function toHTMLElement(value) {
19
- if (!value) return null;
20
- if (value instanceof HTMLElement) return value;
21
- return null;
22
- }
23
- function createAdapterComponent(tagName, displayName, additionalEventBindings = []) {
24
- const eventBindings = [...BASE_EVENT_BINDINGS, ...additionalEventBindings];
25
- const callbackAttrs = new Set(eventBindings.map(([, callbackAttr]) => callbackAttr));
26
- return defineComponent({
27
- name: displayName,
28
- inheritAttrs: false,
29
- setup(_props, { attrs, slots }) {
30
- const elementRef = shallowRef(null);
31
- watchEffect((onCleanup) => {
32
- const element = elementRef.value;
33
- if (!element) return;
34
- const adapterAttrs = attrs;
35
- const handlers = eventBindings.map(
36
- ([eventName, callbackAttr]) => {
37
- const callback = adapterAttrs[callbackAttr];
38
- return [
39
- eventName,
40
- typeof callback === "function" ? (event) => callback(event.detail) : void 0
41
- ];
42
- }
43
- );
44
- for (const [eventName, handler] of handlers) {
45
- if (handler) element.addEventListener(eventName, handler);
46
- }
47
- onCleanup(() => {
48
- for (const [eventName, handler] of handlers) {
49
- if (handler) element.removeEventListener(eventName, handler);
50
- }
51
- });
52
- });
53
- return () => {
54
- const forwardedAttrs = Object.fromEntries(
55
- Object.entries(attrs).filter(([name]) => !callbackAttrs.has(name))
56
- );
57
- const children = Object.entries(slots).flatMap(([slotName, slotFn]) => {
58
- if (!slotFn) return [];
59
- return slotFn().map((node) => {
60
- if (slotName === "default") return node;
61
- if (isVNode(node)) return cloneVNode(node, { slot: slotName });
62
- return h("span", { slot: slotName }, node);
63
- });
64
- });
65
- return h(
66
- tagName,
67
- {
68
- "data-allow-mismatch": forwardedAttrs["data-allow-mismatch"] ?? "class",
69
- ...forwardedAttrs,
70
- ref: (value) => {
71
- elementRef.value = toHTMLElement(value);
72
- }
73
- },
74
- children.length > 0 ? children : void 0
75
- );
76
- };
77
- }
78
- });
79
- }
80
-
81
- export {
82
- toHTMLElement,
83
- createAdapterComponent
84
- };