Ponys provides three ways to promote templates or strings to browser-native custom elements:
1. Automatic detection of <template> elements
Use Ponys.defineAll() to automatically promote any <template> element in your HTML that has a name attribute to a custom element.
<script type="module">
import Ponys from './ponys.js';
Ponys.defineAll();
</script>
<template name="hello-world">
Hello, <slot>world</slot>!
</template>
2. Define via name and string content
Use Ponys.define(name, content) to define an element using a string for its inner HTML.
import Ponys from './ponys.js';
Ponys.define('hello-world', 'Hello, <slot>world</slot>!');
3. Import from a separate file
You can import component content from an external file using Ponys.import(name, url) in JavaScript, or by using the src attribute on a named <template> in HTML.
Via JavaScript:
import Ponys from './ponys.js';
Ponys.import(
'hello-world',
import.meta.resolve('./components/hello-world.html')
);
Via HTML:
<script type="module">
import Ponys from './ponys.js';
Ponys.defineAll();
</script>
<template name="hello-world" src="./components/hello-world.html"></template>
import Ponys from './ponys.js';
// Method 1: Define by string
Ponys.define('hello-world', 'Hello, <slot>world</slot>!');
// Method 2: Import from file
Ponys.import(
'hello-world',
import.meta.resolve('./components/hello-world.html')
);