- v1.9.3Latest
- v1.9.2
- v1.9.0
- v1.8.23
- v1.8.22
- v1.8.21
- v1.8.20
- v1.8.19
- v1.8.18
- v1.8.17
- 1.8.18
- 1.8.17
- v1.8.16
- v1.8.15
- v1.8.14
- v1.8.13
- v1.8.12
- v1.8.11
- v1.8.10
- v1.8.9
- v1.8.8
- v1.8.7
- v1.8.6
- v1.8.5
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.1
- v1.7.12
- v1.8.0
- v1.7.11
- v1.7.10
- v1.7.9
- v1.7.8
- v1.7.7
- v1.7.6
- v1.7.5
- v1.7.4
- v1.7.3
- v1.7.2
- v1.7.1
- v1.6.15
- v1.7.0
- v1.7.0-beta.5
- v1.6.16
- v1.7.0-beta.4
- v1.7.0-beta.2
- v1.7.0-beta.1
- v1.7.0-beta.0
- v1.6.14
- v1.6.13
- v1.6.12
- v1.6.11
- v1.6.10
- v1.6.9
- v1.6.8
- v1.6.7
- v1.6.6
- v1.6.5
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.5.0-beta.6
- v1.5.0-beta.7
- v1.5.0-beta.5
- v1.5.0-beta.4
- v1.5.0-beta.3
- v1.5.0-beta.1
- v1.5.0-beta.2
- v1.4.8
- v1.5.0-beta.0
- v1.4.7
- v1.4.6
- v1.4.5
Solid Element
This library extends Solid by adding Custom Web Components and extensions to manage modular behaviors and composition. It uses 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).
Example
See here for an example of a webcomponent created by solid-element
.
Installation
npm i solid-element solid-js babel-preset-solid
Custom Elements
The simplest way to create a Web Component is to use the customElement
method.
The arguments of customElement
are:
- custom element tag (e.g.
'my-component'
) - (optional) Default prop values (e.g.
{someProp: 'one', otherProp: 'two'}
). Props without default values will be ignored by the customElement. - 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 }
)
import { customElement } from 'solid-element';
customElement('my-component', {someProp: 'one', otherProp: 'two'}, (props, { element }) => {
// ... Solid code
})
Props get assigned as element properties and hyphenated attributes. This exposes the component that can be used in HTML/JSX as:
<my-component some-prop="some value" other-prop="some value"></my-component>
This is all you need to get started with Solid Element.
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:
import { customElement, noShadowDOM } from 'solid-element';
customElement('my-component', {someProp: 'one', otherProp: 'two'}, (props, { element }) => {
noShadowDOM();
// ... Solid code
})
Examples
Web Component Todos Simple Todos Comparison
Hot Module Replacement (new)
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.
import { customElement, hot } from 'solid-element';
hot(module, 'my-component');
This is a new feature that is actively seeking feedback. Read more: Component Register
There is also a webpack loader that handles adding this automatically. Check out Component Register Loader
withSolid
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.
import { register } from 'component-register';
/*
register(tag, defaultProps)
*/
register('my-component', {someProp: 'one', otherProp: 'two'})((props, options) =>
// ....
)
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.
import { register, compose } from 'component-register';
import { withSolid } from 'solid-element';
/*
withSolid
*/
compose(
register('my-component'),
withSolid
)((props, options) =>
// ....
)