| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 9 | import {findParentWithClassName} from 'isl-components/utils'; |
| b69ab31 | | | 10 | import {getZoomLevel} from 'isl-components/zoom'; |
| b69ab31 | | | 11 | import {atom, useAtom, useSetAtom} from 'jotai'; |
| b69ab31 | | | 12 | import React, {useEffect, useRef, useState} from 'react'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import './ContextMenu.css'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | /** |
| b69ab31 | | | 17 | * Hook to create a context menu in HTML. |
| b69ab31 | | | 18 | * Pass in a function that returns the list of context menu items. |
| b69ab31 | | | 19 | * Then use the result in onContextMenu: |
| b69ab31 | | | 20 | * ``` |
| b69ab31 | | | 21 | * function MyComponent() { |
| b69ab31 | | | 22 | * const menu = useContextMenu(() => [ |
| b69ab31 | | | 23 | * {label: 'Choice 1', onClick: () => console.log('clicked!')} |
| b69ab31 | | | 24 | * ]); |
| b69ab31 | | | 25 | * return <div onContextMenu={menu}>...</div> |
| b69ab31 | | | 26 | * } |
| b69ab31 | | | 27 | * ``` |
| b69ab31 | | | 28 | */ |
| b69ab31 | | | 29 | export function useContextMenu<T>( |
| b69ab31 | | | 30 | creator: () => Array<ContextMenuItem>, |
| b69ab31 | | | 31 | ): React.MouseEventHandler<T> { |
| b69ab31 | | | 32 | const setState = useSetAtom(contextMenuState); |
| b69ab31 | | | 33 | return e => { |
| b69ab31 | | | 34 | const zoom = getZoomLevel(); |
| b69ab31 | | | 35 | const items = creator(); |
| b69ab31 | | | 36 | if (items.length === 0) { |
| b69ab31 | | | 37 | return; |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | setState({x: e.clientX / zoom, y: e.clientY / zoom, items}); |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | e.preventDefault(); |
| b69ab31 | | | 42 | e.stopPropagation(); |
| b69ab31 | | | 43 | }; |
| b69ab31 | | | 44 | } |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | type ContextMenuData = {x: number; y: number; items: Array<ContextMenuItem>}; |
| b69ab31 | | | 47 | export type ContextMenuItem = |
| b69ab31 | | | 48 | | {type?: undefined; label: string | React.ReactNode; onClick?: () => void; tooltip?: string} |
| b69ab31 | | | 49 | | { |
| b69ab31 | | | 50 | type: 'submenu'; |
| b69ab31 | | | 51 | label: string | React.ReactNode; |
| b69ab31 | | | 52 | children: Array<ContextMenuItem>; |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | | {type: 'divider'}; |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | export const contextMenuState = atom<null | ContextMenuData>(null); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | export function ContextMenus() { |
| b69ab31 | | | 59 | const [state, setState] = useAtom(contextMenuState); |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | const ref = useRef<HTMLDivElement>(null); |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | useEffect(() => { |
| b69ab31 | | | 64 | if (state != null) { |
| b69ab31 | | | 65 | const hide = (e: Event) => { |
| b69ab31 | | | 66 | if (e.type === 'keyup') { |
| b69ab31 | | | 67 | if ((e as KeyboardEvent).key === 'Escape') { |
| b69ab31 | | | 68 | setState(null); |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | return; |
| b69ab31 | | | 71 | } else if (e.type === 'click' || e.type === 'scroll') { |
| b69ab31 | | | 72 | // if click or scroll inside the context menu, don't dismiss |
| b69ab31 | | | 73 | if (findParentWithClassName(e.target as HTMLElement, 'context-menu-container')) { |
| b69ab31 | | | 74 | return; |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | } |
| b69ab31 | | | 77 | setState(null); |
| b69ab31 | | | 78 | }; |
| b69ab31 | | | 79 | window.addEventListener('click', hide, true); |
| b69ab31 | | | 80 | window.addEventListener('scroll', hide, true); |
| b69ab31 | | | 81 | window.addEventListener('resize', hide, true); |
| b69ab31 | | | 82 | window.addEventListener('keyup', hide, true); |
| b69ab31 | | | 83 | return () => { |
| b69ab31 | | | 84 | window.removeEventListener('click', hide, true); |
| b69ab31 | | | 85 | window.removeEventListener('scroll', hide, true); |
| b69ab31 | | | 86 | window.removeEventListener('resize', hide, true); |
| b69ab31 | | | 87 | window.removeEventListener('keyup', hide, true); |
| b69ab31 | | | 88 | }; |
| b69ab31 | | | 89 | } |
| b69ab31 | | | 90 | }, [state, setState]); |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | if (state == null) { |
| b69ab31 | | | 93 | return null; |
| b69ab31 | | | 94 | } |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | const zoom = getZoomLevel(); |
| b69ab31 | | | 97 | const topOrBottom = state.y > window.innerHeight / zoom / 2 ? 'bottom' : 'top'; |
| b69ab31 | | | 98 | const leftOrRight = state.x > window.innerWidth / zoom / 2 ? 'right' : 'left'; |
| b69ab31 | | | 99 | const yOffset = 10; |
| b69ab31 | | | 100 | const xOffset = -10; // var(--pad) |
| b69ab31 | | | 101 | let position: React.CSSProperties; |
| b69ab31 | | | 102 | if (topOrBottom === 'top') { |
| b69ab31 | | | 103 | if (leftOrRight === 'left') { |
| b69ab31 | | | 104 | position = {top: state.y + yOffset, left: state.x + xOffset}; |
| b69ab31 | | | 105 | } else { |
| b69ab31 | | | 106 | position = {top: state.y + yOffset, right: window.innerWidth / zoom - (state.x - xOffset)}; |
| b69ab31 | | | 107 | } |
| b69ab31 | | | 108 | } else { |
| b69ab31 | | | 109 | if (leftOrRight === 'left') { |
| b69ab31 | | | 110 | position = {bottom: window.innerHeight / zoom - (state.y - yOffset), left: state.x + xOffset}; |
| b69ab31 | | | 111 | } else { |
| b69ab31 | | | 112 | position = { |
| b69ab31 | | | 113 | bottom: window.innerHeight / zoom - (state.y - yOffset), |
| b69ab31 | | | 114 | right: window.innerWidth / zoom - (state.x - xOffset), |
| b69ab31 | | | 115 | }; |
| b69ab31 | | | 116 | } |
| b69ab31 | | | 117 | } |
| b69ab31 | | | 118 | position.maxHeight = |
| b69ab31 | | | 119 | window.innerHeight / zoom - |
| b69ab31 | | | 120 | ((position.top as number | null) ?? 0) - |
| b69ab31 | | | 121 | ((position.bottom as number | null) ?? 0); |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | return ( |
| b69ab31 | | | 124 | <div |
| b69ab31 | | | 125 | ref={ref} |
| b69ab31 | | | 126 | className={'context-menu-container'} |
| b69ab31 | | | 127 | data-testid="context-menu-container" |
| b69ab31 | | | 128 | style={position}> |
| b69ab31 | | | 129 | {topOrBottom === 'top' ? ( |
| b69ab31 | | | 130 | <div |
| b69ab31 | | | 131 | className={`context-menu-arrow context-menu-arrow-top context-menu-arrow-${leftOrRight}`} |
| b69ab31 | | | 132 | /> |
| b69ab31 | | | 133 | ) : null} |
| b69ab31 | | | 134 | <ContextMenuList |
| b69ab31 | | | 135 | items={state.items} |
| b69ab31 | | | 136 | clickItem={item => { |
| b69ab31 | | | 137 | if (item.type != null) { |
| b69ab31 | | | 138 | return; |
| b69ab31 | | | 139 | } |
| b69ab31 | | | 140 | // don't allow double-clicking to run the action twice |
| b69ab31 | | | 141 | if (state != null) { |
| b69ab31 | | | 142 | item.onClick?.(); |
| b69ab31 | | | 143 | setState(null); |
| b69ab31 | | | 144 | } |
| b69ab31 | | | 145 | }} |
| b69ab31 | | | 146 | /> |
| b69ab31 | | | 147 | |
| b69ab31 | | | 148 | {topOrBottom === 'bottom' ? ( |
| b69ab31 | | | 149 | <div |
| b69ab31 | | | 150 | className={`context-menu-arrow context-menu-arrow-bottom context-menu-arrow-${leftOrRight}`} |
| b69ab31 | | | 151 | /> |
| b69ab31 | | | 152 | ) : null} |
| b69ab31 | | | 153 | </div> |
| b69ab31 | | | 154 | ); |
| b69ab31 | | | 155 | } |
| b69ab31 | | | 156 | |
| b69ab31 | | | 157 | function ContextMenuList({ |
| b69ab31 | | | 158 | items, |
| b69ab31 | | | 159 | clickItem, |
| b69ab31 | | | 160 | }: { |
| b69ab31 | | | 161 | items: Array<ContextMenuItem>; |
| b69ab31 | | | 162 | clickItem: (item: ContextMenuItem) => void; |
| b69ab31 | | | 163 | }) { |
| b69ab31 | | | 164 | // Each ContextMenuList renders one additional layer of submenu |
| b69ab31 | | | 165 | const [submenuNavigation, setSubmenuNavigation] = useState< |
| b69ab31 | | | 166 | {x: number; y: number; children: Array<ContextMenuItem>} | undefined |
| b69ab31 | | | 167 | >(undefined); |
| b69ab31 | | | 168 | const [tooltip, setTooltip] = useState<{x: number; y: number; tooltip: string} | undefined>( |
| b69ab31 | | | 169 | undefined, |
| b69ab31 | | | 170 | ); |
| b69ab31 | | | 171 | const ref = useRef<HTMLDivElement | null>(null); |
| b69ab31 | | | 172 | |
| b69ab31 | | | 173 | function getCoordinatesForSubElement(e: React.PointerEvent) { |
| b69ab31 | | | 174 | const target = e.currentTarget as HTMLElement; |
| b69ab31 | | | 175 | const parent = ref.current; |
| b69ab31 | | | 176 | if (!parent) { |
| b69ab31 | | | 177 | return; |
| b69ab31 | | | 178 | } |
| b69ab31 | | | 179 | const parentRect = parent?.getBoundingClientRect(); |
| b69ab31 | | | 180 | const rect = target.getBoundingClientRect(); |
| b69ab31 | | | 181 | // attach to top right corner |
| b69ab31 | | | 182 | const x = -1 * parentRect.left + rect.right; |
| b69ab31 | | | 183 | const y = -1 * parentRect.top + rect.top; |
| b69ab31 | | | 184 | return {x, y}; |
| b69ab31 | | | 185 | } |
| b69ab31 | | | 186 | |
| b69ab31 | | | 187 | return ( |
| b69ab31 | | | 188 | <> |
| b69ab31 | | | 189 | <div className="context-menu" ref={ref}> |
| b69ab31 | | | 190 | {items.map((item, i) => |
| b69ab31 | | | 191 | item.type === 'divider' ? ( |
| b69ab31 | | | 192 | <div className="context-menu-divider" key={i} /> |
| b69ab31 | | | 193 | ) : item.type === 'submenu' ? ( |
| b69ab31 | | | 194 | <div |
| b69ab31 | | | 195 | key={i} |
| b69ab31 | | | 196 | className={'context-menu-item context-menu-submenu'} |
| b69ab31 | | | 197 | onPointerEnter={e => { |
| b69ab31 | | | 198 | const coordinates = getCoordinatesForSubElement(e); |
| b69ab31 | | | 199 | if (!coordinates) { |
| b69ab31 | | | 200 | return; |
| b69ab31 | | | 201 | } |
| b69ab31 | | | 202 | const {x, y} = coordinates; |
| b69ab31 | | | 203 | setSubmenuNavigation({ |
| b69ab31 | | | 204 | x, |
| b69ab31 | | | 205 | y, |
| b69ab31 | | | 206 | children: item.children, |
| b69ab31 | | | 207 | }); |
| b69ab31 | | | 208 | setTooltip(undefined); |
| b69ab31 | | | 209 | }}> |
| b69ab31 | | | 210 | <span>{item.label}</span> |
| b69ab31 | | | 211 | <Icon icon="chevron-right" /> |
| b69ab31 | | | 212 | </div> |
| b69ab31 | | | 213 | ) : ( |
| b69ab31 | | | 214 | <div |
| b69ab31 | | | 215 | key={i} |
| b69ab31 | | | 216 | onPointerEnter={e => { |
| b69ab31 | | | 217 | if (item.tooltip) { |
| b69ab31 | | | 218 | const coordinates = getCoordinatesForSubElement(e); |
| b69ab31 | | | 219 | if (!coordinates) { |
| b69ab31 | | | 220 | return; |
| b69ab31 | | | 221 | } |
| b69ab31 | | | 222 | const {x, y} = coordinates; |
| b69ab31 | | | 223 | setTooltip({x, y, tooltip: item.tooltip}); |
| b69ab31 | | | 224 | } else { |
| b69ab31 | | | 225 | setTooltip(undefined); |
| b69ab31 | | | 226 | } |
| b69ab31 | | | 227 | setSubmenuNavigation(undefined); |
| b69ab31 | | | 228 | }} |
| b69ab31 | | | 229 | onClick={() => { |
| b69ab31 | | | 230 | clickItem(item); |
| b69ab31 | | | 231 | }} |
| b69ab31 | | | 232 | className={'context-menu-item'}> |
| b69ab31 | | | 233 | {item.label} |
| b69ab31 | | | 234 | </div> |
| b69ab31 | | | 235 | ), |
| b69ab31 | | | 236 | )} |
| b69ab31 | | | 237 | </div> |
| b69ab31 | | | 238 | {submenuNavigation != null && ( |
| b69ab31 | | | 239 | <div |
| b69ab31 | | | 240 | className="context-menu-submenu-navigation" |
| b69ab31 | | | 241 | style={{position: 'absolute', top: submenuNavigation.y, left: submenuNavigation.x}}> |
| b69ab31 | | | 242 | <ContextMenuList items={submenuNavigation.children} clickItem={clickItem} /> |
| b69ab31 | | | 243 | </div> |
| b69ab31 | | | 244 | )} |
| b69ab31 | | | 245 | {tooltip != null && ( |
| b69ab31 | | | 246 | <div |
| b69ab31 | | | 247 | className="context-menu-tooltip" |
| b69ab31 | | | 248 | style={{position: 'absolute', top: tooltip.y, left: tooltip.x}}> |
| b69ab31 | | | 249 | {tooltip.tooltip} |
| b69ab31 | | | 250 | </div> |
| b69ab31 | | | 251 | )} |
| b69ab31 | | | 252 | </> |
| b69ab31 | | | 253 | ); |
| b69ab31 | | | 254 | } |