atoms-element v5.0.0
git clone https://git.pyrossh.dev/atoms-element
A simple web component library for defining your custom elements. It works on both client and server.
examples/elements/app-total.js
| e955350 | 1 | import { createElement, css, html } from '../../index.js'; |
| 24398b1 | 2 | |
| e955350 | 3 | // No shared store: Total.watch below declares "give me the count attribute |
| e955350 | 4 | // off every app-counter on the page", and createElement resolves that into |
| e955350 | 5 | // the counts array passed in here — server-side by parsing the already- |
| e955350 | 6 | // rendered preceding markup, client-side via a MutationObserver it sets up |
| e955350 | 7 | // and tears down on its own. |
| e955350 | 8 | const Total = ({ counts }) => { |
| ba74771 | 9 | const total = counts.reduce((sum, v) => sum + (Number(v) || 0), 0); |
| 24398b1 | 10 | return html` |
| e264357 | 11 | <div> |
| e264357 | 12 | <h1>Total of 2 Counters: ${total}</h1> |
| 24398b1 | 13 | </div> |
| 24398b1 | 14 | `; |
| 24398b1 | 15 | }; |
| 24398b1 | 16 | |
| e955350 | 17 | Total.watch = { counts: { selector: 'app-counter', attribute: 'count' } }; |
| e955350 | 18 | |
| e264357 | 19 | Total.styles = css` |
| e264357 | 20 | :scope { |
| e264357 | 21 | display: block; |
| e264357 | 22 | margin: 5rem; |
| e264357 | 23 | } |
| e264357 | 24 | h1 { |
| e264357 | 25 | font-size: 1.25rem; |
| e264357 | 26 | font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; |
| e264357 | 27 | } |
| e264357 | 28 | `; |
| e264357 | 29 | |
| 24398b1 | 30 | export default createElement(import.meta, Total); |