mirror of
https://github.com/ershisan99/flashcards-example-project.git
synced 2025-12-18 12:33:22 +00:00
47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
import {
|
|
ComponentPropsWithoutRef,
|
|
ElementRef,
|
|
ElementType,
|
|
ForwardedRef,
|
|
ReactNode,
|
|
forwardRef,
|
|
} from 'react'
|
|
|
|
import { clsx } from 'clsx'
|
|
|
|
import s from './button.module.scss'
|
|
|
|
export type ButtonProps<T extends ElementType = 'button'> = {
|
|
as?: T
|
|
children: ReactNode
|
|
className?: string
|
|
fullWidth?: boolean
|
|
variant?: 'icon' | 'link' | 'primary' | 'secondary' | 'tertiary'
|
|
} & ComponentPropsWithoutRef<T>
|
|
|
|
const ButtonPolymorph = <T extends ElementType = 'button'>(props: ButtonProps<T>, ref: any) => {
|
|
const {
|
|
as: Component = 'button',
|
|
className,
|
|
fullWidth,
|
|
rounded,
|
|
variant = 'primary',
|
|
...rest
|
|
} = props
|
|
|
|
return (
|
|
<Component
|
|
className={clsx(s[variant], fullWidth && s.fullWidth, className)}
|
|
{...rest}
|
|
ref={ref}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export const Button = forwardRef(ButtonPolymorph) as <T extends ElementType = 'button'>(
|
|
props: ButtonProps<T> &
|
|
Omit<ComponentPropsWithoutRef<T>, keyof ButtonProps<T>> & {
|
|
ref?: ForwardedRef<ElementRef<T>>
|
|
}
|
|
) => ReturnType<typeof ButtonPolymorph>
|