Skip to content

Commit

Permalink
feat: export Card component as forwardRef
Browse files Browse the repository at this point in the history
  • Loading branch information
gndz07 authored Jan 13, 2023
1 parent 005a545 commit c20b199
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions components/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, ComponentProps, ElementRef } from 'react';
import React, { ComponentProps, ElementRef } from 'react';
import { styled, VariantProps } from '../../stitches.config';
import { elevationVariants } from '../Elevation/Elevation';

Expand Down Expand Up @@ -71,21 +71,34 @@ const StyledInteractiveCard = styled('button', StyledCard, {
},
});

interface CardVariantProps
export interface CardVariantProps
extends ComponentProps<typeof StyledCard>,
VariantProps<typeof StyledCard> {
interactive?: false;
}
interface InteractiveCardProps
export interface InteractiveCardProps
extends VariantProps<typeof StyledInteractiveCard>,
ComponentProps<typeof StyledInteractiveCard> {
interactive: true;
}
export type CardProps = CardVariantProps | InteractiveCardProps;

export const Card = ({ interactive, ...props }: CardProps) => {
if (interactive) {
return <StyledInteractiveCard tabIndex={0} {...(props as InteractiveCardProps)} />;
export const Card = React.forwardRef<ElementRef<typeof StyledCard>, CardProps>(
({ interactive, ...props }, forwardedRef) => {
if (interactive) {
return (
<StyledInteractiveCard
tabIndex={0}
ref={forwardedRef as React.RefObject<HTMLButtonElement>}
{...(props as InteractiveCardProps)}
/>
);
}
return (
<StyledCard
ref={forwardedRef as React.RefObject<HTMLDivElement>}
{...(props as CardVariantProps)}
/>
);
}
return <StyledCard {...(props as CardVariantProps)} />;
};
);

0 comments on commit c20b199

Please sign in to comment.