1. Home
  2. /
  3. Polymer
  4. /
  5. Mega Counter

Mega Counter - Polymer Typing CST Test

Loading…

Mega Counter — Polymer Code

Combines step, max, auto-reset, history, auto-increment, and theme toggle in one Polymer component.

import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
class MegaCounter extends PolymerElement {
	static get properties() {
		return {
		count: { type: Number, value: 0 },
		history: { type: Array, value: () => [] },
		isDark: { type: Boolean, value: false },
		step: { type: Number, value: 2 },
		max: { type: Number, value: 10 }
		};
	}
	constructor() {
		super();
		setInterval(() => {
		let n = this.count + this.step;
		if(n > this.max) n = 0;
		this.count = n;
		this.history = [...this.history, 'Auto Increment'];
		}, 1000);
	}
	static get template() {
		return html`<div class$="${this.isDark?'dark-theme':'light-theme'}">
		<h2>Counter: [[count]]</h2>
		<div>History: [[history]]</div>
		<button on-click="_increment">+</button>
		<button on-click="_decrement">-</button>
		<button on-click="_reset">Reset</button>
		<button on-click="_toggleTheme">Toggle Theme</button>
		</div>`;
	}
	_increment(){ let n=this.count+this.step; if(n>this.max)n=0; this.count=n; this.history=[...this.history,'Increment']; }
	_decrement(){ this.count-=this.step; this.history=[...this.history,'Decrement']; }
	_reset(){ this.count=0; this.history=[...this.history,'Reset']; }
	_toggleTheme(){ this.isDark=!this.isDark; }
}
customElements.define('mega-counter', MegaCounter);

Polymer Language Guide

Polymer.js is an open-source JavaScript library developed by Google for building reusable Web Components using modern browser APIs. It emphasizes encapsulation, custom elements, and leveraging native browser features with minimal framework overhead.

Primary Use Cases

  • ▸Web Components and design systems
  • ▸Reusable UI libraries across multiple apps/frameworks
  • ▸Single-page applications (SPAs) with Polymer CLI
  • ▸Embedding custom widgets into legacy or multi-framework environments
  • ▸Progressive web applications leveraging native browser features

Notable Features

  • ▸Native Web Components support (Custom Elements, Shadow DOM, HTML Templates)
  • ▸Two-way data binding
  • ▸Polymer CLI for scaffolding and building apps
  • ▸Light DOM and Shadow DOM encapsulation
  • ▸Built-in support for reusable templates and style modules

Origin & Creator

Created by Google in 2013 and led by the Chrome Web Components team, especially supported by the Polymer Project Group.

Industrial Note

Polymer.js is highly aligned with browsers’ native Web Components standards, making it ideal for design systems, reusable UI libraries, and framework-agnostic component deployment.

More Polymer Typing Exercises

Polymer Counter ComponentPolymer Basic CounterPolymer Counter with StepPolymer Counter with Max LimitPolymer Counter with Auto-ResetPolymer Counter with HistoryPolymer Dark Mode CounterPolymer Auto-Increment CounterPolymer Conditional Theme CounterPolymer Full Featured Counter

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher