GithubHelp home page GithubHelp logo

sveltejs / svelte-custom-elements Goto Github PK

View Code? Open in Web Editor NEW
49.0 10.0 7.0 133 KB

Turn Svelte components into web components

License: MIT License

JavaScript 100.00%
svelte webcomponents customelements

svelte-custom-elements's Introduction

This project is deprecated โ€” Svelte can compile directly to custom elements as of version 1.37 (see sveltejs/svelte#797 for more info)


svelte-custom-elements

Register Svelte components as custom elements.

Installation and usage

First, you need to be familiar with Svelte. Read the guide if you haven't already, then come back here!

Install svelte-custom-elements to your project...

npm install -S svelte-custom-elements

...then use it in your app like so:

import Counter from './Counter.html';
import { register } from 'svelte-custom-elements';

register( 'my-component', Counter, [ 'value' ] );

document.body.innerHTML = '<my-component value="42"></my-component>';

The register function takes three arguments:

  1. The tag name you wish to use
  2. A Svelte component constructor
  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.

Demo

svelte-custom-elements demo

License

MIT

svelte-custom-elements's People

Contributors

gary-mycase avatar rich-harris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

svelte-custom-elements's Issues

Browser-specific error?

First, off I will declare the standard disclaimer - that I am new to Svelte, so this could very well be an error due to the way I am implementing.

I successfully installed svelte-custom-elements and added your example element to the svelte example code that I had downloaded prior (the one with the list, the counter, and the hello world). Anyway, when I add the new component, everything compiles cleanly, and it works in Chrome, but in Safari and Firefox on the Mac, I get:

ReferenceError: Can't find variable: customElements

This happens, of course, because customElements is undefined when this line is executed:

customElements.define( tagName, SvelteElement );

I am guessing this is my error, but am posting just in case... The main.js file is simply:

import CatList from './js/CatList';
import Counter from './js/Counter';
import HelloWorld from './js/HelloWorld';
import GeneCounter from './components/MyCounter.html';
import { register } from '../../node_modules/svelte-custom-elements/dist/svelte-custom-elements.es';

register('my-counter', MyCounter, ['value']);

Like I said, it compiles, but there is a runtime error.

By the way, thanks for all your hard work!

Embedded styles do not work?

Great library, but it looks like embedded component styles are not working.
Adding this to Counter.html has no effect on Chrome or Safari:

<style>
  button {
    color: red;
  }
</style>

If it helps, I was using another approach that uses the older(?) document.registerElement(...) and it does pick up the styles

class SvelteElement extends HTMLElement {
...
}

return document.registerElement(tagName, SvelteElement);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.