Files
ThreadNet-Web/src/components/views/elements/ToggleSwitch.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
2.0 KiB
TypeScript
Raw Normal View History

/*
Copyright 2019 New Vector Ltd
2019-10-08 12:10:37 +01:00
Copyright 2019 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-06-10 14:11:36 +01:00
import React from "react";
import classNames from "classnames";
2021-10-22 17:23:32 -05:00
2022-10-07 20:10:17 +02:00
import AccessibleTooltipButton from "./AccessibleTooltipButton";
2020-06-10 14:11:36 +01:00
interface IProps {
// Whether or not this toggle is in the 'on' position.
2020-06-10 15:57:28 +01:00
checked: boolean;
2020-06-10 14:11:36 +01:00
2022-10-07 20:10:17 +02:00
// Title to use
title?: string;
2020-06-10 14:11:36 +01:00
// Whether or not the user can interact with the switch
disabled?: boolean;
2020-06-10 14:11:36 +01:00
2022-10-07 20:10:17 +02:00
// Tooltip to show
tooltip?: string;
2020-06-10 14:11:36 +01:00
// Called when the checked state changes. First argument will be the new state.
2020-06-10 15:57:28 +01:00
onChange(checked: boolean): void;
// id to bind with other elements
id?: string;
2020-06-18 14:32:43 +01:00
}
2020-06-10 14:11:36 +01:00
// Controlled Toggle Switch element, written with Accessibility in mind
export default ({ checked, disabled = false, title, tooltip, onChange, ...props }: IProps): JSX.Element => {
const _onClick = (): void => {
2019-10-08 12:18:44 +01:00
if (disabled) return;
onChange(!checked);
};
2019-10-08 12:10:37 +01:00
const classes = classNames({
mx_ToggleSwitch: true,
mx_ToggleSwitch_on: checked,
mx_ToggleSwitch_enabled: !disabled,
});
2019-10-08 12:10:37 +01:00
return (
2022-10-07 20:10:17 +02:00
<AccessibleTooltipButton
{...props}
2019-10-08 12:10:37 +01:00
className={classes}
onClick={_onClick}
role="switch"
2019-10-08 12:10:37 +01:00
aria-checked={checked}
aria-disabled={disabled}
2022-10-07 20:10:17 +02:00
title={title}
tooltip={tooltip}
2019-10-08 12:10:37 +01:00
>
<div className="mx_ToggleSwitch_ball" />
2022-10-07 20:10:17 +02:00
</AccessibleTooltipButton>
2019-10-08 12:10:37 +01:00
);
};