File size: 3,493 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Popover } from '@wordpress/components';
import {
	LegacyRef,
	useCallback,
	useEffect,
	useMemo,
	useRef,
	useState,
	ComponentProps,
} from 'react';

type SubmenuPopoverProps = ComponentProps< typeof Popover > & {
	isVisible?: boolean;
	placement?:
		| 'top'
		| 'top-start'
		| 'top-end'
		| 'bottom'
		| 'bottom-start'
		| 'bottom-end'
		| 'right'
		| 'right-start'
		| 'right-end'
		| 'left'
		| 'left-start'
		| 'left-end';
	anchor?: Element | undefined;
	flip?: boolean;
	resize?: boolean;
	inline?: boolean;
};

/**
 * Adds a11y support to the submenu popover.
 * - Closes the popover when pressing Escape.
 * - Closes the popover when pressing Tab and the focus is on the last element.
 */
function useCloseSubmenuA11y() {
	return useCallback(
		( {
			event,
			lastChild,
			setIsVisible,
		}: {
			event: React.KeyboardEvent< HTMLElement >;
			lastChild: Element | null | undefined;
			setIsVisible: ( isVisible: boolean ) => void;
		} ) => {
			const isEscape = event.key === 'Escape';
			const tabOnLastChild = event.key === 'Tab' && ! event.shiftKey && lastChild === event.target;
			if ( isEscape || tabOnLastChild ) {
				setIsVisible( false );
			}
		},
		[]
	);
}

/**
 * Checks if the submenu popover has enough space to be displayed on the right.
 * If not, it will return false to be displayed on the left.
 */
function useHasRightSpace( parentElement: HTMLElement | undefined, isVisible: boolean ): boolean {
	const [ widthSubmenu, setWidthSubmenu ] = useState( 0 );

	useEffect( () => {
		if ( isVisible && parentElement ) {
			const submenuElement = parentElement.querySelector< HTMLElement >( '.submenu-popover' );
			if ( submenuElement ) {
				setWidthSubmenu( submenuElement.offsetWidth );
			}
		}
	}, [ parentElement, isVisible ] );

	return useMemo( () => {
		if ( ! parentElement ) {
			return true;
		}
		const calculatedThreshold = widthSubmenu;
		const { right } = parentElement.getBoundingClientRect();
		return window.innerWidth - right > calculatedThreshold;
	}, [ parentElement, widthSubmenu ] );
}

export function useSubmenuPopoverProps< T extends HTMLElement >(
	options: Omit< SubmenuPopoverProps, 'isVisible' | 'placement' | 'anchor' | 'children' > = {
		offset: 0,
		flip: true,
		resize: true,
		inline: false,
	}
) {
	const { offset, inline, flip, resize } = options;
	const [ isVisible, setIsVisible ] = useState( false );
	const anchor = useRef< T >();
	const parentElement = anchor?.current;
	const hasRightSpace = useHasRightSpace( parentElement, isVisible );
	const closeSubmenuA11y = useCloseSubmenuA11y();

	const submenu: Partial< SubmenuPopoverProps > = {
		isVisible,
		placement: hasRightSpace ? 'right-start' : 'left-start',
		anchor: anchor?.current,
		offset,
		flip,
		resize,
		inline,
	};

	const parent = {
		ref: anchor as LegacyRef< T >,
		onMouseOver: () => setIsVisible( true ),
		onMouseLeave: () => setIsVisible( false ),
		onClick: () => setIsVisible( true ),
		onKeyDown: ( event: React.KeyboardEvent< T > ) => {
			const lastChild = anchor.current?.querySelector(
				'.submenu-popover > :last-child > :last-child'
			);
			closeSubmenuA11y( { event, lastChild, setIsVisible } );
		},
	};

	return {
		parent,
		submenu,
	};
}

function SubmenuPopover( props: SubmenuPopoverProps ) {
	const { children, isVisible = false, ...rest } = props;
	if ( ! isVisible ) {
		return null;
	}
	return (
		<Popover className="submenu-popover" { ...rest }>
			{ children }
		</Popover>
	);
}

export default SubmenuPopover;