Tai*_*ara 2 html javascript css reactjs
如何在 ReactJS 的 Switch 组件中添加文本?我正在尝试在开关组件内添加文本EN。PT
我没有使用任何库,我只使用 css 构建了组件,因为我需要它进行特定的自定义,所以我发现使用 css 更容易。
我把我的项目放入codesandbox
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;Run Code Online (Sandbox Code Playgroud)
.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;
}Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!