@solidjs/element 2.0.0-experimental.0

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) 2016-2023 Ryan Carniato
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,102 @@
1
+ # Solid Element
2
+ [![Build Status](https://github.com/solidjs/solid/workflows/Solid%20CI/badge.svg)](https://github.com/solidjs/solid/actions/workflows/main-ci.yml)
3
+ [![NPM Version](https://img.shields.io/npm/v/solid-element.svg?style=flat)](https://www.npmjs.com/package/solid-element)
4
+ ![](https://img.shields.io/librariesio/release/npm/solid-element)
5
+ ![](https://img.shields.io/npm/dm/solid-element.svg?style=flat)
6
+
7
+ This library extends [Solid](https://github.com/solidjs/solid) by adding Custom Web Components and extensions to manage modular behaviors and composition. It uses [Component Register](https://github.com/ryansolid/component-register) to create Web Components and its composed mixin pattern to construct modular re-usable behaviors. This allows your code to available as simple HTML elements for library interop and to leverage Shadow DOM style isolation. Solid already supports binding to Web Components so this fills the gap allowing full modular applications to be built out of nested Web Components. Component Register makes use of the V1 Standards and on top of being compatible with the common webcomponent.js polyfills, has a solution for Polyfilling Shadow DOM CSS using the ShadyCSS Parser from Polymer in a generic framework agnostic way (unlike the ShadyCSS package).
8
+
9
+ ## Example
10
+
11
+ [See here](https://webcomponents.dev/edit/yGRb00wV4AMncPupRmA9) for an example of a webcomponent created by `solid-element`.
12
+
13
+ ## Installation
14
+
15
+ ```sh
16
+ npm i solid-element solid-js babel-preset-solid
17
+ ```
18
+
19
+ ## Custom Elements
20
+
21
+ The simplest way to create a Web Component is to use the `customElement` method.
22
+
23
+ The arguments of `customElement` are:
24
+ 1) custom element tag (e.g. `'my-component'`)
25
+ 2) (optional) Default prop values (e.g. `{someProp: 'one', otherProp: 'two'}`). Props without default values will be ignored by the customElement.
26
+ 3) the Solid template function. The arguments of this function are state wrapped props as the first argument, and the underlying element as the 2nd (e.g. `(props, { element }) => { solid code here
27
+ }`)
28
+
29
+ ```jsx
30
+ import { customElement } from 'solid-element';
31
+
32
+ customElement('my-component', {someProp: 'one', otherProp: 'two'}, (props, { element }) => {
33
+ // ... Solid code
34
+ })
35
+ ```
36
+
37
+ Props get assigned as element properties and hyphenated attributes. This exposes the component that can be used in HTML/JSX as:
38
+ ```html
39
+ <my-component some-prop="some value" other-prop="some value"></my-component>
40
+ ```
41
+
42
+ This is all you need to get started with Solid Element.
43
+
44
+ A shadow DOM is used by default for style isolation. If you want to disable the shadow DOM, you can do it with `noShadowDOM()` like this:
45
+
46
+ ```jsx
47
+ import { customElement, noShadowDOM } from 'solid-element';
48
+
49
+ customElement('my-component', {someProp: 'one', otherProp: 'two'}, (props, { element }) => {
50
+ noShadowDOM();
51
+ // ... Solid code
52
+ })
53
+ ```
54
+
55
+ ## Examples
56
+
57
+ [Web Component Todos](https://wc-todo.firebaseapp.com/) Simple Todos Comparison
58
+
59
+ ## Hot Module Replacement (new)
60
+
61
+ Solid Element exposes Component Register's Hot Module Replacement solution for Webpack and Parcel. It does not preserve state, swapping Components that are changed and their descendants. This approach is simple but predictable. It works by indicating the component to be Hot Replaced with the `hot` method in your file.
62
+
63
+ ```js
64
+ import { customElement, hot } from 'solid-element';
65
+
66
+ hot(module, 'my-component');
67
+ ```
68
+ This is a new feature that is actively seeking feedback. Read more: [Component Register](https://github.com/ryansolid/component-register#hot-module-replacement-new)
69
+
70
+ There is also a webpack loader that handles adding this automatically. Check out [Component Register Loader](https://github.com/ryansolid/component-register-loader)
71
+
72
+ ## withSolid
73
+
74
+ Under the hood the customElement method is using Component Register's mixins to create our Custom Element. So this library also provides the way to do so directly if you wish to mixin your own functionality. It all starts by using the register HOC which upgrades your class or method to a WebComponent. It is always the start of the chain.
75
+
76
+ ```jsx
77
+ import { register } from 'component-register';
78
+
79
+ /*
80
+ register(tag, defaultProps)
81
+ */
82
+ register('my-component', {someProp: 'one', otherProp: 'two'})((props, options) =>
83
+ // ....
84
+ )
85
+ ```
86
+
87
+ Component Register exposes a convenient compose method (a reduce right) that makes it easier compose multiple mixins. From there we can use withSolid mixin to basically produce the Component method above. However, now you are able to add more HOC mixins in the middle to add additional behavior in your components.
88
+
89
+ ```jsx
90
+ import { register, compose } from 'component-register';
91
+ import { withSolid } from 'solid-element';
92
+
93
+ /*
94
+ withSolid
95
+ */
96
+ compose(
97
+ register('my-component'),
98
+ withSolid
99
+ )((props, options) =>
100
+ // ....
101
+ )
102
+ ```
@@ -0,0 +1,14 @@
1
+ import { ComponentType as mComponentType, PropsDefinitionInput } from "component-register";
2
+ export { hot, getCurrentElement, noShadowDOM } from "component-register";
3
+ export type ComponentType<T> = mComponentType<T>;
4
+ declare function withSolid<T extends object>(ComponentType: ComponentType<T>): ComponentType<T>;
5
+ declare function customElement<T extends object>(
6
+ tag: string,
7
+ ComponentType: ComponentType<T>
8
+ ): CustomElementConstructor;
9
+ declare function customElement<T extends object>(
10
+ tag: string,
11
+ props: PropsDefinitionInput<T>,
12
+ ComponentType: ComponentType<T>
13
+ ): CustomElementConstructor;
14
+ export { withSolid, customElement };
package/dist/index.js ADDED
@@ -0,0 +1,52 @@
1
+ import { register } from "component-register";
2
+ export { hot, getCurrentElement, noShadowDOM } from "component-register";
3
+ import { createRoot, createSignal, runWithOwner } from "solid-js";
4
+ import { insert } from "@solidjs/web";
5
+ function createProps(raw) {
6
+ const keys = Object.keys(raw);
7
+ const props = {};
8
+ for (let i = 0; i < keys.length; i++) {
9
+ const [get, set] = createSignal(() => raw[keys[i]]);
10
+ Object.defineProperty(props, keys[i], {
11
+ get,
12
+ set(v) {
13
+ set(() => v);
14
+ }
15
+ });
16
+ }
17
+ return props;
18
+ }
19
+ function lookupContext(el) {
20
+ if (el.assignedSlot && el.assignedSlot._$owner) return el.assignedSlot._$owner;
21
+ let next = el.parentNode;
22
+ while (next && !next._$owner && !(next.assignedSlot && next.assignedSlot._$owner))
23
+ next = next.parentNode;
24
+ return next && next.assignedSlot ? next.assignedSlot._$owner : el._$owner;
25
+ }
26
+ function withSolid(ComponentType) {
27
+ return (rawProps, options) => {
28
+ const { element } = options;
29
+ const owner = lookupContext(element);
30
+ function createComponent() {
31
+ return createRoot(dispose => {
32
+ const props = createProps(rawProps);
33
+ element.addPropertyChangedCallback((key, val) => (props[key] = val));
34
+ element.addReleaseCallback(() => {
35
+ element.renderRoot.textContent = "";
36
+ dispose();
37
+ });
38
+ const comp = ComponentType(props, options);
39
+ return insert(element.renderRoot, comp);
40
+ });
41
+ }
42
+ return owner ? runWithOwner(owner, createComponent) : createComponent();
43
+ };
44
+ }
45
+ function customElement(tag, props, ComponentType) {
46
+ if (arguments.length === 2) {
47
+ ComponentType = props;
48
+ props = {};
49
+ }
50
+ return register(tag, props)(withSolid(ComponentType));
51
+ }
52
+ export { withSolid, customElement };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@solidjs/element",
3
+ "description": "Webcomponents wrapper for Solid",
4
+ "author": "Ryan Carniato",
5
+ "license": "MIT",
6
+ "version": "2.0.0-experimental.0",
7
+ "homepage": "https://github.com/solidjs/solid/blob/main/packages/solid-element#readme",
8
+ "type": "module",
9
+ "main": "dist/index.js",
10
+ "module": "dist/index.js",
11
+ "types": "dist/index.d.ts",
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "sideEffects": false,
19
+ "dependencies": {
20
+ "component-register": "^0.8.7"
21
+ },
22
+ "peerDependencies": {
23
+ "solid-js": "^2.0.0-experimental.0",
24
+ "@solidjs/web": "^2.0.0-experimental.0"
25
+ },
26
+ "devDependencies": {
27
+ "solid-js": "2.0.0-experimental.0",
28
+ "@solidjs/web": "2.0.0-experimental.0"
29
+ },
30
+ "scripts": {
31
+ "clean": "rimraf dist/",
32
+ "build": "pnpm run clean && tsc"
33
+ }
34
+ }