|
| 1 | +import { useEffect, useRef } from 'react' |
| 2 | +import { Text, Button, Heading, Popover, useOnEscapePress } from '@primer/react' |
| 3 | +import { focusTrap } from '@primer/behaviors' |
| 4 | + |
| 5 | +import { useTranslation } from '@/languages/components/useTranslation' |
| 6 | +import { useMaxWidthBreakpoint, useMinWidthBreakpoint } from '../hooks/useBreakpoint' |
| 7 | + |
| 8 | +let previouslyFocused: HTMLElement | null = null |
| 9 | + |
| 10 | +export function AISearchCTAPopup({ isOpen, dismiss }: { isOpen: boolean; dismiss: () => void }) { |
| 11 | + const { t } = useTranslation('search') |
| 12 | + const isLargeOrUp = useMinWidthBreakpoint('large') |
| 13 | + const isTooSmallForCTA = useMaxWidthBreakpoint('293px') |
| 14 | + let overlayRef = useRef<HTMLDivElement>(null) |
| 15 | + let dismissButtonRef = useRef<HTMLButtonElement>(null) |
| 16 | + |
| 17 | + // For a11y, focus trap the CTA and allow it to be closed with Escape |
| 18 | + useEffect(() => { |
| 19 | + if (isTooSmallForCTA) { |
| 20 | + return |
| 21 | + } |
| 22 | + if (isOpen && overlayRef.current && dismissButtonRef.current) { |
| 23 | + focusTrap(overlayRef.current, dismissButtonRef.current) |
| 24 | + previouslyFocused = document.activeElement as HTMLElement | null |
| 25 | + } |
| 26 | + }, [isOpen, isTooSmallForCTA]) |
| 27 | + |
| 28 | + const onDismiss = () => { |
| 29 | + if (isTooSmallForCTA) { |
| 30 | + return |
| 31 | + } |
| 32 | + if (previouslyFocused) { |
| 33 | + previouslyFocused.focus() |
| 34 | + } |
| 35 | + dismiss() |
| 36 | + } |
| 37 | + |
| 38 | + useOnEscapePress(onDismiss) |
| 39 | + |
| 40 | + if (isTooSmallForCTA) { |
| 41 | + return null |
| 42 | + } |
| 43 | + |
| 44 | + return ( |
| 45 | + <Popover |
| 46 | + ref={overlayRef} |
| 47 | + role="alertdialog" |
| 48 | + aria-modal="true" |
| 49 | + aria-labelledby="ai-search-cta-heading" |
| 50 | + aria-describedby="ai-search-cta-description" |
| 51 | + open={isOpen} |
| 52 | + caret={isLargeOrUp ? 'top' : 'top-right'} |
| 53 | + sx={{ |
| 54 | + top: '55px', |
| 55 | + boxShadow: '0 4px 12px rgba(0, 0, 0, 0.1)', |
| 56 | + width: '270px', |
| 57 | + // When in mobile (< large) the search bar collapses to a button on the RHS of the screen |
| 58 | + // To align the popover with the button we need to move it to the left |
| 59 | + marginLeft: isLargeOrUp ? 0 : -235, |
| 60 | + }} |
| 61 | + > |
| 62 | + <Popover.Content |
| 63 | + sx={{ |
| 64 | + width: '270px', |
| 65 | + }} |
| 66 | + > |
| 67 | + <img |
| 68 | + src="/assets/images/search/copilot-action.png" |
| 69 | + width={220} |
| 70 | + alt="The Copilot Icon in front of an explosion of color." |
| 71 | + /> |
| 72 | + <Heading |
| 73 | + as="h2" |
| 74 | + id="ai-search-cta-heading" |
| 75 | + sx={{ |
| 76 | + fontSize: '16px', |
| 77 | + fontWeight: 'bold', |
| 78 | + marginTop: '12px', |
| 79 | + }} |
| 80 | + > |
| 81 | + {t('search.cta.heading')} |
| 82 | + </Heading> |
| 83 | + <Text |
| 84 | + id="ai-search-cta-description" |
| 85 | + sx={{ |
| 86 | + display: 'block', |
| 87 | + fontSize: '15px', |
| 88 | + marginTop: '12px', |
| 89 | + }} |
| 90 | + > |
| 91 | + {t('search.cta.description')} |
| 92 | + </Text> |
| 93 | + <Button |
| 94 | + ref={dismissButtonRef} |
| 95 | + aria-label="Dismiss" |
| 96 | + sx={{ |
| 97 | + marginTop: '16px', |
| 98 | + fontWeight: 'bold', |
| 99 | + }} |
| 100 | + onClick={onDismiss} |
| 101 | + > |
| 102 | + Dismiss |
| 103 | + </Button> |
| 104 | + </Popover.Content> |
| 105 | + </Popover> |
| 106 | + ) |
| 107 | +} |
0 commit comments