-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathAISearchCTAPopup.tsx
More file actions
177 lines (166 loc) · 4.41 KB
/
AISearchCTAPopup.tsx
File metadata and controls
177 lines (166 loc) · 4.41 KB
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
169
170
171
172
173
174
175
176
177
import { useEffect, useRef } from 'react'
import { Text, Button, Heading, Popover, useOnEscapePress, Box } from '@primer/react'
import { focusTrap } from '@primer/behaviors'
import { useTranslation } from '@/languages/components/useTranslation'
import { useMaxWidthBreakpoint, useMinWidthBreakpoint } from '../hooks/useBreakpoint'
import { useCTAPopoverContext } from '@/frame/components/context/CTAContext'
let previouslyFocused: HTMLElement | null = null
export function AISearchCTAPopup({
isOpen,
dismiss,
setIsSearchOpen,
isDismissible = true,
}: {
isOpen: boolean
dismiss?: () => void
setIsSearchOpen: (value: boolean) => void
isDismissible?: boolean
}) {
const { t } = useTranslation('search')
const { permanentDismiss } = useCTAPopoverContext()
const isLargeOrUp = useMinWidthBreakpoint('large')
const isTooSmallForCTA = useMaxWidthBreakpoint('293px')
let overlayRef = useRef<HTMLDivElement>(null)
let dismissButtonRef = useRef<HTMLButtonElement>(null)
const openSearch = () => {
setIsSearchOpen(true)
// They engaged with the CTA, so let's not show this popup for them anymore
permanentDismiss()
}
// For a11y, focus trap the CTA and allow it to be closed with Escape
useEffect(() => {
if (isTooSmallForCTA) {
return
}
if (isOpen && overlayRef.current && dismissButtonRef.current) {
focusTrap(overlayRef.current, dismissButtonRef.current)
previouslyFocused = document.activeElement as HTMLElement | null
}
}, [isOpen, isTooSmallForCTA])
const onDismiss = () => {
if (isTooSmallForCTA) {
return
}
if (previouslyFocused) {
previouslyFocused.focus()
}
if (dismiss) {
dismiss()
}
}
useOnEscapePress(onDismiss)
if (isTooSmallForCTA) {
return null
}
const innerContent = (
<>
<img
src="/assets/images/search/copilot-action.png"
width="100%"
alt="The Copilot Icon in front of an explosion of color."
/>
<Heading
as="h2"
id="ai-search-cta-heading"
sx={{
fontSize: '16px',
fontWeight: 'bold',
marginTop: '12px',
}}
>
{t('search.cta.heading')}
</Heading>
<Text
id="ai-search-cta-description"
sx={{
display: 'block',
fontSize: '15px',
marginTop: '12px',
}}
>
{t('search.cta.description')}
</Text>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
}}
>
{isDismissible ? (
<Button
ref={dismissButtonRef}
aria-label="Dismiss"
sx={{
marginTop: '16px',
fontWeight: 'bold',
}}
onClick={onDismiss}
>
{t('search.cta.dismiss')}
</Button>
) : null}
<Button
variant="primary"
sx={
isDismissible
? {
marginTop: '16px',
marginLeft: '8px',
fontWeight: 'bold',
}
: {
marginTop: '16px',
width: '100%',
}
}
onClick={openSearch}
>
{t('search.cta.ask_copilot')}
</Button>
</Box>
</>
)
// If not dismissible, it's not being used as a popover
if (!isDismissible) {
return (
<Box
sx={{
position: 'relative',
width: '270px',
border: '1px solid var(--borderColor-default, var(--color-border-default, #d0d7de))',
borderRadius: '6px',
padding: '16px',
}}
>
{innerContent}
</Box>
)
}
return (
<Popover
ref={overlayRef}
role="alertdialog"
aria-modal="true"
aria-labelledby="ai-search-cta-heading"
aria-describedby="ai-search-cta-description"
open={isOpen}
caret={isLargeOrUp ? 'top' : 'top-right'}
sx={{
top: '55px',
boxShadow: '0 4px 12px rgba(0, 0, 0, 0.1)',
width: '270px',
// When in mobile (< large) the search bar collapses to a button on the RHS of the screen
// To align the popover with the button we need to move it to the left
marginLeft: isLargeOrUp ? 0 : -235,
}}
>
<Popover.Content
sx={{
width: '270px',
}}
>
{innerContent}
</Popover.Content>
</Popover>
)
}