1
1
import React from 'react'
2
- import styled from 'styled-components'
3
2
import { XIcon } from '@primer/octicons-react'
4
3
import { getFocusableChild } from '@primer/behaviors/utils'
5
- import { get } from '../constants'
6
4
import VisuallyHidden from '../_VisuallyHidden'
7
5
import { AnchoredOverlay } from '../AnchoredOverlay'
8
6
import { Button , IconButton } from '../Button'
7
+ import { clsx } from 'clsx'
9
8
import theme from '../theme'
10
9
import classes from './LabelGroup.module.css'
11
10
@@ -18,45 +17,6 @@ export type LabelGroupProps = {
18
17
visibleChildCount ?: 'auto' | number
19
18
className ?: string
20
19
}
21
-
22
- const StyledLabelGroupContainer = styled . div `
23
- display: flex;
24
- flex-wrap: nowrap;
25
- gap: ${ get ( 'space.1' ) } ;
26
- line-height: 1;
27
- max-width: 100%;
28
- overflow: hidden;
29
-
30
- &[data-overflow='inline'] {
31
- flex-wrap: wrap;
32
- }
33
-
34
- &[data-list] {
35
- padding-inline-start: 0;
36
- margin-block-start: 0;
37
- margin-block-end: 0;
38
- list-style-type: none;
39
- }
40
- `
41
-
42
- const ItemWrapper = styled . div `
43
- display: flex;
44
- align-items: center;
45
-
46
- /* This min-height matches the height of the expand/collapse button.
47
- Without it, the labels/tokens will do a slight layout shift when expanded.
48
- This is because the height of the first row will match the token sizes,
49
- but the height of the second row will be the height of the collapse button.
50
- */
51
- min-height: 28px;
52
-
53
- &.ItemWrapper--hidden {
54
- order: 9999;
55
- pointer-events: none;
56
- visibility: hidden;
57
- }
58
- `
59
-
60
20
// Calculates the width of the overlay to cover the labels/tokens and the expand button.
61
21
const getOverlayWidth = (
62
22
buttonClientRect : DOMRect ,
@@ -151,7 +111,7 @@ const LabelGroup: React.FC<React.PropsWithChildren<LabelGroupProps>> = ({
151
111
children,
152
112
visibleChildCount,
153
113
overflowStyle = 'overlay' ,
154
- as = 'ul' ,
114
+ as : Component = 'ul' ,
155
115
className,
156
116
} ) => {
157
117
const containerRef = React . useRef < HTMLElement > ( null )
@@ -320,28 +280,30 @@ const LabelGroup: React.FC<React.PropsWithChildren<LabelGroupProps>> = ({
320
280
}
321
281
} , [ overflowStyle , isOverflowShown ] )
322
282
323
- const isList = as === 'ul' || as === 'ol'
283
+ const isList = Component === 'ul' || Component === 'ol'
324
284
const ToggleWrapper = isList ? 'li' : React . Fragment
325
285
286
+ const ItemWrapperComponent = isList ? 'li' : 'span'
287
+
326
288
// If truncation is enabled, we need to render based on truncation logic.
327
289
return visibleChildCount ? (
328
- < StyledLabelGroupContainer
290
+ < Component
329
291
ref = { containerRef }
330
292
data-overflow = { overflowStyle === 'inline' && isOverflowShown ? 'inline' : undefined }
331
293
data-list = { isList || undefined }
332
- className = { className }
333
- as = { as }
294
+ className = { clsx ( className , classes . Container ) }
334
295
>
335
296
{ React . Children . map ( children , ( child , index ) => (
336
- < ItemWrapper
297
+ < ItemWrapperComponent
337
298
// data-index is used as an identifier we can use in the IntersectionObserver
338
299
data-index = { index }
339
- className = { hiddenItemIds . includes ( index . toString ( ) ) ? 'ItemWrapper--hidden' : undefined }
340
- as = { isList ? 'li' : 'span' }
300
+ className = { clsx ( classes . ItemWrapper , {
301
+ [ classes [ 'ItemWrapper--hidden' ] ] : hiddenItemIds . includes ( index . toString ( ) ) ,
302
+ } ) }
341
303
key = { index }
342
304
>
343
305
{ child }
344
- </ ItemWrapper >
306
+ </ ItemWrapperComponent >
345
307
) ) }
346
308
< ToggleWrapper >
347
309
{ overflowStyle === 'inline' ? (
@@ -369,15 +331,15 @@ const LabelGroup: React.FC<React.PropsWithChildren<LabelGroupProps>> = ({
369
331
</ OverlayToggle >
370
332
) }
371
333
</ ToggleWrapper >
372
- </ StyledLabelGroupContainer >
334
+ </ Component >
373
335
) : (
374
- < StyledLabelGroupContainer data-overflow = "inline" data-list = { isList || undefined } as = { as } className = { className } >
336
+ < Component data-overflow = "inline" data-list = { isList || undefined } className = { clsx ( className , classes . Container ) } >
375
337
{ isList
376
338
? React . Children . map ( children , ( child , index ) => {
377
339
return < li key = { index } > { child } </ li >
378
340
} )
379
341
: children }
380
- </ StyledLabelGroupContainer >
342
+ </ Component >
381
343
)
382
344
}
383
345
0 commit comments