1
- import React , { forwardRef , HTMLAttributes , useEffect , useRef , useState } from 'react'
1
+ import React , { ElementType , forwardRef , HTMLAttributes , useEffect , useRef , useState } from 'react'
2
2
import { createPortal } from 'react-dom'
3
3
import PropTypes from 'prop-types'
4
4
import classNames from 'classnames'
@@ -7,8 +7,13 @@ import { CBackdrop } from '../backdrop'
7
7
8
8
import { isInViewport } from '../../utils'
9
9
import { useForkedRef } from '../../hooks'
10
+ import { PolymorphicRefForwardingComponent } from '../../helpers'
10
11
11
12
export interface CSidebarProps extends HTMLAttributes < HTMLDivElement > {
13
+ /**
14
+ * Component used for the root node. Either a string to use a HTML element or a component.
15
+ */
16
+ as ?: ElementType
12
17
/**
13
18
* A string of all className you want applied to the component.
14
19
*/
@@ -65,10 +70,14 @@ export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
65
70
const isOnMobile = ( element : HTMLDivElement ) =>
66
71
Boolean ( getComputedStyle ( element ) . getPropertyValue ( '--cui-is-mobile' ) )
67
72
68
- export const CSidebar = forwardRef < HTMLDivElement , CSidebarProps > (
73
+ export const CSidebar : PolymorphicRefForwardingComponent < 'div' , CSidebarProps > = forwardRef <
74
+ HTMLDivElement ,
75
+ CSidebarProps
76
+ > (
69
77
(
70
78
{
71
79
children,
80
+ as : Component = 'div' ,
72
81
className,
73
82
colorScheme,
74
83
narrow,
@@ -183,7 +192,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
183
192
184
193
return (
185
194
< >
186
- < div
195
+ < Component
187
196
className = { classNames (
188
197
'sidebar' ,
189
198
{
@@ -203,7 +212,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
203
212
ref = { forkedRef }
204
213
>
205
214
{ children }
206
- </ div >
215
+ </ Component >
207
216
{ typeof window !== 'undefined' &&
208
217
mobile &&
209
218
createPortal (
@@ -216,6 +225,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
216
225
)
217
226
218
227
CSidebar . propTypes = {
228
+ as : PropTypes . elementType ,
219
229
children : PropTypes . node ,
220
230
className : PropTypes . string ,
221
231
colorScheme : PropTypes . oneOf ( [ 'dark' , 'light' ] ) ,
@@ -231,4 +241,4 @@ CSidebar.propTypes = {
231
241
visible : PropTypes . bool ,
232
242
}
233
243
234
- CSidebar . displayName = 'CSidebar'
244
+ CSidebar . displayName = 'CSidebar'
0 commit comments