Skip to content

Commit 6fae5b5

Browse files
author
Brandon Vigne
committed
Fix zIndex to allow Draggable being rendered on top of others
Mentionned in #90
1 parent 502f92d commit 6fae5b5

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

Draggable.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ export default function Draggable(props) {
8686
toValue: newOffset || originalOffset,
8787
useNativeDriver: false,
8888
}).start();
89-
9089
}, [pan]);
9190

9291
const onPanResponderRelease = React.useCallback(
@@ -164,13 +163,13 @@ export default function Draggable(props) {
164163
if (!shouldReverse) {
165164
curPan.addListener((c) => (offsetFromStart.current = c));
166165
} else {
167-
reversePosition();
166+
reversePosition();
168167
}
169168
return () => {
170169
curPan.removeAllListeners();
171170
};
172171
}, [shouldReverse]);
173-
172+
174173
const positionCss = React.useMemo(() => {
175174
const Window = Dimensions.get('window');
176175
return {
@@ -179,15 +178,15 @@ export default function Draggable(props) {
179178
left: 0,
180179
width: Window.width,
181180
height: Window.height,
181+
elevation: z,
182+
zIndex: z,
182183
};
183184
}, []);
184185

185186
const dragItemCss = React.useMemo(() => {
186187
const style = {
187188
top: y,
188189
left: x,
189-
elevation: z,
190-
zIndex: z,
191190
};
192191
if (renderColor) {
193192
style.backgroundColor = renderColor;

Draggable.tsx

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,49 @@ import {
1818
StyleProp,
1919
} from 'react-native';
2020
import PropTypes from 'prop-types';
21-
import { ViewStyle } from 'react-native/Libraries/StyleSheet/StyleSheet';
21+
import {ViewStyle} from 'react-native/Libraries/StyleSheet/StyleSheet';
2222

2323
function clamp(number: number, min: number, max: number) {
2424
return Math.max(min, Math.min(number, max));
2525
}
2626

2727
interface IProps {
28-
/**** props that should probably be removed in favor of "children" */
29-
renderText?: string;
30-
isCircle?: boolean;
31-
renderSize?: number;
32-
imageSource?: number;
33-
renderColor?: string;
34-
/**** */
35-
children?: React.ReactNode;
36-
shouldReverse?: boolean;
37-
disabled?: boolean;
38-
debug?: boolean;
39-
animatedViewProps?: object;
40-
touchableOpacityProps?: object;
41-
onDrag?: (e: GestureResponderEvent, gestureState: PanResponderGestureState) => void;
42-
onShortPressRelease?: (event: GestureResponderEvent) => void;
43-
onDragRelease?: (e: GestureResponderEvent, gestureState: PanResponderGestureState) => void;
44-
onLongPress?: (event: GestureResponderEvent) => void;
45-
onPressIn?: (event: GestureResponderEvent) => void;
46-
onPressOut?: (event: GestureResponderEvent) => void;
47-
onRelease?: (event: GestureResponderEvent, wasDragging: boolean) => void;
48-
onReverse?: () => {x: number, y: number},
49-
x?: number;
50-
y?: number;
51-
// z/elevation should be removed because it doesn't sync up visually and haptically
52-
z?: number;
53-
minX?: number;
54-
minY?: number;
55-
maxX?: number;
56-
maxY?: number;
57-
};
28+
/**** props that should probably be removed in favor of "children" */
29+
renderText?: string;
30+
isCircle?: boolean;
31+
renderSize?: number;
32+
imageSource?: number;
33+
renderColor?: string;
34+
/**** */
35+
children?: React.ReactNode;
36+
shouldReverse?: boolean;
37+
disabled?: boolean;
38+
debug?: boolean;
39+
animatedViewProps?: object;
40+
touchableOpacityProps?: object;
41+
onDrag?: (
42+
e: GestureResponderEvent,
43+
gestureState: PanResponderGestureState,
44+
) => void;
45+
onShortPressRelease?: (event: GestureResponderEvent) => void;
46+
onDragRelease?: (
47+
e: GestureResponderEvent,
48+
gestureState: PanResponderGestureState,
49+
) => void;
50+
onLongPress?: (event: GestureResponderEvent) => void;
51+
onPressIn?: (event: GestureResponderEvent) => void;
52+
onPressOut?: (event: GestureResponderEvent) => void;
53+
onRelease?: (event: GestureResponderEvent, wasDragging: boolean) => void;
54+
onReverse?: () => {x: number; y: number};
55+
x?: number;
56+
y?: number;
57+
// z/elevation should be removed because it doesn't sync up visually and haptically
58+
z?: number;
59+
minX?: number;
60+
minY?: number;
61+
maxX?: number;
62+
maxY?: number;
63+
}
5864

5965
export default function Draggable(props: IProps) {
6066
const {
@@ -108,7 +114,7 @@ export default function Draggable(props: IProps) {
108114
}, [x, y]);
109115

110116
const shouldStartDrag = React.useCallback(
111-
gs => {
117+
(gs) => {
112118
return !disabled && (Math.abs(gs.dx) > 2 || Math.abs(gs.dy) > 2);
113119
},
114120
[disabled],
@@ -195,10 +201,10 @@ export default function Draggable(props: IProps) {
195201
React.useEffect(() => {
196202
const curPan = pan.current; // Using an instance to avoid losing the pointer before the cleanup
197203
if (!shouldReverse) {
198-
curPan.addListener(c => (offsetFromStart.current = c));
204+
curPan.addListener((c) => (offsetFromStart.current = c));
199205
}
200206
return () => {
201-
// Typed incorrectly
207+
// Typed incorrectly
202208
curPan.removeAllListeners();
203209
};
204210
}, [shouldReverse]);
@@ -211,15 +217,15 @@ export default function Draggable(props: IProps) {
211217
left: 0,
212218
width: Window.width,
213219
height: Window.height,
220+
elevation: z,
221+
zIndex: z,
214222
};
215223
}, []);
216224

217225
const dragItemCss = React.useMemo(() => {
218226
const style: StyleProp<ViewStyle> = {
219227
top: y,
220228
left: x,
221-
elevation: z,
222-
zIndex: z,
223229
};
224230
if (renderColor) {
225231
style.backgroundColor = renderColor;
@@ -257,7 +263,7 @@ export default function Draggable(props: IProps) {
257263
}
258264
}, [children, imageSource, renderSize, renderText]);
259265

260-
const handleOnLayout = React.useCallback(event => {
266+
const handleOnLayout = React.useCallback((event) => {
261267
const {height, width} = event.nativeEvent.layout;
262268
childSize.current = {x: width, y: height};
263269
}, []);

0 commit comments

Comments
 (0)