-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDialog.tsx
More file actions
170 lines (153 loc) · 4.53 KB
/
Dialog.tsx
File metadata and controls
170 lines (153 loc) · 4.53 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as RadixDialog from '@radix-ui/react-dialog';
import { keyframes, styled } from 'styled-components';
import { Button, ButtonProps } from '@/components/Button';
import { Icon } from '@/components/Icon';
import { Spacer } from '@/components/Spacer';
import { CrossButton } from '@/components/CrossButton';
import { DialogContentProps } from './Dialog.types';
export const Dialog = ({ children, ...props }: RadixDialog.DialogProps) => {
return <RadixDialog.Root {...props}>{children}</RadixDialog.Root>;
};
// Dialog Trigger
const Trigger = styled(RadixDialog.Trigger)`
width: fit-content;
background: transparent;
border: none;
cursor: pointer;
`;
const DialogTrigger = ({
children,
asChild,
...props
}: RadixDialog.DialogTriggerProps) => {
if (asChild) {
// Pass all props to RadixDialog.Trigger, no styled wrapper
return (
<RadixDialog.Trigger
asChild
{...props}
>
{children}
</RadixDialog.Trigger>
);
}
// Use styled Trigger if not asChild
return <Trigger {...props}>{children}</Trigger>;
};
DialogTrigger.displayName = 'DialogTrigger';
Dialog.Trigger = DialogTrigger;
const DialogClose = ({ label = 'Close', type = 'secondary', ...props }: ButtonProps) => (
<RadixDialog.Close asChild>
<Button
type={type}
label={label}
{...props}
/>
</RadixDialog.Close>
);
DialogClose.displayName = 'DialogClose';
Dialog.Close = DialogClose;
// Dialog Content
const overlayShow = keyframes({
'0%': { opacity: 0 },
'100%': { opacity: 1 },
});
const contentShow = keyframes({
'0%': { opacity: 0, transform: 'translate(-50%, -48%) scale(.96)' },
'100%': { opacity: 1, transform: 'translate(-50%, -50%) scale(1)' },
});
const DialogOverlay = styled(RadixDialog.Overlay)`
background-color: ${({ theme }) => theme.click.dialog.color.opaqueBackground.default};
position: fixed;
inset: 0;
animation: ${overlayShow} 150ms cubic-bezier(0.16, 1, 0.3, 1);
`;
const ContentArea = styled(RadixDialog.Content)<{ $reducePadding?: boolean }>`
background: ${({ theme }) => theme.click.dialog.color.background.default};
border-radius: ${({ theme }) => theme.click.dialog.radii.all};
padding-block: ${({ theme, $reducePadding = false }) =>
$reducePadding ? theme.sizes[4] : theme.click.dialog.space.y};
padding-inline: ${({ theme, $reducePadding = false }) =>
$reducePadding ? theme.sizes[4] : theme.click.dialog.space.x};
box-shadow: ${({ theme }) => theme.click.dialog.shadow.default};
border: 1px solid ${({ theme }) => theme.click.global.color.stroke.default};
width: 75%;
max-width: 670px;
position: fixed;
top: 50%;
left: 50%;
max-height: 75%;
overflow: auto;
transform: translate(-50%, -50%);
animation: ${contentShow} 150ms cubic-bezier(0.16, 1, 0.3, 1);
outline: none;
@media (max-width: ${({ theme }) => theme.breakpoint.sizes.sm}) {
max-height: 100%;
border-radius: 0;
width: 100%;
}
`;
const TitleArea = styled.div<{ $onlyClose?: boolean }>`
display: flex;
justify-content: ${({ $onlyClose }) => ($onlyClose ? 'flex-end' : 'space-between')};
align-items: center;
min-height: ${({ theme }) => theme.sizes[9]}; // 32px
`;
const Title = styled.h2`
font: ${({ theme }) => theme.click.dialog.typography.title.default};
padding: 0;
margin: 0;
`;
const CloseButton = ({ onClose }: { onClose?: () => void }) => (
<RadixDialog.Close asChild>
<CrossButton onClick={onClose}>
<Icon
name="cross"
size="lg"
/>
</CrossButton>
</RadixDialog.Close>
);
const DialogContent = ({
title,
children,
showClose,
onClose,
forceMount,
container,
showOverlay = true,
reducePadding = false,
...props
}: DialogContentProps) => {
return (
<RadixDialog.Portal
forceMount={forceMount}
container={container}
>
{showOverlay && <DialogOverlay />}
<ContentArea
data-testid="click-dialog-contentarea"
$reducePadding={reducePadding}
{...props}
>
{(title || showClose) && (
<>
<TitleArea $onlyClose={!!showClose && !title}>
{title && <Title data-testid="click-dialog-title">{title}</Title>}
{showClose && (
<CloseButton
data-testid="click-dialog-close"
onClose={onClose}
/>
)}
</TitleArea>
<Spacer size="sm" />
</>
)}
{children}
</ContentArea>
</RadixDialog.Portal>
);
};
DialogContent.displayName = 'DialogContent';
Dialog.Content = DialogContent;