Skip to content

Commit

Permalink
refactor: Added toggle switch component
Browse files Browse the repository at this point in the history
  • Loading branch information
yaswanth-deriv committed Jan 26, 2024
1 parent 697d507 commit e0ca8c0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/components/ToggleSwitch/ToggleSwitch.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.deriv-toggle-switch {
position: relative;
display: inline-flex;
width: 4.4rem;
height: 2.4rem;

@include mobile {
width: 3.8rem;
}

& > input {
width: 0;
height: 0;
}

&__slider {
position: absolute;
inset: 0;
cursor: pointer;
background-color: var(--light-4-disabled-text, #d6d6d6);
transition: 0.4s;
border-radius: 1.6rem;

&:before {
position: absolute;
content: '';
height: 1.8rem;
width: 1.8rem;
left: 0.3rem;
bottom: 0.3rem;
background-color: var(--system-dark-1-prominent-text, #fff);
transition: 0.4s;
border-radius: 50%;

@include mobile {
left: 0.4rem;
}
}
}
}

.deriv-toggle-switch input:checked + .deriv-toggle-switch__slider {
background-color: var(--light-status-success, #4bb4b3);
}

.deriv-toggle-switch input:checked + .deriv-toggle-switch__slider:before {
-webkit-transform: translateX(2rem);
-ms-transform: translateX(2rem);
transform: translateX(2rem);

@include mobile {
-webkit-transform: translateX(1.2rem);
-ms-transform: translateX(1.2rem);
transform: translateX(1.2rem);
}
}
14 changes: 14 additions & 0 deletions lib/components/ToggleSwitch/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { ChangeEvent, forwardRef } from 'react';
import './ToggleSwitch.scss';

interface ToggleSwitchProps {
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
value: boolean;
}

export const ToggleSwitch = forwardRef<HTMLInputElement, ToggleSwitchProps>(({ onChange, value }, ref) => (
<label className='deriv-toggle-switch'>
<input checked={value} onChange={onChange} ref={ref} type='checkbox' />
<span className='deriv-toggle-switch__slider' />
</label>
));
1 change: 1 addition & 0 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export { Button } from "./components/Button";
export { Text } from "./components/Text";
export { Tab, Tabs } from "./components/Tabs";
export { Tooltip } from "./components/Tooltip";
export {ToggleSwitch} from "./components/ToggleSwitch"
export { Input } from "./components/Input";

0 comments on commit e0ca8c0

Please sign in to comment.