Skip to content

Commit a44926e

Browse files
ethansharM-i-k-e-l
authored andcommitted
Fix issue with hint targetFrame prop (#3541)
* Fix issue with hint targetFrame prop and change its example the example screen * Fix TS
1 parent 07092a2 commit a44926e

File tree

2 files changed

+18
-38
lines changed

2 files changed

+18
-38
lines changed

demo/src/screens/componentScreens/HintsScreen.tsx

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
33
import {Alert, ViewStyle} from 'react-native';
4-
import {Colors, View, Text, Hint, Button, Assets, Incubator} from 'react-native-ui-lib';
4+
import {Colors, View, Text, Hint, Button, Assets, Incubator, Constants} from 'react-native-ui-lib';
55
import {renderMultipleSegmentOptions, renderBooleanOption} from '../ExampleScreenPresenter';
66

77
const settingsIcon = require('../../assets/icons/settings.png');
88
const reactions = ['❤️', '😮', '😔', '😂', '😡'];
99

1010
type HintScreenProps = {};
1111

12+
const SHORT_MESSAGE = 'Add other cool and useful stuff.';
13+
const DEFAULT_MESSAGE = 'Add other cool and useful stuff through adding apps to your visitors to enjoy.';
14+
const TARGET_FRAMES_MESSAGE = 'Press Back button to go to previous screen';
15+
1216
export default class HintsScreen extends Component<HintScreenProps> {
1317
state = {
1418
showHint: true,
@@ -135,10 +139,14 @@ export default class HintsScreen extends Component<HintScreenProps> {
135139
showReactionStrip,
136140
enableShadow
137141
} = this.state;
138-
const targetFrame = {x: 140, y: 100, width: 10, height: 10};
139-
const message = useShortMessage
140-
? 'Add other cool and useful stuff.'
141-
: 'Add other cool and useful stuff through adding apps to your visitors to enjoy.';
142+
const targetFrame = {
143+
x: 20,
144+
y: Constants.getSafeAreaInsets().top + Constants.statusBarHeight,
145+
width: 1,
146+
height: 1
147+
};
148+
149+
const message = useTargetFrame ? TARGET_FRAMES_MESSAGE : useShortMessage ? SHORT_MESSAGE : DEFAULT_MESSAGE;
142150
const color = !showCustomContent && showReactionStrip ? {color: Colors.$backgroundDefault} : undefined;
143151

144152
const hintKey = `${useSideTip}-${targetPosition}-${useShortMessage}-${showIcon}-${useTargetFrame}-${showCustomContent}-${showReactionStrip}`;
@@ -170,14 +178,14 @@ export default class HintsScreen extends Component<HintScreenProps> {
170178
icon={showIcon ? settingsIcon : undefined}
171179
// iconStyle={{tintColor: 'red'}}
172180
// offset={35}
173-
position={showBottomHint ? Hint.positions.BOTTOM : Hint.positions.TOP}
174-
useSideTip={useSideTip}
181+
position={showBottomHint || useTargetFrame ? Hint.positions.BOTTOM : Hint.positions.TOP}
182+
useSideTip={useSideTip || useTargetFrame}
175183
key={hintKey}
176184
onPress={this.onHintPressed}
177185
targetFrame={useTargetFrame ? targetFrame : undefined}
178186
// borderRadius={BorderRadiuses.br40}
179187
// edgeMargins={30}
180-
onBackgroundPress={useBackdrop && !useTargetFrame ? this.toggleHint : undefined}
188+
onBackgroundPress={useBackdrop || useTargetFrame ? this.toggleHint : undefined}
181189
backdropColor={Colors.rgba(Colors.$backgroundInverted, 0.3)}
182190
customContent={
183191
showCustomContent
@@ -207,17 +215,6 @@ export default class HintsScreen extends Component<HintScreenProps> {
207215

208216
{useTargetFrame && (
209217
<>
210-
<View
211-
bg-red50
212-
style={{
213-
position: 'absolute',
214-
top: targetFrame.y,
215-
left: targetFrame.x,
216-
width: targetFrame.width,
217-
height: targetFrame.height
218-
}}
219-
/>
220-
221218
<View absL absB margin-page>
222219
<Button label="Show Hint" onPress={this.toggleHint}/>
223220
</View>

src/components/hint/hooks/useHintLayout.ts

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
import {type ElementRef, useState, useCallback, useRef, useEffect} from 'react';
1+
import {type ElementRef, useState, useCallback, useRef} from 'react';
22
import type {LayoutChangeEvent, LayoutRectangle, View as RNView} from 'react-native';
33
import _ from 'lodash';
4-
import {SafeAreaInsetsManager} from 'uilib-native';
54
import {HintProps} from '../types';
6-
import {Constants} from '../../../commons/new';
75

86
type UseHintLayoutProps = Pick<HintProps, 'onBackgroundPress' | 'targetFrame'>;
97

108
export default function useHintLayout({onBackgroundPress, targetFrame}: UseHintLayoutProps) {
119
const [targetLayoutState, setTargetLayout] = useState<LayoutRectangle | undefined>(targetFrame);
12-
const [targetLayoutInWindowState, setTargetLayoutInWindow] = useState<LayoutRectangle | undefined>();
10+
const [targetLayoutInWindowState, setTargetLayoutInWindow] = useState<LayoutRectangle | undefined>(targetFrame);
1311
const [hintMessageWidth, setHintMessageWidth] = useState<number | undefined>();
1412
const targetRef = useRef<ElementRef<typeof RNView> | null>(null);
1513

16-
useEffect(() => {
17-
if (targetFrame) {
18-
if (!onBackgroundPress) {
19-
setTargetLayoutInWindow(targetFrame);
20-
} else {
21-
SafeAreaInsetsManager.getSafeAreaInsets().then(insets => {
22-
setTargetLayoutInWindow({
23-
...targetFrame,
24-
y: targetFrame.y + (insets?.top ?? 0) + Constants.getSafeAreaInsets().top
25-
});
26-
});
27-
}
28-
}
29-
}, []);
30-
3114
const onTargetLayout = useCallback(({nativeEvent: {layout}}: LayoutChangeEvent) => {
3215
if (!_.isEqual(targetLayoutState, layout)) {
3316
setTargetLayout(layout);

0 commit comments

Comments
 (0)