-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCheckboxInput.tsx
More file actions
81 lines (68 loc) · 2.15 KB
/
CheckboxInput.tsx
File metadata and controls
81 lines (68 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use client"
import styled from "styled-components"
import { forwardRef } from "react"
// Types //
type BaseCheckboxProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "size" | "type">
interface CheckboxInputProps extends BaseCheckboxProps {
variant?: "primary" | "secondary"
size?: "small" | "default"
}
// Components //
export const CheckboxInput = forwardRef<HTMLInputElement, CheckboxInputProps>(
({ variant = "secondary", size = "default", ...props }, ref) => {
return <StyledCheckbox ref={ref} $variant={variant} $size={size} {...props} />
}
)
CheckboxInput.displayName = "CheckboxInput"
// Styled Components //
const StyledCheckbox = styled.input.attrs({ type: "checkbox" })<{
$variant: "primary" | "secondary"
$size: "small" | "default"
}>`
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
width: ${(props) => (props.$size === "small" ? "1rem" : "1.25rem")};
height: ${(props) => (props.$size === "small" ? "1rem" : "1.25rem")};
border: 2px solid
${(props) =>
props.$variant === "secondary" ? "rgba(255, 255, 255, 0.3)" : "rgba(0, 0, 0, 0.3)"};
border-radius: 0.25rem;
background-color: transparent;
cursor: pointer;
position: relative;
transition: all 0.2s ease;
margin: 0;
flex-shrink: 0;
&:hover {
border-color: ${(props) =>
props.$variant === "secondary" ? "rgba(255, 255, 255, 0.5)" : "rgba(0, 0, 0, 0.5)"};
}
&:checked {
border-color: ${(props) =>
props.$variant === "secondary" ? "rgba(156, 163, 255, 0.9)" : "rgba(0, 0, 0, 0.7)"};
background-color: ${(props) =>
props.$variant === "secondary" ? "rgba(156, 163, 255, 0.9)" : "rgba(0, 0, 0, 0.7)"};
}
&:checked::after {
content: "";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(45deg);
width: ${(props) => (props.$size === "small" ? "0.25rem" : "0.375rem")};
height: ${(props) => (props.$size === "small" ? "0.5rem" : "0.625rem")};
border: solid white;
border-width: 0 2px 2px 0;
}
&:focus {
outline: 2px solid
${(props) =>
props.$variant === "secondary" ? "rgba(156, 163, 255, 0.5)" : "rgba(0, 0, 0, 0.3)"};
outline-offset: 2px;
}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
`