Skip to content

Commit 05771a5

Browse files
Merge pull request #101 from solid-contrib/97-shadcn-file-list-grid
97 shadcn file list grid
1 parent 81eddda commit 05771a5

4 files changed

Lines changed: 302 additions & 65 deletions

File tree

‎app/components/FileItem.tsx‎

Lines changed: 86 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
import { useState, useRef } from "react";
44
import { getFileIcon, formatFileSize, formatDate, type FileType } from "../lib/helpers";
55
import FileItemMenu from "./FileItemMenu";
6+
import {
7+
Item,
8+
ItemActions,
9+
ItemContent,
10+
ItemDescription,
11+
ItemMedia,
12+
ItemTitle,
13+
} from "@/components/ui/item";
614

715
export type { FileType };
816

@@ -61,7 +69,7 @@ export default function FileItem({
6169
}
6270

6371
clickCountRef.current += 1;
64-
72+
6573
if (clickCountRef.current === 1) {
6674
clickTimeoutRef.current = setTimeout(() => {
6775
if (clickCountRef.current === 1) {
@@ -88,13 +96,13 @@ export default function FileItem({
8896
setTimeout(() => {
8997
touchHandledRef.current = false;
9098
}, 400);
91-
99+
92100
const currentTime = new Date().getTime();
93101
const tapLength = currentTime - lastTapRef.current;
94-
102+
95103
if (tapLength < 300 && tapLength > 0) {
96104
// Double tap detected
97-
105+
98106
if (clickTimeoutRef.current) {
99107
clearTimeout(clickTimeoutRef.current);
100108
clickTimeoutRef.current = null;
@@ -112,18 +120,26 @@ export default function FileItem({
112120
clickTimeoutRef.current = null;
113121
}, 300);
114122
}
115-
123+
116124
lastTapRef.current = currentTime;
117125
};
118126

127+
const secondaryLabel =
128+
file.lastModified
129+
? formatDate(file.lastModified)
130+
: file.type === "folder"
131+
? "Folder"
132+
: file.size
133+
? formatFileSize(file.size)
134+
: "File";
135+
119136
if (view === "grid") {
120137
return (
121-
<section
122-
className={`group relative flex cursor-pointer select-none flex-col items-center justify-center rounded-lg border-2 p-2 transition-colors sm:p-4 ${isSelected
123-
? "border-primary bg-accent"
124-
: "border-transparent bg-background hover:border-border hover:bg-muted"
125-
}`}
126-
style={{ touchAction: 'manipulation' }}
138+
<Item
139+
variant={isSelected ? "muted" : "outline"}
140+
size="default"
141+
className="relative h-full cursor-pointer select-none flex-col items-center justify-center gap-3 py-5 text-center hover:bg-muted"
142+
style={{ touchAction: "manipulation" }}
127143
onMouseEnter={() => setIsHovered(true)}
128144
onMouseLeave={() => setIsHovered(false)}
129145
onClick={handleClick}
@@ -138,34 +154,42 @@ export default function FileItem({
138154
}}
139155
>
140156
{isHovered && (
141-
<FileItemMenu
142-
file={file}
143-
position="top-right"
144-
onRename={onRename}
145-
onPreview={onPreview}
146-
onDownload={onDownload}
147-
onCopy={onCopy}
148-
onMove={onMove}
149-
onDelete={onDelete}
150-
onShare={onShare}
151-
/>
157+
<ItemActions className="absolute top-1.5 right-1.5 z-10">
158+
<FileItemMenu
159+
file={file}
160+
position="top-right"
161+
onRename={onRename}
162+
onPreview={onPreview}
163+
onDownload={onDownload}
164+
onCopy={onCopy}
165+
onMove={onMove}
166+
onDelete={onDelete}
167+
onShare={onShare}
168+
/>
169+
</ItemActions>
152170
)}
153-
<div className="mb-1 flex h-12 w-12 items-center justify-center sm:mb-2 sm:h-16 sm:w-16">
171+
<ItemMedia className="mb-0 flex size-14 items-center justify-center rounded-2xl bg-muted sm:size-16">
154172
{getFileIcon(file.type, file.mimeType)}
155-
</div>
156-
<p className="max-w-full truncate text-center text-xs font-medium text-foreground sm:text-sm">
157-
{file.name}
158-
</p>
159-
</section>
173+
</ItemMedia>
174+
<ItemContent className="w-full min-w-0 items-center gap-0.5">
175+
<ItemTitle className="w-full justify-center truncate text-xs sm:text-sm">
176+
{file.name}
177+
</ItemTitle>
178+
<ItemDescription className="line-clamp-1 w-full text-center text-xs">
179+
{secondaryLabel}
180+
</ItemDescription>
181+
</ItemContent>
182+
</Item>
160183
);
161184
}
162185

163186
// List view
164187
return (
165-
<section
166-
className={`group flex cursor-pointer select-none items-center gap-2 border-b border-border px-2 py-2 transition-colors sm:gap-4 sm:px-4 sm:py-3 ${isSelected ? "bg-accent" : "bg-background hover:bg-muted"
167-
}`}
168-
style={{ touchAction: 'manipulation' }}
188+
<Item
189+
variant={isSelected ? "muted" : "outline"}
190+
size="sm"
191+
className="cursor-pointer select-none hover:bg-muted"
192+
style={{ touchAction: "manipulation" }}
169193
onMouseEnter={() => setIsHovered(true)}
170194
onMouseLeave={() => setIsHovered(false)}
171195
onClick={handleClick}
@@ -179,32 +203,39 @@ export default function FileItem({
179203
onContextMenu?.(file, event);
180204
}}
181205
>
182-
<div className="flex h-8 w-8 flex-shrink-0 items-center justify-center sm:h-10 sm:w-10">
206+
<ItemMedia className="flex size-9 items-center justify-center rounded-xl bg-muted sm:size-10">
183207
{getFileIcon(file.type, file.mimeType)}
184-
</div>
185-
<div className="min-w-0 flex-1">
186-
<p className="truncate text-xs font-medium text-foreground sm:text-sm">{file.name}</p>
187-
</div>
188-
<div className="hidden flex-shrink-0 text-xs text-muted-foreground sm:block sm:text-sm">
189-
{file.lastModified && formatDate(file.lastModified)}
190-
</div>
191-
<div className="hidden flex-shrink-0 text-xs text-muted-foreground md:block md:text-sm">
192-
{file.size && formatFileSize(file.size)}
193-
</div>
208+
</ItemMedia>
209+
210+
<ItemContent className="min-w-0 gap-0.5">
211+
<ItemTitle className="w-full truncate">{file.name}</ItemTitle>
212+
<ItemDescription className="line-clamp-1 text-xs">
213+
{secondaryLabel}
214+
</ItemDescription>
215+
</ItemContent>
216+
217+
<ItemActions className="ml-auto hidden min-w-20 shrink-0 justify-end text-muted-foreground md:flex">
218+
<span className="text-xs sm:text-sm">
219+
{file.size ? formatFileSize(file.size) : file.type === "folder" ? "—" : ""}
220+
</span>
221+
</ItemActions>
222+
194223
{isHovered && (
195-
<FileItemMenu
196-
file={file}
197-
position="right"
198-
onRename={onRename}
199-
onPreview={onPreview}
200-
onDownload={onDownload}
201-
onCopy={onCopy}
202-
onMove={onMove}
203-
onDelete={onDelete}
204-
onShare={onShare}
205-
/>
224+
<ItemActions className="shrink-0">
225+
<FileItemMenu
226+
file={file}
227+
position="right"
228+
onRename={onRename}
229+
onPreview={onPreview}
230+
onDownload={onDownload}
231+
onCopy={onCopy}
232+
onMove={onMove}
233+
onDelete={onDelete}
234+
onShare={onShare}
235+
/>
236+
</ItemActions>
206237
)}
207-
</section>
238+
</Item>
208239
);
209240
}
210241

‎app/components/FileList.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useState, useEffect } from "react";
44
import FileItem, { FileItemData } from "./FileItem";
55
import Toolbar from "./shared/Toolbar";
66
import EmptyState from "./shared/EmptyState";
7+
import { ItemGroup } from "@/components/ui/item";
78

89
interface FileListProps {
910
files: FileItemData[];
@@ -60,7 +61,7 @@ export default function FileList({
6061
{files.length === 0 ? (
6162
<EmptyState message="No files or folders" />
6263
) : view === "grid" ? (
63-
<div className="grid grid-cols-2 gap-2 p-2 sm:grid-cols-3 sm:gap-3 sm:p-4 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6">
64+
<ItemGroup className="grid grid-cols-2 gap-3 p-3 sm:grid-cols-3 sm:gap-3 sm:p-4 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6">
6465
{files.map((file) => (
6566
<FileItem
6667
key={file.id}
@@ -79,9 +80,9 @@ export default function FileList({
7980
onContextMenu={onFileContextMenu}
8081
/>
8182
))}
82-
</div>
83+
</ItemGroup>
8384
) : (
84-
<div className="divide-y divide-border">
85+
<ItemGroup className="gap-1 p-2 sm:p-4" data-size="sm">
8586
{files.map((file) => (
8687
<FileItem
8788
key={file.id}
@@ -100,7 +101,7 @@ export default function FileList({
100101
onContextMenu={onFileContextMenu}
101102
/>
102103
))}
103-
</div>
104+
</ItemGroup>
104105
)}
105106
</section>
106107
</main>

‎app/components/shared/Toolbar.tsx‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export default function Toolbar({
1818
actions,
1919
}: ToolbarProps) {
2020
return (
21-
<header className="flex items-center justify-between border-b border-border px-2 py-2 sm:px-4">
22-
<nav className="flex items-center gap-1 sm:gap-2" aria-label="View options">
21+
<header className="flex items-center justify-between border-b border-border px-3 py-2.5 sm:px-4">
22+
<nav className="flex items-center gap-2" aria-label="View options">
2323
<ToggleGroup
2424
value={[view]}
2525
onValueChange={(groupValue) => {
@@ -33,15 +33,19 @@ export default function Toolbar({
3333
spacing={0}
3434
>
3535
<ToggleGroupItem value="list" aria-label="List view">
36-
<List className="h-4 w-4 sm:h-5 sm:w-5" />
36+
<List className="size-4" />
3737
</ToggleGroupItem>
3838
<ToggleGroupItem value="grid" aria-label="Grid view">
39-
<LayoutGrid className="h-4 w-4 sm:h-5 sm:w-5" />
39+
<LayoutGrid className="size-4" />
4040
</ToggleGroupItem>
4141
</ToggleGroup>
42-
{actions && <div className="flex items-center gap-1 sm:gap-2">{actions}</div>}
42+
{actions && <div className="flex items-center gap-2">{actions}</div>}
4343
</nav>
44-
<div className="text-xs text-muted-foreground sm:text-sm" role="status" aria-live="polite">
44+
<div
45+
className="text-xs text-muted-foreground sm:text-sm"
46+
role="status"
47+
aria-live="polite"
48+
>
4549
{itemCount} {itemCount === 1 ? "item" : "items"}
4650
</div>
4751
</header>

0 commit comments

Comments
 (0)