* migrate the message context menu to IconizedContextMenu Signed-off-by: Michael Weimann <mail@michael-weimann.eu> * migrate the message context menu to IconizedContextMenu Signed-off-by: Michael Weimann <mail@michael-weimann.eu> * Added right-click menu Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * add message context menu group keys Signed-off-by: Michael Weimann <mail@michael-weimann.eu> * add message context menu icons Signed-off-by: Michael Weimann <mail@michael-weimann.eu> * add _MessageContextMenu.scss license header Signed-off-by: Michael Weimann <mail@michael-weimann.eu> * use null vars for context menu lists * Add allowOverridingNativeContextMenus() Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Use allowOverridingNativeContextMenus() Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix types Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix types Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Remove mistaken line Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix styling Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * InputHTMLAttributes -> AllHTMLAttributes Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Convert to TS Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add some types Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Make onClick optional Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add rightClick prop Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add copy button Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * What about upgrading deps after the eslint migration, Simon? Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add edit button Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * fix Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add reply button Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add react button Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Cleanup render() Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix comments Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add save button Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Don't show context menu if editing Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add special handling for click a timestamp Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix double empty line Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Don't show context menu for images Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Cleanup Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix order Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Keep action bar shown when right-clicking Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Highlight event tile when right-clicking Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Delint Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Pointless change so that I can re-run the CI Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Remove dowload button Because we don't use this menu when clicking on images Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Be more clear for non-bools Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Use triggerOnMouse down prop Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Remove a comment Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Remove unused var Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Remove unnecessary import Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add some missing types Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add missing type Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Remove unused import Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add a missing type Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix types Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix types/naming Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add missing current Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Remove unused var Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix editing and replying Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * i18n Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix import Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Support right-click context menu for threads Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Make button order match `MessageActionBar` Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix missing permalink button Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Remove useless part of if statement Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Some small refactoring for consistency Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Some more refactoring Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix `editEvent()` call Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Make editing polls work Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix collapse reply chain button Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix timelineRenderingType Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Fix reply button Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Hide right-click context menu behind a labs flag Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Add missing return type Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Make `contextMene` optional Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Move `renderContextMenu()` Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Simplify `renderContextMenu()` Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Improve `aboveLeftOf` typing Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Use `InputHTMLAttributes` Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Disable message right-click context menu in browser (for now) Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> * Give permalink button more props Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com> Co-authored-by: Michael Weimann <mail@michael-weimann.eu>
159 lines
5.1 KiB
TypeScript
159 lines
5.1 KiB
TypeScript
/*
|
|
Copyright 2016 Jani Mustonen
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import React, { ReactHTML } from 'react';
|
|
import classnames from 'classnames';
|
|
|
|
import { getKeyBindingsManager } from "../../../KeyBindingsManager";
|
|
import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
|
|
|
|
export type ButtonEvent = React.MouseEvent<Element> | React.KeyboardEvent<Element> | React.FormEvent<Element>;
|
|
|
|
type AccessibleButtonKind = | 'primary'
|
|
| 'primary_outline'
|
|
| 'primary_sm'
|
|
| 'secondary'
|
|
| 'danger'
|
|
| 'danger_outline'
|
|
| 'danger_sm'
|
|
| 'link'
|
|
| 'link_inline'
|
|
| 'link_sm'
|
|
| 'confirm_sm'
|
|
| 'cancel_sm';
|
|
|
|
/**
|
|
* children: React's magic prop. Represents all children given to the element.
|
|
* element: (optional) The base element type. "div" by default.
|
|
* onClick: (required) Event handler for button activation. Should be
|
|
* implemented exactly like a normal onClick handler.
|
|
*/
|
|
interface IProps extends React.InputHTMLAttributes<Element> {
|
|
inputRef?: React.Ref<Element>;
|
|
element?: keyof ReactHTML;
|
|
// The kind of button, similar to how Bootstrap works.
|
|
// See available classes for AccessibleButton for options.
|
|
kind?: AccessibleButtonKind | string;
|
|
// The ARIA role
|
|
role?: string;
|
|
// The tabIndex
|
|
tabIndex?: number;
|
|
disabled?: boolean;
|
|
className?: string;
|
|
triggerOnMouseDown?: boolean;
|
|
onClick(e?: ButtonEvent): void | Promise<void>;
|
|
}
|
|
|
|
interface IAccessibleButtonProps extends React.InputHTMLAttributes<Element> {
|
|
ref?: React.Ref<Element>;
|
|
}
|
|
|
|
/**
|
|
* AccessibleButton is a generic wrapper for any element that should be treated
|
|
* as a button. Identifies the element as a button, setting proper tab
|
|
* indexing and keyboard activation behavior.
|
|
*
|
|
* @param {Object} props react element properties
|
|
* @returns {Object} rendered react
|
|
*/
|
|
export default function AccessibleButton({
|
|
element,
|
|
onClick,
|
|
children,
|
|
kind,
|
|
disabled,
|
|
inputRef,
|
|
className,
|
|
onKeyDown,
|
|
onKeyUp,
|
|
triggerOnMouseDown,
|
|
...restProps
|
|
}: IProps) {
|
|
const newProps: IAccessibleButtonProps = restProps;
|
|
if (disabled) {
|
|
newProps["aria-disabled"] = true;
|
|
} else {
|
|
if (triggerOnMouseDown) {
|
|
newProps.onMouseDown = onClick;
|
|
} else {
|
|
newProps.onClick = onClick;
|
|
}
|
|
// We need to consume enter onKeyDown and space onKeyUp
|
|
// otherwise we are risking also activating other keyboard focusable elements
|
|
// that might receive focus as a result of the AccessibleButtonClick action
|
|
// It's because we are using html buttons at a few places e.g. inside dialogs
|
|
// And divs which we report as role button to assistive technologies.
|
|
// Browsers handle space and enter keypresses differently and we are only adjusting to the
|
|
// inconsistencies here
|
|
newProps.onKeyDown = (e) => {
|
|
const action = getKeyBindingsManager().getAccessibilityAction(e);
|
|
|
|
switch (action) {
|
|
case KeyBindingAction.Enter:
|
|
e.stopPropagation();
|
|
e.preventDefault();
|
|
return onClick(e);
|
|
case KeyBindingAction.Space:
|
|
e.stopPropagation();
|
|
e.preventDefault();
|
|
break;
|
|
default:
|
|
onKeyDown?.(e);
|
|
}
|
|
};
|
|
newProps.onKeyUp = (e) => {
|
|
const action = getKeyBindingsManager().getAccessibilityAction(e);
|
|
|
|
switch (action) {
|
|
case KeyBindingAction.Enter:
|
|
e.stopPropagation();
|
|
e.preventDefault();
|
|
break;
|
|
case KeyBindingAction.Space:
|
|
e.stopPropagation();
|
|
e.preventDefault();
|
|
return onClick(e);
|
|
default:
|
|
onKeyUp?.(e);
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
|
|
// Pass through the ref - used for keyboard shortcut access to some buttons
|
|
newProps.ref = inputRef;
|
|
|
|
newProps.className = classnames(
|
|
"mx_AccessibleButton",
|
|
className,
|
|
{
|
|
"mx_AccessibleButton_hasKind": kind,
|
|
[`mx_AccessibleButton_kind_${kind}`]: kind,
|
|
"mx_AccessibleButton_disabled": disabled,
|
|
},
|
|
);
|
|
|
|
// React.createElement expects InputHTMLAttributes
|
|
return React.createElement(element, newProps, children);
|
|
}
|
|
|
|
AccessibleButton.defaultProps = {
|
|
element: 'div' as keyof ReactHTML,
|
|
role: 'button',
|
|
tabIndex: 0,
|
|
};
|
|
|
|
AccessibleButton.displayName = "AccessibleButton";
|