A fully customizable, accessible, and lightweight select dropdown built with React and Tailwind CSS.
Install the package via npm:
npm install react-tw-select
Or using yarn:
yarn add react-tw-select
import { useState } from "react";
import { Select } from "react-tw-select";
const options = [
{ label: "Option 1", value: "1" },
{ label: "Option 2", value: "2" },
{ label: "Option 3", value: "3" },
];
export default function App() {
const [selected, setSelected] = useState(null);
return (
<Select options={options} selected={selected} onChange={setSelected} />
);
}
You can override default styles using Tailwind classes:
<Select
options={options}
selected={selected}
onChange={setSelected}
buttonClassName="bg-blue-500 text-white p-2 rounded"
dropdownClassName="bg-gray-100 border border-gray-300"
optionClassName="hover:bg-gray-300"
selectedClassName="bg-blue-700 text-white"
/>
If you want to customize how options are displayed:
<Select
options={options}
selected={selected}
onChange={setSelected}
renderOption={(option, isSelected, isHighlighted) => (
<div
className={`p-2 ${isSelected ? "bg-green-500" : ""} ${
isHighlighted ? "bg-gray-200" : ""
}`}
>
{option.label}
</div>
)}
/>
Prop Name | Type | Description |
---|---|---|
options |
{ label: string, value: string }[] |
The list of options to display in the dropdown. |
selected |
{ label: string, value: string } | null |
The currently selected option. |
onChange |
(option: { label: string, value: string }) => void |
Function called when an option is selected. |
placeholder |
string |
Placeholder text when no option is selected. |
buttonClassName |
string |
Custom styles for the button. |
dropdownClassName |
string |
Custom styles for the dropdown container. |
optionClassName |
string |
Custom styles for options. |
selectedClassName |
string |
Styles for the selected option. |
icon |
ReactNode |
Custom icon for the dropdown button. |
renderOption |
(option, isSelected, isHighlighted) => ReactNode |
Custom rendering function for options. |
MIT License. Feel free to use and modify as needed!
If you find any issues or want to contribute, feel free to open an issue or a pull request on GitHub.
Happy coding! 🚀