A multi-select component designed with shadcn/ui
A multi-select component designed with shadcn/ui
A powerful and flexible multi-select component built with React, TypeScript, Tailwind CSS, and shadcn/ui components.
Compatible with: Next.js, Vite, Create React App, and any React environment that supports path aliases and shadcn/ui components.
This component is compatible with any React project but requires proper setup:
@/ imports in your bundlercp src/components/multi-select.tsx your-project/components/npm install react react-dom
npm install @radix-ui/react-popover @radix-ui/react-separator
npm install lucide-react class-variance-authority clsx tailwind-merge cmdkInstall the required shadcn/ui components:
npx shadcn@latest add button badge popover command separatorAdd to your tsconfig.json or jsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}Add to your vite.config.ts:
import { defineConfig } from "vite";
import path from "path";
export default defineConfig({
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
});You may need to eject or use CRACO to configure path aliases.
Ensure you have the cn utility function in src/lib/utils.ts:
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}import { MultiSelect } from "@/components/multi-select";
import { useState } from "react";
const options = [
{ value: "react", label: "React" },
{ value: "vue", label: "Vue.js" },
{ value: "angular", label: "Angular" },
];
function App() {
const [selectedValues, setSelectedValues] = useState<string[]>([]);
return (
<MultiSelect
options={options}
onValueChange={setSelectedValues}
defaultValue={selectedValues}
/>
);
}const styledOptions = [
{
value: "react",
label: "React",
icon: ReactIcon,
style: {
badgeColor: "#61DAFB",
iconColor: "#282C34",
},
},
{
value: "vue",
label: "Vue.js",
icon: VueIcon,
style: {
gradient: "linear-gradient(135deg, #4FC08D 0%, #42B883 100%)",
},
},
];
<MultiSelect
options={styledOptions}
onValueChange={setSelected}
placeholder="Select with custom styles..."
/>;For Next.js projects, you can use the component in client components with the "use client" directive:
"use client";
import { MultiSelect } from "@/components/multi-select";
import { useState } from "react";
const options = [
{ value: "next", label: "Next.js" },
{ value: "react", label: "React" },
{ value: "typescript", label: "TypeScript" },
];
export default function MyPage() {
const [selected, setSelected] = useState<string[]>([]);
return (
);
}const groupedOptions = [
{
heading: "Frontend Frameworks",
options: [
{ value: "react", label: "React" },
{ value: "vue", label: "Vue.js" },
{ value: "angular", label: "Angular", disabled: true },
],
},
{
heading: "Backend Technologies",
options: [
{ value: "node", label: "Node.js" },
{ value: "python", label: "Python" },
],
},
];
<MultiSelect
options={groupedOptions}
onValueChange={setSelected}
placeholder="Select from groups..."
/>;The Multi-Select component includes comprehensive responsive design capabilities that automatically adapt to different screen sizes.
Enable responsive design with default settings:
<MultiSelect
options={options}
onValueChange={setSelected}
responsive={true} // Enable automatic responsive behavior
placeholder="Responsive component"
/>Default responsive settings:
Control component width with responsive adaptation:
<MultiSelect
options={options}
onValueChange={setSelected}
responsive={true}
minWidth="200px"
maxWidth="400px"
placeholder="Constrained width"
/>The Multi-Select component provides powerful integration with analytics dashboards and data visualization libraries.
import { MultiSelect } from "@/components/multi-select";
import { BarChart, Bar, XAxis, YAxis, ResponsiveContainer } from "recharts";
const Dashboard = () => {
const [selectedCategories, setSelectedCategories] = useState(["2024"]);
const filteredData = data.filter((item) =>
selectedCategories.includes(item.category)
);
return (
);
};const AdvancedDashboard = () => {
const [primaryFilters, setPrimaryFilters] = useState(["Performance"]);
const [secondaryFilters, setSecondaryFilters] = useState(["Speed"]);
return (
<ComposedChart data={filteredData}>
{/* Multiple chart types combined */}
</ComposedChart>
</div>
);
};const [selectedValues, setSelectedValues] = useState<string[]>([]);
<MultiSelect
options={frameworks}
onValueChange={setSelectedValues}
defaultValue={selectedValues}
placeholder="Select frameworks"
/>;const [selectedValues, setSelectedValues] = useState<string[]>([]);
<MultiSelect
options={frameworksWithIcons}
onValueChange={setSelectedValues}
defaultValue={selectedValues}
placeholder="Select technologies"
variant="inverted"
maxCount={3}
modalPopover={true}
/>;const [options, setOptions] = useState<Option[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
loadData().then((data) => {
setOptions(data);
setLoading(false);
});
}, []);
<MultiSelect
options={options}
onValueChange={setSelectedValues}
defaultValue={selectedValues}
placeholder={loading ? "Loading..." : "Select items"}
disabled={loading}
/>;maxCount for large selections to maintain UI
claritymodalPopover={true} for better mobile
experience| Prop | Type | Default | Description |
|---|---|---|---|
options |
MultiSelectOption[] | MultiSelectGroup[] |
- | Array of selectable options or groups |
onValueChange |
(value: string[]) => void |
- | Callback when selection changes |
defaultValue |
string[] |
[] |
Initially selected values |
placeholder |
string |
"Select options" |
Placeholder text |
variant |
"default" | "secondary" | "destructive" | "inverted" |
"default" |
Visual variant |
animation |
number |
0 |
Legacy animation duration in seconds |
animationConfig |
AnimationConfig |
- | Advanced animation configuration |
maxCount |
number |
3 |
Maximum visible selected items |
modalPopover |
boolean |
false |
Modal behavior for popover |
asChild |
boolean |
false |
Render as child component |
className |
string |
- | Additional CSS classes |
hideSelectAll |
boolean |
false |
Hide "Select All" option |
searchable |
boolean |
true |
Enable search functionality |
emptyIndicator |
ReactNode |
- | Custom empty state component |
autoSize |
boolean |
false |
Allow component to grow/shrink with content |
singleLine |
boolean |
false |
Show badges in single line with scroll |
popoverClassName |
string |
- | Custom CSS cla |
No open issues yet, or sync has not completed.