deno.land / x / solid_element@v1.5.6 / src / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { register, ComponentType as mComponentType, ICustomElement, FunctionComponent, ComponentOptions, PropsDefinitionInput} from "component-register";export { hot, getCurrentElement, noShadowDOM } from "component-register";export type ComponentType<T> = mComponentType<T>;import { createRoot, createSignal } from "solid-js";import { insert } from "solid-js/web";
function createProps<T>(raw: T) { const keys = Object.keys(raw) as (keyof T)[]; const props = {}; for (let i = 0; i < keys.length; i++) { const [get, set] = createSignal(raw[keys[i]]); Object.defineProperty(props, keys[i], { get, set(v) { set(() => v); } }); } return props as T;}
function lookupContext(el: ICustomElement & { _$owner?: any }) { if (el.assignedSlot && el.assignedSlot._$owner) return el.assignedSlot._$owner; let next: Element & { _$owner?: any } = el.parentNode; while ( next && !next._$owner && !(next.assignedSlot && (next.assignedSlot as Element & { _$owner?: any })._$owner) ) next = next.parentNode as Element; return next && next.assignedSlot ? (next.assignedSlot as Element & { _$owner?: any })._$owner : el._$owner;}
function withSolid<T>(ComponentType: ComponentType<T>): ComponentType<T> { return (rawProps: T, options: ComponentOptions) => { const { element } = options as { element: ICustomElement & { _$owner?: any }; }; return createRoot((dispose: Function) => { const props = createProps<T>(rawProps);
element.addPropertyChangedCallback((key: string, val: any) => (props[key as keyof T] = val)); element.addReleaseCallback(() => { element.renderRoot.textContent = ""; dispose(); });
const comp = (ComponentType as FunctionComponent<T>)(props as T, options); return insert(element.renderRoot, comp); }, lookupContext(element)); };}
function customElement<T>( tag: string, ComponentType: ComponentType<T>): (ComponentType: ComponentType<T>) => any;function customElement<T>( tag: string, props: PropsDefinitionInput<T>, ComponentType: ComponentType<T>): (ComponentType: ComponentType<T>) => any;function customElement<T>( tag: string, props: PropsDefinitionInput<T> | ComponentType<T>, ComponentType?: ComponentType<T>): (ComponentType: ComponentType<T>) => any { if (arguments.length === 2) { ComponentType = props as ComponentType<T>; props = {} as PropsDefinitionInput<T>; } return register<T>(tag, props as PropsDefinitionInput<T>)(withSolid(ComponentType!));}
export { withSolid, customElement };
solid_element

Version Info

Tagged at
a year ago