Add UI components and Tooltip provider

Introduce a set of reusable UI primitives and integrate tooltip provider in the app layout. Adds: Combobox (with virtualized and non-virtualized variants), Command palette components (cmdk wrapper and related subcomponents), DatePicker and DateTimePicker (popover-based date/time selection with time pickers and Done footer), LazyImage (progressive image with blur/scale transition), and Tooltip (Radix wrapper and provider). Also imports TooltipProvider in app/layout.tsx and wraps the app content so tooltips are available globally.
This commit is contained in:
Ichsan Kurniawan
2026-04-16 22:37:45 +07:00
parent 154574ff45
commit fb2d746fc0
8 changed files with 626 additions and 4 deletions
+7 -4
View File
@@ -12,6 +12,7 @@ import SessionProvider from '@/providers/SessionProvider'
import { Toaster } from '@/components/ui/sonner';
import AntdConfigProvider from '@/providers/AntdConfigProvider';
import ProgressBarProviders from '@/providers/ProgressBarProvider';
import { TooltipProvider } from '@/components/ui/tooltip';
const jakarta = Plus_Jakarta_Sans({ subsets: ['latin'] })
@@ -81,10 +82,12 @@ export default async function RootLayout({
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<AntdConfigProvider>
<ProgressBarProviders>
<div className='bg-soft dark:bg-zinc-950'>
{children}
{modal}
</div>
<TooltipProvider>
<div className='bg-soft dark:bg-zinc-950'>
{children}
{modal}
</div>
</TooltipProvider>
</ProgressBarProviders>
</AntdConfigProvider>
<Toaster richColors />
+133
View File
@@ -0,0 +1,133 @@
import { Check, ChevronsUpDown } from "lucide-react"
import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import {
Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList,
} from "@/components/ui/command"
import {
Popover, PopoverContent, PopoverTrigger,
} from "@/components/ui/popover"
import { Dispatch, SetStateAction, useDeferredValue, useMemo, useState } from "react"
import { VList } from "virtua"
type Props = {
title?: string
options: { value: any, label: any }[]
value: any
setValue: Dispatch<SetStateAction<any>>
onSelectItem?: (item: any) => any
className?: string
disabled?: boolean
};
export function Combobox({ value, setValue, onSelectItem, options, title, className, disabled }: Props) {
const [open, setOpen] = useState(false)
const [search, setSearch] = useState("")
// Deferred state for the heavy filtering
const deferredSearch = useDeferredValue(search)
const filteredOptions = useMemo(() => {
return options.filter((item) =>
item.label.toLowerCase().includes(deferredSearch.toLowerCase())
)
}, [deferredSearch, options])
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild disabled={disabled}>
<Button variant="outline" role="combobox" aria-expanded={open} className={cn("justify-between", !value && "text-muted-foreground", className)}>
{value ? options.find((item) => item.value === value)?.label : `Pilih ${title || 'data'}...`}
<ChevronsUpDown className="opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className='w-(--radix-popover-trigger-width)'>
<Command shouldFilter={false}>
<CommandInput placeholder={`Cari ${title || 'data'}...`} value={search} onValueChange={(val) => setSearch(val)} />
<CommandList className="max-h-64 overflow-auto">
{/* MANUAL SEARCH */}
{filteredOptions.length === 0 ? (
<CommandEmpty>Data tidak ditemukan.</CommandEmpty>
) : (
<CommandGroup>
<VList style={{ height: Math.min(filteredOptions.length * 40, 240) }} // Dynamic height, max 240px (6 items)
>
{filteredOptions.map((item) => (
<CommandItem
key={item.value}
value={item.value}
onSelect={() => {
setValue(item.value === value ? "" : item.value);
if (onSelectItem) onSelectItem(item)
setOpen(false)
setSearch('')
}}
// className="h-10" // Fixed height for each item
>
{item.label}
<Check className={cn("ml-auto h-4 w-4", value === item.value ? "opacity-100" : "opacity-0")} />
</CommandItem>
))}
</VList>
</CommandGroup>
)}
</CommandList>
</Command>
</PopoverContent>
</Popover >
)
}
export function ComboboxWithoutVirtua({ value, setValue, onSelectItem, options, title, className, disabled }: Props) {
const [open, setOpen] = useState(false)
const [search, setSearch] = useState("")
const deferredSearch = useDeferredValue(search)
const filteredOptions = useMemo(() => {
return options.filter((item) =>
item.label.toLowerCase().includes(deferredSearch.toLowerCase())
)
}, [deferredSearch, options])
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild disabled={disabled}>
<Button variant="outline" role="combobox" aria-expanded={open} className={cn("justify-between", className)}>
{value ? options.find((item) => item.value === value)?.label : `Pilih ${title || 'data'}...`}
<ChevronsUpDown className="opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="p-0">
<Command shouldFilter={false}>
<CommandInput placeholder={`Cari ${title || 'data'}...`} value={search} onValueChange={(val) => setSearch(val)} />
<CommandList className="max-h-64 overflow-auto">
{/* MANUAL SEARCH */}
{filteredOptions.length === 0 ? (
<CommandEmpty>Data tidak ditemukan.</CommandEmpty>
) : (
<CommandGroup>
{filteredOptions.map((item) => (
<CommandItem
key={item.value}
value={item.value}
onSelect={() => {
setValue(item.value === value ? "" : item.value);
if (onSelectItem) onSelectItem(item)
setOpen(false)
setSearch('')
}}
>
{item.label}
<Check className={cn("ml-auto h-4 w-4", value === item.value ? "opacity-100" : "opacity-0")} />
</CommandItem>
))}
</CommandGroup>
)}
</CommandList>
</Command>
</PopoverContent>
</Popover >
)
}
+184
View File
@@ -0,0 +1,184 @@
"use client"
import * as React from "react"
import { Command as CommandPrimitive } from "cmdk"
import { SearchIcon } from "lucide-react"
import { cn } from "@/lib/utils"
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
function Command({
className,
...props
}: React.ComponentProps<typeof CommandPrimitive>) {
return (
<CommandPrimitive
data-slot="command"
className={cn(
"flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground",
className
)}
{...props}
/>
)
}
function CommandDialog({
title = "Command Palette",
description = "Search for a command to run...",
children,
className,
showCloseButton = true,
...props
}: React.ComponentProps<typeof Dialog> & {
title?: string
description?: string
className?: string
showCloseButton?: boolean
}) {
return (
<Dialog {...props}>
<DialogHeader className="sr-only">
<DialogTitle>{title}</DialogTitle>
<DialogDescription>{description}</DialogDescription>
</DialogHeader>
<DialogContent
className={cn("overflow-hidden p-0", className)}
showCloseButton={showCloseButton}
>
<Command className="**:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]]:px-2 [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
{children}
</Command>
</DialogContent>
</Dialog>
)
}
function CommandInput({
className,
...props
}: React.ComponentProps<typeof CommandPrimitive.Input>) {
return (
<div
data-slot="command-input-wrapper"
className="flex h-9 items-center gap-2 border-b px-3"
>
<SearchIcon className="size-4 shrink-0 opacity-50" />
<CommandPrimitive.Input
data-slot="command-input"
className={cn(
"flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-hidden placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
className
)}
{...props}
/>
</div>
)
}
function CommandList({
className,
...props
}: React.ComponentProps<typeof CommandPrimitive.List>) {
return (
<CommandPrimitive.List
data-slot="command-list"
className={cn(
"max-h-[300px] scroll-py-1 overflow-x-hidden overflow-y-auto",
className
)}
{...props}
/>
)
}
function CommandEmpty({
...props
}: React.ComponentProps<typeof CommandPrimitive.Empty>) {
return (
<CommandPrimitive.Empty
data-slot="command-empty"
className="py-6 text-center text-sm"
{...props}
/>
)
}
function CommandGroup({
className,
...props
}: React.ComponentProps<typeof CommandPrimitive.Group>) {
return (
<CommandPrimitive.Group
data-slot="command-group"
className={cn(
"overflow-hidden p-1 text-foreground [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground",
className
)}
{...props}
/>
)
}
function CommandSeparator({
className,
...props
}: React.ComponentProps<typeof CommandPrimitive.Separator>) {
return (
<CommandPrimitive.Separator
data-slot="command-separator"
className={cn("-mx-1 h-px bg-border", className)}
{...props}
/>
)
}
function CommandItem({
className,
...props
}: React.ComponentProps<typeof CommandPrimitive.Item>) {
return (
<CommandPrimitive.Item
data-slot="command-item"
className={cn(
"relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 data-[selected=true]:bg-accent data-[selected=true]:text-accent-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground",
className
)}
{...props}
/>
)
}
function CommandShortcut({
className,
...props
}: React.ComponentProps<"span">) {
return (
<span
data-slot="command-shortcut"
className={cn(
"ml-auto text-xs tracking-widest text-muted-foreground",
className
)}
{...props}
/>
)
}
export {
Command,
CommandDialog,
CommandInput,
CommandList,
CommandEmpty,
CommandGroup,
CommandItem,
CommandShortcut,
CommandSeparator,
}
+139
View File
@@ -0,0 +1,139 @@
"use client"
import { useMemo, useState } from "react";
import { CalendarIcon } from "@radix-ui/react-icons";
import { format } from "date-fns";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Calendar } from "@/components/ui/calendar";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
import type { Matcher } from "react-day-picker";
interface DateTimePickerProps {
date: Date | undefined;
setDate: (date: Date | undefined) => void;
placeholder?: string;
disabled?: boolean;
disabledDate?: Matcher | Matcher[] | undefined;
captionLayout?: "label" | "dropdown" | "dropdown-months" | "dropdown-years";
formatDate?: string;
}
export function DateTimePicker({ date, setDate, placeholder, disabled, disabledDate, captionLayout, formatDate }: DateTimePickerProps) {
const [isOpen, setIsOpen] = useState(false);
const hours = Array.from({ length: 24 }, (_, i) => i).reverse();
const minutes = Array.from({ length: 12 }, (_, i) => i * 5);
const parsedDate = useMemo(() => {
if (!date) return undefined;
if (typeof date === "string") return new Date(date);
return date;
}, [date]);
const handleDateSelect = (selectedDate: Date | undefined) => {
if (selectedDate) {
if (parsedDate) {
selectedDate.setHours(parsedDate.getHours());
selectedDate.setMinutes(parsedDate.getMinutes());
}
setDate(selectedDate);
}
};
const handleTimeChange = (type: "hour" | "minute", value: string) => {
if (date) {
const current = parsedDate || new Date();
const newDate = new Date(current);
if (type === "hour") {
newDate.setHours(parseInt(value));
} else if (type === "minute") {
newDate.setMinutes(parseInt(value));
}
setDate(newDate);
}
};
return (
<Popover open={isOpen} onOpenChange={setIsOpen}>
<PopoverTrigger asChild>
<Button
variant={"outline"} disabled={disabled}
className={cn(
"w-full justify-start text-left font-normal",
!parsedDate && "text-muted-foreground"
)}
>
<CalendarIcon className="mr-2 h-4 w-4" />
{parsedDate ?
format(parsedDate, formatDate || "yyyy-MM-dd HH:mm")
:
<span>{placeholder || "YYYY-MM-DD HH:mm"}</span>
}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0">
<div className="sm:flex">
<Calendar
mode="single"
captionLayout={captionLayout || 'dropdown'}
disabled={disabledDate}
selected={parsedDate}
defaultMonth={parsedDate}
onSelect={handleDateSelect}
initialFocus
endMonth={new Date(2050, 11)}
/>
<div className="flex flex-col sm:flex-row sm:h-[300px] divide-y sm:divide-y-0 sm:divide-x">
<ScrollArea className="w-64 sm:w-auto">
<div className="flex sm:flex-col p-2">
{hours.map((hour) => (
<Button
type="button"
key={hour}
size="icon"
variant={parsedDate && parsedDate?.getHours() === hour ? "default" : "ghost"}
className="sm:w-full shrink-0 aspect-square"
onClick={() => handleTimeChange("hour", hour.toString())}
>
{hour}
</Button>
))}
</div>
<ScrollBar orientation="horizontal" className="sm:hidden" />
</ScrollArea>
<ScrollArea className="w-64 sm:w-auto">
<div className="flex sm:flex-col p-2">
{minutes.map((minute) => (
<Button
type="button"
key={minute}
size="icon"
variant={parsedDate && parsedDate?.getMinutes() === minute ? "default" : "ghost"}
className="sm:w-full shrink-0 aspect-square"
onClick={() => handleTimeChange("minute", minute.toString())}
>
{minute.toString().padStart(2, "0")}
</Button>
))}
</div>
<ScrollBar orientation="horizontal" className="sm:hidden" />
</ScrollArea>
</div>
</div>
{/* --- ADDED FOOTER HERE --- */}
<div className="p-3 border-t border-border flex justify-end">
<Button onClick={() => setIsOpen(false)} type="button">
Done
</Button>
</div>
</PopoverContent>
</Popover>
);
}
+68
View File
@@ -0,0 +1,68 @@
"use client"
import { useMemo, useState } from "react";
import { CalendarIcon } from "@radix-ui/react-icons";
import { format } from "date-fns";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Calendar } from "@/components/ui/calendar";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import type { Matcher } from "react-day-picker";
interface DatePickerProps {
date: Date | undefined;
setDate: (date: Date | undefined) => void;
placeholder?: string;
disabled?: boolean;
disabledDate?: Matcher | Matcher[] | undefined;
captionLayout?: "label" | "dropdown" | "dropdown-months" | "dropdown-years";
formatDate?: string;
className?: string
}
export function DatePicker({ date, setDate, placeholder, disabled, disabledDate, captionLayout, formatDate, className }: DatePickerProps) {
const [open, setOpen] = useState(false);
// Safety check to ensure we always work with a Date object
const parsedDate = useMemo(() => {
if (!date) return undefined;
if (typeof date === "string") return new Date(date);
return date;
}, [date]);
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant={"outline"} disabled={disabled}
className={cn(
"w-full justify-start text-left font-normal",
!parsedDate && "text-muted-foreground", className
)}
>
<CalendarIcon className="mr-2 h-4 w-4" />
{parsedDate ? format(parsedDate, formatDate || "PPP") : <span>{placeholder || "Pick a date"}</span>}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
captionLayout={captionLayout || 'dropdown'}
disabled={disabledDate}
selected={parsedDate}
defaultMonth={parsedDate}
onSelect={(selectedDate) => {
setDate(selectedDate);
setOpen(false); // Close popover immediately after selection
}}
initialFocus
endMonth={new Date(2050, 11)}
/>
</PopoverContent>
</Popover>
);
}
+33
View File
@@ -0,0 +1,33 @@
"use client"
import { cn } from "@/lib/utils";
import { useState, useRef, useEffect } from "react";
export function LazyImage({ src, alt, className }: { src: string; alt: string; className?: string }) {
const [isLoaded, setIsLoaded] = useState(false);
const imgRef = useRef<HTMLImageElement>(null);
// Check if the image is already loaded from the cache
useEffect(() => {
if (imgRef.current?.complete) {
setIsLoaded(true);
}
}, [src]); // Re-run this check if the src prop changes
return (
<div className={cn("relative overflow-hidden bg-secondary/50", className)}>
<img
ref={imgRef}
src={src}
alt={alt}
loading="lazy"
referrerPolicy="no-referrer"
onLoad={() => setIsLoaded(true)}
className={cn(
"w-full h-full object-cover transition-all duration-700 ease-in-out",
isLoaded ? "blur-0 scale-100 opacity-100" : "blur-xl scale-110 opacity-0"
)}
/>
</div>
);
}
+57
View File
@@ -0,0 +1,57 @@
"use client"
import * as React from "react"
import { Tooltip as TooltipPrimitive } from "radix-ui"
import { cn } from "@/lib/utils"
function TooltipProvider({
delayDuration = 0,
...props
}: React.ComponentProps<typeof TooltipPrimitive.Provider>) {
return (
<TooltipPrimitive.Provider
data-slot="tooltip-provider"
delayDuration={delayDuration}
{...props}
/>
)
}
function Tooltip({
...props
}: React.ComponentProps<typeof TooltipPrimitive.Root>) {
return <TooltipPrimitive.Root data-slot="tooltip" {...props} />
}
function TooltipTrigger({
...props
}: React.ComponentProps<typeof TooltipPrimitive.Trigger>) {
return <TooltipPrimitive.Trigger data-slot="tooltip-trigger" {...props} />
}
function TooltipContent({
className,
sideOffset = 0,
children,
...props
}: React.ComponentProps<typeof TooltipPrimitive.Content>) {
return (
<TooltipPrimitive.Portal>
<TooltipPrimitive.Content
data-slot="tooltip-content"
sideOffset={sideOffset}
className={cn(
"z-50 w-fit origin-(--radix-tooltip-content-transform-origin) animate-in rounded-md bg-foreground px-3 py-1.5 text-xs text-balance text-background fade-in-0 zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95",
className
)}
{...props}
>
{children}
<TooltipPrimitive.Arrow className="z-50 size-2.5 translate-y-[calc(-50%_-_2px)] rotate-45 rounded-[2px] bg-foreground fill-foreground" />
</TooltipPrimitive.Content>
</TooltipPrimitive.Portal>
)
}
export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }
+5
View File
@@ -42,6 +42,7 @@
"axios": "^1.13.2",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
"date-fns": "^4.1.0",
"dayjs": "^1.11.19",
"exceljs": "^4.4.0",
@@ -50,10 +51,12 @@
"get-orientation": "^1.1.2",
"heic2any": "^0.0.4",
"lucide-react": "^0.556.0",
"motion": "^12.38.0",
"next": "^16.0.10",
"next-auth": "^4.24.13",
"next-themes": "^0.4.6",
"pdf-lib": "^1.17.1",
"radix-ui": "^1.4.3",
"react": "^19.2.3",
"react-apexcharts": "^1.9.0",
"react-day-picker": "^9.12.0",
@@ -69,6 +72,8 @@
"swr": "^2.3.7",
"tailwind-merge": "^3.4.0",
"tailwind-scrollbar-hide": "^4.0.0",
"virtua": "^0.49.1",
"yup": "^1.7.1",
"zod": "^4.1.13"
},
"devDependencies": {