-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathknobs.ts
More file actions
80 lines (67 loc) · 2.07 KB
/
knobs.ts
File metadata and controls
80 lines (67 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type {
Attribute,
ClassField,
ClassMember,
CustomElementDeclaration,
CustomElementField,
Declaration,
Slot,
} from 'custom-elements-manifest';
import type { PftElementKnobs } from '../pft-element-knobs.js';
export interface KnobInfo<E> {
element: E;
knobId: string;
}
export interface AttributeKnobInfo<E> extends KnobInfo<E> {
isBoolean: boolean;
isEnum: boolean;
isNullable: boolean;
isNumber: boolean;
isOptional: boolean;
values: string[];
}
export interface PropertyKnobInfo<E> extends KnobInfo<E> {
isBoolean: boolean;
isEnum: boolean;
isNullable: boolean;
isNumber: boolean;
isOptional: boolean;
isSerializable: boolean;
values: string[];
}
export type ContentKnobInfo<E> = KnobInfo<E>;
export type KnobRenderer<T, E extends HTMLElement = HTMLElement> = (
this: PftElementKnobs<E>,
member: T,
info:
T extends ClassField ? PropertyKnobInfo<E>
: T extends Attribute ? AttributeKnobInfo<E>
: T extends Slot[] ? ContentKnobInfo<E>
: KnobInfo<E>,
) => unknown;
export type PropertyRenderer<E extends HTMLElement> = KnobRenderer<ClassField, E>;
export type AttributeRenderer<E extends HTMLElement> = KnobRenderer<Attribute, E>;
export type ContentRenderer<E extends HTMLElement> = KnobRenderer<Slot[], E>;
export const isAttributelessProperty = (x: ClassMember): x is CustomElementField =>
x.kind === 'field'
&& !x.privacy
&& !('attribute' in x);
export const isCheckable = (el: HTMLElement): el is HTMLElement & { checked: boolean } =>
'checked' in el;
export const isValue = (el: HTMLElement): el is HTMLElement & { value: string } =>
'value' in el;
export const isCustomElementDecl = (decl: Declaration): decl is CustomElementDeclaration =>
'customElement' in decl;
export const dedent = (str: string): string => {
const stripped = str.replace(/^\n/, '');
const match = stripped.match(/^\s+/);
return match ? stripped.replace(new RegExp(`^${match[0]}`, 'gm'), '') : str;
};
export const isSerializable = (x: unknown): boolean => {
try {
JSON.stringify(x);
return true;
} catch {
return false;
}
};