cynx-ui 1.2.5 → 1.2.6
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.
- package/components/Toggle.jsx +29 -31
- package/package.json +1 -1
package/components/Toggle.jsx
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
.tgl-sm .tgl-tr{width:28px;height:16px}
|
|
7
|
-
.tgl-sm .tgl-th{width:12px;height:12px;left:2px}
|
|
8
|
-
.tgl-sm.on .tgl-th{left:14px}
|
|
9
|
-
.tgl-md .tgl-tr{width:34px;height:18px}
|
|
10
|
-
.tgl-md .tgl-th{width:14px;height:14px;left:2px}
|
|
11
|
-
.tgl-md.on .tgl-th{left:18px}
|
|
12
|
-
.tgl-lg .tgl-tr{width:44px;height:24px}
|
|
13
|
-
.tgl-lg .tgl-th{width:18px;height:18px;left:3px}
|
|
14
|
-
.tgl-lg.on .tgl-th{left:23px}
|
|
15
|
-
.tgl:disabled{opacity:.4;cursor:not-allowed;pointer-events:none}
|
|
16
|
-
`;
|
|
1
|
+
const SIZES = {
|
|
2
|
+
sm: { w: 28, h: 16, r: 8, dot: 12, off: 2, on: 14 },
|
|
3
|
+
md: { w: 34, h: 18, r: 9, dot: 14, off: 2, on: 18 },
|
|
4
|
+
lg: { w: 44, h: 24, r: 12, dot: 18, off: 3, on: 23 },
|
|
5
|
+
};
|
|
17
6
|
|
|
18
7
|
const COLORS = {
|
|
19
8
|
primary: 'var(--accent)',
|
|
@@ -22,8 +11,6 @@ const COLORS = {
|
|
|
22
11
|
danger: 'var(--danger)',
|
|
23
12
|
};
|
|
24
13
|
|
|
25
|
-
let styleInjected = false;
|
|
26
|
-
|
|
27
14
|
export default function Toggle({
|
|
28
15
|
checked = false,
|
|
29
16
|
onChange,
|
|
@@ -36,22 +23,33 @@ export default function Toggle({
|
|
|
36
23
|
className = '',
|
|
37
24
|
style = {},
|
|
38
25
|
}) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
tag.textContent = STYLES;
|
|
42
|
-
document.head.appendChild(tag);
|
|
43
|
-
styleInjected = true;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const sizeClass = ` tgl-${size}`;
|
|
47
|
-
const classes = `tgl${sizeClass}${checked ? ' on' : ''}${className ? ' ' + className : ''}`;
|
|
48
|
-
const trackColor = checked ? (COLORS[color] || COLORS.primary) : 'var(--bd2,#d7d8e0)';
|
|
26
|
+
const s = SIZES[size] || SIZES.md;
|
|
27
|
+
const trackBg = checked ? (COLORS[color] || COLORS.primary) : 'var(--bd2,#d7d8e0)';
|
|
49
28
|
|
|
50
29
|
return (
|
|
51
|
-
<label
|
|
30
|
+
<label
|
|
31
|
+
className={className}
|
|
32
|
+
style={{ display:'inline-flex', alignItems:'center', gap:8, cursor: disabled ? 'not-allowed' : 'pointer', userSelect:'none', opacity: disabled ? .4 : 1, ...style }}
|
|
33
|
+
onClick={onClick}
|
|
34
|
+
>
|
|
52
35
|
<input type="checkbox" checked={checked} disabled={disabled}
|
|
53
|
-
onChange={e => onChange?.(e.target.checked)}
|
|
54
|
-
|
|
36
|
+
onChange={e => onChange?.(e.target.checked)}
|
|
37
|
+
style={{ position:'absolute', opacity:0, width:0, height:0 }} />
|
|
38
|
+
<span style={{
|
|
39
|
+
position:'relative', width: s.w, height: s.h, borderRadius: s.r,
|
|
40
|
+
background: trackBg, flexShrink:0, transition:'background .2s ease',
|
|
41
|
+
boxShadow:'0 1px 3px rgba(20,22,41,.1)',
|
|
42
|
+
}}>
|
|
43
|
+
<span style={{
|
|
44
|
+
position:'absolute', top:'50%',
|
|
45
|
+
transform:'translateY(-50%)',
|
|
46
|
+
left: checked ? s.on : s.off,
|
|
47
|
+
width: s.dot, height: s.dot,
|
|
48
|
+
background:'#fff', borderRadius:'50%',
|
|
49
|
+
boxShadow:'0 1px 3px rgba(0,0,0,.25)',
|
|
50
|
+
transition:'left .25s cubic-bezier(.34,1.56,.64,1)',
|
|
51
|
+
}} />
|
|
52
|
+
</span>
|
|
55
53
|
{label && <span style={labelStyle}>{label}</span>}
|
|
56
54
|
</label>
|
|
57
55
|
);
|