Skip to content

Commit 2651fd0

Browse files
fix(rewards): keep referral dialogs above destination modals (#47682)
## **Description** Referral invitations can compete with destination dialogs for focus and dismissal events. Keep Terms of Use first, then show the invite and activation sheets above ordinary destination dialogs using a small native `<dialog>` wrapper. Other dialogs remain mounted underneath. The wrapper owns the shared sheet container and preserves MMDS styling, isolates Escape and mouse events, and only dismisses backdrop gestures that also started on the backdrop. The documented `data-no-focus-lock` marker prevents background focus locks with `autoFocus={false}` from blurring native-dialog inputs. Two narrow integration guards cover the existing exceptions: Token Insights leaves clicks in another native dialog untouched, and the global drawer waits through native-dialog handoffs. The header also holds drawer requests while Terms is displayed. Background menus may close during referral interactions. Both referral dialogs use visible titles as accessible names, with an existing localized label as the invite-title fallback when server copy is missing. Native-dialog lifecycle shims live in setup shared by unit and integration tests. **Scope: six production files and six test/setup files; 83 net production lines.** Stacked on #47665. MMDS does not expose priority focus handling, so [native modality](https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element) is an intentional local adapter. No dependency changes. Validation: **71 unit tests across seven suites and 101 integration tests across 26 suites passed** (one existing integration test remains skipped). Changed-file lint, full TypeScript checking, and circular dependency checks passed. The invite's accessible-name assertion failed before the naming fix and passed afterward, including the missing-server-copy fallback. The previously failing notification integration suites pass with the shared native-dialog setup. Earlier Chromium and Firefox harness checks covered focus, backdrop gestures, Escape, queued opening, cancellation, and invite-to-activation handoff. Full Extension flow verification remains pending. ## **Changelog** CHANGELOG entry: Fixed referral dialogs losing focus behind destination dialogs while preserving Terms of Use priority. ## **Related issues** Related: #47665 ## **Manual testing steps** 1. With an eligible wallet, enable the referral feature and leave Terms-of-Use acceptance pending. Open a supported deep link to Home with one valid `ref` code. Verify Terms appears first, with the invite waiting until acceptance. A drawer requested through `drawerOpen=true` must also wait. 2. Open a referral over a destination dialog, including Token Insights and a dialog that appears after data loads. Edit the code and use Tab to verify focus stays in the referral. Selecting text and releasing over the backdrop must keep the invite open. 3. Press Escape or click the backdrop. Verify only the referral closes and the destination dialog remains open. Background menus are allowed to close. 4. Accept an invite. Verify the activation sheet stays above the destination dialog and focuses its primary action. A queued global drawer must wait until the activation sheet closes, then receive focus. 5. Repeat the deferred install/onboarding path and check the behavior in both Chrome and Firefox. ## **Screenshots/Recordings** ToS working: [terms-before-referral.webm](https://github.com/user-attachments/assets/78e9ac45-e4c3-4b15-88d1-0b1b4a3f9b87) Referral modal stays on top of others: [697762543-7e7eabbb-ce32-4be9-9842-ad9c89616865.webm](https://github.com/user-attachments/assets/0ea07a3a-c7e3-437f-b1d1-f74ea7b95f28) <!-- Full Extension flow verification pending. Component behavior was checked in isolated Chromium and Firefox harnesses. ### **Before** ### **After** --> ## **Pre-merge author checklist** - [x] I've followed MetaMask Contributor Docs and MetaMask Extension Coding Standards. - [x] I've completed the PR template to the best of my ability. - [x] I've included tests where applicable. - [x] I've documented the native-dialog rationale and focus-lock interaction. - [x] I've applied the right labels on the PR. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR. - [ ] I confirm that this PR addresses the described behavior and includes the necessary testing evidence. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Touches overlay stacking, focus, and dismissal across referral flows, global drawer, and Token Insights—behavior-sensitive UI with moderate integration surface but well-tested. > > **Overview** > Introduces a shared **`ReferralDialog`** native `<dialog>` wrapper so money-referral invite and activation sheets sit above ordinary MMDS modals without losing focus or leaking Escape/backdrop events to the UI underneath. Invite and activated flows keep MMDS header/body/footer inside the wrapper, add **`aria-labelledby`** headings, and fall back the invite title to the localized referral-code label when server copy is missing. > > **Global menu drawer** now defers `showModal()` until no other open native dialog remains (and cancels deferred opens on unmount), marks **`data-no-focus-lock`**, and the app header withholds drawer open requests on home while **Terms of Use** is up. **Token Insights** ignores document outside-click handling when the press targets another open native dialog. > > Jest gets shared **`HTMLDialogElement.showModal`/`close`** shims; unit tests cover dialog focus/isolation, drawer queuing, Terms gating, and referral a11y assertions. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 94bf01c. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Baptiste Marchand <75846779+baptiste-marchand@users.noreply.github.com>
1 parent 6870129 commit 2651fd0

12 files changed

Lines changed: 465 additions & 205 deletions

‎test/helpers/setup-after-helper.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
import nock from 'nock';
44
import '@testing-library/jest-dom';
55

6+
// JSDOM does not implement native dialog lifecycle or browser focus isolation.
7+
if (globalThis.HTMLDialogElement) {
8+
globalThis.HTMLDialogElement.prototype.showModal = function () {
9+
this.open = true;
10+
};
11+
globalThis.HTMLDialogElement.prototype.close = function () {
12+
if (this.open) {
13+
this.open = false;
14+
this.dispatchEvent(new Event('close'));
15+
}
16+
};
17+
}
18+
619
jest.mock('webextension-polyfill', () => {
720
return {
821
runtime: {

‎ui/components/app/rewards/money-referral/AcceptInviteSheet.test.tsx‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ describe('AcceptInviteSheet', () => {
213213
});
214214

215215
it('prefills the route code and lets the user edit it', () => {
216-
renderSheet({ initialCode: 'ab-12!' });
216+
const view = renderSheet({ initialCode: 'ab-12!' });
217+
218+
expect(
219+
view.getByRole('dialog', { name: copy.inviteTitle }),
220+
).toBeInTheDocument();
217221

218222
const input = document.querySelector(
219223
'#money-referral-code',
@@ -256,7 +260,14 @@ describe('AcceptInviteSheet', () => {
256260
expect(onClose).not.toHaveBeenCalled();
257261
expect(view.getByTestId('money-referral-invite')).toBeInTheDocument();
258262
expect(
259-
view.getByText(messages.rewardsMoneyReferralCodeLabel.message),
263+
view.getByRole('textbox', {
264+
name: messages.rewardsMoneyReferralCodeLabel.message,
265+
}),
266+
).toBeInTheDocument();
267+
expect(
268+
view.getByRole('dialog', {
269+
name: messages.rewardsMoneyReferralCodeLabel.message,
270+
}),
260271
).toBeInTheDocument();
261272
expect(
262273
view.getByRole('button', { name: messages.continue.message }),

‎ui/components/app/rewards/money-referral/AcceptInviteSheet.tsx‎

Lines changed: 91 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect, useRef, useState } from 'react';
1+
import React, { useCallback, useEffect, useId, useRef, useState } from 'react';
22
import { useSelector } from 'react-redux';
33
import {
44
Box,
@@ -9,14 +9,11 @@ import {
99
HelpText,
1010
HelpTextSeverity,
1111
Label,
12-
Modal,
1312
ModalBody,
14-
ModalContent,
15-
ModalContentSize,
1613
ModalFooter,
1714
ModalHeader,
18-
ModalOverlay,
1915
Text,
16+
TextAlign,
2017
TextColor,
2118
TextField,
2219
TextFieldSize,
@@ -48,6 +45,7 @@ import {
4845
import { useReferralMe } from '../../../../hooks/rewards/useReferralMe';
4946
import { REWARDS_MONEY_INVITE_ALLOWED_CAPABILITIES } from '../../../../hooks/rewards/rewards-money-messenger';
5047
import { RouteMessengerProvider } from '../../../../contexts/route-messenger';
48+
import { ReferralDialog } from './ReferralDialog';
5149
import { ReferralActivatedModal } from './ReferralActivatedModal';
5250

5351
const MONEY_REFERRAL_CODE_ERROR_ID = 'money-referral-code-error';
@@ -57,8 +55,8 @@ type Translate = (key: string) => string;
5755
/**
5856
* Invite copy, resolved per key.
5957
*
60-
* The server owns this screen's words. A missing key means referral me never
61-
* arrived. Title and body stay empty, and the body is omitted. The buttons
58+
* The server owns this screen's words. A missing title falls back to the
59+
* localized referral-code label, and a missing body is omitted. The buttons
6260
* reuse strings the app already has. The code label uses
6361
* `rewardsMoneyReferralCodeLabel`, the same words as mobile's referral-code
6462
* string, which extension did not already have.
@@ -72,7 +70,7 @@ function inviteCopy(
7270
t: Translate,
7371
) {
7472
return {
75-
title: localizedText?.inviteTitle ?? '',
73+
title: localizedText?.inviteTitle || t('rewardsMoneyReferralCodeLabel'),
7674
body: localizedText?.inviteMessageBody ?? '',
7775
codeLabel:
7876
localizedText?.inviteReferralCode ?? t('rewardsMoneyReferralCodeLabel'),
@@ -98,6 +96,7 @@ const AcceptInviteSheetContent = ({
9896
initialCode,
9997
onClose,
10098
}: AcceptInviteSheetProps) => {
99+
const titleId = useId();
101100
const t = useI18nContext();
102101
const { trackEvent, createEventBuilder } = useAnalytics();
103102
const { referralMe, isSettled, fetchReferralMe } = useReferralMe();
@@ -319,91 +318,94 @@ const AcceptInviteSheetContent = ({
319318
isValid && acceptAllowedForGeo && !isAccepting && !isAcceptCoolingDown;
320319

321320
return (
322-
<Modal isOpen onClose={handleDismiss} data-testid="money-referral-invite">
323-
<ModalOverlay />
324-
<ModalContent className="items-center" size={ModalContentSize.Md}>
325-
<ModalHeader
326-
onClose={handleDismiss}
327-
closeButtonProps={{ ariaLabel: t('close'), isDisabled: isAccepting }}
321+
<ReferralDialog
322+
onClose={handleDismiss}
323+
data-testid="money-referral-invite"
324+
aria-labelledby={titleId}
325+
>
326+
<ModalHeader
327+
onClose={handleDismiss}
328+
closeButtonProps={{ ariaLabel: t('close'), isDisabled: isAccepting }}
329+
>
330+
<Text
331+
asChild
332+
variant={TextVariant.HeadingSm}
333+
textAlign={TextAlign.Center}
328334
>
329-
{copy.title}
330-
</ModalHeader>
331-
<ModalBody>
332-
<Box flexDirection={BoxFlexDirection.Column} gap={4}>
333-
{copy.body ? (
334-
<Text
335-
variant={TextVariant.BodyMd}
336-
color={TextColor.TextAlternative}
337-
>
338-
{copy.body}
339-
</Text>
340-
) : null}
341-
<Box flexDirection={BoxFlexDirection.Column} gap={2}>
342-
<Label
343-
htmlFor="money-referral-code"
344-
color={TextColor.TextAlternative}
345-
>
346-
{copy.codeLabel}
347-
</Label>
348-
<TextField
349-
id="money-referral-code"
350-
value={code}
351-
size={TextFieldSize.Lg}
352-
isError={Boolean(fieldError)}
353-
isDisabled={isAccepting}
354-
className="h-14 w-full"
355-
onChange={(event) => {
356-
setCode(normalizeMoneyReferralCode(event.target.value));
357-
clearError();
358-
}}
359-
inputProps={{
360-
...referralCodeInputProps,
361-
'aria-describedby': fieldError
362-
? MONEY_REFERRAL_CODE_ERROR_ID
363-
: undefined,
364-
}}
365-
/>
366-
{fieldError ? (
367-
<HelpText severity={HelpTextSeverity.Danger} asChild>
368-
<span id={MONEY_REFERRAL_CODE_ERROR_ID} role="alert">
369-
{fieldError}
370-
</span>
371-
</HelpText>
372-
) : null}
373-
</Box>
374-
</Box>
375-
</ModalBody>
376-
<ModalFooter>
377-
<Box
378-
flexDirection={BoxFlexDirection.Column}
379-
gap={2}
380-
className="w-full"
381-
>
382-
<Button
383-
variant={ButtonVariant.Primary}
384-
size={ButtonSize.Lg}
385-
isFullWidth
386-
isDisabled={!canAccept}
387-
onClick={handleAccept}
388-
data-testid="money-referral-accept"
335+
<h2 id={titleId}>{copy.title}</h2>
336+
</Text>
337+
</ModalHeader>
338+
<ModalBody>
339+
<Box flexDirection={BoxFlexDirection.Column} gap={4}>
340+
{copy.body ? (
341+
<Text
342+
variant={TextVariant.BodyMd}
343+
color={TextColor.TextAlternative}
389344
>
390-
{copy.accept}
391-
</Button>
392-
<Button
393-
variant={ButtonVariant.Tertiary}
394-
size={ButtonSize.Lg}
395-
isFullWidth
396-
onClick={handleDecline}
397-
isDisabled={isAccepting}
398-
className="text-default"
399-
data-testid="money-referral-decline"
345+
{copy.body}
346+
</Text>
347+
) : null}
348+
<Box flexDirection={BoxFlexDirection.Column} gap={2}>
349+
<Label
350+
htmlFor="money-referral-code"
351+
color={TextColor.TextAlternative}
400352
>
401-
{copy.decline}
402-
</Button>
353+
{copy.codeLabel}
354+
</Label>
355+
<TextField
356+
id="money-referral-code"
357+
value={code}
358+
size={TextFieldSize.Lg}
359+
isError={Boolean(fieldError)}
360+
isDisabled={isAccepting}
361+
className="h-14 w-full"
362+
onChange={(event) => {
363+
setCode(normalizeMoneyReferralCode(event.target.value));
364+
clearError();
365+
}}
366+
inputProps={{
367+
...referralCodeInputProps,
368+
'aria-describedby': fieldError
369+
? MONEY_REFERRAL_CODE_ERROR_ID
370+
: undefined,
371+
}}
372+
/>
373+
{fieldError ? (
374+
<HelpText severity={HelpTextSeverity.Danger} asChild>
375+
<span id={MONEY_REFERRAL_CODE_ERROR_ID} role="alert">
376+
{fieldError}
377+
</span>
378+
</HelpText>
379+
) : null}
403380
</Box>
404-
</ModalFooter>
405-
</ModalContent>
406-
</Modal>
381+
</Box>
382+
</ModalBody>
383+
<ModalFooter>
384+
<Box flexDirection={BoxFlexDirection.Column} gap={2} className="w-full">
385+
<Button
386+
variant={ButtonVariant.Primary}
387+
size={ButtonSize.Lg}
388+
isFullWidth
389+
isDisabled={!canAccept}
390+
onClick={handleAccept}
391+
data-testid="money-referral-accept"
392+
>
393+
{copy.accept}
394+
</Button>
395+
<Button
396+
variant={ButtonVariant.Tertiary}
397+
size={ButtonSize.Lg}
398+
isFullWidth
399+
onClick={handleDecline}
400+
isDisabled={isAccepting}
401+
className="text-default"
402+
data-testid="money-referral-decline"
403+
>
404+
{copy.decline}
405+
</Button>
406+
</Box>
407+
</ModalFooter>
408+
</ReferralDialog>
407409
);
408410
};
409411

0 commit comments

Comments
 (0)