Files
Verso/services/web/frontend/js/shared/components/menu-bar/menu-bar-dropdown.tsx
T
Rebeka DekanyandCopybot d751b88e6b Bootstrap files and folders cleanup (#27692)
* Remove icons folder

* Create folders for badge, button, and dropdown components

* Remove Bootstrap 5 from test

* Rename `getBootstrap5Breakpoint` to `getBootstrapBreakpoint`

* Cleanup and update BS 5 comments

* Move components to the shared folder

* Rename `tooltips-bs5` to `tooltip`

* Remove `-bs5` suffix

* Fix path

* Delete BS3 version file

* Rename `_form_marketing-bootstrap-5` to `_form_marketing`

* Delete BS3 version file

* Rename `_contact_general_modal-marketing-bootstrap-5` to `_contact_general_modal-marketing`

* Delete BS3 version file

* Rename `_contact_modal-marketing-bootstrap-5` to `_contact_modal-marketing`

* Delete BS3 version file

* Rename `thin-footer-bootstrap-5` to `thin-footer`

* Delete BS3 version file

* Rename `language-picker-bootstrap-5` to `language-picker`

* Rename `fat-footer-react-bootstrap-5` to `fat-footer-react`

* Delete BS3 version file

* Rename `navbar-marketing-bootstrap-5` to `navbar-marketing`

* Rename `navbar-marketing-react-bootstrap-5` to `navbar-marketing-react`

* Delete BS3 version file

* Rename `layout-website-redesign-cms-bootstrap-5` to `layout-website-redesign-cms`

* Source format

* Fix path

GitOrigin-RevId: cf0f5db7c84cf545c69213dcc271d9ff17fe5db7
2025-08-11 08:06:16 +00:00

139 lines
3.4 KiB
TypeScript

import {
Dropdown,
DropdownMenu,
DropdownToggle,
} from '@/shared/components/dropdown/dropdown-menu'
import { FC, forwardRef, useCallback } from 'react'
import classNames from 'classnames'
import { useNestableDropdown } from '@/shared/hooks/use-nestable-dropdown'
import { NestableDropdownContextProvider } from '@/shared/context/nestable-dropdown-context'
import { AnchorProps } from 'react-bootstrap'
import MaterialIcon from '../material-icon'
import { DropdownMenuProps } from '@/shared/components/types/dropdown-menu-props'
type MenuBarDropdownProps = {
title: string
id: string
className?: string
align?: 'start' | 'end'
}
export const MenuBarDropdown: FC<
React.PropsWithChildren<MenuBarDropdownProps>
> = ({ title, children, id, className, align = 'start' }) => {
const { menuId, selected, setSelected } = useNestableDropdown()
const onToggle = useCallback(
(show: boolean) => {
setSelected(show ? id : null)
},
[id, setSelected]
)
const onHover = useCallback(() => {
setSelected(prev => {
if (prev === null) {
return null
}
return id
})
}, [id, setSelected])
return (
<Dropdown
show={selected === id}
align={align}
onToggle={onToggle}
autoClose
>
<DropdownToggle
id={`${menuId}-${id}`}
variant="secondary"
className={classNames(className, 'menu-bar-toggle')}
onMouseEnter={onHover}
>
{title}
</DropdownToggle>
<NestableDropdownMenu renderOnMount id={`${menuId}-${id}`}>
{children}
</NestableDropdownMenu>
</Dropdown>
)
}
const NestableDropdownMenu: FC<
React.PropsWithChildren<DropdownMenuProps & { id: string }>
> = ({ children, id, ...props }) => {
return (
<DropdownMenu {...props}>
<NestableDropdownContextProvider id={id}>
{children}
</NestableDropdownContextProvider>
</DropdownMenu>
)
}
const NestedDropdownToggle: FC<React.PropsWithChildren> = forwardRef<
HTMLAnchorElement,
AnchorProps
>(function NestedDropdownToggle(
{ children, className, onMouseEnter, id },
ref
) {
return (
// eslint-disable-next-line jsx-a11y/anchor-is-valid
<a
id={id}
href="#"
ref={ref}
onMouseEnter={onMouseEnter}
onClick={onMouseEnter}
className={classNames(
className,
'nested-dropdown-toggle',
'dropdown-item'
)}
>
{children}
<MaterialIcon type="chevron_right" />
</a>
)
})
export const NestedMenuBarDropdown: FC<
React.PropsWithChildren<{ id: string; title: string }>
> = ({ children, id, title }) => {
const { menuId, selected, setSelected } = useNestableDropdown()
const select = useCallback(() => {
setSelected(id)
}, [id, setSelected])
const onToggle = useCallback(
(show: boolean) => {
setSelected(show ? id : null)
},
[setSelected, id]
)
const active = selected === id
return (
<Dropdown
align="start"
drop="end"
show={active}
autoClose
onToggle={onToggle}
>
<DropdownToggle
id={`${menuId}-${id}`}
onMouseEnter={select}
className={classNames({ 'nested-dropdown-toggle-shown': active })}
as={NestedDropdownToggle}
>
{title}
</DropdownToggle>
<NestableDropdownMenu renderOnMount id={`${menuId}-${id}`}>
{children}
</NestableDropdownMenu>
</Dropdown>
)
}