-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
portal-app.jsx
159 lines (138 loc) · 3.81 KB
/
portal-app.jsx
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
// @flow
import React, { Component, type Node } from 'react';
import ReactDOM from 'react-dom';
import styled from '@emotion/styled';
import { colors } from '@atlaskit/theme';
import { DragDropContext, Droppable, Draggable } from '../../../src';
import reorder from '../reorder';
import { grid } from '../constants';
import type { Quote } from '../types';
import type {
DropResult,
DroppableProvided,
DraggableProvided,
DraggableStateSnapshot,
} from '../../../src';
type ItemProps = {|
provided: DraggableProvided,
snapshot: DraggableStateSnapshot,
quote: Quote,
|};
const portal: HTMLElement = document.createElement('div');
portal.classList.add('my-super-cool-portal');
if (!document.body) {
throw new Error('body not ready for portal creation!');
}
document.body.appendChild(portal);
const SimpleQuote = styled.div`
padding: ${grid}px;
margin-bottom: ${grid}px;
background-color: ${colors.B50};
border: 1px solid ${colors.B200};
/* used for positioning the after content */
position: relative;
/* stylelint-disable comment-empty-line-before */
/* add little portal indicator when in a portal */
${(props) =>
props.inPortal
? `
::after {
position: absolute;
background: lightgreen;
padding: ${grid}px;
bottom: 0;
right: 0;
content: "in portal";
}
`
: ''}/* stylelint-enable */;
`;
class PortalAwareItem extends Component<ItemProps> {
render() {
const provided: DraggableProvided = this.props.provided;
const snapshot: DraggableStateSnapshot = this.props.snapshot;
const quote: Quote = this.props.quote;
const usePortal: boolean = snapshot.isDragging;
const child: Node = (
<SimpleQuote
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
inPortal={usePortal}
>
{quote.content} <small>- {quote.author.name}</small>
</SimpleQuote>
);
if (!usePortal) {
return child;
}
// if dragging - put the item in a portal
return ReactDOM.createPortal(child, portal);
}
}
type AppProps = {|
initial: Quote[],
|};
type AppState = {|
quotes: Quote[],
|};
const Container = styled.div`
margin: 0 auto;
width: 300px;
`;
export default class PortalApp extends Component<AppProps, AppState> {
state: AppState = {
quotes: this.props.initial,
};
onDragEnd = (result: DropResult) => {
// dropped outside the list
if (
!result.destination ||
result.destination.index === result.source.index
) {
return;
}
// no movement
if (result.destination.index === result.source.index) {
return;
}
const quotes = reorder(
this.state.quotes,
result.source.index,
result.destination.index,
);
this.setState({
quotes,
});
};
render() {
return (
<DragDropContext onDragEnd={this.onDragEnd}>
<Droppable droppableId="droppable">
{(droppableProvided: DroppableProvided) => (
<Container
ref={droppableProvided.innerRef}
{...droppableProvided.droppableProps}
>
{this.state.quotes.map((quote: Quote, index: number) => (
<Draggable draggableId={quote.id} index={index} key={quote.id}>
{(
draggableProvided: DraggableProvided,
draggableSnapshot: DraggableStateSnapshot,
) => (
<PortalAwareItem
quote={quote}
provided={draggableProvided}
snapshot={draggableSnapshot}
/>
)}
</Draggable>
))}
{droppableProvided.placeholder}
</Container>
)}
</Droppable>
</DragDropContext>
);
}
}