A reusable counter component
A reusable counter component: two instances, reactive state, CSS scoped to the component.
Apache-2.0Runs on this page
Package: @awacloud/fw
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
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. - Keep
main: trueon 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,requiredanddefault; a missing required prop throws at mount. self.bind(areactiveBindcontroller) binding a signal totextContent;self.on(localId, 'click', fn)andself.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, seefw/dom-theme.
Code
main.js
// 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
<!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>