Skip to content

Commit

Permalink
chore(templates): removes all instances of React.forwardRef (#10331)
Browse files Browse the repository at this point in the history
Fixes #10325. Since React 19, refs can now be passed directly through
props without the need for `React.forwardRef`. This greatly simplifies
components types and overall syntax.
  • Loading branch information
jacobsfletch authored Jan 3, 2025
1 parent 1cade17 commit 47e8158
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 318 deletions.
21 changes: 12 additions & 9 deletions templates/website/src/components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean
ref?: React.Ref<HTMLButtonElement>
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ asChild = false, className, size, variant, ...props }, ref) => {
const Comp = asChild ? Slot : 'button'
return (
<Comp className={cn(buttonVariants({ className, size, variant }))} ref={ref} {...props} />
)
},
)
Button.displayName = 'Button'
const Button: React.FC<ButtonProps> = ({
asChild = false,
className,
size,
variant,
ref,
...props
}) => {
const Comp = asChild ? Slot : 'button'
return <Comp className={cn(buttonVariants({ className, size, variant }))} ref={ref} {...props} />
}

export { Button, buttonVariants }
71 changes: 32 additions & 39 deletions templates/website/src/components/ui/card.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,48 @@
import { cn } from 'src/utilities/cn'
import * as React from 'react'

const Card = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
({ className, ...props }, ref) => (
<div
className={cn('rounded-lg border bg-card text-card-foreground shadow-sm', className)}
ref={ref}
{...props}
/>
),
const Card: React.FC<
{ ref?: React.Ref<HTMLDivElement> } & React.HTMLAttributes<HTMLDivElement>
> = ({ className, ref, ...props }) => (
<div
className={cn('rounded-lg border bg-card text-card-foreground shadow-sm', className)}
ref={ref}
{...props}
/>
)
Card.displayName = 'Card'

const CardHeader = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
({ className, ...props }, ref) => (
<div className={cn('flex flex-col space-y-1.5 p-6', className)} ref={ref} {...props} />
),
const CardHeader: React.FC<
{ ref?: React.Ref<HTMLDivElement> } & React.HTMLAttributes<HTMLDivElement>
> = ({ className, ref, ...props }) => (
<div className={cn('flex flex-col space-y-1.5 p-6', className)} ref={ref} {...props} />
)
CardHeader.displayName = 'CardHeader'

const CardTitle = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLHeadingElement>>(
({ className, ...props }, ref) => (
<h3
className={cn('text-2xl font-semibold leading-none tracking-tight', className)}
ref={ref}
{...props}
/>
),
const CardTitle: React.FC<
{ ref?: React.Ref<HTMLHeadingElement> } & React.HTMLAttributes<HTMLHeadingElement>
> = ({ className, ref, ...props }) => (
<h3
className={cn('text-2xl font-semibold leading-none tracking-tight', className)}
ref={ref}
{...props}
/>
)
CardTitle.displayName = 'CardTitle'

const CardDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
const CardDescription: React.FC<
{ ref?: React.Ref<HTMLParagraphElement> } & React.HTMLAttributes<HTMLParagraphElement>
> = ({ className, ref, ...props }) => (
<p className={cn('text-sm text-muted-foreground', className)} ref={ref} {...props} />
))
CardDescription.displayName = 'CardDescription'
)

const CardContent = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
({ className, ...props }, ref) => (
<div className={cn('p-6 pt-0', className)} ref={ref} {...props} />
),
const CardContent: React.FC<
{ ref?: React.Ref<HTMLDivElement> } & React.HTMLAttributes<HTMLDivElement>
> = ({ className, ref, ...props }) => (
<div className={cn('p-6 pt-0', className)} ref={ref} {...props} />
)
CardContent.displayName = 'CardContent'

const CardFooter = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
({ className, ...props }, ref) => (
<div className={cn('flex items-center p-6 pt-0', className)} ref={ref} {...props} />
),
const CardFooter: React.FC<
{ ref?: React.Ref<HTMLDivElement> } & React.HTMLAttributes<HTMLDivElement>
> = ({ className, ref, ...props }) => (
<div className={cn('flex items-center p-6 pt-0', className)} ref={ref} {...props} />
)
CardFooter.displayName = 'CardFooter'

export { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle }
12 changes: 6 additions & 6 deletions templates/website/src/components/ui/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import * as CheckboxPrimitive from '@radix-ui/react-checkbox'
import { Check } from 'lucide-react'
import * as React from 'react'

const Checkbox = React.forwardRef<
React.ElementRef<typeof CheckboxPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
>(({ className, ...props }, ref) => (
const Checkbox: React.FC<
{
ref?: React.Ref<HTMLButtonElement>
} & React.ComponentProps<typeof CheckboxPrimitive.Root>
> = ({ className, ref, ...props }) => (
<CheckboxPrimitive.Root
className={cn(
'peer h-4 w-4 shrink-0 rounded border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground',
Expand All @@ -21,7 +22,6 @@ const Checkbox = React.forwardRef<
<Check className="h-4 w-4" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
))
Checkbox.displayName = CheckboxPrimitive.Root.displayName
)

export { Checkbox }
35 changes: 17 additions & 18 deletions templates/website/src/components/ui/input.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { cn } from 'src/utilities/cn'
import * as React from 'react'

export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {}

const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ type, className, ...props }, ref) => {
return (
<input
className={cn(
'flex h-10 w-full rounded border border-border bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
className,
)}
ref={ref}
type={type}
{...props}
/>
)
},
)
Input.displayName = 'Input'
const Input: React.FC<
{
ref?: React.Ref<HTMLInputElement>
} & React.InputHTMLAttributes<HTMLInputElement>
> = ({ type, className, ref, ...props }) => {
return (
<input
className={cn(
'flex h-10 w-full rounded border border-border bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
className,
)}
ref={ref}
type={type}
{...props}
/>
)
}

export { Input }
11 changes: 5 additions & 6 deletions templates/website/src/components/ui/label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ const labelVariants = cva(
'text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70',
)

const Label = React.forwardRef<
React.ElementRef<typeof LabelPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> & VariantProps<typeof labelVariants>
>(({ className, ...props }, ref) => (
const Label: React.FC<
{ ref?: React.Ref<HTMLLabelElement> } & React.ComponentProps<typeof LabelPrimitive.Root> &
VariantProps<typeof labelVariants>
> = ({ className, ref, ...props }) => (
<LabelPrimitive.Root className={cn(labelVariants(), className)} ref={ref} {...props} />
))
Label.displayName = LabelPrimitive.Root.displayName
)

export { Label }
21 changes: 7 additions & 14 deletions templates/website/src/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ const Pagination = ({ className, ...props }: React.ComponentProps<'nav'>) => (
{...props}
/>
)
Pagination.displayName = 'Pagination'

const PaginationContent = React.forwardRef<HTMLUListElement, React.ComponentProps<'ul'>>(
({ className, ...props }, ref) => (
<ul className={cn('flex flex-row items-center gap-1', className)} ref={ref} {...props} />
),
const PaginationContent: React.FC<
{ ref?: React.Ref<HTMLUListElement> } & React.HTMLAttributes<HTMLUListElement>
> = ({ className, ref, ...props }) => (
<ul className={cn('flex flex-row items-center gap-1', className)} ref={ref} {...props} />
)
PaginationContent.displayName = 'PaginationContent'

const PaginationItem = React.forwardRef<HTMLLIElement, React.ComponentProps<'li'>>(
({ className, ...props }, ref) => <li className={cn('', className)} ref={ref} {...props} />,
)
PaginationItem.displayName = 'PaginationItem'
const PaginationItem: React.FC<
{ ref?: React.Ref<HTMLLIElement> } & React.HTMLAttributes<HTMLLIElement>
> = ({ className, ref, ...props }) => <li className={cn('', className)} ref={ref} {...props} />

type PaginationLinkProps = {
isActive?: boolean
Expand All @@ -45,7 +42,6 @@ const PaginationLink = ({ className, isActive, size = 'icon', ...props }: Pagina
{...props}
/>
)
PaginationLink.displayName = 'PaginationLink'

const PaginationPrevious = ({
className,
Expand All @@ -61,7 +57,6 @@ const PaginationPrevious = ({
<span>Previous</span>
</PaginationLink>
)
PaginationPrevious.displayName = 'PaginationPrevious'

const PaginationNext = ({ className, ...props }: React.ComponentProps<typeof PaginationLink>) => (
<PaginationLink
Expand All @@ -74,7 +69,6 @@ const PaginationNext = ({ className, ...props }: React.ComponentProps<typeof Pag
<ChevronRight className="h-4 w-4" />
</PaginationLink>
)
PaginationNext.displayName = 'PaginationNext'

const PaginationEllipsis = ({ className, ...props }: React.ComponentProps<'span'>) => (
<span
Expand All @@ -86,7 +80,6 @@ const PaginationEllipsis = ({ className, ...props }: React.ComponentProps<'span'
<span className="sr-only">More pages</span>
</span>
)
PaginationEllipsis.displayName = 'PaginationEllipsis'

export {
Pagination,
Expand Down
76 changes: 34 additions & 42 deletions templates/website/src/components/ui/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ const SelectGroup = SelectPrimitive.Group

const SelectValue = SelectPrimitive.Value

const SelectTrigger = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
>(({ children, className, ...props }, ref) => (
const SelectTrigger: React.FC<
{ ref?: React.Ref<HTMLButtonElement> } & React.ComponentProps<typeof SelectPrimitive.Trigger>
> = ({ children, className, ref, ...props }) => (
<SelectPrimitive.Trigger
className={cn(
'flex h-10 w-full items-center justify-between rounded border border-input bg-background px-3 py-2 text-inherit ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1',
Expand All @@ -28,41 +27,39 @@ const SelectTrigger = React.forwardRef<
<ChevronDown className="h-4 w-4 opacity-50" />
</SelectPrimitive.Icon>
</SelectPrimitive.Trigger>
))
SelectTrigger.displayName = SelectPrimitive.Trigger.displayName
)

const SelectScrollUpButton = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
>(({ className, ...props }, ref) => (
const SelectScrollUpButton: React.FC<
{ ref?: React.Ref<HTMLDivElement> } & React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>
> = ({ className, ref, ...props }) => (
<SelectPrimitive.ScrollUpButton
className={cn('flex cursor-default items-center justify-center py-1', className)}
ref={ref}
{...props}
>
<ChevronUp className="h-4 w-4" />
</SelectPrimitive.ScrollUpButton>
))
SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName
)

const SelectScrollDownButton = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
>(({ className, ...props }, ref) => (
const SelectScrollDownButton: React.FC<
{ ref?: React.Ref<HTMLDivElement> } & React.ComponentProps<
typeof SelectPrimitive.ScrollDownButton
>
> = ({ className, ref, ...props }) => (
<SelectPrimitive.ScrollDownButton
className={cn('flex cursor-default items-center justify-center py-1', className)}
ref={ref}
{...props}
>
<ChevronDown className="h-4 w-4" />
</SelectPrimitive.ScrollDownButton>
))
SelectScrollDownButton.displayName = SelectPrimitive.ScrollDownButton.displayName
)

const SelectContent = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
>(({ children, className, position = 'popper', ...props }, ref) => (
const SelectContent: React.FC<
{
ref?: React.Ref<HTMLDivElement>
} & React.ComponentProps<typeof SelectPrimitive.Content>
> = ({ children, className, position = 'popper', ref, ...props }) => (
<SelectPrimitive.Portal>
<SelectPrimitive.Content
className={cn(
Expand All @@ -88,25 +85,23 @@ const SelectContent = React.forwardRef<
<SelectScrollDownButton />
</SelectPrimitive.Content>
</SelectPrimitive.Portal>
))
SelectContent.displayName = SelectPrimitive.Content.displayName
)

const SelectLabel = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
>(({ className, ...props }, ref) => (
const SelectLabel: React.FC<
{ ref?: React.Ref<HTMLDivElement> } & React.ComponentProps<typeof SelectPrimitive.Label>
> = ({ className, ref, ...props }) => (
<SelectPrimitive.Label
className={cn('py-1.5 pl-8 pr-2 text-sm font-semibold', className)}
ref={ref}
{...props}
/>
))
SelectLabel.displayName = SelectPrimitive.Label.displayName
)

const SelectItem = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
>(({ children, className, ...props }, ref) => (
const SelectItem: React.FC<
{ ref?: React.Ref<HTMLDivElement>; value: string } & React.ComponentProps<
typeof SelectPrimitive.Item
>
> = ({ children, className, ref, ...props }) => (
<SelectPrimitive.Item
className={cn(
'relative flex w-full cursor-default select-none items-center rounded py-1.5 pl-8 pr-2 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
Expand All @@ -123,20 +118,17 @@ const SelectItem = React.forwardRef<

<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
))
SelectItem.displayName = SelectPrimitive.Item.displayName
)

const SelectSeparator = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
>(({ className, ...props }, ref) => (
const SelectSeparator: React.FC<
{ ref?: React.Ref<HTMLDivElement> } & React.ComponentProps<typeof SelectPrimitive.Separator>
> = ({ className, ref, ...props }) => (
<SelectPrimitive.Separator
className={cn('-mx-1 my-1 h-px bg-muted', className)}
ref={ref}
{...props}
/>
))
SelectSeparator.displayName = SelectPrimitive.Separator.displayName
)

export {
Select,
Expand Down
Loading

0 comments on commit 47e8158

Please sign in to comment.