opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Dropdowns
  4. File Menu
Back to Dropdowns

Components / Dropdowns

File Menu

Free Dropdowns React component — FileMenuDropdown. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Folder icon you can right-click or tap to open a centered action menu — edit, duplicate, pin, move, delete. Keyboard-friendly item hints included.

Preview

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/dropdowns/file-menu-dropdown.tsx in your project.

  4. 4

    Import and render:

    Example

    import { FileMenuDropdown } from "@/components/dropdowns/file-menu-dropdown";

    <FileMenuDropdown cardTitle="My file" onItemClick={(item) => console.log(item.id)} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/dropdowns/file-menu-dropdown.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use client";
 
import {
forwardRef,
useEffect,
useRef,
useState,
type ComponentPropsWithoutRef,
type ComponentType,
} from "react";
 
import { cn } from "@/lib/cn";
 
import { Edit, Copy, Pin, Folder, Trash } from "lucide-react";
 
type IconComponent = ComponentType<
Readonly<{ size?: number; className?: string }>
>;
 
export type ContextMenuItem = Readonly<{
id: string;
label?: string;
icon?: IconComponent;
kbd?: string;
danger?: boolean;
separator?: boolean;
}>;
 
export type FileMenuDropdownProps = Readonly<
{
cardTitle?: string;
cardHint?: string;
triggerAriaLabel?: string;
menuAriaLabel?: string;
items?: readonly ContextMenuItem[];
onItemClick?: (item: ContextMenuItem) => void;
} & ComponentPropsWithoutRef<"div">
>;
 
const defaultItems: readonly ContextMenuItem[] = [
{ id: "edit", label: "Edit", icon: Edit, kbd: "ctrl + E" },
{ id: "duplicate", label: "Duplicate", icon: Copy, kbd: "ctrl + D" },
{ id: "pin", label: "Pin to top", icon: Pin },
{ id: "move", label: "Move to folder", icon: Folder },
{ id: "separator-delete", separator: true },
{ id: "delete", label: "Delete", icon: Trash, danger: true, kbd: "⌫" },
];
 
type ContextMenuItemRowProps = Readonly<{
item: ContextMenuItem;
onSelect: (item: ContextMenuItem) => void;
}>;
 
function ContextMenuItemRow({ item, onSelect }: ContextMenuItemRowProps) {
if (item.separator) {
return <hr className="mx-1 my-1 h-px border-0 bg-neutral-100" />;
}
 
const Icon = item.icon;
 
return (
<button
type="button"
role="menuitem"
aria-label={item.label}
onClick={() => onSelect(item)}
className={cn(
"flex w-full cursor-pointer items-center justify-between rounded-lg px-2.5 py-2 text-[12px] font-medium transition-colors",
item.danger
? "text-red-500 hover:bg-red-50"
: "text-neutral-700 hover:bg-neutral-50",
)}
>
<div className="flex items-center gap-2">
{Icon ? (
<Icon
size={12}
className={item.danger ? "text-red-400" : "text-neutral-400"}
/>
) : null}
{item.label}
</div>
{item.kbd ? (
<kbd className="font-mono text-[9px] text-neutral-400">{item.kbd}</kbd>
) : null}
</button>
);
}
 
// Production-ready Context Menu component — styled with Tailwind CSS.
export const FileMenuDropdown = forwardRef<
HTMLDivElement,
FileMenuDropdownProps
>(
(
{
cardTitle = "Card Component",
cardHint = "Right-click or tap",
triggerAriaLabel = "Open context menu",
menuAriaLabel = "Context menu",
items = defaultItems,
onItemClick,
className,
...props
},
ref,
) => {
const [open, setOpen] = useState(false);
const innerRef = useRef<HTMLDivElement>(null);
 
useEffect(() => {
const close = (event: MouseEvent) => {
if (!innerRef.current?.contains(event.target as Node)) {
setOpen(false);
}
};
document.addEventListener("mousedown", close);
return () => document.removeEventListener("mousedown", close);
}, []);
 
useEffect(() => {
const onEscape = (event: KeyboardEvent) => {
if (event.key === "Escape") setOpen(false);
};
document.addEventListener("keydown", onEscape);
return () => document.removeEventListener("keydown", onEscape);
}, []);
 
const handleItemSelect = (item: ContextMenuItem) => {
setOpen(false);
onItemClick?.(item);
};
 
const toggleOpen = () => setOpen((prev) => !prev);
 
return (
<div
ref={ref}
data-slot="context-menu-dropdown"
className={cn("relative inline-block font-sans", className)}
{...props}
>
<div ref={innerRef}>
<button
type="button"
aria-label={triggerAriaLabel}
aria-expanded={open}
aria-haspopup="menu"
onContextMenu={(event) => {
event.preventDefault();
setOpen(true);
}}
onClick={toggleOpen}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
toggleOpen();
}
}}
className="flex flex-col items-center justify-center select-none"
>
<div className="mb-2 flex h-24 w-24 cursor-pointer items-center justify-center rounded-3xl bg-cyan-200">
<Folder size={48} className="text-white" fill="white" />
</div>
<p className="text-[10px] font-medium text-neutral-700">
{cardTitle}
</p>
<p className="mt-1 text-[10px] text-neutral-200">{cardHint}</p>
</button>
 
{open ? (
<div
role="menu"
aria-label={menuAriaLabel}
className="absolute top-1/2 left-1/2 z-100 w-48 -translate-x-1/2 -translate-y-1/2 rounded-xl border border-neutral-100 bg-white p-1 shadow-[0_20px_50px_-12px_rgba(0,0,0,0.05)]"
>
{items.map((item) => (
<ContextMenuItemRow
key={item.id}
item={item}
onSelect={handleItemSelect}
/>
))}
</div>
) : null}
</div>
</div>
);
},
);
 
FileMenuDropdown.displayName = "FileMenuDropdown";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/dropdowns/file-menu-dropdown.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.