Skip to content

Commit caa1007

Browse files
authored
Merge pull request #421 from matt-jb/add-as-prop-to-sidebar
feat(CSidebar): add 'as' prop to CSidebar
2 parents 746377d + 72ec01f commit caa1007

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

packages/coreui-react/src/components/sidebar/CSidebar.tsx

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, HTMLAttributes, useEffect, useRef, useState } from 'react'
1+
import React, { ElementType, forwardRef, HTMLAttributes, useEffect, useRef, useState } from 'react'
22
import { createPortal } from 'react-dom'
33
import PropTypes from 'prop-types'
44
import classNames from 'classnames'
@@ -7,8 +7,13 @@ import { CBackdrop } from '../backdrop'
77

88
import { isInViewport } from '../../utils'
99
import { useForkedRef } from '../../hooks'
10+
import { PolymorphicRefForwardingComponent } from '../../helpers'
1011

1112
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
1217
/**
1318
* A string of all className you want applied to the component.
1419
*/
@@ -65,10 +70,14 @@ export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
6570
const isOnMobile = (element: HTMLDivElement) =>
6671
Boolean(getComputedStyle(element).getPropertyValue('--cui-is-mobile'))
6772

68-
export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
73+
export const CSidebar: PolymorphicRefForwardingComponent<'div', CSidebarProps> = forwardRef<
74+
HTMLDivElement,
75+
CSidebarProps
76+
>(
6977
(
7078
{
7179
children,
80+
as: Component = 'div',
7281
className,
7382
colorScheme,
7483
narrow,
@@ -183,7 +192,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
183192

184193
return (
185194
<>
186-
<div
195+
<Component
187196
className={classNames(
188197
'sidebar',
189198
{
@@ -203,7 +212,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
203212
ref={forkedRef}
204213
>
205214
{children}
206-
</div>
215+
</Component>
207216
{typeof window !== 'undefined' &&
208217
mobile &&
209218
createPortal(
@@ -216,6 +225,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
216225
)
217226

218227
CSidebar.propTypes = {
228+
as: PropTypes.elementType,
219229
children: PropTypes.node,
220230
className: PropTypes.string,
221231
colorScheme: PropTypes.oneOf(['dark', 'light']),
@@ -231,4 +241,4 @@ CSidebar.propTypes = {
231241
visible: PropTypes.bool,
232242
}
233243

234-
CSidebar.displayName = 'CSidebar'
244+
CSidebar.displayName = 'CSidebar'

0 commit comments

Comments
 (0)