about summary refs log tree commit diff
path: root/src/view/com/modals/composer/Autocomplete.tsx
blob: 4e4bdfc8e940b34bb5e132560aa8c58157de4a7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React, {useEffect} from 'react'
import {
  useWindowDimensions,
  Text,
  TouchableOpacity,
  StyleSheet,
} from 'react-native'
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withTiming,
  interpolate,
} from 'react-native-reanimated'
import {colors} from '../../../lib/styles'

export function Autocomplete({
  active,
  items,
  onSelect,
}: {
  active: boolean
  items: string[]
  onSelect: (item: string) => void
}) {
  const winDim = useWindowDimensions()
  const positionInterp = useSharedValue<number>(0)

  useEffect(() => {
    if (active) {
      positionInterp.value = withTiming(1, {duration: 250})
    } else {
      positionInterp.value = withTiming(0, {duration: 250})
    }
  }, [positionInterp, active])

  const topAnimStyle = useAnimatedStyle(() => ({
    top: interpolate(
      positionInterp.value,
      [0, 1.0],
      [winDim.height, winDim.height / 4],
    ),
  }))
  return (
    <Animated.View style={[styles.outer, topAnimStyle]}>
      {items.map((item, i) => (
        <TouchableOpacity
          key={i}
          style={styles.item}
          onPress={() => onSelect(item)}>
          <Text style={styles.itemText}>@{item}</Text>
        </TouchableOpacity>
      ))}
    </Animated.View>
  )
}

const styles = StyleSheet.create({
  outer: {
    position: 'absolute',
    left: 0,
    right: 0,
    bottom: 0,
    backgroundColor: colors.white,
    borderTopWidth: 1,
    borderTopColor: colors.gray2,
  },
  item: {
    borderBottomWidth: 1,
    borderBottomColor: colors.gray1,
    paddingVertical: 16,
    paddingHorizontal: 16,
  },
  itemText: {
    fontSize: 16,
  },
})