Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions packages/coreui-react/src/components/sidebar/CSidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, HTMLAttributes, useEffect, useRef, useState } from 'react'
import React, { ElementType, forwardRef, HTMLAttributes, useEffect, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import PropTypes from 'prop-types'
import classNames from 'classnames'
Expand All @@ -7,8 +7,13 @@ import { CBackdrop } from '../backdrop'

import { isInViewport } from '../../utils'
import { useForkedRef } from '../../hooks'
import { PolymorphicRefForwardingComponent } from '../../helpers'

export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
/**
* Component used for the root node. Either a string to use a HTML element or a component.
*/
as?: ElementType
/**
* A string of all className you want applied to the component.
*/
Expand Down Expand Up @@ -65,10 +70,14 @@ export interface CSidebarProps extends HTMLAttributes<HTMLDivElement> {
const isOnMobile = (element: HTMLDivElement) =>
Boolean(getComputedStyle(element).getPropertyValue('--cui-is-mobile'))

export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
export const CSidebar: PolymorphicRefForwardingComponent<'div', CSidebarProps> = forwardRef<
HTMLDivElement,
CSidebarProps
>(
(
{
children,
as: Component = 'div',
className,
colorScheme,
narrow,
Expand Down Expand Up @@ -183,7 +192,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(

return (
<>
<div
<Component
className={classNames(
'sidebar',
{
Expand All @@ -203,7 +212,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
ref={forkedRef}
>
{children}
</div>
</Component>
{typeof window !== 'undefined' &&
mobile &&
createPortal(
Expand All @@ -216,6 +225,7 @@ export const CSidebar = forwardRef<HTMLDivElement, CSidebarProps>(
)

CSidebar.propTypes = {
as: PropTypes.elementType,
children: PropTypes.node,
className: PropTypes.string,
colorScheme: PropTypes.oneOf(['dark', 'light']),
Expand All @@ -231,4 +241,4 @@ CSidebar.propTypes = {
visible: PropTypes.bool,
}

CSidebar.displayName = 'CSidebar'
CSidebar.displayName = 'CSidebar'