1
1
// Used for UnderlineNav and UnderlinePanels components
2
2
3
- import type React from 'react'
4
- import { forwardRef , type FC , type PropsWithChildren } from 'react'
3
+ import React from 'react'
4
+ import { type ForwardedRef , forwardRef , type FC , type PropsWithChildren , type ElementType } from 'react'
5
5
import { isElement } from 'react-is'
6
6
import type { IconProps } from '@primer/octicons-react'
7
7
import CounterLabel from '../../CounterLabel'
8
- import { type SxProp } from '../../sx'
9
8
import type { ForwardRefComponent as PolymorphicForwardRefComponent } from '../../utils/polymorphic'
10
9
11
10
import classes from './UnderlineTabbedInterface.module.css'
12
11
import { clsx } from 'clsx'
13
- import { BoxWithFallback } from './BoxWithFallback'
14
12
15
13
// The gap between the list items. It is a constant because the gap is used to calculate the possible number of items that can fit in the container.
16
14
export const GAP = 8
17
15
18
- type UnderlineWrapperProps = {
16
+ type UnderlineWrapperProps < As extends React . ElementType > = {
19
17
slot ?: string
20
- as ?: React . ElementType
18
+ as ?: As
21
19
className ?: string
22
- ref ?: React . Ref < unknown >
23
- } & SxProp
20
+ ref ?: React . Ref < HTMLElement >
21
+ }
24
22
25
- export const UnderlineWrapper = forwardRef (
26
- ( { children, className, ...rest } : PropsWithChildren < UnderlineWrapperProps > , forwardedRef ) => {
27
- return (
28
- < BoxWithFallback className = { clsx ( classes . UnderlineWrapper , className ) } ref = { forwardedRef } { ...rest } >
29
- { children }
30
- </ BoxWithFallback >
31
- )
32
- } ,
33
- )
23
+ export const UnderlineWrapper = forwardRef ( ( props , ref ) => {
24
+ const { children, className, as : Component = 'nav' , ...rest } = props
25
+ return (
26
+ < Component
27
+ className = { clsx ( classes . UnderlineWrapper , className ) }
28
+ ref = { ref as ForwardedRef < HTMLDivElement > }
29
+ { ...rest }
30
+ >
31
+ { children }
32
+ </ Component >
33
+ )
34
+ } ) as PolymorphicForwardRefComponent < ElementType , UnderlineWrapperProps < ElementType > >
34
35
35
36
export const UnderlineItemList = forwardRef ( ( { children, ...rest } : PropsWithChildren , forwardedRef ) => {
36
37
return (
@@ -44,51 +45,38 @@ export const LoadingCounter = () => {
44
45
return < span className = { classes . LoadingCounter } />
45
46
}
46
47
47
- export type UnderlineItemProps = {
48
- as ?: React . ElementType | 'a' | 'button'
48
+ export type UnderlineItemProps < As extends React . ElementType > = {
49
+ as ?: As | 'a' | 'button'
49
50
className ?: string
50
51
iconsVisible ?: boolean
51
52
loadingCounters ?: boolean
52
53
counter ?: number | string
53
54
icon ?: FC < IconProps > | React . ReactElement
54
55
id ?: string
55
56
ref ?: React . Ref < unknown >
56
- } & SxProp
57
+ } & React . ComponentPropsWithoutRef < As extends 'a' ? 'a' : As extends 'button' ? 'button' : As >
57
58
58
- export const UnderlineItem = forwardRef (
59
- (
60
- {
61
- as = 'a' ,
62
- children,
63
- counter,
64
- icon : Icon ,
65
- iconsVisible,
66
- loadingCounters,
67
- className,
68
- ...rest
69
- } : PropsWithChildren < UnderlineItemProps > ,
70
- forwardedRef ,
71
- ) => {
72
- return (
73
- < BoxWithFallback ref = { forwardedRef } as = { as } className = { clsx ( classes . UnderlineItem , className ) } { ...rest } >
74
- { iconsVisible && Icon && < span data-component = "icon" > { isElement ( Icon ) ? Icon : < Icon /> } </ span > }
75
- { children && (
76
- < span data-component = "text" data-content = { children } >
77
- { children }
59
+ export const UnderlineItem = React . forwardRef ( ( props , ref ) => {
60
+ const { as : Component = 'a' , children, counter, icon : Icon , iconsVisible, loadingCounters, className, ...rest } = props
61
+ return (
62
+ < Component { ...rest } ref = { ref } className = { clsx ( classes . UnderlineItem , className ) } >
63
+ { iconsVisible && Icon && < span data-component = "icon" > { isElement ( Icon ) ? Icon : < Icon /> } </ span > }
64
+ { children && (
65
+ < span data-component = "text" data-content = { children } >
66
+ { children }
67
+ </ span >
68
+ ) }
69
+ { counter !== undefined ? (
70
+ loadingCounters ? (
71
+ < span data-component = "counter" >
72
+ < LoadingCounter />
78
73
</ span >
79
- ) }
80
- { counter !== undefined ? (
81
- loadingCounters ? (
82
- < span data-component = "counter" >
83
- < LoadingCounter />
84
- </ span >
85
- ) : (
86
- < span data-component = "counter" >
87
- < CounterLabel > { counter } </ CounterLabel >
88
- </ span >
89
- )
90
- ) : null }
91
- </ BoxWithFallback >
92
- )
93
- } ,
94
- ) as PolymorphicForwardRefComponent < 'a' , UnderlineItemProps >
74
+ ) : (
75
+ < span data-component = "counter" >
76
+ < CounterLabel > { counter } </ CounterLabel >
77
+ </ span >
78
+ )
79
+ ) : null }
80
+ </ Component >
81
+ )
82
+ } ) as PolymorphicForwardRefComponent < ElementType , UnderlineItemProps < ElementType > >
0 commit comments