generated from dgd03146/monorepo-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: Implement Menubar & Toast component with storybook #24
Open
dgd03146
wants to merge
4
commits into
main
Choose a base branch
from
feat/menubar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2e93281
feat: Implement Menubar & Toast component with storybook
dgd03146 62bca73
chore: Install react-dom to design-system workspace
dgd03146 8f54c59
chore: downgrade react and react version
dgd03146 6898952
chore: Install react dependencies
dgd03146 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { Menubar } from '@jung/design-system'; | ||
|
||
const meta: Meta<typeof Menubar> = { | ||
title: 'Components/Menubar', | ||
component: Menubar, | ||
tags: ['autodocs'], | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
} satisfies Meta<typeof Menubar>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Menubar>; | ||
|
||
export const Default: Story = { | ||
render: () => ( | ||
<Menubar> | ||
<Menubar.Menu> | ||
<Menubar.Trigger>File</Menubar.Trigger> | ||
<Menubar.Content> | ||
<Menubar.Item>New Tab</Menubar.Item> | ||
<Menubar.Item>New Window</Menubar.Item> | ||
<Menubar.Separator /> | ||
<Menubar.Item>Share</Menubar.Item> | ||
</Menubar.Content> | ||
</Menubar.Menu> | ||
<Menubar.Menu> | ||
<Menubar.Trigger>Edit</Menubar.Trigger> | ||
<Menubar.Content> | ||
<Menubar.Item>Undo</Menubar.Item> | ||
<Menubar.Item>Redo</Menubar.Item> | ||
</Menubar.Content> | ||
</Menubar.Menu> | ||
</Menubar> | ||
), | ||
}; | ||
|
||
export const Disabled: Story = { | ||
render: () => ( | ||
<Menubar> | ||
<Menubar.Menu> | ||
<Menubar.Trigger>File</Menubar.Trigger> | ||
<Menubar.Content> | ||
<Menubar.Item>New Tab</Menubar.Item> | ||
<Menubar.Item>New Window</Menubar.Item> | ||
<Menubar.Item disabled>New Incongnito Window</Menubar.Item> | ||
<Menubar.Separator /> | ||
<Menubar.Item>hihi</Menubar.Item> | ||
</Menubar.Content> | ||
</Menubar.Menu> | ||
<Menubar.Menu> | ||
<Menubar.Trigger>Edit</Menubar.Trigger> | ||
<Menubar.Content> | ||
<Menubar.Item>Undo</Menubar.Item> | ||
<Menubar.Item>Redo</Menubar.Item> | ||
</Menubar.Content> | ||
</Menubar.Menu> | ||
</Menubar> | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { | ||
Button, | ||
Toast, | ||
ToastContainer, | ||
ToastProvider, | ||
useToast, | ||
} from '@jung/design-system'; | ||
|
||
const meta = { | ||
title: 'COMPONENTS/Toast', | ||
component: Toast, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
} satisfies Meta<typeof Toast>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Toast>; | ||
|
||
const Template = () => { | ||
const showToast = useToast(); | ||
return ( | ||
<Button | ||
variant='secondary' | ||
size='sm' | ||
onClick={() => showToast('Toast Message')} | ||
> | ||
Show Toast | ||
</Button> | ||
); | ||
}; | ||
|
||
export const Default: Story = { | ||
render: () => ( | ||
<ToastProvider> | ||
<ToastContainer /> | ||
<Template /> | ||
</ToastProvider> | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { createVar } from '@vanilla-extract/css'; | ||
import { style } from '@vanilla-extract/css'; | ||
import { recipe } from '@vanilla-extract/recipes'; | ||
import { sprinkles } from '../../styles'; | ||
|
||
export const menubar = style([ | ||
{ | ||
border: '1px solid #ccc', | ||
}, | ||
sprinkles({ | ||
width: 'fit', | ||
display: 'flex', | ||
columnGap: '1', | ||
alignItems: 'center', | ||
borderRadius: 'md', | ||
padding: '1', | ||
}), | ||
]); | ||
|
||
export const trigger = recipe({ | ||
base: sprinkles({ | ||
paddingX: '2', | ||
paddingY: '1', | ||
}), | ||
variants: { | ||
isActive: { | ||
true: sprinkles({ background: 'gray100' }), | ||
false: sprinkles({ background: 'transparent' }), | ||
}, | ||
}, | ||
}); | ||
|
||
export const topVar = createVar(); | ||
export const leftVar = createVar(); | ||
|
||
export const content = style([ | ||
{ | ||
border: '1px solid #ccc', | ||
top: topVar, | ||
left: leftVar, | ||
}, | ||
sprinkles({ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
borderRadius: 'md', | ||
position: 'absolute', | ||
zIndex: '100', | ||
paddingY: '0.5', | ||
}), | ||
]); | ||
|
||
// export const item = sprinkles({ | ||
// background: { | ||
// hover: 'gray100', | ||
// }, | ||
|
||
// paddingX: '1', | ||
// }); | ||
|
||
export const item = recipe({ | ||
base: sprinkles({ | ||
paddingX: '1', | ||
paddingY: '2', | ||
}), | ||
variants: { | ||
disabled: { | ||
true: sprinkles({ color: 'gray200' }), | ||
false: sprinkles({ background: { hover: 'gray100' } }), | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useOutsideClick } from '@jung/shared/hooks'; | ||
import { | ||
type HTMLAttributes, | ||
forwardRef, | ||
useCallback, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
import type { OmitAtomProps } from '../../types/atoms'; | ||
import { Box } from '../Box'; | ||
import * as styles from './Menubar.css'; | ||
import { MenubarContext } from './context/MenubarContext'; | ||
|
||
export interface MenubarProps | ||
extends HTMLAttributes<HTMLDivElement>, | ||
OmitAtomProps { | ||
defaultValue?: string; | ||
onValueChange?: (value: string) => void; | ||
} | ||
|
||
export const Menubar = forwardRef<HTMLDivElement, MenubarProps>( | ||
({ children, defaultValue, onValueChange, ...restProps }, ref) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [selectedMenu, setSelectedMenu] = useState(defaultValue); | ||
const onCloseMenu = useCallback( | ||
() => setSelectedMenu(''), | ||
[setSelectedMenu], | ||
); | ||
|
||
const onSelectedMenu = useCallback( | ||
(menu: string) => setSelectedMenu(menu), | ||
[setSelectedMenu], | ||
); | ||
|
||
const contextValue = { | ||
selectedMenu, | ||
isOpen, | ||
setIsOpen, | ||
onOpenMenu: () => setIsOpen(true), | ||
onSelectedMenu, | ||
onCloseMenu, | ||
}; | ||
Comment on lines
+35
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이렇게 해서 넘기니까 가독성이 좋네요 👍🏻 |
||
|
||
const menubarRef = useRef<HTMLDivElement>(null); | ||
useOutsideClick(menubarRef, () => onCloseMenu()); | ||
|
||
return ( | ||
<MenubarContext.Provider value={contextValue}> | ||
<Box ref={menubarRef}> | ||
<Box | ||
className={styles.menubar} | ||
ref={ref} | ||
{...restProps} | ||
role='menubar' | ||
> | ||
{children} | ||
</Box> | ||
</Box> | ||
</MenubarContext.Provider> | ||
); | ||
}, | ||
); |
61 changes: 61 additions & 0 deletions
61
packages/design-system/components/Menubar/MenubarContent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { usePortal } from '@jung/shared/hooks'; | ||
import { assignInlineVars } from '@vanilla-extract/dynamic'; | ||
import { | ||
type HTMLAttributes, | ||
forwardRef, | ||
useLayoutEffect, | ||
useState, | ||
} from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import type { OmitAtomProps } from '../../types/atoms'; | ||
import { Box } from '../Box'; | ||
import * as styles from './Menubar.css'; | ||
import { useMenubarContext } from './context/MenubarContext'; | ||
import { useMenubarMenuContext } from './context/MenubarMenuContext'; | ||
|
||
export interface MenubarContent | ||
extends HTMLAttributes<HTMLDivElement>, | ||
OmitAtomProps { | ||
value?: string; | ||
} | ||
|
||
export const MenubarContent = forwardRef<HTMLDivElement, MenubarContent>( | ||
({ children, value, ...restProps }, ref) => { | ||
const { isOpen } = useMenubarContext(); | ||
const { isSelected, contentId, triggerId, triggerRef } = | ||
useMenubarMenuContext(); | ||
const portalNode = usePortal('menubar-portal'); | ||
const [position, setPosition] = useState({ top: 0, left: 0 }); | ||
|
||
useLayoutEffect(() => { | ||
if (triggerRef.current) { | ||
const rect = triggerRef.current.getBoundingClientRect(); | ||
setPosition({ | ||
top: rect.bottom + 8, | ||
left: rect.left, | ||
}); | ||
} | ||
}, [triggerRef]); | ||
|
||
if (!isOpen || !isSelected || !portalNode) return null; | ||
|
||
const inlineVars = assignInlineVars({ | ||
[styles.topVar]: `${position.top}px`, | ||
[styles.leftVar]: `${position.left}px`, | ||
}); | ||
|
||
return createPortal( | ||
<Box | ||
style={inlineVars} | ||
id={contentId} | ||
aria-labelledby={triggerId} | ||
className={styles.content} | ||
ref={ref} | ||
{...restProps} | ||
> | ||
{children} | ||
</Box>, | ||
portalNode, | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { type HTMLAttributes, forwardRef } from 'react'; | ||
import type { OmitAtomProps } from '../../types/atoms'; | ||
import { Box } from '../Box'; | ||
import * as styles from './Menubar.css'; | ||
import { useMenubarContext } from './context/MenubarContext'; | ||
|
||
export interface MenubarItemProps | ||
extends HTMLAttributes<HTMLDivElement>, | ||
OmitAtomProps { | ||
disabled?: boolean; | ||
} | ||
|
||
export const MenubarItem = forwardRef<HTMLDivElement, MenubarItemProps>( | ||
({ children, disabled = false, ...restProps }, ref) => { | ||
const { onCloseMenu } = useMenubarContext(); | ||
return ( | ||
<Box | ||
role='button' | ||
aria-disabled={disabled} | ||
className={styles.item({ disabled })} | ||
onClick={onCloseMenu} | ||
ref={ref} | ||
{...restProps} | ||
> | ||
{children} | ||
</Box> | ||
); | ||
}, | ||
); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 satisfies 배워갑니다