Repository navigation
[FEAT] Dropdown 컴포넌트 구현 #78
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f0993b
feat(tailwind-config): shadow-timo 토큰 추가 (#74)
ehye1 762c92f
feat(design-system): Dropdown 컴포넌트 구현 (#74)
ehye1 db3ca56
refactor(design-system): Dropdown boolean 네이밍 및 접근성 개선 (#74)
ehye1 147218f
Merge remote-tracking branch 'origin/develop' into feat/ui/74-dropdow…
ehye1 ccde69e
refactor(ui): Dropdown을 compound 패턴 기반 DropdownView로 교체 (#74)
ehye1 18150ee
fix(ui): Dropdown compound 컴포넌트 버그 수정 (#74)
ehye1 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| @import "./tokens/colors.css"; | ||
| @import "./tokens/typography.css"; | ||
| @import "./tokens/radius.css"; | ||
| @import "./scrollbar.css"; | ||
| @import "./scrollbar.css"; | ||
|
|
||
| @theme { | ||
| --shadow-timo: 0px 0px 3px 0px rgba(0, 0, 0, 0.12); | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
packages/timo-design-system/src/components/dropdown/Dropdown.stories.tsx
This file contains hidden or 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,24 @@ | ||
| import { useState } from "react"; | ||
|
|
||
| import { Dropdown } from "./Dropdown"; | ||
|
ehye1 marked this conversation as resolved.
Outdated
|
||
|
|
||
| import type { Meta, StoryObj } from "@storybook/react"; | ||
|
|
||
| const meta = { | ||
| title: "Components/Dropdown", | ||
| component: Dropdown, | ||
| parameters: { layout: "centered" }, | ||
| } satisfies Meta<typeof Dropdown>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| const DefaultStory = () => { | ||
| const [value, setValue] = useState(""); | ||
| return <Dropdown items={["기본", "7일"]} value={value} onChange={setValue} />; | ||
| }; | ||
|
|
||
| export const Default: Story = { | ||
| args: { items: [], value: "", onChange: () => {} }, | ||
| render: () => <DefaultStory />, | ||
| }; | ||
122 changes: 122 additions & 0 deletions
122
packages/timo-design-system/src/components/dropdown/Dropdown.tsx
This file contains hidden or 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,122 @@ | ||
| import { cn } from "@lib"; | ||
| import { useEffect, useRef, useState } from "react"; | ||
|
|
||
| export interface DropdownProps { | ||
| items: string[]; | ||
| value: string; | ||
| onChange: (value: string) => void; | ||
| placeholder?: string; | ||
| className?: string; | ||
| } | ||
|
|
||
| export const Dropdown = ({ | ||
|
ehye1 marked this conversation as resolved.
Outdated
|
||
| items, | ||
| value, | ||
| onChange, | ||
| placeholder = "기본", | ||
| className, | ||
| }: DropdownProps) => { | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const containerRef = useRef<HTMLDivElement>(null); | ||
| const triggerRef = useRef<HTMLButtonElement>(null); | ||
|
|
||
| const handleSelect = (item: string) => { | ||
| onChange(item); | ||
| setIsOpen(false); | ||
| }; | ||
|
ehye1 marked this conversation as resolved.
Outdated
|
||
|
|
||
| const closeWithFocus = () => { | ||
| setIsOpen(false); | ||
| triggerRef.current?.focus(); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (!isOpen) return; | ||
| const handleKeyDown = (e: KeyboardEvent) => { | ||
| if (e.key === "Escape") closeWithFocus(); | ||
| }; | ||
| const handleClickOutside = (e: MouseEvent) => { | ||
| if ( | ||
| containerRef.current && | ||
| !containerRef.current.contains(e.target as Node) | ||
| ) { | ||
| closeWithFocus(); | ||
| } | ||
| }; | ||
|
ehye1 marked this conversation as resolved.
Outdated
|
||
| document.addEventListener("keydown", handleKeyDown); | ||
| document.addEventListener("mousedown", handleClickOutside); | ||
| return () => { | ||
| document.removeEventListener("keydown", handleKeyDown); | ||
| document.removeEventListener("mousedown", handleClickOutside); | ||
| }; | ||
| }, [isOpen]); | ||
|
|
||
| return ( | ||
| <div | ||
| ref={containerRef} | ||
| className={cn("inline-flex flex-col", isOpen && "shadow-timo", className)} | ||
| > | ||
| <button | ||
| ref={triggerRef} | ||
| type="button" | ||
| onClick={() => setIsOpen((prev) => !prev)} | ||
| aria-haspopup="listbox" | ||
| aria-expanded={isOpen} | ||
| className={cn( | ||
| "flex h-8 items-center gap-2.5 bg-white px-2", | ||
| "border-timo-gray-500 border", | ||
| isOpen ? "rounded-t-[4px]" : "rounded-[4px]", | ||
| )} | ||
| > | ||
| <span className="typo-headline-m-14 text-timo-gray-900 whitespace-nowrap"> | ||
| {value || placeholder} | ||
| </span> | ||
| <div className="flex size-6 shrink-0 items-center justify-center"> | ||
| <svg | ||
|
ehye1 marked this conversation as resolved.
Outdated
|
||
| width="16" | ||
| height="8" | ||
| viewBox="0 0 16 8" | ||
| fill="none" | ||
| aria-hidden="true" | ||
| className="text-timo-gray-900" | ||
| > | ||
| <path | ||
| d={isOpen ? "M1 7L8 1L15 7" : "M1 1L8 7L15 1"} | ||
| stroke="currentColor" | ||
| strokeWidth="1.5" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| /> | ||
| </svg> | ||
| </div> | ||
| </button> | ||
|
|
||
| {isOpen && ( | ||
| <ul | ||
| role="listbox" | ||
| className="border-timo-gray-500 flex flex-col gap-1.5 rounded-b-[4px] border-x border-b bg-white px-2 py-1" | ||
| > | ||
| {items.map((item, i) => ( | ||
| <li key={item} className="flex flex-col gap-1"> | ||
|
ehye1 marked this conversation as resolved.
Outdated
|
||
| <span | ||
| role="option" | ||
| aria-selected={item === value} | ||
| tabIndex={0} | ||
| onClick={() => handleSelect(item)} | ||
| onKeyDown={(e) => { | ||
| if (e.key === "Enter" || e.key === " ") handleSelect(item); | ||
| }} | ||
| className="typo-body-m-12 text-timo-gray-900 cursor-pointer" | ||
| > | ||
| {item} | ||
| </span> | ||
| {i < items.length - 1 && ( | ||
| <div className="bg-timo-gray-500 h-px w-full" /> | ||
| )} | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.