├── .eslintrc.json
├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── README.md
├── demo
├── bundle.js
├── demo.gif
├── index.html
├── package.json
├── rollup.config.js
└── src
│ ├── Counter.html
│ └── main.js
├── package.json
├── rollup.config.js
├── src
└── index.js
└── test
└── index.js
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "root": true,
3 | "rules": {
4 | "indent": [ 2, "tab", { "SwitchCase": 1 } ],
5 | "semi": [ 2, "always" ],
6 | "keyword-spacing": [ 2, { "before": true, "after": true } ],
7 | "space-before-blocks": [ 2, "always" ],
8 | "space-before-function-paren": [ 2, "always" ],
9 | "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
10 | "no-cond-assign": 0,
11 | "no-unused-vars": 2,
12 | "object-shorthand": [ 2, "always" ],
13 | "no-const-assign": 2,
14 | "no-class-assign": 2,
15 | "no-this-before-super": 2,
16 | "no-var": 2,
17 | "no-unreachable": 2,
18 | "valid-typeof": 2,
19 | "quote-props": [ 2, "as-needed" ],
20 | "one-var": [ 2, "never" ],
21 | "prefer-arrow-callback": 2,
22 | "prefer-const": [ 2, { "destructuring": "all" } ],
23 | "arrow-spacing": 2,
24 | "no-inner-declarations": 0
25 | },
26 | "env": {
27 | "es6": true,
28 | "browser": true,
29 | "node": true,
30 | "mocha": true
31 | },
32 | "extends": [
33 | "eslint:recommended"
34 | ],
35 | "parserOptions": {
36 | "ecmaVersion": 6,
37 | "sourceType": "module"
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | dist
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # svelte-custom-elements changelog
2 |
3 | ## 1.0.2
4 |
5 | * Use `mode: 'open'` so that styles work ([#1](https://github.com/sveltejs/svelte-custom-elements/issues/1))
6 |
7 | ## 1.0.1
8 |
9 | * Include `files` in package.json (I forget this every time...)
10 |
11 | ## 1.0.0
12 |
13 | * First release
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2016 [these people](https://github.com/sveltejs/svelte-custom-elements/graphs/contributors)
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # This project is deprecated — Svelte can compile directly to custom elements as of version 1.37 (see [sveltejs/svelte#797](https://github.com/sveltejs/svelte/issues/797) for more info)
2 |
3 | ---
4 |
5 | # svelte-custom-elements
6 |
7 | Register Svelte components as custom elements.
8 |
9 |
10 | ## Installation and usage
11 |
12 | First, you need to be familiar with [Svelte](https://svelte.technology). Read the [guide](https://svelte.technology/guide) if you haven't already, then come back here!
13 |
14 | Install svelte-custom-elements to your project...
15 |
16 | ```bash
17 | npm install -S svelte-custom-elements
18 | ```
19 |
20 | ...then use it in your app like so:
21 |
22 | ```js
23 | import Counter from './Counter.html';
24 | import { register } from 'svelte-custom-elements';
25 |
26 | register( 'my-component', Counter, [ 'value' ] );
27 |
28 | document.body.innerHTML = '';
29 | ```
30 |
31 | The `register` function takes three arguments:
32 |
33 | 1. The tag name you wish to use
34 | 2. A Svelte component constructor
35 | 3. An optional list of 'observed attributes'. Any properties that you want to get or set (i.e. `component.thing = 'foo'`) must be included in this list.
36 |
37 |
38 | ## Demo
39 |
40 | ### [Link](https://svelte-custom-elements.surge.sh/) / [Source](https://github.com/sveltejs/svelte-custom-elements/tree/master/demo)
41 |
42 | 
43 |
44 |
45 | ## License
46 |
47 | MIT
48 |
--------------------------------------------------------------------------------
/demo/bundle.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | 'use strict';
3 |
4 | function appendNode ( node, target ) {
5 | target.appendChild( node );
6 | }
7 |
8 | function insertNode ( node, target, anchor ) {
9 | target.insertBefore( node, anchor );
10 | }
11 |
12 | function detachNode ( node ) {
13 | node.parentNode.removeChild( node );
14 | }
15 |
16 | function createElement ( name ) {
17 | return document.createElement( name );
18 | }
19 |
20 | function createText ( data ) {
21 | return document.createTextNode( data );
22 | }
23 |
24 | function addEventListener ( node, event, handler ) {
25 | node.addEventListener ( event, handler, false );
26 | }
27 |
28 | function removeEventListener ( node, event, handler ) {
29 | node.removeEventListener ( event, handler, false );
30 | }
31 |
32 | function get ( key ) {
33 | return key ? this._state[ key ] : this._state;
34 | }
35 |
36 | function fire ( eventName, data ) {
37 | var handlers = eventName in this._handlers && this._handlers[ eventName ].slice();
38 | if ( !handlers ) return;
39 |
40 | for ( var i = 0; i < handlers.length; i += 1 ) {
41 | handlers[i].call( this, data );
42 | }
43 | }
44 |
45 | function observe ( key, callback, options ) {
46 | var group = ( options && options.defer ) ? this._observers.pre : this._observers.post;
47 |
48 | ( group[ key ] || ( group[ key ] = [] ) ).push( callback );
49 |
50 | if ( !options || options.init !== false ) {
51 | callback.__calling = true;
52 | callback.call( this, this._state[ key ] );
53 | callback.__calling = false;
54 | }
55 |
56 | return {
57 | cancel: function () {
58 | var index = group[ key ].indexOf( callback );
59 | if ( ~index ) group[ key ].splice( index, 1 );
60 | }
61 | };
62 | }
63 |
64 | function on ( eventName, handler ) {
65 | var handlers = this._handlers[ eventName ] || ( this._handlers[ eventName ] = [] );
66 | handlers.push( handler );
67 |
68 | return {
69 | cancel: function () {
70 | var index = handlers.indexOf( handler );
71 | if ( ~index ) handlers.splice( index, 1 );
72 | }
73 | };
74 | }
75 |
76 | function set ( newState ) {
77 | this._set( newState );
78 | ( this._root || this )._flush();
79 | }
80 |
81 | function _flush () {
82 | if ( !this._renderHooks ) return;
83 |
84 | while ( this._renderHooks.length ) {
85 | var hook = this._renderHooks.pop();
86 | hook.fn.call( hook.context );
87 | }
88 | }
89 |
90 | function dispatchObservers ( component, group, newState, oldState ) {
91 | for ( var key in group ) {
92 | if ( !( key in newState ) ) continue;
93 |
94 | var newValue = newState[ key ];
95 | var oldValue = oldState[ key ];
96 |
97 | if ( newValue === oldValue && typeof newValue !== 'object' ) continue;
98 |
99 | var callbacks = group[ key ];
100 | if ( !callbacks ) continue;
101 |
102 | for ( var i = 0; i < callbacks.length; i += 1 ) {
103 | var callback = callbacks[i];
104 | if ( callback.__calling ) continue;
105 |
106 | callback.__calling = true;
107 | callback.call( component, newValue, oldValue );
108 | callback.__calling = false;
109 | }
110 | }
111 | }
112 |
113 | function renderMainFragment ( root, component ) {
114 | var div = createElement( 'div' );
115 |
116 | var div1 = createElement( 'div' );
117 |
118 | appendNode( div1, div );
119 | appendNode( createText( "Count: " ), div1 );
120 | var last_text1 = root.value;
121 | var text1 = createText( last_text1 );
122 | appendNode( text1, div1 );
123 | appendNode( createText( "\n\t" ), div );
124 |
125 | var button = createElement( 'button' );
126 |
127 | function clickHandler ( event ) {
128 | var root = this.__svelte.root;
129 |
130 | component.set({ value: root.value + 1 });
131 | }
132 |
133 | addEventListener( button, 'click', clickHandler );
134 |
135 | button.__svelte = {
136 | root: root
137 | };
138 |
139 | appendNode( button, div );
140 | appendNode( createText( "+" ), button );
141 | appendNode( createText( "\n\t" ), div );
142 |
143 | var button1 = createElement( 'button' );
144 |
145 | function clickHandler1 ( event ) {
146 | var root = this.__svelte.root;
147 |
148 | component.set({ value: root.value - 1 });
149 | }
150 |
151 | addEventListener( button1, 'click', clickHandler1 );
152 |
153 | button1.__svelte = {
154 | root: root
155 | };
156 |
157 | appendNode( button1, div );
158 | appendNode( createText( "-" ), button1 );
159 |
160 | return {
161 | mount: function ( target, anchor ) {
162 | insertNode( div, target, anchor );
163 | },
164 |
165 | update: function ( changed, root ) {
166 | var __tmp;
167 |
168 | if ( ( __tmp = root.value ) !== last_text1 ) {
169 | text1.data = last_text1 = __tmp;
170 | }
171 |
172 | button.__svelte.root = root;
173 |
174 | button1.__svelte.root = root;
175 | },
176 |
177 | teardown: function ( detach ) {
178 | removeEventListener( button, 'click', clickHandler );
179 | removeEventListener( button1, 'click', clickHandler1 );
180 |
181 | if ( detach ) {
182 | detachNode( div );
183 | }
184 | }
185 | };
186 | }
187 |
188 | function Counter ( options ) {
189 | options = options || {};
190 | this._state = options.data || {};
191 |
192 | this._observers = {
193 | pre: Object.create( null ),
194 | post: Object.create( null )
195 | };
196 |
197 | this._handlers = Object.create( null );
198 |
199 | this._root = options._root;
200 | this._yield = options._yield;
201 |
202 | this._torndown = false;
203 |
204 | this._fragment = renderMainFragment( this._state, this );
205 | if ( options.target ) this._fragment.mount( options.target, null );
206 | }
207 |
208 | Counter.prototype.get = get;
209 | Counter.prototype.fire = fire;
210 | Counter.prototype.observe = observe;
211 | Counter.prototype.on = on;
212 | Counter.prototype.set = set;
213 | Counter.prototype._flush = _flush;
214 |
215 | Counter.prototype._set = function _set ( newState ) {
216 | var oldState = this._state;
217 | this._state = Object.assign( {}, oldState, newState );
218 |
219 | dispatchObservers( this, this._observers.pre, newState, oldState );
220 | if ( this._fragment ) this._fragment.update( newState, this._state );
221 | dispatchObservers( this, this._observers.post, newState, oldState );
222 | };
223 |
224 | Counter.prototype.teardown = Counter.prototype.destroy = function destroy ( detach ) {
225 | this.fire( 'teardown' );
226 |
227 | this._fragment.teardown( detach !== false );
228 | this._fragment = null;
229 |
230 | this._state = {};
231 | this._torndown = true;
232 | };
233 |
234 | function register ( tagName, Component, props = [] ) {
235 | class SvelteElement extends HTMLElement {
236 | constructor () {
237 | super();
238 |
239 | this.target = this.attachShadow({ mode: 'closed' });
240 | this.data = {};
241 | }
242 |
243 | connectedCallback () {
244 | props.forEach( prop => {
245 | const value = this[ prop ];
246 | if ( value !== undefined ) {
247 | this.data[ prop ] = this[ prop ];
248 | }
249 | });
250 |
251 | this.instance = new Component({
252 | target: this.target,
253 | data: this.data
254 | });
255 |
256 | props.forEach( prop => {
257 | this.instance.observe( prop, value => {
258 | this.setAttribute( prop, value );
259 | });
260 | });
261 | }
262 |
263 | detachedCallback () {
264 | this.instance.destroy();
265 | this.instance = null;
266 | }
267 |
268 | attributeChangedCallback ( attr, oldValue, newValue ) {
269 | const value = isNaN( newValue ) ? newValue : +newValue;
270 | this.data[ attr ] = value;
271 | if ( this.instance ) this.instance.set({ [ attr ]: value });
272 | }
273 | }
274 |
275 | Object.defineProperty( SvelteElement, 'observedAttributes', {
276 | get () {
277 | return props;
278 | }
279 | });
280 |
281 | props.forEach( prop => {
282 | Object.defineProperty( SvelteElement.prototype, prop, {
283 | get () {
284 | return this.instance ? this.instance.get( prop ) : this.data[ prop ];
285 | },
286 |
287 | set ( value ) {
288 | this.data[ prop ] = value;
289 | if ( this.instance ) this.instance.set({ [ prop ]: value });
290 | }
291 | });
292 | });
293 |
294 | const result = customElements.define( tagName, SvelteElement );
295 | return SvelteElement;
296 | }
297 |
298 | register( 'my-component', Counter, [ 'value' ] );
299 |
300 | document.body.innerHTML = ``;
301 |
302 | }());
303 |
--------------------------------------------------------------------------------
/demo/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sveltejs/svelte-custom-elements/2eb4e4c5f56dbf394debb26ce3693e15b7ea634f/demo/demo.gif
--------------------------------------------------------------------------------
/demo/index.html:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/demo/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "devDependencies": {
3 | "rollup": "^0.41.4",
4 | "rollup-plugin-node-resolve": "^2.0.0",
5 | "rollup-plugin-svelte": "^1.6.0",
6 | "rollup-watch": "^3.2.2"
7 | },
8 | "scripts": {
9 | "dev": "rollup -c -w",
10 | "build": "rollup -c"
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/demo/rollup.config.js:
--------------------------------------------------------------------------------
1 | import svelte from 'rollup-plugin-svelte';
2 | import path from 'path';
3 |
4 | export default {
5 | entry: 'src/main.js',
6 | dest: 'bundle.js',
7 | format: 'iife',
8 | plugins: [
9 | // for the purposes of this demo... normally you'd
10 | // use rollup-plugin-node-resolve
11 | {
12 | resolveId ( id ) {
13 | if ( id === 'svelte-custom-elements' ) {
14 | return path.resolve( '../dist/svelte-custom-elements.es.js' );
15 | }
16 | }
17 | },
18 | svelte()
19 | ]
20 | };
--------------------------------------------------------------------------------
/demo/src/Counter.html:
--------------------------------------------------------------------------------
1 |
2 |
Count: {{value}}
3 |
4 |
5 |
--------------------------------------------------------------------------------
/demo/src/main.js:
--------------------------------------------------------------------------------
1 | import Counter from './Counter.html';
2 | import { register } from 'svelte-custom-elements';
3 |
4 | register( 'my-component', Counter, [ 'value' ] );
5 |
6 | document.body.innerHTML = ``;
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "svelte-custom-elements",
3 | "version": "1.0.2",
4 | "description": "Turn Svelte components into web components",
5 | "main": "dist/svelte-custom-elements.js",
6 | "module": "dist/svelte-custom-elements.es.js",
7 | "files": [
8 | "dist"
9 | ],
10 | "scripts": {
11 | "test": "mocha",
12 | "pretest": "npm run build",
13 | "build": "rollup -c",
14 | "lint": "eslint src",
15 | "prepublish": "npm run lint && npm test"
16 | },
17 | "repository": {
18 | "type": "git",
19 | "url": "git+https://github.com/sveltejs/svelte-custom-elements.git"
20 | },
21 | "keywords": [
22 | "svelte",
23 | "web components",
24 | "custom elements",
25 | "polymer"
26 | ],
27 | "author": "Rich Harris",
28 | "license": "MIT",
29 | "bugs": {
30 | "url": "https://github.com/sveltejs/svelte-custom-elements/issues"
31 | },
32 | "homepage": "https://github.com/sveltejs/svelte-custom-elements#readme",
33 | "devDependencies": {
34 | "eslint": "^3.16.1",
35 | "mocha": "^3.2.0",
36 | "rollup": "^0.41.4"
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/rollup.config.js:
--------------------------------------------------------------------------------
1 | const pkg = require( './package.json' );
2 |
3 | export default {
4 | entry: 'src/index.js',
5 | moduleName: 'svelteCustomElements',
6 | targets: [
7 | { dest: pkg.main, format: 'umd' },
8 | { dest: pkg.module, format: 'es' }
9 | ]
10 | };
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | export function register ( tagName, Component, props = [] ) {
2 | class SvelteElement extends HTMLElement {
3 | constructor () {
4 | super();
5 |
6 | this.target = this.attachShadow({ mode: 'open' });
7 | this.data = {};
8 | }
9 |
10 | connectedCallback () {
11 | props.forEach( prop => {
12 | const value = this[ prop ];
13 | if ( value !== undefined ) {
14 | this.data[ prop ] = this[ prop ];
15 | }
16 | });
17 |
18 | this.instance = new Component({
19 | target: this.target,
20 | data: this.data
21 | });
22 |
23 | props.forEach( prop => {
24 | this.instance.observe( prop, value => {
25 | this.setAttribute( prop, value );
26 | });
27 | });
28 | }
29 |
30 | detachedCallback () {
31 | this.instance.destroy();
32 | this.instance = null;
33 | }
34 |
35 | attributeChangedCallback ( attr, oldValue, newValue ) {
36 | const value = isNaN( newValue ) ? newValue : +newValue;
37 | this.data[ attr ] = value;
38 | if ( this.instance ) this.instance.set({ [ attr ]: value });
39 | }
40 | }
41 |
42 | Object.defineProperty( SvelteElement, 'observedAttributes', {
43 | get () {
44 | return props;
45 | }
46 | });
47 |
48 | props.forEach( prop => {
49 | Object.defineProperty( SvelteElement.prototype, prop, {
50 | get () {
51 | return this.instance ? this.instance.get( prop ) : this.data[ prop ];
52 | },
53 |
54 | set ( value ) {
55 | this.data[ prop ] = value;
56 | if ( this.instance ) this.instance.set({ [ prop ]: value });
57 | }
58 | });
59 | });
60 |
61 | customElements.define( tagName, SvelteElement );
62 | return SvelteElement;
63 | }
--------------------------------------------------------------------------------
/test/index.js:
--------------------------------------------------------------------------------
1 | console.error( 'TODO add some tests' );
--------------------------------------------------------------------------------