2024-11-22 web, development, javascript
Styling a react-select Combobox to Match shadcn/ui
By O. Wolfson
If you're using the shadcn/ui component system in your Next.js 13 application and want to integrate a multi-select dropdown combobox with similar styles, the react-select library can be a good choice. In this tutorial, I'll walk through the process of styling a react-select combobox component to match the styles of shadcn/ui, including support for dark and light modes.
Here is a deployed example: https://shadcn-tester.vercel.app/multi-select
Here is the source code: https://github.com/owolfdev/shadcn-tester
Prerequisites
To follow along with this tutorial, make sure you have the following installed:
- Next.js
- react-select package
- shadcn/ui component system (or a similar component system)
- next-themes package
Note that we are comparing the shadcn/ui input component to the react-select combobox component, so we'll need to import the the shadcn/ui input component as well as the react-select combobox component. We've set up a component called InputDemo that uses the shadcn input component.
Note we are styling a dark mode and light mode version of the component, so we'll need to use the next-themes package to access the current theme. If you are using shadcn/ui template, as I am, next-themes is already installed.
You can install the template as follows:
I am not giving comprehensive installation instructions here, so it's highly recommend to read the installation documentation for shadcn/ui:
https://ui.shadcn.com/docs/installation
Here is the the page component that we'll be working with:
The useEffect
hook is used to update the custom styles of the React Select component based on the current theme.
The theme
variable is obtained from the useTheme
hook provided by the next-themes
library. It represents the current theme mode, which can be either "dark" or "light".
Inside the useEffect
hook, a new set of custom styles is created using the setCustomStyles
function. These custom styles are defined as an object with properties corresponding to different elements of the React Select component.
Let's go through each property and understand how it works with dark mode:
-
option
: This property defines the styles for each option in the select dropdown. TheisFocused
argument represents whether the option is currently focused. Based on theisFocused
value and the current theme, the background color and text color of the option are set. In dark mode, the background color is set to#e2e8f0
and the text color toblack
when focused. -
placeholder
: This property defines the styles for the placeholder text in the select input. The provided styles include the color (#6B7280
) and font size (14px
). These styles are applied regardless of the theme mode. -
multiValue
: This property defines the styles for the selected values when multiple options are selected. The provided styles include the background color (#e2e8f0
), border radius (0.35rem
), text color (#6B7280
), and font size (14px
). These styles are applied regardless of the theme mode. -
control
: This property defines the styles for the control element of the select input, including the input field and the dropdown indicator. The styles are defined using thedefaultStyles
argument, which represents the default styles provided by the React Select library. Additionally, thestate.isFocused
property is used to determine whether the control element is currently focused. Based on the focus state and the current theme, the styles for the control element are modified. In dark mode, a black box shadow is added when focused.
By including the theme
variable in the dependency array [theme]
of the useEffect
hook, the custom styles will be updated whenever the theme changes. This ensures that the React Select component adapts its styles based on the selected theme mode.
That's how the provided code section works with dark mode to style the React Select component. By modifying the custom styles based on the theme, you can create a visually consistent and appealing select input in your React application.