Skip to content

Commit

Permalink
added temperature toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardomcv committed Jul 19, 2020
1 parent bd7f75c commit 16da41b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/components/TemperatureToggle/TemperatureToggle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.container {
display: flex;
align-items: center;
height: 3rem;
}

.container > span {
padding: 0 1rem;
color: white;
}

.switch {
position: relative; /* so that the slider can fill it */
height: 1rem;
width: 2rem;
margin: 0 0.5rem; /* horizontal margin to take into account the slider's circle */
}

/* hide the default input */
.switch > input {
display: none;
}

/* make the slider "fill" the parent */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #354552;
border-radius: 1rem;
transition: .2s;
}

/* the slider's circle */
.slider:before {
position: absolute;
content: "";
height: 1.5rem;
width: 1.5rem;
left: -0.5rem;
bottom: -0.25rem;
background-color: #d46226;
transition: .2s;
border-radius: 50%;
}

/* target the circle when the input is checked and translate it horizontally */
.switch > input:checked + .slider:before {
transform: translateX(1.5rem);
}
42 changes: 42 additions & 0 deletions src/components/TemperatureToggle/TemperatureToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { FunctionComponent, ChangeEventHandler } from 'react';
import { TemperatureUnit } from '../../sharedTypes';
import './TemperatureToggle.css';

interface TemperatureToggleProps {
value: TemperatureUnit;
onToggle: (unit: TemperatureUnit) => void;
}

const TemperatureToggle: FunctionComponent<TemperatureToggleProps> = ({
value,
onToggle,
}) => {
const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {
const { checked } = event.target;

const unit: TemperatureUnit = checked ? 'farenheit' : 'celsius';

onToggle(unit);
};

return (
<div className="container">
<span>°C</span>
<label
htmlFor="slider-checkbox"
className="switch"
>
<input
id="slider-checkbox"
type="checkbox"
checked={value === 'farenheit'}
onChange={handleChange}
/>
<span className="slider" />
</label>
<span>°F</span>
</div>
);
};

export default TemperatureToggle;
1 change: 1 addition & 0 deletions src/components/TemperatureToggle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './TemperatureToggle';

0 comments on commit 16da41b

Please sign in to comment.