Skip to content

Instantly share code, notes, and snippets.

@jraedisch
Created November 4, 2023 11:15
Show Gist options
  • Save jraedisch/8d781ce0a4eb3e637416a73eaf4d69ea to your computer and use it in GitHub Desktop.
Save jraedisch/8d781ce0a4eb3e637416a73eaf4d69ea to your computer and use it in GitHub Desktop.
Extract Web Component properties from fallback content
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Component Example</title>
<script type="module">
import { html, css, svg, LitElement } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js';
class Transformer extends LitElement {
static properties = {
_titles: { type: Array }
}
constructor () {
super()
const items = Array.from(this.querySelectorAll('li'))
this._titles = items.map((item) => item.innerText)
}
render() {
return html`<svg
xmlns="http://www.w3.org/2000/svg"
height="300"
width="400"
viewBox="0 0 400 300"
fill="#000000"
>
${this._titles.map((title,i) => svg`
<text x=10 y=${20*(i+1)}>${title}</text>
`)}
</svg>`
}
}
window.customElements.define('my-transformer', Transformer)
</script>
</head>
<body>
<my-transformer>
<ol>
<li>one</li>
<li>two</li>
<li>three</li>
</ol>
</my-transformer>
</body>
</html>
@jraedisch
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment