Skip to content

Instantly share code, notes, and snippets.

View deebloo's full-sized avatar
👋
Hello!

Danny Blue deebloo

👋
Hello!
View GitHub Profile
function* windowed<T>(arr: T[], size: number) {
for (let i = 0; i < arr.length; i++) {
yield arr.slice(i, size + i);
}
}
let thing = windowed([0, 1, 2, 3], 2);
for (let [a, b] of thing) {
console.log(a, b);
@deebloo
deebloo / shadow-element.ts
Last active January 25, 2023 22:04
Small utility for apply html template and construct-able stylesheets to custom elements.
import { shadow, ShadowTemplate, html, css } from './shadow.js';
const template: ShadowTemplate = {
css: css`
:host {
display: contents;
}
`.
html: html`
<slot></slot>
class MySelectElement extends HTMLElement {
constructor() {
super();
const observer = new MutationObserver((records) => {
for (let record of records) {
this.addItem(record.addedNodes);
this.removeItem(record.removedNodes);
}
});

Minimum integration for custom form inputs

  • Allow value to be set via an attribute
  • Do not reflect that value back to an attribute if set via a property
  • If a text input. Listen for keyboard events. If enter key is called the form should be submitted.
  • DO NOT just call form.submit since this will not fire the submit event. Instead create a submit button, append it to the form. click the button and then remove it
    • const btn = document.createElement("button");
      btn.type = "submit";

form.append(btn);