about summary refs log tree commit diff
path: root/src/tours/Tooltip.tsx
blob: e7727763ba3fe1d04843a4fae82010f07596a3ff (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as React from 'react'
import {
  AccessibilityInfo,
  findNodeHandle,
  Pressable,
  Text as RNText,
  View,
} from 'react-native'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {FocusScope} from '@tamagui/focus-scope'
import {IStep, Labels} from 'rn-tourguide'

import {useWebBodyScrollLock} from '#/lib/hooks/useWebBodyScrollLock'
import {useA11y} from '#/state/a11y'
import {Logo} from '#/view/icons/Logo'
import {atoms as a, useTheme} from '#/alf'
import {Button, ButtonText} from '#/components/Button'
import {leading, Text} from '#/components/Typography'

const stopPropagation = (e: any) => e.stopPropagation()

export interface TooltipComponentProps {
  isFirstStep?: boolean
  isLastStep?: boolean
  currentStep: IStep
  labels?: Labels
  handleNext?: () => void
  handlePrev?: () => void
  handleStop?: () => void
}

export function TooltipComponent({
  isLastStep,
  handleNext,
  handleStop,
  currentStep,
  labels,
}: TooltipComponentProps) {
  const t = useTheme()
  const {_} = useLingui()
  const btnRef = React.useRef<View>(null)
  const textRef = React.useRef<RNText>(null)
  const {screenReaderEnabled} = useA11y()
  useWebBodyScrollLock(true)

  const focusTextNode = () => {
    const node = textRef.current ? findNodeHandle(textRef.current) : undefined
    if (node) {
      AccessibilityInfo.setAccessibilityFocus(node)
    }
  }

  // handle initial focus immediately on mount
  React.useLayoutEffect(() => {
    focusTextNode()
  }, [])

  // handle focus between steps
  const innerHandleNext = () => {
    handleNext?.()
    setTimeout(() => focusTextNode(), 200)
  }

  return (
    <FocusScope loop enabled trapped>
      <View
        role="alert"
        aria-role="alert"
        aria-label={_(msg`A help tooltip`)}
        accessibilityLiveRegion="polite"
        // iOS
        accessibilityViewIsModal
        // Android
        importantForAccessibility="yes"
        // @ts-ignore web only
        onClick={stopPropagation}
        onStartShouldSetResponder={_ => true}
        onTouchEnd={stopPropagation}
        style={[
          t.atoms.bg,
          a.px_lg,
          a.py_lg,
          a.flex_col,
          a.gap_md,
          a.rounded_sm,
          a.shadow_md,
          {maxWidth: 300},
        ]}>
        {screenReaderEnabled && (
          <Pressable
            style={[
              a.absolute,
              a.inset_0,
              a.z_10,
              {height: 10, bottom: 'auto'},
            ]}
            accessibilityLabel={_(
              msg`Start of onboarding tour window. Do not move backward. Instead, go forward for more options, or press to skip.`,
            )}
            accessibilityHint={undefined}
            onPress={handleStop}
          />
        )}

        <View style={[a.flex_row, a.align_center, a.gap_sm]}>
          <Logo width={16} style={{position: 'relative', top: 0}} />
          <Text
            accessible={false}
            style={[a.text_sm, a.font_semibold, t.atoms.text_contrast_medium]}>
            <Trans>Quick tip</Trans>
          </Text>
        </View>
        <RNText
          ref={textRef}
          testID="stepDescription"
          accessibilityLabel={_(
            msg`Onboarding tour step ${currentStep.name}: ${currentStep.text}`,
          )}
          accessibilityHint={undefined}
          style={[
            a.text_md,
            t.atoms.text,
            a.pb_sm,
            {
              lineHeight: leading(a.text_md, a.leading_snug),
            },
          ]}>
          {currentStep.text}
        </RNText>
        {!isLastStep ? (
          <Button
            ref={btnRef}
            variant="gradient"
            color="gradient_sky"
            size="medium"
            onPress={innerHandleNext}
            label={labels?.next || _(msg`Go to the next step of the tour`)}>
            <ButtonText>{labels?.next || _(msg`Next`)}</ButtonText>
          </Button>
        ) : (
          <Button
            variant="gradient"
            color="gradient_sky"
            size="medium"
            onPress={handleStop}
            label={
              labels?.finish ||
              _(msg`Finish tour and begin using the application`)
            }>
            <ButtonText>{labels?.finish || _(msg`Let's go!`)}</ButtonText>
          </Button>
        )}

        {screenReaderEnabled && (
          <Pressable
            style={[a.absolute, a.inset_0, a.z_10, {height: 10, top: 'auto'}]}
            accessibilityLabel={_(
              msg`End of onboarding tour window. Do not move forward. Instead, go backward for more options, or press to skip.`,
            )}
            accessibilityHint={undefined}
            onPress={handleStop}
          />
        )}
      </View>
    </FocusScope>
  )
}