11// Used for UnderlineNav and UnderlinePanels components
22
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'
55import { isElement } from 'react-is'
66import type { IconProps } from '@primer/octicons-react'
77import CounterLabel from '../../CounterLabel'
8- import { type SxProp } from '../../sx'
98import type { ForwardRefComponent as PolymorphicForwardRefComponent } from '../../utils/polymorphic'
109
1110import classes from './UnderlineTabbedInterface.module.css'
1211import { clsx } from 'clsx'
13- import { BoxWithFallback } from './BoxWithFallback'
1412
1513// 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.
1614export const GAP = 8
1715
18- type UnderlineWrapperProps = {
16+ type UnderlineWrapperProps < As extends React . ElementType > = {
1917 slot ?: string
20- as ?: React . ElementType
18+ as ?: As
2119 className ?: string
22- ref ?: React . Ref < unknown >
23- } & SxProp
20+ ref ?: React . Ref < HTMLElement >
21+ }
2422
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 > >
3435
3536export const UnderlineItemList = forwardRef ( ( { children, ...rest } : PropsWithChildren , forwardedRef ) => {
3637 return (
@@ -44,51 +45,38 @@ export const LoadingCounter = () => {
4445 return < span className = { classes . LoadingCounter } />
4546}
4647
47- export type UnderlineItemProps = {
48- as ?: React . ElementType | 'a' | 'button'
48+ export type UnderlineItemProps < As extends React . ElementType > = {
49+ as ?: As | 'a' | 'button'
4950 className ?: string
5051 iconsVisible ?: boolean
5152 loadingCounters ?: boolean
5253 counter ?: number | string
5354 icon ?: FC < IconProps > | React . ReactElement
5455 id ?: string
5556 ref ?: React . Ref < unknown >
56- } & SxProp
57+ } & React . ComponentPropsWithoutRef < As extends 'a' ? 'a' : As extends 'button' ? 'button' : As >
5758
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 />
7873 </ 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