Skip to content

feat(CSidebar): add 'as' prop to CSidebar #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2024
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'