@ludo.ninja/components 2.4.40 → 2.4.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
type Props = {
|
|
3
|
+
checked?: boolean;
|
|
4
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
5
|
+
defaultCheked?: boolean;
|
|
6
|
+
className?: string;
|
|
7
|
+
styles?: React.CSSProperties;
|
|
8
|
+
};
|
|
9
|
+
declare const Switch: React.FC<Props>;
|
|
10
|
+
export default Switch;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
|
+
const ScreenWidth_1 = require("../../styles/ScreenWidth");
|
|
8
|
+
const colors_1 = require("@ludo.ninja/ui/build/styles/colors");
|
|
9
|
+
const _4k_1 = require("@ludo.ninja/ui/build/utils/4k");
|
|
10
|
+
const react_1 = require("react");
|
|
11
|
+
const styled_components_1 = __importDefault(require("styled-components"));
|
|
12
|
+
const Wrapper = styled_components_1.default.label `
|
|
13
|
+
--switch-width: 32px;
|
|
14
|
+
--switch-height: 20px;
|
|
15
|
+
--switch-inner-size: 16px;
|
|
16
|
+
--switch-inner-padding: 2px;
|
|
17
|
+
|
|
18
|
+
${ScreenWidth_1.mediaQuery.minWidthFourK} {
|
|
19
|
+
--switch-width: ${(0, _4k_1.adaptiveValueCalc)(32)};
|
|
20
|
+
--switch-height: ${(0, _4k_1.adaptiveValueCalc)(20)};
|
|
21
|
+
--switch-inner-size: ${(0, _4k_1.adaptiveValueCalc)(16)};
|
|
22
|
+
--switch-inner-padding: ${(0, _4k_1.adaptiveValueCalc)(2)};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
position: relative;
|
|
26
|
+
display: inline-block;
|
|
27
|
+
width: var(--switch-width);
|
|
28
|
+
height: var(--switch-height);
|
|
29
|
+
|
|
30
|
+
input {
|
|
31
|
+
opacity: 0;
|
|
32
|
+
width: 0;
|
|
33
|
+
height: 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
span.slider {
|
|
37
|
+
position: absolute;
|
|
38
|
+
cursor: pointer;
|
|
39
|
+
top: 0;
|
|
40
|
+
left: 0;
|
|
41
|
+
right: 0;
|
|
42
|
+
bottom: 0;
|
|
43
|
+
background-color: #ccc;
|
|
44
|
+
-webkit-transition: 0.4s;
|
|
45
|
+
transition: 0.4s;
|
|
46
|
+
border-radius: var(--switch-height);
|
|
47
|
+
|
|
48
|
+
-webkit-tap-highlight-color: transparent;
|
|
49
|
+
-webkit-touch-callout: none;
|
|
50
|
+
-webkit-user-select: none;
|
|
51
|
+
-moz-user-select: none;
|
|
52
|
+
-ms-user-select: none;
|
|
53
|
+
user-select: none;
|
|
54
|
+
|
|
55
|
+
:focus {
|
|
56
|
+
outline: none !important;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
span.slider:before {
|
|
61
|
+
position: absolute;
|
|
62
|
+
content: "";
|
|
63
|
+
height: var(--switch-inner-size);
|
|
64
|
+
width: var(--switch-inner-size);
|
|
65
|
+
left: var(--switch-inner-padding);
|
|
66
|
+
bottom: var(--switch-inner-padding);
|
|
67
|
+
background-color: white;
|
|
68
|
+
-webkit-transition: 0.4s;
|
|
69
|
+
transition: 0.4s;
|
|
70
|
+
border-radius: 50%;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
input:checked + span.slider {
|
|
74
|
+
background-color: ${colors_1.ProgressColor};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
input:checked + span.slider:before {
|
|
78
|
+
transform: translateX(calc(var(--switch-width) - var(--switch-inner-size) - var(--switch-inner-padding) * 2));
|
|
79
|
+
}
|
|
80
|
+
`;
|
|
81
|
+
const Switch = ({ checked: checkedFromProps, onChange: onChangeFromProps, defaultCheked, className, styles, }) => {
|
|
82
|
+
const isControlled = typeof checkedFromProps != "undefined";
|
|
83
|
+
const hasDefaultValue = typeof defaultCheked != "undefined";
|
|
84
|
+
const [isChecked, setChecked] = (0, react_1.useState)(hasDefaultValue ? defaultCheked : false);
|
|
85
|
+
const checked = isControlled ? checkedFromProps : isChecked;
|
|
86
|
+
const handleToggle = (e) => {
|
|
87
|
+
if (onChangeFromProps) {
|
|
88
|
+
onChangeFromProps(e);
|
|
89
|
+
}
|
|
90
|
+
if (!isControlled) {
|
|
91
|
+
setChecked(!isChecked);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
return ((0, jsx_runtime_1.jsxs)(Wrapper, { className: className, style: styles, children: [(0, jsx_runtime_1.jsx)("input", { type: "checkbox", checked: checked, onChange: handleToggle }), (0, jsx_runtime_1.jsx)("span", { className: "slider" })] }));
|
|
95
|
+
};
|
|
96
|
+
exports.default = Switch;
|