Skip to content

Commit

Permalink
Merge pull request #199 from gloddy-dev/feature/198-flex-component
Browse files Browse the repository at this point in the history
Component : Flex 컴포넌트 구현
  • Loading branch information
kangju2000 authored Aug 17, 2023
2 parents 38f49ff + 2ef3fd7 commit ccce94b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/components/Layout/Flex.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import cn from '@/utils/cn';
import { forwardRef } from 'react';

import type { StrictPropsWithChildren } from '@/types';

interface FlexProps<T extends React.ElementType> extends React.HTMLAttributes<T> {
as?: T;
children?: React.ReactNode;
direction?: 'row' | 'column';
justify?: 'center' | 'start' | 'end' | 'between' | 'around' | 'evenly' | 'stretch';
align?: 'center' | 'start' | 'end' | 'between' | 'around' | 'evenly' | 'stretch';
wrap?: 'wrap' | 'nowrap' | 'wrap-reverse';
className?: string;
}

export default forwardRef(function Flex<T extends React.ElementType>(
{
as,
children,
direction,
justify,
align,
wrap,
className,
...props
}: StrictPropsWithChildren<FlexProps<T> & React.ComponentPropsWithoutRef<T>>,
ref: React.ComponentPropsWithRef<T>['ref']
) {
const Element = as ?? 'div';

return (
<Element
ref={ref}
className={
(cn(
'flex',
{
'justify-center': justify === 'center',
'justify-start': justify === 'start',
'justify-end': justify === 'end',
'justify-between': justify === 'between',
'justify-around': justify === 'around',
'justify-evenly': justify === 'evenly',
'justify-stretch': justify === 'stretch',
},
{
'items-center': align === 'center',
'items-start': align === 'start',
'items-end': align === 'end',
'items-between': align === 'between',
'items-around': align === 'around',
'items-evenly': align === 'evenly',
'items-stretch': align === 'stretch',
},
{
'flex-row': direction === 'row',
'flex-column': direction === 'column',
},
{
'flex-wrap': wrap === 'wrap',
'flex-nowrap': wrap === 'nowrap',
'flex-wrap-reverse': wrap === 'wrap-reverse',
}
),
className)
}
{...props}
>
{children}
</Element>
);
}) as <T extends React.ElementType>(
props: StrictPropsWithChildren<FlexProps<T> & React.ComponentPropsWithoutRef<T>>
) => JSX.Element;
1 change: 1 addition & 0 deletions src/components/Layout/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Flex } from './Flex';

0 comments on commit ccce94b

Please sign in to comment.