Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DESIGN-15 Menu, InfoPopover Component 추가 #83

Open
wants to merge 1 commit into
base: DESIGN-12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/components/Avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { AvatarProps } from '@/core/components/Avatar/types';
import { getFirstLetter } from '@/utilities/letter';
import Icon from '@/core/components/Icon';
import getAvatarColorTheme from '@/core/components/Avatar/utils/getAvatarColorTheme';
import Popover from '@/core/components/Popover';
import Popover from '@/core/components/Popover/PopoverBase';

const Avatar = ({
src,
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/Avatar/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HTMLAttributes } from 'react';
import { AVATAR_SIZE_VARIANTS } from '@/core/components/Avatar/constants';
import { RoundedType } from '@/core/components/Button/ButtonBase/types';
import { ColorThemeType } from '@/types';
import { PopoverProps } from '@/core/components/Popover/types';
import { PopoverProps } from '@/core/components/Popover/PopoverBase/types';

export type AvatarSizeVariants =
(typeof AVATAR_SIZE_VARIANTS)[keyof typeof AVATAR_SIZE_VARIANTS];
Expand Down
4 changes: 4 additions & 0 deletions src/core/components/Button/ButtonBase/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const GAP = {
GAP_12: 'gap-12',
GAP_10: 'gap-10',
GAP_8: 'gap-8',
GAP_6: 'gap-6',
GAP_4: 'gap-4',
} as const;

export const BUTTON_SIZE: Record<SizeType, string> = {
Expand All @@ -48,4 +50,6 @@ export const BUTTON_GAP: Record<GapType, string> = {
[GAP['GAP_12']]: 'gap-x-3',
[GAP['GAP_10']]: 'gap-x-2.5',
[GAP['GAP_8']]: 'gap-x-2',
[GAP['GAP_6']]: 'gap-x-1.5',
[GAP['GAP_4']]: 'gap-x-1',
};
36 changes: 36 additions & 0 deletions src/core/components/Menu/Menu.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Meta } from '@storybook/react';

import IconButton from '@/core/components/Button/IconButton';
import Icon from '@/core/components/Icon';
import Menu from '@/core/components/Menu';

const meta = {
title: 'core/Menu',
component: Menu,
} satisfies Meta<typeof Menu>;

export default meta;

export const Default = () => {
return (
<Menu
trigger={
<IconButton
colorTheme={'secondary'}
icon={<Icon iconKey={'gear'} weight={'fill'} className={'text-lg'} />}
size={'h-40'}
/>
}
items={[
<Menu.Item key={'update'} content={'수정'} />,
<Menu.Item
key={'delete'}
content={'삭제'}
colorTheme={'error'}
rightIcon={<Icon iconKey={'trash'} weight={'bold'} />}
/>,
]}
popoverOptions={{ className: 'w-20' }}
/>
);
};
60 changes: 60 additions & 0 deletions src/core/components/Menu/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ComponentPropsWithoutRef, ElementType, MouseEvent } from 'react';
import clsx from 'clsx';

import { LIGHT_COLOR_THEME } from '@/constants/theme';
import { MENU_ITEM_THEME } from './constants';
import { BUTTON_GAP, GAP } from '@/core/components/Button/ButtonBase/constants';
import { MenuItemProps } from './types';

const MenuItem = <T extends ElementType = 'button'>({
element: Element,
content,
leftIcon,
rightIcon,
className,
disabled,
onClick,
gap = GAP['GAP_6'],
colorTheme = LIGHT_COLOR_THEME['SECONDARY'],
...props
}: MenuItemProps<T> & ComponentPropsWithoutRef<T>) => {
const Component: ElementType = Element || 'button';
const defaultProps =
Component === 'button' ? { disabled, type: 'button' } : {};

const handleClick = (e: MouseEvent<T>) => {
e.preventDefault();

if (!disabled) {
onClick?.(e);
}
};

return (
<li
className={clsx(
'text-body-02-medium',
!disabled && 'rounded-md transition-all',
MENU_ITEM_THEME[colorTheme],
)}
>
<Component
className={clsx(
'flex w-full items-center p-2 text-start',
disabled && 'cursor-not-allowed text-gray-04',
(leftIcon || rightIcon) && gap && BUTTON_GAP[gap],
className,
)}
onClick={handleClick}
{...defaultProps}
{...props}
>
{leftIcon && leftIcon}
{content}
{rightIcon && rightIcon}
</Component>
</li>
);
};

export default MenuItem;
7 changes: 7 additions & 0 deletions src/core/components/Menu/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LIGHT_COLOR_THEME } from '@/constants/theme';
import { MenuItemColorTheme } from '@/core/components/Menu/types';

export const MENU_ITEM_THEME: Record<MenuItemColorTheme, string> = {
[LIGHT_COLOR_THEME['ERROR']]: 'text-rose-600 hover:bg-[#FFEDEF]',
[LIGHT_COLOR_THEME['SECONDARY']]: 'hover:bg-gray-00 hover:text-primary-03',
} as const;
27 changes: 27 additions & 0 deletions src/core/components/Menu/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import clsx from 'clsx';

import { MenuProps, MenuReturnType } from '@/core/components/Menu/types';
import Popover from '@/core/components/Popover/PopoverBase';
import MenuPopoverItem from '@/core/components/Menu/MenuItem';

const Menu = ({ items, popoverOptions = {}, ...props }: MenuProps) => {
const { className, ...rest } = popoverOptions;

return (
<Popover
popover={<ul className={'flex-v-stack gap-y-1'}>{items}</ul>}
popoverOptions={{
backgroundColor: 'white',
hasShadow: true,
className: clsx('p-1.5', className),
...rest,
}}
{...props}
/>
);
};

export default Menu as unknown as MenuReturnType;

Menu.displayName = 'Menu';
Menu.Item = MenuPopoverItem;
31 changes: 31 additions & 0 deletions src/core/components/Menu/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ElementType, ReactElement, ReactNode } from 'react';

import { ButtonProps } from '@/core/components/Button/Button/types';
import { LightColorThemeType } from '@/types';
import { PopoverProps } from '@/core/components/Popover/PopoverBase/types';
import MenuPopoverItem from '@/core/components/Menu/MenuItem';

export interface MenuProps extends Omit<PopoverProps, 'popover'> {
items: ReactNode[];
}

export type MenuItemColorTheme = Extract<
LightColorThemeType,
'error' | 'secondary'
>;

export interface MenuItemProps<T extends ElementType>
extends Pick<
ButtonProps,
'content' | 'leftIcon' | 'rightIcon' | 'disabled' | 'gap'
> {
element?: T;
colorTheme?: MenuItemColorTheme;
}

type Menu = (props: MenuProps) => ReactElement;

export type MenuReturnType = Menu & {
displayName: string;
Item: typeof MenuPopoverItem;
};
53 changes: 53 additions & 0 deletions src/core/components/Popover/InfoPopover/InfoPopover.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Meta } from '@storybook/react';

import IconButton from '@/core/components/Button/IconButton';
import Icon from '@/core/components/Icon';
import InfoPopover from '@/core/components/Popover/InfoPopover/index';

const meta = {
title: 'core/InfoPopover',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3: 디렉터리 구조로 맞춰주는건 어떨까요?

Suggested change
title: 'core/InfoPopover',
title: 'core/Popover/InfoPopover',

component: InfoPopover,
argTypes: {
heading: {
control: 'text',
description: 'Information Popover Heading',
},
items: {
control: 'object',
description: 'Information Popover Items',
},
},
} satisfies Meta<typeof InfoPopover>;

export default meta;

export const Default = () => {
const data = [
{
title: '제목1',
description: '설명',
},
{
title: '제목2',
description: '설명',
},
];

return (
<InfoPopover
heading={'정보 설명'}
trigger={
<IconButton
colorTheme={'warning'}
rounded={'rounded-full'}
icon={
<Icon iconKey={'question'} weight={'bold'} className={'text-lg'} />
}
size={'h-40'}
/>
}
items={data}
popoverOptions={{ className: 'w-32' }}
/>
);
};
67 changes: 67 additions & 0 deletions src/core/components/Popover/InfoPopover/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { InfoPopoverProps } from '@/core/components/Popover/InfoPopover/types';
import Typography from '@/core/components/Typography';
import Icon from '@/core/components/Icon';
import Popover from '@/core/components/Popover/PopoverBase';

const InfoPopover = ({
trigger,
heading,
items,
popoverOptions = {},
...props
}: InfoPopoverProps) => {
const ITEMS = () => {
return items.map(({ title, description }) => (
<li key={title + description}>
<div className={'flex gap-x-1'}>
<Typography
theme={'body-02-bold'}
color={'primary-03'}
text={
<>
<Icon
iconKey={'circle'}
weight={'fill'}
className={'text-[6px]'}
/>
{title}
</>
}
className={'flex gap-1'}
/>
</div>
<Typography
element={'p'}
theme={'body-02-regular'}
text={description}
/>
</li>
));
};

return (
<Popover
trigger={trigger}
popover={
<>
<Typography
element={'strong'}
theme={'body-02-bold'}
color={'primary-06'}
text={heading}
className={'block border-b p-3'}
/>
<ul className={'flex-v-stack gap-y-4 p-3'}>{ITEMS()}</ul>
</>
}
popoverOptions={{
backgroundColor: 'white',
hasShadow: true,
...popoverOptions,
}}
{...props}
/>
);
};

export default InfoPopover;
6 changes: 6 additions & 0 deletions src/core/components/Popover/InfoPopover/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { PopoverProps } from '@/core/components/Popover/PopoverBase/types';

export interface InfoPopoverProps extends Omit<PopoverProps, 'popover'> {
heading: string;
items: { title: string; description: string }[];
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meta } from '@storybook/react';
import { useRef } from 'react';

import { PopoverProps } from '@/core/components/Popover/types';
import { PopoverProps } from '@/core/components/Popover/PopoverBase/types';
import Popover from './index';
import Button from '@/core/components/Button/Button';
import Section from '@/core/components/Section';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from 'react';

import { PopoverProps } from '@/core/components/Popover/types';
import { PopoverProps } from '@/core/components/Popover/PopoverBase/types';

export const useRootScrollLockEffect = ({
isOpen,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from 'react';

import { getPopoverPosition } from '@/core/components/Popover/utils/getPopoverPosition';
import { UseUpdatePopoverPositionEffectProps } from '@/core/components/Popover/types/PopoverPosition';
import { getPopoverPosition } from '@/core/components/Popover/PopoverBase/utils/getPopoverPosition';
import { UseUpdatePopoverPositionEffectProps } from '@/core/components/Popover/PopoverBase/types/PopoverPosition';

export const useUpdatePopoverPositionEffect = ({
isOpen,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CSSProperties, useRef, useState } from 'react';

import { useUpdatePopoverPositionEffect } from '@/core/components/Popover/hooks/effects/useUpdatePopoverPositionEffect';
import { UseUpdatePopoverPositionProps } from '@/core/components/Popover/types/PopoverPosition';
import { useUpdatePopoverPositionEffect } from '@/core/components/Popover/PopoverBase/hooks/effects/useUpdatePopoverPositionEffect';
import { UseUpdatePopoverPositionProps } from '@/core/components/Popover/PopoverBase/types/PopoverPosition';

export const usePopoverPosition = ({
gap,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { cloneElement, MouseEvent, useState } from 'react';

import { PopoverProps } from '@/core/components/Popover/types';
import { useRootScrollLockEffect } from '@/core/components/Popover/hooks/effects/useRootScrollLockEffect';
import { usePopoverPosition } from '@/core/components/Popover/hooks/usePopoverPosition';
import { PopoverProps } from '@/core/components/Popover/PopoverBase/types';
import { useRootScrollLockEffect } from '@/core/components/Popover/PopoverBase/hooks/effects/useRootScrollLockEffect';
import { usePopoverPosition } from '@/core/components/Popover/PopoverBase/hooks/usePopoverPosition';
import useClickOutside from '@/hooks/useClickOutSide';
import Section from '@/core/components/Section';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CSSProperties, Dispatch, RefObject, SetStateAction } from 'react';

import { PopoverProps } from '@/core/components/Popover/types';
import { PopoverProps } from '@/core/components/Popover/PopoverBase/types';

export interface UseUpdatePopoverPositionProps
extends Pick<PopoverProps, 'rootRef'> {
Expand Down
Loading