This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
forked from Temzasse/react-modal-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
174 lines (150 loc) · 3.96 KB
/
index.tsx
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
171
172
173
174
import * as React from 'react';
import styled from 'styled-components';
import { FiEdit as MessageIcon, FiSearch as SearchIcon } from 'react-icons/fi';
import { useOverlayTriggerState } from 'react-stately';
import {
useOverlay,
useModal,
OverlayProvider,
FocusScope,
useButton,
useDialog,
} from 'react-aria';
import Sheet from '../../../src';
import NewMessageHeader from './NewMessageHeader';
import NewMessageContent from './NewMessageContent';
import { useMetaThemeColor } from 'components/hooks';
// A11y added with React Aria: https://react-spectrum.adobe.com/react-aria/useDialog.html
const SlackMessage = () => {
const sheetState = useOverlayTriggerState({});
const inputRef = React.useRef<HTMLInputElement>(null);
const openButtonRef = React.useRef<HTMLButtonElement>(null);
const openButton = useButton({ onPress: sheetState.open }, openButtonRef);
const focusInput = () => inputRef.current?.focus();
useMetaThemeColor({ when: sheetState.isOpen, from: '#111', to: '#000' });
useMetaThemeColor({ to: '#111' });
return (
<Wrapper>
<Topbar>
<Logo />
<WorkspaceTitle>A11y Workspace</WorkspaceTitle>
<SearchIcon size={20} />
</Topbar>
<Content>
<Fab {...openButton.buttonProps} ref={openButtonRef}>
<MessageIcon size={20} color="#fff" />
</Fab>
</Content>
<MessageSheet
isOpen={sheetState.isOpen}
onOpenEnd={focusInput}
onClose={sheetState.close}
rootId="root"
>
<OverlayProvider>
<FocusScope contain autoFocus={false} restoreFocus>
<MessageSheetComp sheetState={sheetState} inputRef={inputRef} />
</FocusScope>
</OverlayProvider>
</MessageSheet>
</Wrapper>
);
};
const MessageSheetComp = ({
sheetState,
inputRef,
}: {
sheetState: any;
inputRef: any;
}) => {
const ref = React.useRef<HTMLDivElement>(null);
const dialog = useDialog({}, ref);
const overlay = useOverlay({ onClose: sheetState.close, isOpen: true, isDismissable: true }, ref); // prettier-ignore
useModal();
// HACK: some props from React Aria need to be cast to `any`
// since they conflict with the Framer Motion props
return (
<>
<Sheet.Container
{...overlay.overlayProps}
{...(dialog.dialogProps as any)}
ref={ref}
>
<Sheet.Header>
<NewMessageHeader
sheetState={sheetState}
titleProps={dialog.titleProps}
/>
</Sheet.Header>
<Sheet.Content>
<NewMessageContent inputRef={inputRef} />
</Sheet.Content>
</Sheet.Container>
<Sheet.Backdrop />
</>
);
};
const Wrapper = styled.div`
display: flex;
flex-direction: column;
margin: 0 auto;
width: 100%;
flex: 1;
max-width: 680px;
`;
const Content = styled.div`
flex: 1;
overflow: hidden;
display: flex;
flex-direction: column;
padding-left: 16px;
padding-right: 16px;
padding-top: max(16px, env(safe-area-inset-top));
padding-bottom: 56px;
background-color: #333;
`;
const Topbar = styled.div`
width: 100%;
background-color: #111;
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 16px;
color: #fff;
`;
const Logo = styled.div`
width: 32px;
height: 32px;
border-radius: 12px;
background-color: slategray;
`;
const WorkspaceTitle = styled.h1`
font-weight: 700;
font-size: 16px;
`;
const Fab = styled.button`
position: fixed;
bottom: 16px;
right: 16px;
width: 48px;
height: 48px;
border-radius: 50%;
background-color: #111;
display: flex;
justify-content: center;
align-items: center;
`;
const MessageSheet = styled(Sheet)`
margin: 0 auto;
max-width: 680px;
.react-modal-sheet-container {
background-color: #222 !important;
}
.react-modal-sheet-backdrop {
background-color: rgba(0, 0, 0, 0.3) !important;
}
.react-modal-sheet-drag-indicator {
background-color: #666 !important;
}
`;
export default SlackMessage;