-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79e72bd
commit c360d63
Showing
15 changed files
with
469 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Direction = 'top left' | 'top right' | 'bottom left' | 'bottom right'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
.Dropdown { | ||
position: relative; | ||
} | ||
|
||
.trigger { | ||
cursor: pointer; | ||
outline-color: transparent; | ||
margin: 0; | ||
padding: 0; | ||
border: none; | ||
background: none; | ||
} | ||
|
||
.options { | ||
background: var(--card-bg); | ||
position: absolute; | ||
z-index: 10; | ||
border-radius: 0 0 4px 4px; | ||
border: 2px solid var(--primary-color); | ||
display: flex; | ||
flex-direction: column; | ||
overflow: hidden; | ||
} | ||
|
||
.option { | ||
padding: 8px 24px; | ||
padding-left: 12px; | ||
width: 100%; | ||
border: none; | ||
background: none; | ||
outline-color: transparent; | ||
color: var(--primary-color); | ||
cursor: pointer; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
white-space: nowrap; | ||
gap: 8px; | ||
} | ||
|
||
.top-rounding { | ||
border-radius: 12px 12px 0 0; | ||
} | ||
|
||
.bottom-rounding { | ||
border-radius: 0 0 12px 12px; | ||
} | ||
|
||
.active { | ||
background: var(--inverted-primary-color) ; | ||
} | ||
|
||
.disabled { | ||
opacity: .5; | ||
} | ||
|
||
.bottom-right { | ||
top: 100%; | ||
right: 0; | ||
} | ||
|
||
.bottom-left { | ||
top: 100%; | ||
left: 0; | ||
} | ||
|
||
.top-right { | ||
bottom: 100%; | ||
right: 0; | ||
} | ||
|
||
.top-left { | ||
bottom: 100%; | ||
left: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { AboutPageIcon, ArticlesPageIcon } from 'shared/assets/icons'; | ||
import { action } from '@storybook/addon-actions'; | ||
import { Button } from '../Button'; | ||
import { Dropdown } from './Dropdown'; | ||
|
||
const meta: Meta<typeof Dropdown> = { | ||
title: 'shared/Dropdown', | ||
component: Dropdown, | ||
tags: ['autodocs'], | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Dropdown>; | ||
|
||
export const Root: Story = { | ||
args: { | ||
component: ( | ||
<Button> | ||
Trigger Button | ||
</Button> | ||
), | ||
items: [ | ||
{ | ||
// ItemIcon: AboutPageIcon, | ||
label: 'Some label 1', | ||
value: '1', | ||
action: () => action('Action 1'), | ||
}, | ||
{ | ||
// ItemIcon: ArticlesPageIcon, | ||
label: 'Some label 2', | ||
value: '2', | ||
action: () => action('Action 2'), | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export const ItemsWithIcons: Story = { | ||
args: { | ||
component: ( | ||
<Button> | ||
Trigger Button | ||
</Button> | ||
), | ||
items: [ | ||
{ | ||
ItemIcon: AboutPageIcon, | ||
label: 'Some label 1', | ||
value: '1', | ||
action: (index) => action(`Action ${index}`), | ||
}, | ||
{ | ||
ItemIcon: ArticlesPageIcon, | ||
label: 'Some label 2', | ||
value: '2', | ||
action: (index) => action(`Action ${index}`), | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export const ItemWithHref: Story = { | ||
args: { | ||
component: ( | ||
<Button> | ||
Trigger Button | ||
</Button> | ||
), | ||
items: [ | ||
{ | ||
ItemIcon: AboutPageIcon, | ||
label: 'Some label 1', | ||
value: '1', | ||
action: (index) => action(`Action ${index}`), | ||
href: '/some-link', | ||
}, | ||
{ | ||
ItemIcon: ArticlesPageIcon, | ||
label: 'Some label 2', | ||
value: '2', | ||
action: (index) => action(`Action ${index}`), | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export const DisabledItem: Story = { | ||
args: { | ||
component: ( | ||
<Button> | ||
Trigger Button | ||
</Button> | ||
), | ||
items: [ | ||
{ | ||
ItemIcon: AboutPageIcon, | ||
label: 'Some label 1', | ||
value: '1', | ||
action: (index) => action(`Action ${index}`), | ||
disabled: true, | ||
}, | ||
{ | ||
ItemIcon: ArticlesPageIcon, | ||
label: 'Some label 2', | ||
value: '2', | ||
action: (index) => action(`Action ${index}`), | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { FC, Fragment, ReactNode, useCallback } from 'react'; | ||
import { Menu } from '@headlessui/react'; | ||
import { classNames, Mods } from 'shared/lib/classNames/classNames'; | ||
import { Direction } from 'shared/types/ui'; | ||
import { Icon } from '../Icon'; | ||
import { AppLink } from '../AppLink'; | ||
import cls from './Dropdown.module.scss'; | ||
|
||
type DropdownAction = (index?: number) => void; | ||
|
||
export interface DropdownItem { | ||
ItemIcon?: FC; | ||
action?: DropdownAction; | ||
label: string; | ||
value?: any; | ||
disabled?: boolean; | ||
href?: string; | ||
} | ||
|
||
interface DropdownProps { | ||
className?: string; | ||
items: DropdownItem[]; | ||
component: ReactNode; | ||
onChange?: (v: any) => void; | ||
position?: Direction; | ||
} | ||
|
||
const positionMapper: Record<Direction, string> = { | ||
'bottom left': cls['bottom-left'], | ||
'bottom right': cls['bottom-right'], | ||
'top left': cls['top-left'], | ||
'top right': cls['top-right'], | ||
}; | ||
|
||
export const Dropdown: FC<DropdownProps> = ({ className, items, component, onChange, position = 'bottom left' }) => { | ||
const onChangeHandler = useCallback((value: any, index: number, action?: DropdownAction) => () => { | ||
onChange?.(value); | ||
action?.(index); | ||
}, [onChange]); | ||
|
||
const optionsMods: Mods = { | ||
[cls['top-rounding']]: position === 'top left' || position === 'top right', | ||
[cls['bottom-rounding']]: position === 'bottom left' || position === 'bottom right', | ||
}; | ||
|
||
return ( | ||
<Menu as="div" className={classNames(cls.Dropdown, {}, [className])}> | ||
<Menu.Button as="button" className={cls.trigger}> | ||
{component} | ||
</Menu.Button> | ||
<Menu.Items className={classNames(cls.options, optionsMods, [positionMapper[position]])}> | ||
{items.map((option, index) => ( | ||
<Menu.Item as={Fragment} key={option.value + index} disabled={option.disabled}> | ||
{({ active, disabled }) => { | ||
const mods: Mods = { [cls.active]: active, [cls.disabled]: disabled }; | ||
|
||
return (option.href | ||
? ( | ||
<AppLink | ||
to={option.href} | ||
className={classNames(cls.option, mods)} | ||
onClick={onChangeHandler(option.value, index, option.action)} | ||
> | ||
{option.ItemIcon && <Icon SVG={<option.ItemIcon />} />} | ||
{option.label} | ||
</AppLink> | ||
) | ||
: ( | ||
<button | ||
type="button" | ||
className={classNames(cls.option, mods)} | ||
onClick={onChangeHandler(option.value, index, option.action)} | ||
disabled={option.disabled} | ||
> | ||
{option.ItemIcon && <Icon SVG={<option.ItemIcon />} />} | ||
{option.label} | ||
</button> | ||
) | ||
); | ||
}} | ||
</Menu.Item> | ||
))} | ||
</Menu.Items> | ||
</Menu> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Dropdown } from './Dropdown'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
width: 100%; | ||
|
||
&:focus { | ||
outline: none; | ||
outline-color: transparent; | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.