Skip to content

Commit

Permalink
animate cancel button in
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzius committed Jan 30, 2025
1 parent c235869 commit e47fc68
Showing 1 changed file with 69 additions and 38 deletions.
107 changes: 69 additions & 38 deletions src/view/screens/Search/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useLayoutEffect} from 'react'
import React, {useEffect, useLayoutEffect} from 'react'
import {
ActivityIndicator,
Image,
Expand All @@ -16,6 +16,9 @@ import Animated, {
FadeOut,
LayoutAnimationConfig,
LinearTransition,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated'
import {AppBskyActorDefs, AppBskyFeedDefs, moderateProfile} from '@atproto/api'
import {
Expand Down Expand Up @@ -827,6 +830,26 @@ export function SearchScreen(
}
}, [setShowAutocomplete])

const cancelWidth = useSharedValue(70)
const showCancelBtnAnimation = useSharedValue(0)

useEffect(() => {
if (showAutocomplete) {
showCancelBtnAnimation.set(withTiming(1))
} else {
showCancelBtnAnimation.set(withTiming(0))
}
}, [showAutocomplete, showCancelBtnAnimation])

const animatedInputContainerStyle = useAnimatedStyle(() => ({
marginRight: showCancelBtnAnimation.get() * cancelWidth.get(),
}))

const animatedCancelBtnStyle = useAnimatedStyle(() => ({
opacity: showCancelBtnAnimation.get(),
right: cancelWidth.get() * -1,
}))

return (
<Layout.Screen testID="searchScreen">
<View
Expand All @@ -846,8 +869,8 @@ export function SearchScreen(
<LayoutAnimationConfig skipEntering skipExiting>
{!gtMobile && !showAutocomplete && (
<Animated.View
entering={FadeIn.delay(100).duration(200)}
exiting={FadeOut.duration(200)}
entering={native(FadeIn.delay(100).duration(200))}
exiting={native(FadeOut.duration(200))}
// HACK: shift up search input. we can't remove the top padding
// on the search input because it messes up the layout animation
// if we add it only when the header is hidden
Expand Down Expand Up @@ -875,10 +898,11 @@ export function SearchScreen(
)}
</LayoutAnimationConfig>
<Animated.View
style={[a.px_md, a.pt_sm, a.pb_sm, a.gap_sm]}
style={[a.px_md, a.pt_sm, a.pb_sm, a.overflow_hidden]}
layout={native(LinearTransition)}>
<View style={[a.flex_row, a.gap_xs]}>
<View style={[a.flex_1]}>
<Animated.View
style={[a.gap_sm, a.relative, animatedInputContainerStyle]}>
<View style={[a.w_full]}>
<SearchInput
ref={textInput}
value={searchText}
Expand All @@ -890,40 +914,47 @@ export function SearchScreen(
hitSlop={{...HITSLOP_20, top: 0}}
/>
</View>
{showAutocomplete && (
<Animated.View entering={FadeIn} exiting={FadeOut}>
<Button
label={_(msg`Cancel search`)}
size="large"
variant="ghost"
color="secondary"
style={[a.px_sm]}
onPress={onPressCancelSearch}
hitSlop={HITSLOP_10}>
<ButtonText>
<Trans>Cancel</Trans>
</ButtonText>
</Button>
</Animated.View>
)}
</View>

{showFilters && gtMobile && (
<View
<Animated.View
style={[
a.flex_row,
a.align_center,
a.justify_between,
a.gap_sm,
]}>
<View style={[{width: 140}]}>
<SearchLanguageDropdown
value={params.lang}
onChange={params.setLang}
/>
a.absolute,
{top: 0, bottom: 0},
a.pl_xs,
animatedCancelBtnStyle,
!showAutocomplete && a.pointer_events_none,
]}
onLayout={evt => cancelWidth.set(evt.nativeEvent.layout.width)}
aria-hidden={!showAutocomplete}>
<Button
label={_(msg`Cancel search`)}
size="large"
variant="ghost"
color="secondary"
style={[a.px_sm]}
onPress={onPressCancelSearch}
hitSlop={HITSLOP_10}>
<ButtonText>
<Trans>Cancel</Trans>
</ButtonText>
</Button>
</Animated.View>

{showFilters && gtMobile && (
<View
style={[
a.flex_row,
a.align_center,
a.justify_between,
a.gap_sm,
]}>
<View style={[{width: 140}]}>
<SearchLanguageDropdown
value={params.lang}
onChange={params.setLang}
/>
</View>
</View>
</View>
)}
)}
</Animated.View>
</Animated.View>
</Layout.Center>
</View>
Expand Down

0 comments on commit e47fc68

Please sign in to comment.