-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
custom-elements.ts
140 lines (125 loc) · 3.62 KB
/
custom-elements.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { getCustomElements, isValidComponent, isValidMetaData } from '@storybook/web-components';
import { ArgTypes } from '@storybook/api';
import { logger } from '@storybook/client-logger';
interface TagItem {
name: string;
type: { text: string };
description: string;
default?: any;
kind?: string;
defaultValue?: any;
}
interface Tag {
name: string;
description: string;
attributes?: TagItem[];
properties?: TagItem[];
events?: TagItem[];
methods?: TagItem[];
members?: TagItem[];
slots?: TagItem[];
cssProperties?: TagItem[];
cssParts?: TagItem[];
}
interface CustomElements {
tags: Tag[];
modules?: [];
}
interface Module {
declarations?: [];
exports?: [];
}
interface Declaration {
tagName: string;
}
interface Sections {
attributes?: any;
properties?: any;
events?: any;
slots?: any;
cssCustomProperties?: any;
cssShadowParts?: any;
}
function mapData(data: TagItem[], category: string) {
return (
data &&
data
.filter((item) => !!item)
.reduce((acc, item) => {
if (item.kind === 'method') return acc;
const type =
category === 'properties' ? { name: item.type?.text || item.type } : { name: 'void' };
acc[item.name] = {
name: item.name,
required: false,
description: item.description,
type,
table: {
category,
type: { summary: item.type?.text || item.type },
defaultValue: {
summary: item.default !== undefined ? item.default : item.defaultValue,
},
},
};
return acc;
}, {} as ArgTypes)
);
}
const getMetaDataExperimental = (tagName: string, customElements: CustomElements) => {
if (!isValidComponent(tagName) || !isValidMetaData(customElements)) {
return null;
}
const metaData = customElements.tags.find(
(tag) => tag.name.toUpperCase() === tagName.toUpperCase()
);
if (!metaData) {
logger.warn(`Component not found in custom-elements.json: ${tagName}`);
}
return metaData;
};
const getMetaDataV1 = (tagName: string, customElements: CustomElements) => {
if (!isValidComponent(tagName) || !isValidMetaData(customElements)) {
return null;
}
let metadata;
customElements?.modules?.forEach((_module: Module) => {
_module?.declarations?.forEach((declaration: Declaration) => {
if (declaration.tagName === tagName) {
metadata = declaration;
}
});
});
if (!metadata) {
logger.warn(`Component not found in custom-elements.json: ${tagName}`);
}
return metadata;
};
export const extractArgTypesFromElements = (tagName: string, customElements: CustomElements) => {
const metaData = getMetaData(tagName, customElements);
return (
metaData && {
...mapData(metaData.attributes, 'attributes'),
...mapData(metaData.members, 'properties'),
...mapData(metaData.properties, 'properties'),
...mapData(metaData.events, 'events'),
...mapData(metaData.slots, 'slots'),
...mapData(metaData.cssProperties, 'css custom properties'),
...mapData(metaData.cssParts, 'css shadow parts'),
}
);
};
const getMetaData = (tagName: string, manifest: any) => {
if (manifest.version === 'experimental') {
return getMetaDataExperimental(tagName, manifest);
}
return getMetaDataV1(tagName, manifest);
};
export const extractArgTypes = (tagName: string) => {
const cem = getCustomElements();
return extractArgTypesFromElements(tagName, cem);
};
export const extractComponentDescription = (tagName: string) => {
const metaData = getMetaData(tagName, getCustomElements());
return metaData && metaData.description;
};