Cannot override SelectItem hover CSS
NextUI Version
2.1.5
Describe the bug
I am trying to override the hover for the
Here's how I'm doing it:
<SelectItem
style={{hover:''}}
className='bg-royal-blue hover:bg-dark-orange text-white font-bold flex justify-between'
key={animal.value}
value={animal.value}>
{animal.label}
</SelectItem>
Thanks & awesome library!
Your Example Website or App
No response
Steps to Reproduce the Bug or Issue
Go to https://nextui.org/docs/components/select#colors Copy-paste any Select. Try to alter the SelectItem hover background color with tailwind.
Expected behavior
I expect to be able to override the hover background color of the SelectItem component.
Screenshots or Videos
No response
Operating System Version
Windows
Browser
Chrome
This isn't a bug, you aren't setting up hover correctly. You need to use the data attributes for the style.
<SelectItem
className="bg-royal-blue data-[hover=true]:bg-dark-orange text-white font-bold flex justify-between"
key={animal.value}
value={animal.value}
>
{animal.label}
</SelectItem>
This should give you what you are looking for in terms of hover background.
@CaseyBlackburn
Thanks a lot for the reply!
It seems like the issue still persists, even after using your solution. Does it work for you?
Yes, it works for me. The colors you are using aren't standard classes available with NextUI/Tailwind so I am assuming you have created those yourself. I switched them to similar colors in my testing.
<Select
label="Select an animal"
className="max-w-xs"
>
{animals.map((animal) => (
<SelectItem
className="bg-sky-700 data-[hover=true]:bg-orange-900 text-white font-bold flex justify-between"
key={animal.value}
value={animal.value}
>
{animal.label}
</SelectItem>
))}
</Select>
@CaseyBlackburn Interesting, it still doesn't work for me (copy-paste):
What could it be?
@CaseyBlackburn
In this video you can see the behavior I experience: https://github.com/nextui-org/nextui/assets/88039412/68b8db74-8496-4e0e-b4bd-76646e7723f0
Hey @CaseyBlackburn please check the custom styles examples in the docs: https://nextui.org/docs/components/select#customizing-the-select
Here's the codesandbox: https://codesandbox.io/p/sandbox/6qt7lp?file=/App.jsx:1,1&utm_medium=sandpack
Hey @jrgarciadev ,this still doesn't seem to be able to override the hover color (used listBoxProps on both the SelectItem and Select):
<Select
color={'primary'}
label="Select someone"
size='lg'
items={users}
className="max-w-lg"
popoverProps={{
classNames: {
base: "p-0 border-small border-divider bg-background data-[hover=true]:bg-red-900",
arrow: "bg-default-200",
},
}}
listBoxProps={{
itemClasses: {
base: 'data-[hover=true]:bg-orange-900'
}
}}
>
{users.slice(0, 6).map((user, idx) => {
return idx === 1 ?
(
<SelectItem
listBoxProps={{
itemClasses: {
base: 'data-[hover=true]:bg-orange-900'
}
}}
size='lg'
className="bg-blue-700 text-white font-bold flex justify-between"
key={user.name}
value={user.name}
textValue={user.name}
endContent={<div>
<div className="font-bold text-dark-orange">Most popular</div>
</div>}
>
<div className="flex justify-between">
<div>{user.name} MVP</div>
</div>
</SelectItem>
) : (
<SelectItem key={user.name} value={user.name} >
{user.name}
</SelectItem>
)
})}
</Select>
Thanks & awesome work!
@vpaul18 I got my custom style color (indigo) to show with the below. BUT... the NextUI theme color flickers just before the list item's background turns indigo on hover. Almost the same as in the clip you have above. Not sure what else to try here. Customizing a theme has not worked for me either as the docs are not as exhaustive as I'd like. Still a really great library though. Great work!
Notice the "!" before my background utility class. It did not work without it.
<SelectItem
key={option.value}
value={option.value}
className="data-[hover=true]:!bg-indigo-600"
>
{option.label}
</SelectItem>
Update
Was able to get it working thanks to @jrgarciadev above... Add the listboxProps and renderValue props to theSelect component.
renderValue={(items: SelectedItems) => {
return items.map((item: SelectedItemProps) => {
const { label, value } = item.props as Record<string, any>;
return <div key={value}>{label}</div>;
});
}}
listboxProps={{
itemClasses: {
base: [
"dark:data-[hover=true]:bg-indigo-500",
"dark:data-[selectable=true]:focus:bg-indigo-500",
"dark:data-[selectable=true]:!text-white",
],
},
}}
@ctwillie @vpaul18 you also don't have to use listboxProps if you just include it in the tailwind but with the proper formatting, specifically if you're using a theme (I was using dark, so overriding the hover wasn't working with data-[hover=true] until i use dark:data-[hover=true]
example: <SelectItem className="dark:data-[selectable=true]:focus:bg-indigo-500 dark:data-[hover=true]:bg-indigo-500 text-black" key={wallet} value={wallet}>