Skip to content

Commit 741f296

Browse files
committed
docs(storybook): add formatter function for web components
This function handles the faulty handling of unnamed slots (default slots) in Storybook by assigning them a name that can then be targeted as a prop in the stories
1 parent b1a4346 commit 741f296

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

.storybook/preview.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { setCustomElements } from '@storybook/web-components';
2-
import customElements from '../custom-elements.json';
31
import '../packages/uui-css/lib/uui-css.css';
42
import 'element-internals-polyfill';
53

4+
import { setCustomElements } from '@storybook/web-components';
5+
6+
import customElements from '../custom-elements.json';
7+
68
const sort = (a, b) => {
79
if (a[1].name === 'Overview') {
810
return 0;
@@ -44,4 +46,24 @@ export const parameters = {
4446
},
4547
};
4648

49+
WebComponentFormatter(customElements);
50+
4751
setCustomElements(customElements);
52+
53+
function WebComponentFormatter(customElements) {
54+
for (let tag of customElements.tags || []) {
55+
for (let slot of tag.slots || []) {
56+
// Replace the name of the default slot so Storybook will show it
57+
if (typeof slot.name === 'string' && slot.name.length === 0) {
58+
slot.name = 'slot';
59+
}
60+
61+
// Set type of slots
62+
if (typeof slot.type === 'undefined' || slot.type.length === 0) {
63+
slot.type = 'HTMLElement';
64+
}
65+
}
66+
}
67+
68+
return customElements;
69+
}

0 commit comments

Comments
 (0)