Skip to content
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

Release/v1.1.0 #8

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
71 changes: 59 additions & 12 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import { useState } from 'react';

export default function App() {
const [inputText, setInputText] = useState('초기 텍스트');
// Example - TextInput
const [inputText, setInputText] = useState('');
const [tooltipMessage, setTooltipMessage] = useState('Hello');
const [isFocused, setIsFocused] = useState(false);

// Example - FlatList
// const data = [1, 2, 3];

return (
<SafeAreaView style={styles.container}>
{/* Header*/}
<View
style={{

Check warning on line 18 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { flexDirection: 'row', justifyContent: 'space-between', zIndex: 1 }
flexDirection: 'row',
justifyContent: 'space-between',
zIndex: 1,
Expand All @@ -21,19 +27,19 @@
anchor={'left'}
onPress={() => {}}
>
<View style={[styles.box, { backgroundColor: 'red' }]} />

Check warning on line 30 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { backgroundColor: 'red' }
</Tooltip>

<Tooltip
text="This is a tooltip"
placement={'bottom'}
styles={{ color: 'black' }}

Check warning on line 36 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { color: 'black' }
>
<View style={[styles.box, { backgroundColor: 'green' }]} />

Check warning on line 38 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { backgroundColor: 'green' }
</Tooltip>

<Tooltip text="This is a tooltip" placement={'bottom'} anchor={'right'}>
<View style={[styles.box, { backgroundColor: 'blue' }]} />

Check warning on line 42 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { backgroundColor: 'blue' }
</Tooltip>
</View>

Expand Down Expand Up @@ -66,27 +72,68 @@
</View>
</View>

<Tooltip placement={'bottom'} text={'Input Text'} visible={true}>
<Tooltip
placement={'bottom'}
anchor={'left'}
text={tooltipMessage}
visible={isFocused}
disableAutoHide
styles={{
color: tooltipMessage === 'Correct!' ? 'blue' : undefined,
}}
>
<TextInput
value={inputText}
placeholder={'Input text'}
onChangeText={setInputText}
style={{ padding: 16, backgroundColor: 'gray' }}
placeholder={'Input secret password'}
placeholderTextColor={'#6cbd67'}
onChangeText={(text) => {
setInputText(text);
if (text.toLowerCase() === 'world') {
setTooltipMessage('Correct!');
setTimeout(() => {
setIsFocused(false);
}, 1500);
}
}}
style={{ padding: 16, backgroundColor: 'gray', color: 'white' }}
onFocus={() => setIsFocused(true)}
onBlur={() => {
setIsFocused(false);
}}
/>
</Tooltip>

{/*<FlatList*/}
{/* data={[1, 2, 3]}*/}
{/* renderItem={() => {*/}
{/* data={data}*/}
{/* keyExtractor={(item) => item.toString()}*/}
{/* CellRendererComponent={({ index, style, ...props }) => {*/}
{/* return (*/}
{/* <TextInput*/}
{/* value={inputText}*/}
{/* onChangeText={setInputText}*/}
{/* placeholder={'Input text'}*/}
{/* style={{ padding: 16, backgroundColor: 'gray' }}*/}
{/* <View*/}
{/* style={[*/}
{/* style,*/}
{/* {*/}
{/* zIndex: data.length - index,*/}
{/* },*/}
{/* ]}*/}
{/* {...props}*/}
{/* />*/}
{/* );*/}
{/* }}*/}
{/* renderItem={() => {*/}
{/* console.log('####################################');*/}
{/* console.log('리렌더링?');*/}
{/* console.log('####################################');*/}
{/* return (*/}
{/* <MemoizedTooltip placement={'bottom'} text={'This is a tooltip'}>*/}
{/* <TextInput*/}
{/* value={inputText}*/}
{/* onChangeText={setInputText}*/}
{/* placeholder={'Input text'}*/}
{/* style={{ padding: 16, backgroundColor: 'gray' }}*/}
{/* />*/}
{/* </MemoizedTooltip>*/}
{/* );*/}
{/* }}*/}
{/*/>*/}
</SafeAreaView>
);
Expand Down
29 changes: 12 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import {
Animated,
Dimensions,
Expand Down Expand Up @@ -65,6 +59,7 @@ interface ToolTipProps {
onVisibleChange?: (isVisible: boolean) => void;
delayShowTime?: number;
autoHideTime?: number;
disableAutoHide?: boolean;
}

const Tooltip = ({
Expand All @@ -84,9 +79,8 @@ const Tooltip = ({
onVisibleChange,
delayShowTime = 0,
autoHideTime = 5000,
disableAutoHide,
}: ToolTipProps) => {
const showAnimationRef = useRef(false);
const hideAnimationRef = useRef(false);
const animatedValue = useMemo(() => new Animated.Value(0), []);

const [tooltipPosition, setTooltipPosition] = useState({
Expand Down Expand Up @@ -120,27 +114,21 @@ const Tooltip = ({
const runAnimation = useCallback(
(isShowAnimation: boolean) => {
if (isShowAnimation) {
if (showAnimationRef.current) return;

Animated.spring(animatedValue, {
toValue: 1,
speed: 6,
useNativeDriver: true,
}).start(() => {
onVisibleChange && onVisibleChange(true);
});
showAnimationRef.current = true;
} else {
if (hideAnimationRef.current) return;

Animated.timing(animatedValue, {
toValue: 0,
duration: 300,
useNativeDriver: true,
}).start(() => {
onVisibleChange && onVisibleChange(false);
});
hideAnimationRef.current = true;
}
},
[animatedValue, onVisibleChange]
Expand All @@ -159,12 +147,19 @@ const Tooltip = ({

// hide animation
useEffect(() => {
if (onPress) return;
if (onPress || disableAutoHide) return;

setTimeout(() => {
runAnimation(false);
}, delayShowTime + autoHideTime);
}, [autoHideTime, delayShowTime, onPress, runAnimation]);
}, [autoHideTime, delayShowTime, disableAutoHide, onPress, runAnimation]);

// hide by props.visible
useEffect(() => {
if (visible === false) {
runAnimation(false);
}
}, [runAnimation, visible]);

const handleVerticalTooltipLayout = (event: LayoutChangeEvent) => {
const { width, height } = event.nativeEvent.layout;
Expand Down
Loading