Open in a new tab

Define a counter component once, mount two instances with different props, bind their state reactively, and scope its CSS to the instances.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

You have a widget that appears several times with different settings and want validated props, scoped CSS, a signal-driven DOM and a clean unmount, without a component framework.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html and main.js next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.
  4. Keep main: true on the first template context of the page.

What it shows

  • component.define({ template, props, css, mount, update }) returning a factory; Counter({ ui, id, props }) mounts an instance.
  • Props with type, required and default; a missing required prop throws at mount.
  • self.bind (a reactiveBind controller) binding a signal to textContent; self.on(localId, 'click', fn) and self.text(localId, value).
  • Scoped CSS: & becomes the instance's scope class, injected once per blueprint.
  • The CSS reads the shared look's --ex-* values with fallbacks, so a copied component keeps its layout. For light and dark tokens of your own, see fw/dom-theme.

Code

main.js

javascriptApache-2.047 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { component } from '@awacloud/fw/dom/rendering/component.js';
import { reactiveBind } from '@awacloud/fw/dom/rendering/reactiveBind.js';
import { uiSession } from '@awacloud/fw/dom/rendering/uiSession.js';
import { template } from '@awacloud/fw/dom/rendering/template.js';
import { signal } from '@awacloud/fw/io/utils/signal.js';

fw.runtime.registerAllDeep([component, reactiveBind, uiSession, template, signal]);
const SIGNAL = fw.runtime.resolve('signal');
const COMPONENT = fw.runtime.resolve('component');
fw.runtime.resolve('template').init('counters', { to: '#app', main: true });
const ui = fw.runtime.resolve('uiSession')('counters');

/** A counter blueprint: props are validated, state is a signal bound to the DOM. */
export const Counter = COMPONENT.define({
    template: ui.parse([
        '<div id="root" class="counter">',
        '<span id="label">#{label}</span> <button id="dec">−</button>',
        ' <span id="value"></span> <button id="inc">+</button>',
        '</div>',
    ].join('')),
    props: {
        label: { type: 'string', required: true },
        start: { type: 'number', default: 0 },
        step: { type: 'number', default: 1 },
    },
    // Scoped to the instances: `&` is the component's root. The fallbacks keep it styled without the shared sheet.
    css: '& { display: inline-flex; gap: var(--ex-space-2, 8px); align-items: center; }'
        + ' & #value { color: var(--ex-accent, #2563eb); min-inline-size: 2ch; text-align: center; }',
    mount(self) {
        const count = SIGNAL.create(self.props.start);
        self.state.count = count;
        self.bind.text(self.blockId, 'value', count);
        self.on('inc', 'click', () => count.update((n) => n + self.props.step));
        self.on('dec', 'click', () => count.update((n) => n - self.props.step));
    },
    update(self) {
        self.text('label', self.props.label);
    },
});

/** Two independent instances of the same blueprint. */
export const counters = [
    Counter({ ui, id: 'apples', props: { label: 'Apples' } }),
    Counter({ ui, id: 'tens', props: { label: 'Tens', start: 10, step: 10 } }),
];

index.html

htmlApache-2.021 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · A reusable counter component</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>A reusable counter component</h1>
    <div id="app"></div>
  </main>
</body>
</html>