2023-12-01 17:43:09 -05:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2023, 2024 New Vector Ltd.
|
2023-12-01 17:43:09 -05:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-12-01 17:43:09 -05:00
|
|
|
*/
|
|
|
|
|
|
2024-12-11 09:27:55 +00:00
|
|
|
import { type FC, useCallback } from "react";
|
2023-12-01 17:43:09 -05:00
|
|
|
import { Root, Track, Range, Thumb } from "@radix-ui/react-slider";
|
|
|
|
|
import classNames from "classnames";
|
2024-11-12 10:18:45 +00:00
|
|
|
import { Tooltip } from "@vector-im/compound-web";
|
2023-12-01 17:43:09 -05:00
|
|
|
|
|
|
|
|
import styles from "./Slider.module.css";
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
className?: string;
|
|
|
|
|
label: string;
|
|
|
|
|
value: number;
|
2024-12-09 11:39:16 +00:00
|
|
|
/**
|
|
|
|
|
* Event handler called when the value changes during an interaction.
|
|
|
|
|
*/
|
2023-12-01 17:43:09 -05:00
|
|
|
onValueChange: (value: number) => void;
|
2024-10-22 17:23:40 -04:00
|
|
|
/**
|
|
|
|
|
* Event handler called when the value changes at the end of an interaction.
|
|
|
|
|
* Useful when you only need to capture a final value to update a backend
|
|
|
|
|
* service, or when you want to remember the last value that the user
|
|
|
|
|
* "committed" to.
|
|
|
|
|
*/
|
2024-10-18 17:51:37 -04:00
|
|
|
onValueCommit?: (value: number) => void;
|
2023-12-01 17:43:09 -05:00
|
|
|
min: number;
|
|
|
|
|
max: number;
|
|
|
|
|
step: number;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A slider control allowing a value to be selected from a range.
|
|
|
|
|
*/
|
|
|
|
|
export const Slider: FC<Props> = ({
|
|
|
|
|
className,
|
|
|
|
|
label,
|
|
|
|
|
value,
|
|
|
|
|
onValueChange: onValueChangeProp,
|
2024-10-18 17:51:37 -04:00
|
|
|
onValueCommit: onValueCommitProp,
|
2023-12-01 17:43:09 -05:00
|
|
|
min,
|
|
|
|
|
max,
|
|
|
|
|
step,
|
|
|
|
|
disabled,
|
|
|
|
|
}) => {
|
|
|
|
|
const onValueChange = useCallback(
|
|
|
|
|
([v]: number[]) => onValueChangeProp(v),
|
|
|
|
|
[onValueChangeProp],
|
|
|
|
|
);
|
2024-10-18 17:51:37 -04:00
|
|
|
const onValueCommit = useCallback(
|
|
|
|
|
([v]: number[]) => onValueCommitProp?.(v),
|
|
|
|
|
[onValueCommitProp],
|
|
|
|
|
);
|
2023-12-01 17:43:09 -05:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Root
|
|
|
|
|
className={classNames(className, styles.slider)}
|
|
|
|
|
value={[value]}
|
|
|
|
|
onValueChange={onValueChange}
|
2024-10-18 17:51:37 -04:00
|
|
|
onValueCommit={onValueCommit}
|
2023-12-01 17:43:09 -05:00
|
|
|
min={min}
|
|
|
|
|
max={max}
|
|
|
|
|
step={step}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
>
|
|
|
|
|
<Track className={styles.track}>
|
|
|
|
|
<Range className={styles.highlight} />
|
|
|
|
|
</Track>
|
2024-11-12 10:18:45 +00:00
|
|
|
{/* Note: This is expected not to be visible on mobile.*/}
|
|
|
|
|
<Tooltip placement="top" label={Math.round(value * 100).toString() + "%"}>
|
|
|
|
|
<Thumb className={styles.handle} aria-label={label} />
|
|
|
|
|
</Tooltip>
|
2023-12-01 17:43:09 -05:00
|
|
|
</Root>
|
|
|
|
|
);
|
|
|
|
|
};
|