1
1
import { Args } from '@storybook/web-components' ;
2
- import { html , nothing , TemplateResult } from 'lit' ;
2
+ import { nothing , TemplateResult } from 'lit' ;
3
+ import { unsafeHTML } from 'lit/directives/unsafe-html.js' ;
3
4
4
5
/**
5
6
* Render a list of slots, filtering out any null or undefined slots to prevent empty lines.
6
7
* Accepts an array of TemplateResults or an Args object. If an Args object is provided, it will render any properties that end with 'slot'.
7
8
*/
8
9
// TODO: add a way to control the new lines. Eg exclude newlines: before, after, between as an array
9
- function renderSlots ( args : Args ) : TemplateResult | typeof nothing ;
10
- function renderSlots (
11
- param : TemplateResult [ ] | Args ,
12
- ) : TemplateResult | typeof nothing {
10
+ function renderSlots ( args : Args ) : TemplateResult [ ] | typeof nothing ;
11
+ function renderSlots ( param : TemplateResult [ ] | Args ) {
13
12
let slots : TemplateResult [ ] = [ ] ;
14
13
15
14
// if param is array, set slots to param
@@ -34,13 +33,14 @@ function renderSlots(
34
33
35
34
// Join slots with consistent formatting; no extra line breaks between them
36
35
const spacing = ' ' ;
37
- const formattedSlots = validSlots . map (
38
- ( slot , index ) =>
39
- // eslint-disable-next-line lit/no-useless-template-literals
40
- html `${ '\n' } ${ spacing } ${ slot } ${ index === slots . length - 1 ? '\n' : '' } ` ,
41
- ) ;
42
-
43
- // Return the combined template results
44
- return html `${ formattedSlots } ` ;
36
+
37
+ const stringSlots = validSlots . map ( slot => slot . strings [ 0 ] ) ;
38
+ const stringSlotsJoined = stringSlots . join ( '\n' ) ;
39
+ const stringSlotsJoinedWithSpacing = stringSlotsJoined
40
+ . split ( '\n' )
41
+ . map ( line => spacing + line )
42
+ . join ( '\n' ) ;
43
+
44
+ return unsafeHTML ( stringSlotsJoinedWithSpacing ) ;
45
45
}
46
46
export { renderSlots } ;
0 commit comments