Skip to content

Fix issue with hint targetFrame prop #3541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions demo/src/screens/componentScreens/HintsScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import _ from 'lodash';
import React, {Component} from 'react';
import {Alert, ViewStyle} from 'react-native';
import {Colors, View, Text, Hint, Button, Assets, Incubator} from 'react-native-ui-lib';
import {Colors, View, Text, Hint, Button, Assets, Incubator, Constants} from 'react-native-ui-lib';
import {renderMultipleSegmentOptions, renderBooleanOption} from '../ExampleScreenPresenter';

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

type HintScreenProps = {};

const SHORT_MESSAGE = 'Add other cool and useful stuff.';
const DEFAULT_MESSAGE = 'Add other cool and useful stuff through adding apps to your visitors to enjoy.';
const TARGET_FRAMES_MESSAGE = 'Press Back button to go to previous screen';

export default class HintsScreen extends Component<HintScreenProps> {
state = {
showHint: true,
Expand Down Expand Up @@ -135,10 +139,14 @@ export default class HintsScreen extends Component<HintScreenProps> {
showReactionStrip,
enableShadow
} = this.state;
const targetFrame = {x: 140, y: 100, width: 10, height: 10};
const message = useShortMessage
? 'Add other cool and useful stuff.'
: 'Add other cool and useful stuff through adding apps to your visitors to enjoy.';
const targetFrame = {
x: 20,
y: Constants.getSafeAreaInsets().top + Constants.statusBarHeight,
width: 1,
height: 1
};

const message = useTargetFrame ? TARGET_FRAMES_MESSAGE : useShortMessage ? SHORT_MESSAGE : DEFAULT_MESSAGE;
const color = !showCustomContent && showReactionStrip ? {color: Colors.$backgroundDefault} : undefined;

const hintKey = `${useSideTip}-${targetPosition}-${useShortMessage}-${showIcon}-${useTargetFrame}-${showCustomContent}-${showReactionStrip}`;
Expand Down Expand Up @@ -170,14 +178,14 @@ export default class HintsScreen extends Component<HintScreenProps> {
icon={showIcon ? settingsIcon : undefined}
// iconStyle={{tintColor: 'red'}}
// offset={35}
position={showBottomHint ? Hint.positions.BOTTOM : Hint.positions.TOP}
useSideTip={useSideTip}
position={showBottomHint || useTargetFrame ? Hint.positions.BOTTOM : Hint.positions.TOP}
useSideTip={useSideTip || useTargetFrame}
key={hintKey}
onPress={this.onHintPressed}
targetFrame={useTargetFrame ? targetFrame : undefined}
// borderRadius={BorderRadiuses.br40}
// edgeMargins={30}
onBackgroundPress={useBackdrop && !useTargetFrame ? this.toggleHint : undefined}
onBackgroundPress={useBackdrop || useTargetFrame ? this.toggleHint : undefined}
backdropColor={Colors.rgba(Colors.$backgroundInverted, 0.3)}
customContent={
showCustomContent
Expand Down Expand Up @@ -207,17 +215,6 @@ export default class HintsScreen extends Component<HintScreenProps> {

{useTargetFrame && (
<>
<View
bg-red50
style={{
position: 'absolute',
top: targetFrame.y,
left: targetFrame.x,
width: targetFrame.width,
height: targetFrame.height
}}
/>

<View absL absB margin-page>
<Button label="Show Hint" onPress={this.toggleHint}/>
</View>
Expand Down
21 changes: 2 additions & 19 deletions src/components/hint/hooks/useHintLayout.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import {type ElementRef, useState, useCallback, useRef, useEffect} from 'react';
import {type ElementRef, useState, useCallback, useRef} from 'react';
import type {LayoutChangeEvent, LayoutRectangle, View as RNView} from 'react-native';
import _ from 'lodash';
import {SafeAreaInsetsManager} from 'uilib-native';
import {HintProps} from '../types';
import {Constants} from '../../../commons/new';

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

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

useEffect(() => {
if (targetFrame) {
if (!onBackgroundPress) {
setTargetLayoutInWindow(targetFrame);
} else {
SafeAreaInsetsManager.getSafeAreaInsets().then(insets => {
setTargetLayoutInWindow({
...targetFrame,
y: targetFrame.y + (insets?.top ?? 0) + Constants.getSafeAreaInsets().top
});
});
}
}
}, []);

const onTargetLayout = useCallback(({nativeEvent: {layout}}: LayoutChangeEvent) => {
if (!_.isEqual(targetLayoutState, layout)) {
setTargetLayout(layout);
Expand Down