Is there a way to insert text inside a Switch component in ReactJS? Specifically, I'm looking to add the text EN
and PT
within the Switch Component.
I opted not to use any libraries for this. Instead, I crafted the component solely using CSS to achieve the desired customization, as I found it easier to implement with CSS.
You can check out my project on codesandbox
https://i.sstatic.net/vp1Cb.png
import React from "react";
import "./switch.css";
const Switch = ({ isOn, handleToggle, onColor }) => {
return (
<>
<input
checked={isOn}
onChange={handleToggle}
className="react-switch-checkbox"
id={`react-switch-new`}
type="checkbox"
/>
<label
style={{ background: isOn && onColor }}
className="react-switch-label"
htmlFor={`react-switch-new`}
>
<span className={`react-switch-button`} />
</label>
</>
);
};
export default Switch;
.react-switch-checkbox {
height: 0;
width: 0;
visibility: hidden;
}
.react-switch-label {
display: flex;
align-items: center;
justify-content: space-between;
cursor: pointer;
width: 100px;
height: 50px;
background: #fff;
position: relative;
transition: background-color 0.2s;
}
.react-switch-label .react-switch-button {
content: "";
position: absolute;
top: 2px;
left: 2px;
width: 45px;
height: 45px;
transition: 0.2s;
background: #000;
box-shadow: 0 0 2px 0 rgba(10, 10, 10, 0.29);
}
.react-switch-checkbox:checked + .react-switch-label .react-switch-button {
left: calc(100% - 2px);
transform: translateX(-100%);
}
.react-switch-label:active .react-switch-button {
width: 60px;
}
Your assistance is highly appreciated!