@coherent.js/web-components 1.0.0-beta.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Coherent.js Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # @coherent.js/web-components
2
+
3
+ Web Components integration for Coherent.js, enabling custom elements and Shadow DOM support.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @coherent.js/web-components
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Define Custom Elements
14
+
15
+ ```js
16
+ import { defineComponent } from '@coherent.js/web-components';
17
+
18
+ const ButtonComponent = {
19
+ button: {
20
+ className: 'custom-button',
21
+ text: 'Click me',
22
+ onclick: 'console.log("Clicked!")'
23
+ }
24
+ };
25
+
26
+ defineComponent('coherent-button', ButtonComponent);
27
+ ```
28
+
29
+ ### Shadow DOM Components
30
+
31
+ ```js
32
+ import { defineComponent } from '@coherent.js/web-components';
33
+
34
+ defineComponent('coherent-card', {
35
+ div: {
36
+ className: 'card',
37
+ children: [
38
+ { h2: { text: 'Card Title' } },
39
+ { p: { text: 'Card content goes here' } }
40
+ ]
41
+ }
42
+ }, {
43
+ shadowDOM: true,
44
+ styles: '.card { padding: 1rem; border: 1px solid #ddd; }'
45
+ });
46
+ ```
47
+
48
+ ## Features
49
+
50
+ - Custom element registration
51
+ - Shadow DOM encapsulation
52
+ - Style isolation
53
+ - Coherent.js component integration
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@coherent.js/web-components",
3
+ "version": "1.0.0-beta.2",
4
+ "description": "Web Components integration for Coherent.js",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "keywords": [
11
+ "web-components",
12
+ "custom-elements",
13
+ "coherent",
14
+ "ssr"
15
+ ],
16
+ "author": "Coherent.js Team",
17
+ "license": "MIT",
18
+ "dependencies": {
19
+ "@coherent.js/core": "1.0.0-beta.2"
20
+ },
21
+ "devDependencies": {
22
+ "vitest": "^3.2.4"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/Tomdrouv1/coherent.js.git"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "types": "./types/index.d.ts",
32
+ "files": [
33
+ "LICENSE",
34
+ "README.md",
35
+ "types/"
36
+ ],
37
+ "scripts": {
38
+ "build": "node build.mjs",
39
+ "test": "vitest"
40
+ }
41
+ }
package/src/index.js ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Coherent.js Web Components Integration
3
+ * Provides custom element and web component utilities
4
+ */
5
+
6
+ import { render } from '@coherent.js/core';
7
+
8
+ /**
9
+ * Define a Coherent.js component as a custom element
10
+ */
11
+ export function defineComponent(name, component, options = {}) {
12
+ if (typeof window === 'undefined') {
13
+ // Server-side: just return a placeholder
14
+ return { name, component, options };
15
+ }
16
+
17
+ if (!window.customElements) {
18
+ throw new Error('Custom Elements API not supported');
19
+ }
20
+
21
+ class CoherentElement extends HTMLElement {
22
+ constructor() {
23
+ super();
24
+ this.component = component;
25
+ this.options = options;
26
+ }
27
+
28
+ connectedCallback() {
29
+ this.render();
30
+ }
31
+
32
+ render() {
33
+ const html = render(this.component);
34
+ if (this.options.shadow) {
35
+ const shadow = this.attachShadow({ mode: 'open' });
36
+ shadow.innerHTML = html;
37
+ } else {
38
+ this.innerHTML = html;
39
+ }
40
+ }
41
+ }
42
+
43
+ window.customElements.define(name, CoherentElement);
44
+ return CoherentElement;
45
+ }
46
+
47
+ /**
48
+ * Integration utilities
49
+ */
50
+ export function integrateWithWebComponents(_runtime) {
51
+ return {
52
+ defineComponent: (name, component, options) => defineComponent(name, component, options)
53
+ };
54
+ }
55
+
56
+ export function defineCoherentElement(name, component, options = {}) {
57
+ return defineComponent(name, component, options);
58
+ }
59
+
60
+ export { defineComponent as default };
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Coherent.js Web Components TypeScript Definitions
3
+ * @module @coherent.js/web-components
4
+ */
5
+
6
+ // ===== Web Component Integration Types =====
7
+
8
+ export interface ComponentOptions {
9
+ shadow?: boolean;
10
+ mode?: 'open' | 'closed';
11
+ delegatesFocus?: boolean;
12
+ observedAttributes?: string[];
13
+ props?: Record<string, {
14
+ type?: StringConstructor | NumberConstructor | BooleanConstructor | ObjectConstructor | ArrayConstructor;
15
+ default?: any;
16
+ required?: boolean;
17
+ validator?: (value: any) => boolean;
18
+ }>;
19
+ lifecycle?: {
20
+ connected?: () => void;
21
+ disconnected?: () => void;
22
+ adopted?: () => void;
23
+ attributeChanged?: (name: string, oldValue: string | null, newValue: string | null) => void;
24
+ };
25
+ }
26
+
27
+ export interface CoherentElementConstructor {
28
+ new (): CoherentElement;
29
+ prototype: CoherentElement;
30
+ }
31
+
32
+ export interface CoherentElement extends HTMLElement {
33
+ component: any;
34
+ options: ComponentOptions;
35
+ render(): void;
36
+ update(props?: Record<string, any>): void;
37
+ hydrate(data?: any): void;
38
+ }
39
+
40
+ /**
41
+ * Define a Coherent.js component as a custom element
42
+ *
43
+ * @param name - Custom element tag name (must contain a hyphen)
44
+ * @param component - Coherent.js component object or function
45
+ * @param options - Configuration options for the custom element
46
+ * @returns The custom element constructor
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const MyButton = { button: { text: 'Click me' } };
51
+ * defineComponent('my-button', MyButton, { shadow: true });
52
+ * ```
53
+ */
54
+ export function defineComponent(
55
+ name: string,
56
+ component: any | ((props: any) => any),
57
+ options?: ComponentOptions
58
+ ): CoherentElementConstructor;
59
+
60
+ /**
61
+ * Integration utilities for web components runtime
62
+ */
63
+ export function integrateWithWebComponents(runtime: any): void;
64
+
65
+ /**
66
+ * Register multiple components at once
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * registerComponents({
71
+ * 'my-button': MyButton,
72
+ * 'my-card': MyCard
73
+ * });
74
+ * ```
75
+ */
76
+ export function registerComponents(components: Record<string, any>, options?: ComponentOptions): void;
77
+
78
+ /**
79
+ * Check if a custom element is defined
80
+ */
81
+ export function isComponentDefined(name: string): boolean;
82
+
83
+ /**
84
+ * Wait for a custom element to be defined
85
+ */
86
+ export function whenDefined(name: string): Promise<CoherentElementConstructor>;
87
+
88
+ /**
89
+ * Upgrade an element to a custom element
90
+ */
91
+ export function upgradeElement(element: Element): void;
92
+
93
+ /**
94
+ * Create a Coherent.js component from a custom element
95
+ */
96
+ export function fromCustomElement(element: HTMLElement): any;
97
+
98
+ /**
99
+ * Helper for creating reactive properties on custom elements
100
+ */
101
+ export interface PropertyDeclaration {
102
+ type?: StringConstructor | NumberConstructor | BooleanConstructor | ObjectConstructor | ArrayConstructor;
103
+ attribute?: boolean | string;
104
+ reflect?: boolean;
105
+ converter?: (value: string) => any;
106
+ default?: any;
107
+ }
108
+
109
+ export function createProperty(declaration: PropertyDeclaration): PropertyDecorator;
110
+
111
+ /**
112
+ * Decorator for defining properties on custom elements
113
+ * @experimental
114
+ */
115
+ export function property(declaration?: PropertyDeclaration): PropertyDecorator;
116
+
117
+ /**
118
+ * Decorator for defining custom element tag name
119
+ * @experimental
120
+ */
121
+ export function customElement(tagName: string): ClassDecorator;
122
+
123
+ // ===== Event System Types =====
124
+
125
+ export interface EventOptions {
126
+ bubbles?: boolean;
127
+ composed?: boolean;
128
+ cancelable?: boolean;
129
+ detail?: any;
130
+ }
131
+
132
+ export function createEvent(type: string, options?: EventOptions): CustomEvent;
133
+ export function dispatchCustomEvent(element: Element, type: string, options?: EventOptions): boolean;
134
+
135
+ // ===== Slot Utilities Types =====
136
+
137
+ export interface SlotChangeEvent extends Event {
138
+ target: HTMLSlotElement;
139
+ }
140
+
141
+ export function getSlotContent(element: Element, slotName?: string): Node[];
142
+ export function hasSlotContent(element: Element, slotName?: string): boolean;
143
+ export function onSlotChange(element: Element, callback: (event: SlotChangeEvent) => void, slotName?: string): () => void;
144
+
145
+ // ===== Shadow DOM Utilities Types =====
146
+
147
+ export interface ShadowRootInit {
148
+ mode: 'open' | 'closed';
149
+ delegatesFocus?: boolean;
150
+ slotAssignment?: 'manual' | 'named';
151
+ }
152
+
153
+ export function createShadowRoot(element: Element, init: ShadowRootInit): ShadowRoot;
154
+ export function adoptStyles(shadowRoot: ShadowRoot, styles: string | CSSStyleSheet[]): void;
155
+ export function getShadowRoot(element: Element): ShadowRoot | null;
156
+
157
+ // ===== Template Utilities Types =====
158
+
159
+ export function createTemplate(html: string): HTMLTemplateElement;
160
+ export function cloneTemplate(template: HTMLTemplateElement): DocumentFragment;
161
+ export function renderToTemplate(component: any): HTMLTemplateElement;
162
+
163
+ // ===== Lifecycle Hooks =====
164
+
165
+ export interface LifecycleHooks {
166
+ onConnected?: () => void;
167
+ onDisconnected?: () => void;
168
+ onAdopted?: () => void;
169
+ onAttributeChanged?: (name: string, oldValue: string | null, newValue: string | null) => void;
170
+ onPropertyChanged?: (name: string, oldValue: any, newValue: any) => void;
171
+ }
172
+
173
+ export function createLifecycleManager(hooks: LifecycleHooks): {
174
+ connected(): void;
175
+ disconnected(): void;
176
+ adopted(): void;
177
+ attributeChanged(name: string, oldValue: string | null, newValue: string | null): void;
178
+ };