|
| 1 | +# react-toggle-slider |
| 2 | + |
| 3 | +A highly-customizable React toggle slider component. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +To add the component, simply import `ToggleSlider` and use it in your app. |
| 8 | + |
| 9 | +```tsx |
| 10 | +import ToggleSlider from "react-toggle-slider"; |
| 11 | + |
| 12 | +function App() { |
| 13 | + return ( |
| 14 | + <div> |
| 15 | + <ToggleSlider/> |
| 16 | + </div> |
| 17 | + ); |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +The slider works without any options, but it can be heavily customized. |
| 22 | + |
| 23 | +### Options |
| 24 | + |
| 25 | +| Option | Default | Description | |
| 26 | +| ------------- | ------------- | ------------- | |
| 27 | +| `active` | `false` | Initial state | |
| 28 | +| `onToggle` | `undefined` | Function to call when slider is toggled, passes active state as parameter | |
| 29 | +| `padding` | `5` | Padding between the handle and the sides of the bar | |
| 30 | +| `flip` | `false` | Handle starts of right instead of left | |
| 31 | +| `transitionDuration` | `100ms` | Transition duration of the slider | |
| 32 | +| `handleSize` | `18` | Diameter of the handle | |
| 33 | +| `handleBorderRadius` | `16` | Border radius of the handle | |
| 34 | +| `handleBackgroundColor` | `#ffffff` | Background color of the handle | |
| 35 | +| `handleBackgroundColorActive` | `undefined` | Background color of the handle while active | |
| 36 | +| `handleStyles` | `undefined` | Extra styles object to be applied to the handle | |
| 37 | +| `handleStylesActive` | `undefined` | Extra styles object to be applied to the handle while active | |
| 38 | +| `handleTransitionDuration` | `undefined` | Transition duration of the handle (overrides `transitionDuration`) | |
| 39 | +| `handleRenderer` | `undefined` | React node to completely replace the handle | |
| 40 | +| `handleRendererActive` | `undefined` | React node to completely replace the handle while active | |
| 41 | +| `barHeight` | `26` | Height of the bar | |
| 42 | +| `barWidth` | `48` | Width of the bar | |
| 43 | +| `barBorderRadius` | `16` | Border radius of the bar | |
| 44 | +| `barBackgroundColor` | `#dedede` | Background color of the bar | |
| 45 | +| `barBackgroundColorActive` | `#06b7e7` | Background color of the bar while active | |
| 46 | +| `barTransitionDuration` | `undefined` | Transition duration of the bar (overrides `transitionDuration`) | |
| 47 | +| `barStyles` | `undefined` | Extra styles object to be applied to the bar | |
| 48 | +| `barStylesActive` | `undefined` | Extra styles object to be applied to the bar while active | |
| 49 | +| `barRenderer` | `undefined` | React node to completely replace the bar | |
| 50 | +| `barRendererActive` | `undefined` | React node to completely replace the bar while active | |
| 51 | + |
| 52 | +If any options which refer to the active state of the slider are undefined, it will default to the |
| 53 | +inactive value. |
| 54 | + |
| 55 | +## Accessing the state of the slider |
| 56 | + |
| 57 | +### Callback |
| 58 | + |
| 59 | +The slider's state can be accessed using a callback. This can be used with `useState` or something |
| 60 | +similar to represent the status on the page. |
| 61 | + |
| 62 | +```tsx |
| 63 | + |
| 64 | +import ToggleSlider from "react-toggle-slider"; |
| 65 | + |
| 66 | +function App() { |
| 67 | + |
| 68 | + const [active, setActive] = useState(false); |
| 69 | + |
| 70 | + return ( |
| 71 | + <div> |
| 72 | + <ToggleSlider onToggle={state => setActive(state)}/> |
| 73 | + Slider is {active ? "active" : "inactive"} |
| 74 | + </div> |
| 75 | + ); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Hook |
| 80 | + |
| 81 | +The slider can also be used as a hook. This can be used to access the state of the slider without |
| 82 | +a callback. To do this, import the `useToggleSlider` function. |
| 83 | +Options can be passed into the function as an object. |
| 84 | + |
| 85 | +```tsx |
| 86 | + |
| 87 | +import { useToggleSlider } from "react-toggle-slider" |
| 88 | + |
| 89 | +function App() { |
| 90 | + |
| 91 | + const [toggleSlider, active] = useToggleSlider({barBackgroundColorActive: "red"}); |
| 92 | + return ( |
| 93 | + <div> |
| 94 | + { toggleSlider } |
| 95 | + Slider is {active ? "active" : "inactive"} |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +``` |
0 commit comments