2020-11-19 14:34:49 +02:00
|
|
|
import { themeStyle } from '@joplin/lib/theme';
|
|
|
|
import * as React from 'react';
|
2024-08-02 15:49:15 +02:00
|
|
|
import { useMemo } from 'react';
|
2020-11-19 14:34:49 +02:00
|
|
|
const ReactToggleButton = require('react-toggle-button');
|
|
|
|
const Color = require('color');
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
value: boolean;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-19 14:34:49 +02:00
|
|
|
onToggle: Function;
|
|
|
|
themeId: number;
|
2024-08-02 15:49:15 +02:00
|
|
|
'aria-label': string;
|
2020-11-19 14:34:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function(props: Props) {
|
|
|
|
const theme = themeStyle(props.themeId);
|
|
|
|
|
2024-08-02 15:49:15 +02:00
|
|
|
const ariaLabel = props['aria-label'];
|
|
|
|
|
|
|
|
const passThroughInputProps = useMemo(() => {
|
|
|
|
return {
|
|
|
|
'aria-label': ariaLabel,
|
|
|
|
|
|
|
|
// Works around a bug in ReactToggleButton -- the hidden checkbox input associated
|
|
|
|
// with the toggle is always read as "unchecked" by screen readers.
|
|
|
|
checked: props.value,
|
|
|
|
// Silences a ReactJS warning: "You provided a `checked` prop to a form field without an `onChange` handler."
|
|
|
|
// Change events are handled by ReactToggleButton.
|
|
|
|
onChange: ()=>{},
|
|
|
|
};
|
|
|
|
}, [ariaLabel, props.value]);
|
|
|
|
|
2020-11-19 14:34:49 +02:00
|
|
|
return (
|
|
|
|
<ReactToggleButton
|
|
|
|
value={props.value}
|
|
|
|
onToggle={props.onToggle}
|
|
|
|
colors={{
|
|
|
|
activeThumb: {
|
|
|
|
base: Color(theme.color5).rgb().string(),
|
|
|
|
},
|
|
|
|
active: {
|
|
|
|
base: Color(theme.backgroundColor5).alpha(0.7).rgb().string(),
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
trackStyle={{
|
|
|
|
opacity: props.value ? 1 : 0.3,
|
|
|
|
}}
|
|
|
|
thumbStyle={{
|
|
|
|
opacity: props.value ? 1 : 0.5,
|
|
|
|
}}
|
|
|
|
inactiveLabel=""
|
|
|
|
activeLabel=""
|
2024-08-02 15:49:15 +02:00
|
|
|
passThroughInputProps={passThroughInputProps}
|
2020-11-19 14:34:49 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|