Skip to content

Commit

Permalink
✨ add ref support to Container component
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnsonMao committed Oct 2, 2023
1 parent 20cf2f3 commit aa02f87
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/components/Container/Container.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { HTMLAttributes } from 'react';
import { HTMLAttributes, forwardRef } from 'react';
import cn from '@/utils/cn';

type ContainerProps = {
as?: 'main' | 'header' | 'footer' | 'div';
} & HTMLAttributes<HTMLElement>;

function Container({ as = 'div', className, ...props }: ContainerProps) {
const Component = as;
const Container = forwardRef<HTMLDivElement, ContainerProps>(
function InternalContainer({ as = 'div', className, ...props }, ref) {
const Component = as;

return <Component className={cn('mx-auto', className)} {...props} />;
}
return (
<Component className={cn('mx-auto', className)} ref={ref} {...props} />
);
}
);

export default Container;

0 comments on commit aa02f87

Please sign in to comment.