Skip to content

Instantly share code, notes, and snippets.

View rowan-m's full-sized avatar
🐢
I'll get to it eventually.

Rowan Merewood rowan-m

🐢
I'll get to it eventually.
View GitHub Profile
<input-knob value="1"></input-knob>
const knob = document.querySelector('input-knob');
knob.value = 2;
class InputKnob extends HTMLElement {
constructor() {
// Nothing to do in here at the moment, but we do need to call the parent
// constructor
super();
}
// Watch for changes on the 'value' attribute
static get observedAttributes() {
return ['value'];
}
input-knob {
display: block;
background: #cadbbc;
border: 1rem dashed #356211;
box-shadow: 0.3rem 0.3rem 0.3rem rgba(0, 0, 0, 0.5);
width: 8rem;
height: 8rem;
}
{
--angle: 0rad;
transform: rotate(var(--angle));
}
<style>
#container {
--angle: 0rad;
transform: rotate(var(--angle));
}
</style>
<div id="container" part="container">
<slot></slot>
</div>
input-knob::part(container) {
display: block;
background: #cadbbc;
border: 1rem dashed #356211;
box-shadow: 0.3rem 0.3rem 0.3rem rgba(0, 0, 0, 0.5);
width: 8rem;
height: 8rem;
}
const template = document.createElement('template');
template.innerHTML = `
<style>
#container {
--angle: 0rad;
transform: rotate(var(--angle));
}
</style>
<div id="container" part="container">
<slot></slot>
class InputKnob extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
this._container = this.shadowRoot.getElementById('container');
} // ✂️ rest of the class omitted
}
// class InputKnob
_drawState() {
this._container.style.setProperty('--angle', `${this.value}rad`);
}