Skip to content

Commit 7b0770d

Browse files
authored
refactor: move events logic from KnobsMixin (#90)
1 parent cd41264 commit 7b0770d

File tree

2 files changed

+54
-42
lines changed

2 files changed

+54
-42
lines changed

src/api-demo-knobs-mixin.ts

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
CSSPropertyInfo,
77
PropertyInfo,
88
SlotInfo,
9-
SlotValue,
10-
EventInfo
9+
SlotValue
1110
} from './lib/types.js';
1211
import {
1312
getTemplates,
@@ -132,19 +131,19 @@ export interface ApiDemoKnobsInterface {
132131
props: PropertyInfo[];
133132
propKnobs: Knob<PropertyInfo>[];
134133
slots: SlotInfo[];
135-
events: EventInfo[];
136134
cssProps: CSSPropertyInfo[];
137135
exclude: string;
138136
vid?: number;
139137
processedSlots: SlotValue[];
140138
processedCss: CSSPropertyInfo[];
141-
eventLog: CustomEvent[];
142139
customKnobs: Knob<PropertyInfo>[];
143140
knobs: Record<string, Knob>;
144141
setKnobs(target: HTMLInputElement): void;
145142
setSlots(target: HTMLInputElement): void;
146143
setCss(target: HTMLInputElement): void;
147-
onRendered(event: CustomEvent): void;
144+
initKnobs(component: HTMLElement): void;
145+
getKnob(name: string): { knob: Knob<PropertyInfo>; custom?: boolean };
146+
syncKnob(component: Element, changed: Knob<PropertyInfo>): void;
148147
}
149148

150149
export const ApiDemoKnobsMixin = <T extends Constructor<LitElement>>(
@@ -159,9 +158,6 @@ export const ApiDemoKnobsMixin = <T extends Constructor<LitElement>>(
159158
@property({ attribute: false })
160159
slots: SlotInfo[] = [];
161160

162-
@property({ attribute: false })
163-
events: EventInfo[] = [];
164-
165161
@property({ attribute: false })
166162
cssProps: CSSPropertyInfo[] = [];
167163

@@ -175,9 +171,6 @@ export const ApiDemoKnobsMixin = <T extends Constructor<LitElement>>(
175171
@property({ attribute: false })
176172
processedCss!: CSSPropertyInfo[];
177173

178-
@property({ attribute: false })
179-
eventLog!: CustomEvent[];
180-
181174
@property({ attribute: false })
182175
customKnobs: Knob<PropertyInfo>[] = [];
183176

@@ -191,7 +184,6 @@ export const ApiDemoKnobsMixin = <T extends Constructor<LitElement>>(
191184
// Reset state if tag changed
192185
if (props.has('tag')) {
193186
this.knobs = {};
194-
this.eventLog = [];
195187
this.processedCss = [];
196188
this.processedSlots = [];
197189
this.propKnobs = getKnobs(this.tag, this.props, this.exclude);
@@ -218,18 +210,18 @@ export const ApiDemoKnobsMixin = <T extends Constructor<LitElement>>(
218210
}
219211
}
220212

221-
private _getProp(name: string): {
222-
prop: Knob<PropertyInfo>;
213+
getKnob(name: string): {
214+
knob: Knob<PropertyInfo>;
223215
custom?: boolean;
224216
} {
225217
const isMatch = isPropMatch(name);
226-
let prop = this.propKnobs.find(isMatch);
218+
let knob = this.propKnobs.find(isMatch);
227219
let custom = false;
228-
if (!prop) {
229-
prop = this.customKnobs.find(isMatch) as Knob<PropertyInfo>;
220+
if (!knob) {
221+
knob = this.customKnobs.find(isMatch) as Knob<PropertyInfo>;
230222
custom = true;
231223
}
232-
return { prop, custom };
224+
return { knob, custom };
233225
}
234226

235227
setCss(target: HTMLInputElement): void {
@@ -259,9 +251,9 @@ export const ApiDemoKnobsMixin = <T extends Constructor<LitElement>>(
259251
value = target.value;
260252
}
261253

262-
const { prop, custom } = this._getProp(name as string);
263-
if (prop) {
264-
const { attribute } = prop;
254+
const { knob, custom } = this.getKnob(name as string);
255+
if (knob) {
256+
const { attribute } = knob;
265257
this.knobs = {
266258
...this.knobs,
267259
[name as string]: {
@@ -288,39 +280,23 @@ export const ApiDemoKnobsMixin = <T extends Constructor<LitElement>>(
288280
});
289281
}
290282

291-
onRendered(e: CustomEvent): void {
292-
const { component } = e.detail;
293-
283+
initKnobs(component: HTMLElement) {
294284
if (hasTemplate(this.vid as number, this.tag, HOST)) {
295285
// Apply property values from template
296286
this.propKnobs
297287
.filter((prop) => {
298288
const { name, knobType } = prop;
299289
const defaultValue = getDefault(prop);
300290
return (
301-
component[name] !== defaultValue ||
291+
(component as any)[name] !== defaultValue ||
302292
(knobType === 'boolean' && defaultValue)
303293
);
304294
})
305295
.forEach((prop) => {
306-
this._syncKnob(component, prop);
296+
this.syncKnob(component, prop);
307297
});
308298
}
309299

310-
this.events.forEach((event) => {
311-
component.addEventListener(event.name, ((evt: CustomEvent) => {
312-
const s = '-changed';
313-
if (event.name.endsWith(s)) {
314-
const { prop } = this._getProp(event.name.replace(s, ''));
315-
if (prop) {
316-
this._syncKnob(component, prop);
317-
}
318-
}
319-
320-
this.eventLog = [...this.eventLog, evt];
321-
}) as EventListener);
322-
});
323-
324300
if (this.cssProps.length) {
325301
const style = getComputedStyle(component);
326302

@@ -339,7 +315,7 @@ export const ApiDemoKnobsMixin = <T extends Constructor<LitElement>>(
339315
}
340316
}
341317

342-
private _syncKnob(component: Element, changed: Knob<PropertyInfo>): void {
318+
syncKnob(component: Element, changed: Knob<PropertyInfo>): void {
343319
const { name, knobType, attribute } = changed;
344320
const value = (component as unknown as ComponentWithProps)[name];
345321

src/api-viewer-demo.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LitElement, html, TemplateResult } from 'lit';
1+
import { LitElement, html, PropertyValues, TemplateResult } from 'lit';
22
import { property } from 'lit/decorators/property.js';
33
import { cache } from 'lit/directives/cache.js';
44
import { renderEvents } from './lib/demo-events.js';
@@ -10,6 +10,7 @@ import {
1010
renderKnobs,
1111
slotRenderer
1212
} from './lib/knobs.js';
13+
import { EventInfo } from './lib/types.js';
1314
import { hasTemplate, TemplateTypes } from './lib/utils.js';
1415
import { ApiDemoKnobsMixin } from './api-demo-knobs-mixin.js';
1516
import './api-viewer-panel.js';
@@ -19,6 +20,12 @@ import './api-viewer-tabs.js';
1920
class ApiViewerDemo extends ApiDemoKnobsMixin(LitElement) {
2021
@property() copyBtnText = 'copy';
2122

23+
@property({ attribute: false })
24+
events: EventInfo[] = [];
25+
26+
@property({ attribute: false })
27+
eventLog!: CustomEvent[];
28+
2229
private _whenDefined: Record<string, Promise<unknown>> = {};
2330

2431
protected createRenderRoot(): this {
@@ -158,6 +165,15 @@ class ApiViewerDemo extends ApiDemoKnobsMixin(LitElement) {
158165
`;
159166
}
160167

168+
willUpdate(props: PropertyValues) {
169+
super.willUpdate(props);
170+
171+
// Reset state if tag changed
172+
if (props.has('tag')) {
173+
this.eventLog = [];
174+
}
175+
}
176+
161177
private _onLogClear(): void {
162178
this.eventLog = [];
163179
const tab = this.renderRoot.querySelector('#events') as HTMLElement;
@@ -192,6 +208,26 @@ class ApiViewerDemo extends ApiDemoKnobsMixin(LitElement) {
192208
}
193209
}
194210

211+
private onRendered(e: CustomEvent): void {
212+
const { component } = e.detail;
213+
214+
this.initKnobs(e.detail.component);
215+
216+
this.events.forEach((event) => {
217+
component.addEventListener(event.name, ((evt: CustomEvent) => {
218+
const s = '-changed';
219+
if (event.name.endsWith(s)) {
220+
const { knob } = this.getKnob(event.name.replace(s, ''));
221+
if (knob) {
222+
this.syncKnob(component, knob);
223+
}
224+
}
225+
226+
this.eventLog = [...this.eventLog, evt];
227+
}) as EventListener);
228+
});
229+
}
230+
195231
private _onCssChanged(e: CustomEvent): void {
196232
this.setCss(e.composedPath()[0] as HTMLInputElement);
197233
}

0 commit comments

Comments
 (0)