Dropdown with option animation
Play options animation on dropdown open
animations
🍎 Apples
🍌 Bananas
🥦 Broccoli
🥕 Carrots
🍫 Chocolate
🍇 Grapes
import { useState } from 'react';
import cx from 'clsx';
import { Combobox, Input, InputBase, useCombobox } from '@mantine/core';
import classes from './DropdownOptionsAnimation.module.css';
const groceries = [
'🍎 Apples',
'🍌 Bananas',
'🥦 Broccoli',
'🥕 Carrots',
'🍫 Chocolate',
'🍇 Grapes',
];
export function DropdownOptionsAnimation() {
const [animating, setAnimating] = useState(false);
const store = useCombobox({
onDropdownClose: () => {
store.resetSelectedOption();
setAnimating(false);
},
onDropdownOpen: () => setAnimating(true),
});
const [value, setValue] = useState<string | null>(null);
const options = groceries.map((item, index) => (
<Combobox.Option
value={item}
key={item}
className={cx({ [classes.animateOption]: animating })}
style={{ animationDelay: `${index * 30}ms` }}
>
{item}
</Combobox.Option>
));
return (
<Combobox
store={store}
withinPortal={false}
onOptionSubmit={(val) => {
setValue(val);
store.closeDropdown();
}}
>
<Combobox.Target targetType="button">
<InputBase
component="button"
type="button"
pointer
rightSection={<Combobox.Chevron />}
onClick={() => store.toggleDropdown()}
onBlur={() => store.closeDropdown()}
rightSectionPointerEvents="none"
aria-label="Pick value"
>
{value || <Input.Placeholder>Pick value</Input.Placeholder>}
</InputBase>
</Combobox.Target>
<Combobox.Dropdown>
<Combobox.Options>{options}</Combobox.Options>
</Combobox.Dropdown>
</Combobox>
);
}