-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
b5e0848
commit 0aa760a
Showing
6 changed files
with
375 additions
and
6 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
27 changes: 27 additions & 0 deletions
27
apps/nextjs/src/components/board/sections/menu/section-menu-context.tsx
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,27 @@ | ||
import type { GetStylesApi } from "@mantine/core"; | ||
import { createSafeContext } from "@mantine/core"; | ||
|
||
import type { SectionMenuFactory } from "./section-menu"; | ||
|
||
interface SectionMenuContext { | ||
toggleDropdown: () => void; | ||
closeDropdownImmediately: () => void; | ||
closeDropdown: () => void; | ||
openDropdown: () => void; | ||
getItemIndex: (node: HTMLButtonElement) => number | null; | ||
setHovered: (index: number | null) => void; | ||
hovered: number | null; | ||
closeOnItemClick: boolean | undefined; | ||
loop: boolean | undefined; | ||
trigger: "click" | "hover" | "click-hover" | undefined; | ||
opened: boolean; | ||
unstyled: boolean | undefined; | ||
getStyles: GetStylesApi<SectionMenuFactory>; | ||
menuItemTabIndex: -1 | 0 | undefined; | ||
openedViaClick: boolean; | ||
setOpenedViaClick: (value: boolean) => void; | ||
} | ||
|
||
export const [SectionMenuContextProvider, useSectionMenuContext] = createSafeContext<SectionMenuContext>( | ||
"SectionMenu component was not found in the tree", | ||
); |
48 changes: 48 additions & 0 deletions
48
apps/nextjs/src/components/board/sections/menu/section-menu-dropdown.tsx
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,48 @@ | ||
import { useRef } from "react"; | ||
import type { Factory } from "@mantine/core"; | ||
import { factory, Popover } from "@mantine/core"; | ||
import { useMergedRef } from "@mantine/hooks"; | ||
|
||
import { useSectionMenuContext } from "./section-menu-context"; | ||
|
||
export type SectionMenuDropdownStylesNames = "dropdown"; | ||
|
||
export interface SectionMenuDropdownProps { | ||
childOpened: boolean; | ||
children: React.ReactNode; | ||
} | ||
|
||
export type SectionMenuDropdownFactory = Factory<{ | ||
props: SectionMenuDropdownProps; | ||
ref: HTMLDivElement; | ||
stylesNames: SectionMenuDropdownStylesNames; | ||
compound: true; | ||
}>; | ||
|
||
export const SectionMenuDropdown = factory<SectionMenuDropdownFactory>(({ childOpened, children }, ref) => { | ||
const wrapperRef = useRef<HTMLDivElement>(null); | ||
const ctx = useSectionMenuContext(); | ||
|
||
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => { | ||
if (childOpened) return; | ||
if (event.key === "ArrowUp" || event.key === "ArrowDown") { | ||
event.preventDefault(); | ||
wrapperRef.current?.querySelectorAll<HTMLButtonElement>("[data-section-menu-item]:not(:disabled)")[0]?.focus(); | ||
} | ||
}; | ||
|
||
return ( | ||
<Popover.Dropdown | ||
role="menu" | ||
aria-orientation="vertical" | ||
ref={useMergedRef(ref, wrapperRef)} | ||
{...ctx.getStyles("dropdown")} | ||
tabIndex={-1} | ||
data-section-menu-dropdown | ||
onKeyDown={handleKeyDown} | ||
> | ||
<div tabIndex={-1} data-autofocus data-mantine-stop-propagation style={{ outline: 0 }} /> | ||
{children} | ||
</Popover.Dropdown> | ||
); | ||
}); |
111 changes: 111 additions & 0 deletions
111
apps/nextjs/src/components/board/sections/menu/section-menu-item.tsx
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,111 @@ | ||
import { KeyboardEvent, useRef } from 'react'; | ||
import { | ||
BoxProps, | ||
CompoundStylesApiProps, | ||
createEventHandler, | ||
createScopedKeydownHandler, | ||
polymorphicFactory, | ||
PolymorphicFactory, | ||
UnstyledButton, | ||
useDirection, | ||
useProps, | ||
} from '@mantine/core'; | ||
import { useMergedRef } from '@mantine/hooks'; | ||
import { useSectionMenuContext } from './section-menu-context'; | ||
|
||
export type SectionMenuItemStylesNames = 'item' | 'itemLabel' | 'itemSection'; | ||
|
||
export interface SectionMenuItemProps | ||
extends BoxProps, | ||
CompoundStylesApiProps<SectionMenuItemFactory> { | ||
/** Item label */ | ||
children?: React.ReactNode; | ||
childMenu: { | ||
opened: boolean; | ||
close: () => void; | ||
open: () => void; | ||
}; | ||
} | ||
|
||
export type SectionMenuItemFactory = PolymorphicFactory<{ | ||
props: SectionMenuItemProps; | ||
defaultRef: HTMLButtonElement; | ||
defaultComponent: 'button'; | ||
stylesNames: SectionMenuItemStylesNames; | ||
compound: true; | ||
}>; | ||
|
||
const defaultProps: Partial<SectionMenuItemProps> = {}; | ||
|
||
export const SectionMenuItem = polymorphicFactory<SectionMenuItemFactory>((props, ref) => { | ||
const { childMenu, classNames, className, style, styles, vars, children, ...others } = useProps( | ||
'SectionMenuItem', | ||
defaultProps, | ||
props | ||
); | ||
|
||
const ctx = useSectionMenuContext(); | ||
const { dir } = useDirection(); | ||
const itemRef = useRef<HTMLButtonElement>(); | ||
const itemIndex = ctx.getItemIndex(itemRef.current!); | ||
const _others: any = others; | ||
|
||
const handleMouseLeave = createEventHandler(_others.onMouseLeave, () => ctx.setHovered(-1)); | ||
const handleMouseEnter = createEventHandler(_others.onMouseEnter, () => | ||
ctx.setHovered(ctx.getItemIndex(itemRef.current!)) | ||
); | ||
|
||
const handleFocus = createEventHandler(_others.onFocus, () => | ||
ctx.setHovered(ctx.getItemIndex(itemRef.current!)) | ||
); | ||
|
||
const scopedKeydownHandler = createScopedKeydownHandler({ | ||
siblingSelector: '[data-section-menu-item]', | ||
parentSelector: '[data-section-menu-dropdown]', | ||
activateOnFocus: false, | ||
loop: ctx.loop, | ||
dir, | ||
orientation: 'vertical', | ||
onKeyDown: _others.onKeyDown, | ||
}); | ||
|
||
const handleKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => { | ||
console.log('hi'); | ||
if ( | ||
(event.key === 'ArrowRight' && dir === 'ltr') || | ||
(event.key === 'ArrowLeft' && dir === 'rtl') | ||
) { | ||
childMenu.open(); | ||
return; | ||
} | ||
|
||
// We don't want to handle keydown event if child menu is opened | ||
if (childMenu.opened) { | ||
console.log('hey'); | ||
return; | ||
} | ||
|
||
console.log('hallo'); | ||
scopedKeydownHandler(event); | ||
}; | ||
|
||
return ( | ||
<UnstyledButton | ||
{...others} | ||
unstyled={ctx.unstyled} | ||
tabIndex={ctx.menuItemTabIndex} | ||
onFocus={handleFocus} | ||
{...ctx.getStyles('item', { className, style, styles, classNames })} | ||
ref={useMergedRef(itemRef, ref)} | ||
role="menuitem" | ||
data-section-menu-item | ||
data-hovered={ctx.hovered === itemIndex ? true : undefined} | ||
data-mantine-stop-propagation | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
onKeyDown={handleKeyDown} | ||
> | ||
{children} | ||
</UnstyledButton> | ||
); | ||
}); |
42 changes: 42 additions & 0 deletions
42
apps/nextjs/src/components/board/sections/menu/section-menu-target.tsx
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,42 @@ | ||
import { cloneElement, forwardRef } from "react"; | ||
import { createEventHandler, isElement, Popover, useProps } from "@mantine/core"; | ||
|
||
import { useSectionMenuContext } from "./section-menu-context"; | ||
|
||
export interface SectionMenuTargetProps { | ||
/** Target element */ | ||
children: React.ReactNode; | ||
|
||
/** Key of the prop that should be used to get element ref */ | ||
refProp?: string; | ||
} | ||
|
||
const defaultProps: Partial<SectionMenuTargetProps> = { | ||
refProp: "ref", | ||
}; | ||
|
||
export const SectionMenuTarget = forwardRef<HTMLElement, SectionMenuTargetProps>((props, ref) => { | ||
const { children, refProp, ...others } = useProps("SectionMenuTarget", defaultProps, props); | ||
|
||
if (!isElement(children)) { | ||
throw new Error( | ||
"SectionMenu.Target component children should be an element or a component that accepts ref. Fragments, strings, numbers and other primitive values are not supported", | ||
); | ||
} | ||
|
||
const ctx = useSectionMenuContext(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access | ||
const onClick = createEventHandler(children.props.onClick, () => { | ||
ctx.toggleDropdown(); | ||
}); | ||
|
||
return ( | ||
<Popover.Target refProp={refProp} popupType="menu" ref={ref} {...others}> | ||
{cloneElement(children, { | ||
onClick, | ||
"data-expanded": ctx.opened ? true : undefined, | ||
})} | ||
</Popover.Target> | ||
); | ||
}); |
Oops, something went wrong.