Finish basic ptt implemenation
This commit is contained in:
36
src/input/Toggle.jsx
Normal file
36
src/input/Toggle.jsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import React, { useRef } from "react";
|
||||
import styles from "./Toggle.module.css";
|
||||
import { useToggleButton } from "@react-aria/button";
|
||||
import { useToggleState } from "@react-stately/toggle";
|
||||
import classNames from "classnames";
|
||||
import { Field } from "./Input";
|
||||
|
||||
export function Toggle({ id, label, className, ...rest }) {
|
||||
const buttonRef = useRef();
|
||||
const state = useToggleState(rest);
|
||||
const { buttonProps, isPressed } = useToggleButton(rest, state, buttonRef);
|
||||
|
||||
return (
|
||||
<Field
|
||||
className={classNames(
|
||||
styles.toggle,
|
||||
{ [styles.on]: isPressed },
|
||||
className
|
||||
)}
|
||||
>
|
||||
<button
|
||||
{...buttonProps}
|
||||
ref={buttonRef}
|
||||
id={id}
|
||||
className={classNames(styles.button, {
|
||||
[styles.isPressed]: isPressed,
|
||||
})}
|
||||
>
|
||||
<div className={styles.ball} />
|
||||
</button>
|
||||
<label className={styles.label} htmlFor={id}>
|
||||
{label}
|
||||
</label>
|
||||
</Field>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user