opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Dropdowns
  4. Filter Sort
Back to Dropdowns

Components / Dropdowns

Filter Sort

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

Native select-style box — dropdown is flush below the trigger with matching width and a left-border active state.

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/filter-sort-dropdown.tsx in your project.

  4. 4

    Import and render:

    Example

    import { FilterSortDropdown } from "@/components/dropdowns/filter-sort-dropdown";

    <FilterSortDropdown value="newest" onValueChange={(o) => console.log(o.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/filter-sort-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
"use client";
 
import {
forwardRef,
useEffect,
useId,
useRef,
useState,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
 
import { ChevronDown } from "lucide-react";
 
export type FilterSortOption = Readonly<{
id: string;
label: string;
}>;
 
export type FilterSortDropdownProps = Readonly<
{
label?: string;
triggerAriaLabel?: string;
menuAriaLabel?: string;
value?: string;
options?: readonly FilterSortOption[];
onValueChange?: (option: FilterSortOption) => void;
} & ComponentPropsWithoutRef<"div">
>;
 
const defaultOptions: readonly FilterSortOption[] = [
{ id: "newest", label: "Newest first" },
{ id: "oldest", label: "Oldest first" },
{ id: "popular", label: "Most popular" },
{ id: "az", label: "A → Z" },
{ id: "za", label: "Z → A" },
];
 
type FilterSortOptionRowProps = Readonly<{
option: FilterSortOption;
selected: boolean;
onSelect: (option: FilterSortOption) => void;
}>;
 
function FilterSortOptionRow({
option,
selected,
onSelect,
}: FilterSortOptionRowProps) {
return (
<button
type="button"
role="menuitemradio"
aria-checked={selected}
aria-label={option.label}
onClick={() => onSelect(option)}
className={cn(
"flex w-full cursor-pointer items-center border-l-2 px-3 py-2 text-left text-[12px] font-medium transition-colors",
selected
? "border-neutral-800 bg-neutral-50 text-neutral-900"
: "border-transparent text-neutral-600 hover:border-neutral-200 hover:bg-neutral-50",
)}
>
{option.label}
</button>
);
}
 
// Native select style — menu width matches trigger exactly.
export const FilterSortDropdown = forwardRef<
HTMLDivElement,
FilterSortDropdownProps
>(
(
{
label = "Sort",
triggerAriaLabel = "Sort options",
menuAriaLabel = "Sort menu",
value = "newest",
options = defaultOptions,
onValueChange,
className,
...props
},
ref,
) => {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
const menuId = useId();
 
const selectedOption =
options.find((option) => option.id === value) ?? options[0];
 
useEffect(() => {
const closeOnOutside = (event: globalThis.MouseEvent) => {
if (!rootRef.current?.contains(event.target as Node)) {
setOpen(false);
}
};
 
const closeOnEscape = (event: KeyboardEvent) => {
if (event.key === "Escape") setOpen(false);
};
 
document.addEventListener("mousedown", closeOnOutside);
document.addEventListener("keydown", closeOnEscape);
return () => {
document.removeEventListener("mousedown", closeOnOutside);
document.removeEventListener("keydown", closeOnEscape);
};
}, []);
 
const handleOptionSelect = (option: FilterSortOption) => {
setOpen(false);
onValueChange?.(option);
};
 
const toggleOpen = () => setOpen((prev) => !prev);
 
return (
<div
ref={ref}
data-slot="filter-sort-dropdown"
className={cn("relative inline-block w-52 font-sans", className)}
{...props}
>
<div ref={rootRef} className="relative">
<button
type="button"
aria-label={`${triggerAriaLabel}: ${selectedOption?.label ?? value}`}
aria-expanded={open}
aria-haspopup="menu"
aria-controls={open ? menuId : undefined}
onClick={toggleOpen}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
toggleOpen();
}
}}
className={cn(
"flex w-full cursor-pointer items-center justify-between rounded-lg border bg-white px-3 py-2.5 text-left transition-colors",
open ? "border-neutral-300" : "border-neutral-200 hover:border-neutral-300",
)}
>
<span className="min-w-0">
<span className="block text-[10px] text-neutral-400">{label}</span>
<span className="mt-0.5 block truncate text-[12px] font-medium text-neutral-800">
{selectedOption?.label}
</span>
</span>
<ChevronDown
size={14}
className={cn(
"shrink-0 text-neutral-400 transition-transform",
open && "rotate-180",
)}
/>
</button>
 
{open ? (
<div
id={menuId}
role="menu"
aria-label={menuAriaLabel}
className="absolute top-[calc(100%+4px)] right-0 left-0 z-100 overflow-hidden rounded-lg border border-neutral-200 bg-white py-1 shadow-[0_8px_24px_-8px_rgba(0,0,0,0.1)]"
>
{options.map((option) => (
<FilterSortOptionRow
key={option.id}
option={option}
selected={option.id === value}
onSelect={handleOptionSelect}
/>
))}
</div>
) : null}
</div>
</div>
);
},
);
 
FilterSortDropdown.displayName = "FilterSortDropdown";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/dropdowns/filter-sort-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.