Skip to content

Commit c607958

Browse files
authored
Fix/ Hint renders on wrong screen (#1818)
* use TouchableWithoutFeedback instead of modal * support modal via useModal prop * pr fixes * useModal by default
1 parent a86d7ee commit c607958

File tree

3 files changed

+70
-44
lines changed

3 files changed

+70
-44
lines changed

generatedTypes/src/components/hint/index.d.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export interface HintProps {
5151
* Provide custom target position instead of wrapping a child
5252
*/
5353
targetFrame?: HintTargetFrame;
54+
/**
55+
* Open the hint using a Modal component
56+
*/
57+
useModal?: boolean;
5458
/**
5559
* Show side tips instead of the middle tip
5660
*/
@@ -119,13 +123,19 @@ declare class Hint extends Component<HintProps, HintState> {
119123
static displayName: string;
120124
static defaultProps: {
121125
position: HintPositions;
126+
useModal: boolean;
122127
};
123128
static positions: typeof HintPositions;
124129
targetRef: ElementRef<typeof RNView> | null;
125130
hintRef: ElementRef<typeof RNView> | null;
126131
animationDuration: number;
127132
state: {
128-
targetLayoutInWindow: undefined;
133+
targetLayoutInWindow: {
134+
x: number;
135+
y: number;
136+
width: number;
137+
height: number;
138+
} | undefined;
129139
targetLayout: HintTargetFrame | undefined;
130140
hintUnmounted: boolean;
131141
};
@@ -153,9 +163,9 @@ declare class Hint extends Component<HintProps, HintState> {
153163
get useSideTip(): boolean;
154164
getTargetPositionOnScreen(): TARGET_POSITIONS;
155165
getContainerPosition(): {
156-
top: number | undefined;
157-
left: number | undefined;
158-
} | undefined;
166+
top: number;
167+
left: number;
168+
};
159169
getHintPosition(): HintPositionStyle;
160170
getHintPadding(): Paddings;
161171
getHintAnimatedStyle: () => {
@@ -165,6 +175,7 @@ declare class Hint extends Component<HintProps, HintState> {
165175
}[];
166176
};
167177
getTipPosition(): Position;
178+
renderOverlay(): JSX.Element | undefined;
168179
renderHintTip(): JSX.Element;
169180
renderContent(): JSX.Element;
170181
renderHint(): JSX.Element | undefined;

src/components/hint/hint.api.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"type": "{x?: number, y?: number, width?: number, height?: number}",
2424
"description": "Provide custom target position instead of wrapping a child"
2525
},
26+
{"name": "useModal", "type": "boolean", "description": "Open the hint using a Modal component"},
2627
{"name": "useSideTip", "type": "boolean", "description": "Show side tips instead of the middle tip"},
2728
{"name": "borderRadius", "type": "number", "description": "The hint's border radius"},
2829
{"name": "edgeMargins", "type": "number", "description": "Hint margins from screen edges"},

src/components/hint/index.tsx

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
findNodeHandle,
88
GestureResponderEvent,
99
ImageSourcePropType,
10+
TouchableWithoutFeedback,
1011
ImageStyle,
1112
StyleProp,
1213
TextStyle,
@@ -87,6 +88,10 @@ export interface HintProps {
8788
* Provide custom target position instead of wrapping a child
8889
*/
8990
targetFrame?: HintTargetFrame;
91+
/**
92+
* Open the hint using a Modal component
93+
*/
94+
useModal?: boolean;
9095
/**
9196
* Show side tips instead of the middle tip
9297
*/
@@ -157,7 +162,8 @@ class Hint extends Component<HintProps, HintState> {
157162
static displayName = 'Hint';
158163

159164
static defaultProps = {
160-
position: HintPositions.BOTTOM
165+
position: HintPositions.BOTTOM,
166+
useModal: true
161167
};
162168

163169
static positions = HintPositions;
@@ -167,7 +173,7 @@ class Hint extends Component<HintProps, HintState> {
167173
animationDuration = 170;
168174

169175
state = {
170-
targetLayoutInWindow: undefined,
176+
targetLayoutInWindow: undefined as {x: number, y: number, width: number, height: number} | undefined,
171177
targetLayout: this.props.targetFrame,
172178
hintUnmounted: !this.props.visible
173179
};
@@ -246,14 +252,14 @@ class Hint extends Component<HintProps, HintState> {
246252
}
247253

248254
get targetLayout() {
249-
const {onBackgroundPress, targetFrame} = this.props;
255+
const {onBackgroundPress, useModal, targetFrame} = this.props;
250256
const {targetLayout, targetLayoutInWindow} = this.state;
251257

252258
if (targetFrame) {
253259
return targetFrame;
254260
}
255261

256-
return onBackgroundPress ? targetLayoutInWindow : targetLayout;
262+
return onBackgroundPress && useModal ? targetLayoutInWindow : targetLayout;
257263
}
258264

259265
get showHint() {
@@ -300,8 +306,9 @@ class Hint extends Component<HintProps, HintState> {
300306

301307
getContainerPosition() {
302308
if (this.targetLayout) {
303-
return {top: this.targetLayout.y, left: this.targetLayout.x};
309+
return {top: this.targetLayout.y || 0, left: this.targetLayout.x || 0};
304310
}
311+
return {top: 0, left: 0};
305312
}
306313

307314
getHintPosition() {
@@ -397,31 +404,34 @@ class Hint extends Component<HintProps, HintState> {
397404
return tipPositionStyle;
398405
}
399406

400-
// renderOverlay() {
401-
// const {targetLayoutInWindow} = this.state;
402-
// const {onBackgroundPress} = this.props;
403-
// if (targetLayoutInWindow) {
404-
// const containerPosition = this.getContainerPosition();
405-
// return (
406-
// <View
407-
// style={[
408-
// styles.overlay,
409-
// {
410-
// top: containerPosition.top - targetLayoutInWindow.y,
411-
// left: containerPosition.left - targetLayoutInWindow.x,
412-
// },
413-
// ]}
414-
// pointerEvents="box-none"
415-
// >
416-
// {onBackgroundPress && (
417-
// <TouchableWithoutFeedback style={[StyleSheet.absoluteFillObject]} onPress={onBackgroundPress}>
418-
// <View flex />
419-
// </TouchableWithoutFeedback>
420-
// )}
421-
// </View>
422-
// );
423-
// }
424-
// }
407+
renderOverlay() {
408+
const {targetLayoutInWindow} = this.state;
409+
const {onBackgroundPress, backdropColor, testID} = this.props;
410+
if (targetLayoutInWindow) {
411+
const containerPosition = this.getContainerPosition();
412+
return (
413+
<Animated.View
414+
style={[
415+
styles.overlay,
416+
{
417+
top: containerPosition.top - targetLayoutInWindow.y,
418+
left: containerPosition.left - targetLayoutInWindow.x,
419+
backgroundColor: backdropColor,
420+
opacity: this.visibleAnimated
421+
}
422+
]}
423+
pointerEvents="box-none"
424+
testID={`${testID}.overlay`}
425+
>
426+
{onBackgroundPress && (
427+
<TouchableWithoutFeedback style={StyleSheet.absoluteFillObject} onPress={onBackgroundPress}>
428+
<View flex/>
429+
</TouchableWithoutFeedback>
430+
)}
431+
</Animated.View>
432+
);
433+
}
434+
}
425435

426436
renderHintTip() {
427437
const {position, color = DEFAULT_COLOR} = this.props;
@@ -549,7 +559,7 @@ class Hint extends Component<HintProps, HintState> {
549559
}
550560

551561
render() {
552-
const {onBackgroundPress, backdropColor, testID} = this.props;
562+
const {onBackgroundPress, backdropColor, useModal, testID} = this.props;
553563

554564
if (!this.props.visible && this.state.hintUnmounted) {
555565
return this.props.children || null;
@@ -558,7 +568,7 @@ class Hint extends Component<HintProps, HintState> {
558568
return (
559569
<>
560570
{this.renderChildren()}
561-
{onBackgroundPress ? (
571+
{onBackgroundPress && useModal ? (
562572
<Modal
563573
visible={this.showHint}
564574
animationType={backdropColor ? 'fade' : 'none'}
@@ -572,8 +582,11 @@ class Hint extends Component<HintProps, HintState> {
572582
{this.renderHintContainer()}
573583
</Modal>
574584
) : (
575-
// this.renderOverlay(),
576-
this.renderHintContainer()
585+
<>
586+
{this.renderOverlay()}
587+
{this.renderMockChildren()}
588+
{this.renderHintContainer()}
589+
</>
577590
)}
578591
</>
579592
);
@@ -582,7 +595,8 @@ class Hint extends Component<HintProps, HintState> {
582595

583596
const styles = StyleSheet.create({
584597
container: {
585-
position: 'absolute'
598+
position: 'absolute',
599+
zIndex: 1
586600
},
587601
mockChildrenContainer: {
588602
position: 'absolute'
@@ -601,11 +615,11 @@ const styles = StyleSheet.create({
601615
right: undefined,
602616
bottom: undefined
603617
},
604-
// overlay: {
605-
// position: 'absolute',
606-
// width: Constants.screenWidth,
607-
// height: Constants.screenHeight
608-
// },
618+
overlay: {
619+
position: 'absolute',
620+
width: Constants.screenWidth,
621+
height: Constants.screenHeight
622+
},
609623
animatedContainer: {
610624
position: 'absolute'
611625
},

0 commit comments

Comments
 (0)