|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | + |
| 19 | +import { Icon as ContextMenuIcon } from '../../../../res/img/element-icons/context-menu.svg'; |
| 20 | +import { ChevronFace, ContextMenuButton, useContextMenu } from '../../structures/ContextMenu'; |
| 21 | +import AccessibleButton from '../elements/AccessibleButton'; |
| 22 | +import IconizedContextMenu, { IconizedContextMenuOptionList } from './IconizedContextMenu'; |
| 23 | + |
| 24 | +const contextMenuBelow = (elementRect: DOMRect) => { |
| 25 | + // align the context menu's icons with the icon which opened the context menu |
| 26 | + const left = elementRect.left + window.scrollX + elementRect.width; |
| 27 | + const top = elementRect.bottom + window.scrollY; |
| 28 | + const chevronFace = ChevronFace.None; |
| 29 | + return { left, top, chevronFace }; |
| 30 | +}; |
| 31 | + |
| 32 | +interface KebabContextMenuProps extends Partial<React.ComponentProps<typeof AccessibleButton>> { |
| 33 | + options: React.ReactNode[]; |
| 34 | + title: string; |
| 35 | +} |
| 36 | + |
| 37 | +export const KebabContextMenu: React.FC<KebabContextMenuProps> = ({ |
| 38 | + options, |
| 39 | + title, |
| 40 | + ...props |
| 41 | +}) => { |
| 42 | + const [menuDisplayed, button, openMenu, closeMenu] = useContextMenu(); |
| 43 | + |
| 44 | + return <> |
| 45 | + <ContextMenuButton |
| 46 | + {...props} |
| 47 | + onClick={openMenu} |
| 48 | + title={title} |
| 49 | + isExpanded={menuDisplayed} |
| 50 | + inputRef={button} |
| 51 | + > |
| 52 | + <ContextMenuIcon className='mx_KebabContextMenu_icon' /> |
| 53 | + </ContextMenuButton> |
| 54 | + { menuDisplayed && (<IconizedContextMenu |
| 55 | + onFinished={closeMenu} |
| 56 | + compact |
| 57 | + rightAligned |
| 58 | + closeOnInteraction |
| 59 | + {...contextMenuBelow(button.current.getBoundingClientRect())} |
| 60 | + > |
| 61 | + <IconizedContextMenuOptionList> |
| 62 | + { options } |
| 63 | + </IconizedContextMenuOptionList> |
| 64 | + </IconizedContextMenu>) } |
| 65 | + </>; |
| 66 | +}; |
0 commit comments