2020-07-15 04:22:19 +01:00
|
|
|
/*
|
2020-07-15 04:26:10 +01:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2020-07-15 04:22:19 +01:00
|
|
|
|
2023-12-20 14:42:31 +00:00
|
|
|
import React, { ComponentProps } from "react";
|
2020-07-15 04:22:19 +01:00
|
|
|
|
|
|
|
|
import AccessibleButton from "../../components/views/elements/AccessibleButton";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { useRovingTabIndex } from "../RovingTabIndex";
|
|
|
|
|
import { Ref } from "./types";
|
2020-07-15 04:22:19 +01:00
|
|
|
|
2023-12-20 14:42:31 +00:00
|
|
|
type Props<T extends keyof JSX.IntrinsicElements> = Omit<
|
|
|
|
|
ComponentProps<typeof AccessibleButton<T>>,
|
|
|
|
|
"inputRef" | "tabIndex"
|
|
|
|
|
> & {
|
2020-07-15 04:22:19 +01:00
|
|
|
inputRef?: Ref;
|
2023-04-20 15:56:21 +01:00
|
|
|
focusOnMouseOver?: boolean;
|
2023-12-20 14:42:31 +00:00
|
|
|
};
|
2020-07-15 04:22:19 +01:00
|
|
|
|
|
|
|
|
// Wrapper to allow use of useRovingTabIndex for simple AccessibleButtons outside of React Functional Components.
|
2023-12-20 14:42:31 +00:00
|
|
|
export const RovingAccessibleButton = <T extends keyof JSX.IntrinsicElements>({
|
2023-04-20 15:56:21 +01:00
|
|
|
inputRef,
|
|
|
|
|
onFocus,
|
|
|
|
|
onMouseOver,
|
|
|
|
|
focusOnMouseOver,
|
|
|
|
|
...props
|
2023-12-20 14:42:31 +00:00
|
|
|
}: Props<T>): JSX.Element => {
|
2022-04-22 17:09:44 +02:00
|
|
|
const [onFocusInternal, isActive, ref] = useRovingTabIndex(inputRef);
|
|
|
|
|
return (
|
|
|
|
|
<AccessibleButton
|
|
|
|
|
{...props}
|
2023-02-13 11:39:16 +00:00
|
|
|
onFocus={(event: React.FocusEvent) => {
|
2022-04-22 17:09:44 +02:00
|
|
|
onFocusInternal();
|
|
|
|
|
onFocus?.(event);
|
|
|
|
|
}}
|
2023-04-20 15:56:21 +01:00
|
|
|
onMouseOver={(event: React.MouseEvent) => {
|
|
|
|
|
if (focusOnMouseOver) onFocusInternal();
|
|
|
|
|
onMouseOver?.(event);
|
|
|
|
|
}}
|
2022-04-22 17:09:44 +02:00
|
|
|
inputRef={ref}
|
|
|
|
|
tabIndex={isActive ? 0 : -1}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2020-07-15 04:22:19 +01:00
|
|
|
};
|