2017-01-13 18:26:59 +02:00
|
|
|
|
/*
|
2017-01-13 19:34:53 +02:00
|
|
|
|
Copyright 2016 Jani Mustonen
|
2017-01-13 18:26:59 +02:00
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2022-07-29 13:43:29 +02:00
|
|
|
|
import React, { HTMLAttributes, InputHTMLAttributes, ReactHTML, ReactNode } from "react";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
import classnames from "classnames";
|
2017-01-13 18:26:59 +02:00
|
|
|
|
|
2022-02-28 17:05:52 +01:00
|
|
|
|
import { getKeyBindingsManager } from "../../../KeyBindingsManager";
|
|
|
|
|
|
import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
|
2017-01-13 18:26:59 +02:00
|
|
|
|
|
2021-08-25 09:05:07 +01:00
|
|
|
|
export type ButtonEvent = React.MouseEvent<Element> | React.KeyboardEvent<Element> | React.FormEvent<Element>;
|
2020-06-10 22:05:29 +01:00
|
|
|
|
|
2022-01-07 10:40:53 +01:00
|
|
|
|
type AccessibleButtonKind =
|
|
|
|
|
|
| "primary"
|
|
|
|
|
|
| "primary_outline"
|
|
|
|
|
|
| "primary_sm"
|
|
|
|
|
|
| "secondary"
|
2022-11-21 08:47:09 +01:00
|
|
|
|
| "secondary_content"
|
2022-09-30 09:07:50 +02:00
|
|
|
|
| "content_inline"
|
2022-01-07 10:40:53 +01:00
|
|
|
|
| "danger"
|
|
|
|
|
|
| "danger_outline"
|
|
|
|
|
|
| "danger_sm"
|
2022-09-14 14:37:36 +02:00
|
|
|
|
| "danger_inline"
|
2022-01-07 10:40:53 +01:00
|
|
|
|
| "link"
|
|
|
|
|
|
| "link_inline"
|
|
|
|
|
|
| "link_sm"
|
|
|
|
|
|
| "confirm_sm"
|
2022-08-15 17:30:18 +02:00
|
|
|
|
| "cancel_sm"
|
|
|
|
|
|
| "icon";
|
2022-01-07 10:40:53 +01:00
|
|
|
|
|
2022-07-29 13:43:29 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* This type construct allows us to specifically pass those props down to the element we’re creating that the element
|
|
|
|
|
|
* actually supports.
|
|
|
|
|
|
*
|
|
|
|
|
|
* e.g., if element is set to "a", we’ll support href and target, if it’s set to "input", we support type.
|
|
|
|
|
|
*
|
|
|
|
|
|
* To remain compatible with existing code, we’ll continue to support InputHTMLAttributes<Element>
|
|
|
|
|
|
*/
|
|
|
|
|
|
type DynamicHtmlElementProps<T extends keyof JSX.IntrinsicElements> =
|
|
|
|
|
|
JSX.IntrinsicElements[T] extends HTMLAttributes<{}> ? DynamicElementProps<T> : DynamicElementProps<"div">;
|
|
|
|
|
|
type DynamicElementProps<T extends keyof JSX.IntrinsicElements> = Partial<
|
|
|
|
|
|
Omit<JSX.IntrinsicElements[T], "ref" | "onClick" | "onMouseDown" | "onKeyUp" | "onKeyDown">
|
|
|
|
|
|
> &
|
|
|
|
|
|
Omit<InputHTMLAttributes<Element>, "onClick">;
|
|
|
|
|
|
|
2017-01-13 19:34:53 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
*/
|
2022-07-29 13:43:29 +02:00
|
|
|
|
type IProps<T extends keyof JSX.IntrinsicElements> = DynamicHtmlElementProps<T> & {
|
2020-06-10 15:57:28 +01:00
|
|
|
|
inputRef?: React.Ref<Element>;
|
2022-07-29 13:43:29 +02:00
|
|
|
|
element?: T;
|
2022-11-07 13:45:34 +00:00
|
|
|
|
children?: ReactNode;
|
2019-01-21 17:27:43 -07:00
|
|
|
|
// The kind of button, similar to how Bootstrap works.
|
|
|
|
|
|
// See available classes for AccessibleButton for options.
|
2022-01-07 10:40:53 +01:00
|
|
|
|
kind?: AccessibleButtonKind | string;
|
2019-10-17 15:53:39 +01:00
|
|
|
|
// The ARIA role
|
2020-06-10 15:57:28 +01:00
|
|
|
|
role?: string;
|
2019-10-17 15:53:39 +01:00
|
|
|
|
// The tabIndex
|
2020-06-10 15:57:28 +01:00
|
|
|
|
tabIndex?: number;
|
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
|
className?: string;
|
2022-04-15 16:22:59 +02:00
|
|
|
|
triggerOnMouseDown?: boolean;
|
2022-09-30 15:26:08 -04:00
|
|
|
|
onClick: ((e: ButtonEvent) => void | Promise<void>) | null;
|
2022-07-29 13:43:29 +02:00
|
|
|
|
};
|
2017-01-13 18:26:59 +02:00
|
|
|
|
|
2022-10-26 11:04:16 +02:00
|
|
|
|
export interface IAccessibleButtonProps extends React.InputHTMLAttributes<Element> {
|
2020-06-10 15:57:28 +01:00
|
|
|
|
ref?: React.Ref<Element>;
|
2020-06-08 16:48:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-10 14:11:36 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* 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
|
|
|
|
|
|
*/
|
2022-07-29 13:43:29 +02:00
|
|
|
|
export default function AccessibleButton<T extends keyof JSX.IntrinsicElements>({
|
2020-06-10 14:11:36 +01:00
|
|
|
|
element,
|
|
|
|
|
|
onClick,
|
|
|
|
|
|
children,
|
|
|
|
|
|
kind,
|
|
|
|
|
|
disabled,
|
|
|
|
|
|
inputRef,
|
|
|
|
|
|
className,
|
2021-06-22 11:50:00 +01:00
|
|
|
|
onKeyDown,
|
|
|
|
|
|
onKeyUp,
|
2022-04-15 16:22:59 +02:00
|
|
|
|
triggerOnMouseDown,
|
2020-06-10 14:11:36 +01:00
|
|
|
|
...restProps
|
2023-01-12 13:25:14 +00:00
|
|
|
|
}: IProps<T>): JSX.Element {
|
2020-06-10 14:11:36 +01:00
|
|
|
|
const newProps: IAccessibleButtonProps = restProps;
|
2021-08-06 12:28:20 +01:00
|
|
|
|
if (disabled) {
|
|
|
|
|
|
newProps["aria-disabled"] = true;
|
2022-05-27 09:13:50 +02:00
|
|
|
|
newProps["disabled"] = true;
|
2021-08-06 12:28:20 +01:00
|
|
|
|
} else {
|
2022-04-15 16:22:59 +02:00
|
|
|
|
if (triggerOnMouseDown) {
|
2022-09-30 15:26:08 -04:00
|
|
|
|
newProps.onMouseDown = onClick ?? undefined;
|
2022-04-15 16:22:59 +02:00
|
|
|
|
} else {
|
2022-09-30 15:26:08 -04:00
|
|
|
|
newProps.onClick = onClick ?? undefined;
|
2022-04-15 16:22:59 +02:00
|
|
|
|
}
|
2020-06-10 14:11:36 +01:00
|
|
|
|
// 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.
|
2022-05-09 16:52:05 -06:00
|
|
|
|
// Browsers handle space and enter key presses differently and we are only adjusting to the
|
2020-06-10 14:11:36 +01:00
|
|
|
|
// inconsistencies here
|
|
|
|
|
|
newProps.onKeyDown = (e) => {
|
2022-02-28 17:05:52 +01:00
|
|
|
|
const action = getKeyBindingsManager().getAccessibilityAction(e);
|
|
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
|
case KeyBindingAction.Enter:
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
e.preventDefault();
|
2022-09-30 15:26:08 -04:00
|
|
|
|
return onClick?.(e);
|
2022-02-28 17:05:52 +01:00
|
|
|
|
case KeyBindingAction.Space:
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
onKeyDown?.(e);
|
2020-06-10 14:11:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
newProps.onKeyUp = (e) => {
|
2022-02-28 17:05:52 +01:00
|
|
|
|
const action = getKeyBindingsManager().getAccessibilityAction(e);
|
|
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
|
case KeyBindingAction.Enter:
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case KeyBindingAction.Space:
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
e.preventDefault();
|
2022-09-30 15:26:08 -04:00
|
|
|
|
return onClick?.(e);
|
2022-02-28 17:05:52 +01:00
|
|
|
|
default:
|
|
|
|
|
|
onKeyUp?.(e);
|
|
|
|
|
|
break;
|
2020-06-10 14:11:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Pass through the ref - used for keyboard shortcut access to some buttons
|
|
|
|
|
|
newProps.ref = inputRef;
|
|
|
|
|
|
|
2020-06-10 15:57:28 +01:00
|
|
|
|
newProps.className = classnames("mx_AccessibleButton", className, {
|
2020-06-10 16:48:34 +01:00
|
|
|
|
mx_AccessibleButton_hasKind: kind,
|
|
|
|
|
|
[`mx_AccessibleButton_kind_${kind}`]: kind,
|
|
|
|
|
|
mx_AccessibleButton_disabled: disabled,
|
2020-06-10 15:57:28 +01:00
|
|
|
|
});
|
2020-06-10 14:11:36 +01:00
|
|
|
|
|
|
|
|
|
|
// React.createElement expects InputHTMLAttributes
|
2021-08-06 12:28:20 +01:00
|
|
|
|
return React.createElement(element, newProps, children);
|
2020-06-10 14:11:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-13 18:26:59 +02:00
|
|
|
|
AccessibleButton.defaultProps = {
|
2021-07-08 14:47:36 +01:00
|
|
|
|
element: "div" as keyof ReactHTML,
|
2019-10-17 15:53:39 +01:00
|
|
|
|
role: "button",
|
2020-07-17 18:15:44 +01:00
|
|
|
|
tabIndex: 0,
|
2017-01-13 18:26:59 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
AccessibleButton.displayName = "AccessibleButton";
|