Skip to content

Commit e51bd2f

Browse files
authored
refactor!: move demo types to api-demo package (#131)
1 parent 549efab commit e51bd2f

File tree

12 files changed

+72
-83
lines changed

12 files changed

+72
-83
lines changed

.changeset/neat-poets-thank.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@api-viewer/common': patch
3+
'@api-viewer/demo': patch
4+
'@api-viewer/docs': patch
5+
'@api-viewer/tabs': patch
6+
'api-viewer-element': patch
7+
---
8+
9+
Move demo types to api-demo package

packages/api-common/src/manifest.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ export {
2626
Slot
2727
};
2828

29-
export type CssCustomPropertyValue = CssCustomProperty & { value?: string };
30-
31-
export interface SlotValue {
32-
name: string;
33-
content: string;
34-
}
35-
3629
export function hasCustomElements(
3730
manifest?: Package | null
3831
): manifest is Package {

packages/api-demo/src/controllers/events-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
AbstractController,
44
AbstractControllerHost
55
} from './abstract-controller.js';
6-
import { HasKnobs } from '../ui/knobs.js';
6+
import { HasKnobs } from '../types.js';
77

88
export class EventsController extends AbstractController<CustomEvent> {
99
constructor(

packages/api-demo/src/controllers/slots-controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Slot, SlotValue } from '@api-viewer/common/lib/index.js';
1+
import { Slot } from '@api-viewer/common/lib/index.js';
22
import {
33
hasTemplate,
44
TemplateTypes
@@ -7,6 +7,7 @@ import {
77
AbstractController,
88
AbstractControllerHost
99
} from './abstract-controller.js';
10+
import { SlotValue } from '../types.js';
1011

1112
const capitalize = (name: string): string =>
1213
name[0].toUpperCase() + name.slice(1);

packages/api-demo/src/controllers/styles-controller.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import {
2-
CssCustomProperty,
3-
CssCustomPropertyValue,
4-
unquote
5-
} from '@api-viewer/common/lib/index.js';
1+
import { CssCustomProperty, unquote } from '@api-viewer/common/lib/index.js';
62
import {
73
AbstractController,
84
AbstractControllerHost
95
} from './abstract-controller.js';
6+
import { CssCustomPropertyValue } from '../types.js';
107

118
export class StylesController extends AbstractController<CssCustomPropertyValue> {
129
constructor(

packages/api-demo/src/layout.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@ import { SlotsController } from './controllers/slots-controller.js';
1717
import { StylesController } from './controllers/styles-controller.js';
1818
import { renderEvents } from './ui/events.js';
1919
import { renderSnippet } from './ui/snippet.js';
20-
import {
21-
ComponentWithProps,
22-
getCustomKnobs,
23-
getInitialKnobs,
24-
getKnobs,
25-
Knob,
26-
KnobValue
27-
} from './ui/knobs.js';
20+
import { getCustomKnobs, getInitialKnobs, getKnobs } from './ui/knobs.js';
2821
import { renderer } from './ui/renderer.js';
2922
import {
3023
cssPropRenderer,
3124
propRenderer,
3225
renderKnobs,
3326
slotRenderer
3427
} from './ui/controls.js';
28+
import { ComponentWithProps, Knob, KnobValue, PropertyKnob } from './types.js';
3529

3630
class ApiDemoLayout extends LitElement {
3731
@property() copyBtnText = 'copy';
@@ -55,13 +49,13 @@ class ApiDemoLayout extends LitElement {
5549
@property({ type: Number }) vid?: number;
5650

5751
@property({ attribute: false })
58-
customKnobs!: Knob<ClassField>[];
52+
customKnobs!: PropertyKnob[];
5953

6054
@property({ attribute: false })
6155
knobs!: Record<string, Knob>;
6256

6357
@property({ attribute: false })
64-
propKnobs!: Knob<ClassField>[];
58+
propKnobs!: PropertyKnob[];
6559

6660
private _whenDefined: Record<string, Promise<unknown>> = {};
6761

@@ -310,15 +304,15 @@ class ApiDemoLayout extends LitElement {
310304
}
311305

312306
getKnob(name: string): {
313-
knob: Knob<ClassField>;
307+
knob: PropertyKnob;
314308
custom?: boolean;
315309
} {
316-
const isMatch = (prop: Knob<ClassField>): boolean =>
310+
const isMatch = (prop: PropertyKnob): boolean =>
317311
prop.name === name || prop.attribute === name;
318312
let knob = this.propKnobs.find(isMatch);
319313
let custom = false;
320314
if (!knob) {
321-
knob = this.customKnobs.find(isMatch) as Knob<ClassField>;
315+
knob = this.customKnobs.find(isMatch) as PropertyKnob;
322316
custom = true;
323317
}
324318
return { knob, custom };
@@ -342,7 +336,7 @@ class ApiDemoLayout extends LitElement {
342336
};
343337
}
344338

345-
syncKnob(component: Element, changed: Knob<ClassField>): void {
339+
syncKnob(component: Element, changed: PropertyKnob): void {
346340
const { name, knobType, attribute } = changed;
347341
const value = (component as unknown as ComponentWithProps)[name];
348342

packages/api-demo/src/types.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { CssCustomProperty, ClassField } from '@api-viewer/common/lib/index.js';
2+
3+
export type CssCustomPropertyValue = CssCustomProperty & { value?: string };
4+
5+
export interface SlotValue {
6+
name: string;
7+
content: string;
8+
}
9+
10+
export type KnobValue = string | number | boolean | null | undefined;
11+
12+
export type ComponentWithProps = {
13+
[key: string]: KnobValue;
14+
};
15+
16+
export type Knobable = unknown | (CssCustomProperty | ClassField | SlotValue);
17+
18+
export type Knob<T extends Knobable = unknown> = T & {
19+
attribute: string | undefined;
20+
value: KnobValue;
21+
custom?: boolean;
22+
options?: string[];
23+
knobType: string;
24+
content?: string;
25+
};
26+
27+
export type PropertyKnob = Knob<ClassField>;
28+
29+
export interface HasKnobs {
30+
getKnob(name: string): { knob: PropertyKnob; custom?: boolean };
31+
syncKnob(component: Element, changed: PropertyKnob): void;
32+
}

packages/api-demo/src/ui/controls.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { html, TemplateResult } from 'lit';
22
import {
3-
ClassField,
43
CssCustomPropertyValue,
4+
Knobable,
5+
PropertyKnob,
56
SlotValue
6-
} from '@api-viewer/common/lib/index.js';
7-
import { Knob, Knobable } from './knobs.js';
7+
} from '../types.js';
88

99
type InputRenderer = (item: Knobable, id: string) => TemplateResult;
1010

@@ -29,7 +29,7 @@ export const propRenderer: InputRenderer = (
2929
knob: Knobable,
3030
id: string
3131
): TemplateResult => {
32-
const { name, knobType, value, options } = knob as Knob<ClassField>;
32+
const { name, knobType, value, options } = knob as PropertyKnob;
3333
let input;
3434
if (knobType === 'select' && Array.isArray(options)) {
3535
input = html`
@@ -90,7 +90,7 @@ export const renderKnobs = (
9090
renderer: InputRenderer
9191
): TemplateResult => {
9292
const rows = items.map((item: Knobable) => {
93-
const { name, content } = item as Knob<ClassField>;
93+
const { name, content } = item as PropertyKnob;
9494
const id = `${type}-${name || 'default'}`;
9595
const label = type === 'slot' ? content : name;
9696
return html`

packages/api-demo/src/ui/events.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { html, nothing, TemplateResult } from 'lit';
2-
import { KnobValue } from './knobs.js';
2+
import { KnobValue } from '../types.js';
33

4-
interface EventDetail {
5-
value: KnobValue;
6-
}
7-
8-
const renderDetail = (detail: EventDetail): string => {
4+
const renderDetail = (detail: { value: KnobValue }): string => {
95
const result = detail;
106
const undef = 'undefined';
117
if ('value' in detail && detail.value === undefined) {

packages/api-demo/src/ui/knobs.ts

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,12 @@
1-
import {
2-
ClassField,
3-
CssCustomProperty,
4-
SlotValue,
5-
unquote
6-
} from '@api-viewer/common/lib/index.js';
1+
import { ClassField, unquote } from '@api-viewer/common/lib/index.js';
72
import {
83
getTemplateNode,
94
getTemplates,
105
TemplateTypes
116
} from '@api-viewer/common/lib/templates.js';
7+
import { ComponentWithProps, KnobValue, PropertyKnob } from '../types.js';
128

13-
export type KnobValue = string | number | boolean | null | undefined;
14-
15-
export type ComponentWithProps = {
16-
[key: string]: KnobValue;
17-
};
18-
19-
export type Knobable = unknown | (CssCustomProperty | ClassField | SlotValue);
20-
21-
export type Knob<T extends Knobable = unknown> = T & {
22-
attribute: string | undefined;
23-
value: KnobValue;
24-
custom?: boolean;
25-
options?: string[];
26-
knobType: string;
27-
content?: string;
28-
};
29-
30-
export interface HasKnobs {
31-
getKnob(name: string): { knob: Knob<ClassField>; custom?: boolean };
32-
syncKnob(component: Element, changed: Knob<ClassField>): void;
33-
}
34-
35-
const getDefault = (prop: Knob<ClassField>): KnobValue => {
9+
const getDefault = (prop: PropertyKnob): KnobValue => {
3610
const { knobType, default: value } = prop;
3711
switch (knobType) {
3812
case 'boolean':
@@ -75,12 +49,12 @@ export const getKnobs = (
7549
tag: string,
7650
props: ClassField[],
7751
exclude = ''
78-
): Knob<ClassField>[] => {
52+
): PropertyKnob[] => {
7953
// Exclude getters and specific properties
8054
let propKnobs = props.filter(
8155
({ name }) =>
8256
!exclude.includes(name) && !isGetter(customElements.get(tag), name)
83-
) as Knob<ClassField>[];
57+
) as PropertyKnob[];
8458

8559
// Set knob types and default knobs values
8660
propKnobs = propKnobs.map((prop) => {
@@ -99,10 +73,7 @@ export const getKnobs = (
9973
return propKnobs;
10074
};
10175

102-
export const getCustomKnobs = (
103-
tag: string,
104-
vid?: number
105-
): Knob<ClassField>[] => {
76+
export const getCustomKnobs = (tag: string, vid?: number): PropertyKnob[] => {
10677
return getTemplates(vid as number, tag, TemplateTypes.KNOB)
10778
.map((template) => {
10879
const { attr, type } = template.dataset;
@@ -134,15 +105,15 @@ export const getCustomKnobs = (
134105
};
135106
}
136107
}
137-
return result as Knob<ClassField>;
108+
return result as PropertyKnob;
138109
})
139110
.filter(Boolean);
140111
};
141112

142113
export const getInitialKnobs = (
143-
propKnobs: Knob<ClassField>[],
114+
propKnobs: PropertyKnob[],
144115
component: HTMLElement
145-
): Knob<ClassField>[] => {
116+
): PropertyKnob[] => {
146117
return propKnobs.filter((prop) => {
147118
const { name, knobType } = prop;
148119
const defaultValue = getDefault(prop);

packages/api-demo/src/ui/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isTemplate,
99
TemplateTypes
1010
} from '@api-viewer/common/lib/templates.js';
11-
import { ComponentWithProps, Knob } from './knobs.js';
11+
import { ComponentWithProps, Knob } from '../types.js';
1212

1313
export type ComponentRendererOptions = {
1414
id: number;

packages/api-demo/src/ui/snippet.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ import { htmlRender } from 'highlight-ts/es/render/html.js';
44
import { registerLanguages } from 'highlight-ts/es/languages.js';
55
import { XML } from 'highlight-ts/es/languages/xml.js';
66
import { init, process } from 'highlight-ts/es/process.js';
7-
import {
8-
CssCustomPropertyValue,
9-
SlotValue
10-
} from '@api-viewer/common/lib/index.js';
117
import {
128
getTemplate,
139
getTemplateNode,
1410
isTemplate,
1511
TemplateTypes
1612
} from '@api-viewer/common/lib/templates.js';
17-
import { Knob } from './knobs.js';
1813
import { CSS } from './highlight-css.js';
14+
import { CssCustomPropertyValue, Knob, SlotValue } from '../types.js';
1915

2016
// register languages
2117
registerLanguages(CSS, XML);

0 commit comments

Comments
 (0)