Skip to content

Commit f9549b8

Browse files
authored
refactor: simplify render helper to reduce size (#97)
1 parent 95cce98 commit f9549b8

File tree

5 files changed

+12
-15
lines changed

5 files changed

+12
-15
lines changed

Diff for: package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
},
169169
{
170170
"path": "hsl-string-color-picker.js",
171-
"limit": "2.7 KB"
171+
"limit": "2.6 KB"
172172
},
173173
{
174174
"path": "hsla-color-picker.js",
@@ -184,7 +184,7 @@
184184
},
185185
{
186186
"path": "hsv-string-color-picker.js",
187-
"limit": "2.7 KB"
187+
"limit": "2.6 KB"
188188
},
189189
{
190190
"path": "hsva-color-picker.js",
@@ -204,7 +204,7 @@
204204
},
205205
{
206206
"path": "rgba-color-picker.js",
207-
"limit": "2.95 KB"
207+
"limit": "2.9 KB"
208208
},
209209
{
210210
"path": "rgba-string-color-picker.js",

Diff for: src/lib/components/color-picker.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { equalColorObjects } from '../utils/compare.js';
2-
import { fire, tpl } from '../utils/dom.js';
2+
import { fire, render } from '../utils/dom.js';
33
import type { AnyColor, ColorModel, HsvaColor } from '../types';
44
import { Hue } from './hue.js';
55
import { Saturation } from './saturation.js';
@@ -54,9 +54,8 @@ export abstract class ColorPicker<C extends AnyColor> extends HTMLElement {
5454

5555
constructor() {
5656
super();
57-
const template = tpl(`<style>${this[$css].join('')}</style>`);
5857
const root = this.attachShadow({ mode: 'open' });
59-
root.appendChild(template.content.cloneNode(true));
58+
render(root, `<style>${this[$css].join('')}</style>`);
6059
root.addEventListener('move', this);
6160
this[$parts] = this[$sliders].map((slider) => new slider(root));
6261
}

Diff for: src/lib/components/slider.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { HsvaColor } from '../types.js';
2-
import { fire, tpl } from '../utils/dom.js';
2+
import { fire, render } from '../utils/dom.js';
33
import { clamp } from '../utils/math.js';
44

55
export interface Offset {
@@ -81,10 +81,10 @@ export abstract class Slider {
8181
declare xy: boolean;
8282

8383
constructor(root: ShadowRoot, part: string, aria: string, xy: boolean) {
84-
const template = tpl(
84+
render(
85+
root,
8586
`<div role="slider" tabindex="0" part="${part}" ${aria}><div part="${part}-pointer"></div></div>`
8687
);
87-
root.appendChild(template.content.cloneNode(true));
8888

8989
const el = root.querySelector(`[part=${part}]`) as HTMLElement;
9090
el.addEventListener('mousedown', this);

Diff for: src/lib/entrypoints/hex-input.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { ColorPickerEventListener, ColorPickerEventMap } from '../types';
22
import { validHex } from '../utils/validate.js';
3-
import { tpl } from '../utils/dom.js';
4-
5-
const template = tpl('<slot><input part="input" spellcheck="false"></slot>');
3+
import { render } from '../utils/dom.js';
64

75
// Escapes all non-hexadecimal characters including "#"
86
const escape = (hex: string, alpha: boolean) =>
@@ -85,7 +83,7 @@ export class HexInputBase extends HTMLElement {
8583
super();
8684

8785
const root = this.attachShadow({ mode: 'open' });
88-
root.appendChild(template.content.cloneNode(true));
86+
render(root, '<slot><input part="input" spellcheck="false"></slot>');
8987
const slot = root.firstElementChild as HTMLSlotElement;
9088
slot.addEventListener('slotchange', () => this[$init](root));
9189
}

Diff for: src/lib/utils/dom.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const cache: Record<string, HTMLTemplateElement> = {};
22

3-
export const tpl = (html: string): HTMLTemplateElement => {
3+
export const render = (root: ShadowRoot, html: string): void => {
44
let template = cache[html];
55
if (!template) {
66
template = document.createElement('template');
77
template.innerHTML = html;
88
cache[html] = template;
99
}
10-
return template;
10+
root.appendChild(template.content.cloneNode(true));
1111
};
1212

1313
export const fire = (target: HTMLElement, type: string, detail: Record<string, unknown>): void => {

0 commit comments

Comments
 (0)