UNPKG

2.03 kBMarkdownView Raw
1The intersperse component separates a list of elements
2by a given character (the default is `, `).
3
4It takes all direct descendants of its default slot and inserts
5the given separator between each item:
6
7`item 1, item 2, item 3`
8
9Optionally the character used for separating each item and the last separator can be set
10independently:
11
12* `separator="/"` renders `item 1/item 2/item 3`
13* `lastSeparator=" and "` will render `item 1, item 2, and item 3`
14* `lastSeparator=" and "` and given two items will render `item 1 and item 2`
15
16**Note**:
17
18The component provides an inline context since the result is wrapped in a `span`.
19
20Also, whitespace elements that are direct children of the default-slot get removed to ensure
21consistent formatting.
22
23## Usage
24
25### Default
26
27```html
28<gl-intersperse>
29 <span>Item 1</span>
30 <span>Item 2</span>
31 <span>Item 3</span>
32</gl-intersperse>
33```
34
35This renders to the following HTML:
36
37```html
38<span><span>Item 1</span>, <span>Item 2</span>, <span>Item 3</span></span>
39```
40
41### Custom Separator
42
43A custom separator can be defined via the `separator` prop:
44
45```html
46<gl-intersperse separator="/">
47 <span>Item 1</span>
48 <span>Item 2</span>
49 <span>Item 3</span>
50</gl-intersperse>
51```
52
53This renders to the following HTML:
54
55```html
56<span><span>Item 1</span>/<span>Item 2</span>/<span>Item 3</span></span>
57```
58
59### Custom Last Separator
60
61A custom last separator can be defined via the `lastSeparator` prop:
62
63```html
64<gl-intersperse last-separator=" and ">
65 <span>Item 1</span>
66 <span>Item 2</span>
67 <span>Item 3</span>
68</gl-intersperse>
69```
70
71This renders to the following HTML:
72
73```html
74<span><span>Item 1</span>, <span>Item 2</span>, and <span>Item 3</span></span>
75```
76
77A custom last separator used on two items will only place `lastSeparator` between them:
78
79```html
80<gl-intersperse last-separator=" and ">
81 <span>Item 1</span>
82 <span>Item 2</span>
83</gl-intersperse>
84```
85
86This renders to the following HTML:
87
88```html
89<span><span>Item 1</span> and <span>Item 2</span></span>
90```