Unverified Commit be31c44f authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[dev-overlay]: remove dependency on platform for focus trapping (#76849)

We currently vendor `platform` and `medialize/ally` to tackle focus
trapping in the error overlay. We already use a custom focus trap util
for most cases such as the indicator / popover, so there isn't much
reason to have forked handling here, and `platform` is quite large.


https://github.com/user-attachments/assets/1bc8a065-9892-4ce1-90a3-c714479dc236

Closes NDX-877
parent dc52e7ec
......@@ -291,7 +291,6 @@
"path-browserify": "1.0.1",
"path-to-regexp": "6.1.0",
"picomatch": "4.0.1",
"platform": "1.3.6",
"postcss-flexbugs-fixes": "5.0.2",
"postcss-modules-extract-imports": "3.0.0",
"postcss-modules-local-by-default": "4.0.4",
......
......@@ -30,7 +30,7 @@ const Dialog: React.FC<DialogProps> = function Dialog({
dialogResizerRef,
...props
}) {
const [dialog, setDialog] = React.useState<HTMLDivElement | null>(null)
const dialogRef = React.useRef<HTMLDivElement | null>(null)
const [role, setRole] = React.useState<string | undefined>(
typeof document !== 'undefined' && document.hasFocus()
? 'dialog'
......@@ -40,17 +40,17 @@ const Dialog: React.FC<DialogProps> = function Dialog({
const ref = React.useRef<HTMLDivElement | null>(null)
const [height, pristine] = useMeasureHeight(ref)
const onDialog = React.useCallback((node: HTMLDivElement | null) => {
setDialog(node)
}, [])
useOnClickOutside(dialog, CSS_SELECTORS_TO_EXCLUDE_ON_CLICK_OUTSIDE, (e) => {
e.preventDefault()
return onClose?.()
})
useOnClickOutside(
dialogRef.current,
CSS_SELECTORS_TO_EXCLUDE_ON_CLICK_OUTSIDE,
(e) => {
e.preventDefault()
return onClose?.()
}
)
React.useEffect(() => {
if (dialog == null) {
if (dialogRef.current == null) {
return
}
......@@ -66,11 +66,12 @@ const Dialog: React.FC<DialogProps> = function Dialog({
window.removeEventListener('focus', handleFocus)
window.removeEventListener('blur', handleFocus)
}
}, [dialog])
}, [])
React.useEffect(() => {
const dialog = dialogRef.current
const root = dialog?.getRootNode()
const activeElement =
const initialActiveElement =
root instanceof ShadowRoot ? (root?.activeElement as HTMLElement) : null
// Trap focus within the dialog
......@@ -80,15 +81,15 @@ const Dialog: React.FC<DialogProps> = function Dialog({
// Blur first to avoid getting stuck, in case `activeElement` is missing
dialog?.blur()
// Restore focus to the previously active element
activeElement?.focus()
initialActiveElement?.focus()
}
}, [dialog])
}, [])
return (
<div
ref={onDialog}
ref={dialogRef}
tabIndex={-1}
data-nextjs-dialog
tabIndex={1}
role={role}
aria-labelledby={ariaLabelledBy}
aria-describedby={ariaDescribedBy}
......
......@@ -2,7 +2,7 @@ import { useEffect } from 'react'
export function useFocusTrap(
rootRef: React.RefObject<HTMLElement | null>,
triggerRef: React.RefObject<HTMLButtonElement | null>,
triggerRef: React.RefObject<HTMLButtonElement | null> | null,
active: boolean,
onOpenFocus?: () => void
) {
......@@ -46,7 +46,7 @@ export function useFocusTrap(
// Only restore focus if the focus was previously on the content.
// This avoids us accidentally focusing on mount when the
// user could want to interact with their own app instead.
if (rootNode?.contains(activeElement)) {
if (triggerRef && rootNode?.contains(activeElement)) {
triggerRef.current?.focus()
}
}
......
......@@ -6,14 +6,6 @@ import { render, screen, fireEvent, act } from '@testing-library/react'
import { ErrorOverlayLayout } from './error-overlay-layout'
import '@testing-library/jest-dom'
// Mock maintain--tab-focus module
jest.mock('../../../components/overlay/maintain--tab-focus', () => ({
__esModule: true,
default: jest.fn(() => ({
disengage: jest.fn(),
})),
}))
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
......
import * as React from 'react'
import type { DebugInfo } from '../../../../types'
import type { ErrorMessageType } from '../error-message/error-message'
import type { ErrorType } from '../error-type-label/error-type-label'
......@@ -33,6 +34,7 @@ import { ErrorOverlayBottomStack } from '../error-overlay-bottom-stack'
import type { ErrorBaseProps } from '../error-overlay/error-overlay'
import type { ReadyRuntimeError } from '../../../../utils/get-error-by-type'
import { EnvironmentNameLabel } from '../environment-name-label/environment-name-label'
import { useFocusTrap } from '../dev-tools-indicator/utils'
interface ErrorOverlayLayoutProps extends ErrorBaseProps {
errorMessage: ErrorMessageType
......@@ -81,10 +83,12 @@ export function ErrorOverlayLayout({
}
const hasFooter = Boolean(footerMessage || errorCode)
const dialogRef = React.useRef<HTMLDivElement | null>(null)
useFocusTrap(dialogRef, null, rendered)
return (
<ErrorOverlayOverlay fixed={isBuildError} {...animationProps}>
<div data-nextjs-dialog-root {...animationProps}>
<div data-nextjs-dialog-root ref={dialogRef} {...animationProps}>
<ErrorOverlayDialog
onClose={onClose}
dialogResizerRef={dialogResizerRef}
......
This source diff could not be displayed because it is too large. You can view the blob instead.
import allyTrap from './maintain--tab-focus'
import * as React from 'react'
import { lock, unlock } from './body-locker'
......@@ -21,29 +20,8 @@ const Overlay: React.FC<OverlayProps> = function Overlay({
}
}, [])
const [overlay, setOverlay] = React.useState<HTMLDivElement | null>(null)
const onOverlay = React.useCallback((el: HTMLDivElement) => {
setOverlay(el)
}, [])
React.useEffect(() => {
if (overlay == null) {
return
}
const handle2 = allyTrap({ context: overlay })
return () => {
handle2.disengage()
}
}, [overlay])
return (
<div
data-nextjs-dialog-overlay
className={className}
ref={onOverlay}
{...props}
>
<div data-nextjs-dialog-overlay className={className} {...props}>
<div
data-nextjs-dialog-backdrop
data-nextjs-dialog-backdrop-fixed={fixed ? true : undefined}
......
Copyright 2014-2020 Benjamin Tan
Copyright 2011-2013 John-David Dalton
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{"name":"platform","main":"platform.js","author":"Benjamin Tan <demoneaux@gmail.com>","license":"MIT"}
This diff is collapsed.
......@@ -287,33 +287,6 @@ export async function ncc_node_shell_quote(task, opts) {
.target('src/compiled/shell-quote')
}
externals['platform'] = 'next/dist/compiled/platform'
externals['next/dist/compiled/platform'] = 'next/dist/compiled/platform'
export async function ncc_node_platform(task, opts) {
await task
.source(relative(__dirname, require.resolve('platform')))
.ncc({ packageName: 'platform', externals })
.target('src/compiled/platform')
const clientFile = join(__dirname, 'src/compiled/platform/platform.js')
const content = await fs.readFile(clientFile, 'utf8')
// remove AMD define branch as this forces the module to not
// be treated as commonjs
await fs.writeFile(
clientFile,
content.replace(
new RegExp(
'if(typeof define=="function"&&typeof define.amd=="object"&&define.amd){r.platform=d;define((function(){return d}))}else '.replace(
/[|\\{}()[\]^$+*?.-]/g,
'\\$&'
),
'g'
),
''
)
)
}
// eslint-disable-next-line camelcase
externals['acorn'] = 'next/dist/compiled/acorn'
export async function ncc_acorn(task, opts) {
......@@ -2304,7 +2277,6 @@ export async function ncc(task, opts) {
'ncc_node_stacktrace_parser',
'ncc_node_data_uri_to_buffer',
'ncc_node_cssescape',
'ncc_node_platform',
'ncc_node_shell_quote',
'ncc_acorn',
'ncc_amphtml_validator',
......
......@@ -673,11 +673,6 @@ declare module 'next/dist/compiled/anser' {
export = m
}
declare module 'next/dist/compiled/platform' {
import * as m from 'platform'
export = m
}
declare module 'next/dist/compiled/css.escape' {
export = CSS.escape
}
......
......@@ -1055,7 +1055,7 @@ importers:
version: 8.6.0(@storybook/test@8.6.0(storybook@8.6.0(prettier@3.3.3)))(react-dom@19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305))(react@19.1.0-canary-e03ac20f-20250305)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)
'@storybook/react-webpack5':
specifier: 8.6.0
version: 8.6.0(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(@storybook/test@8.6.0(storybook@8.6.0(prettier@3.3.3)))(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(react-dom@19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305))(react@19.1.0-canary-e03ac20f-20250305)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)
version: 8.6.0(@storybook/test@8.6.0(storybook@8.6.0(prettier@3.3.3)))(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(react-dom@19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305))(react@19.1.0-canary-e03ac20f-20250305)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)
'@storybook/test':
specifier: 8.6.0
version: 8.6.0(storybook@8.6.0(prettier@3.3.3))
......@@ -1395,9 +1395,6 @@ importers:
picomatch:
specifier: 4.0.1
version: 4.0.1
platform:
specifier: 1.3.6
version: 1.3.6
postcss-flexbugs-fixes:
specifier: 5.0.2
version: 5.0.2(postcss@8.4.31)
......@@ -1445,7 +1442,7 @@ importers:
version: 0.13.4
sass-loader:
specifier: 15.0.0
version: 15.0.0(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(sass@1.77.8)(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2))
version: 15.0.0(sass@1.77.8)(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2))
schema-utils2:
specifier: npm:schema-utils@2.7.1
version: schema-utils@2.7.1
......@@ -13331,9 +13328,6 @@ packages:
resolution: {integrity: sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==}
engines: {node: '>=8'}
platform@1.3.6:
resolution: {integrity: sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==}
playwright-chromium@1.48.0:
resolution: {integrity: sha512-jPFKhB4+zGEcAwc+h1mTLa8L08Rb+FwGKFqd/o7mWSGyxAtwBu5R4QxSG/a8KcHbewOj4DLBUVVK2N/uKrYmiQ==}
engines: {node: '>=18'}
......@@ -19624,7 +19618,7 @@ snapshots:
'@eslint/config-array@0.18.0':
dependencies:
'@eslint/object-schema': 2.1.4
debug: 4.3.7
debug: 4.4.0
minimatch: 3.1.2
transitivePeerDependencies:
- supports-color
......@@ -19662,7 +19656,7 @@ snapshots:
'@eslint/eslintrc@3.1.0':
dependencies:
ajv: 6.12.6
debug: 4.3.7
debug: 4.4.0
espree: 10.2.0
globals: 14.0.0
ignore: 5.3.2
......@@ -21807,7 +21801,7 @@ snapshots:
react: 19.1.0-canary-e03ac20f-20250305
react-dom: 19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305)
'@storybook/builder-webpack5@8.6.0(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)':
'@storybook/builder-webpack5@8.6.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)':
dependencies:
'@storybook/core-webpack': 8.6.0(storybook@8.6.0(prettier@3.3.3))
'@types/semver': 7.5.6
......@@ -21815,10 +21809,10 @@ snapshots:
case-sensitive-paths-webpack-plugin: 2.4.0
cjs-module-lexer: 1.4.1
constants-browserify: 1.0.0
css-loader: 6.11.0(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2))
css-loader: 6.11.0(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2))
es-module-lexer: 1.6.0
fork-ts-checker-webpack-plugin: 8.0.0(typescript@5.8.2)(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2))
html-webpack-plugin: 5.6.3(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2))
html-webpack-plugin: 5.6.3(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2))
magic-string: 0.30.17
path-browserify: 1.0.1
process: 0.11.10
......@@ -21949,9 +21943,9 @@ snapshots:
react-dom: 19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305)
storybook: 8.6.0(prettier@3.3.3)
'@storybook/react-webpack5@8.6.0(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(@storybook/test@8.6.0(storybook@8.6.0(prettier@3.3.3)))(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(react-dom@19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305))(react@19.1.0-canary-e03ac20f-20250305)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)':
'@storybook/react-webpack5@8.6.0(@storybook/test@8.6.0(storybook@8.6.0(prettier@3.3.3)))(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(react-dom@19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305))(react@19.1.0-canary-e03ac20f-20250305)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)':
dependencies:
'@storybook/builder-webpack5': 8.6.0(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)
'@storybook/builder-webpack5': 8.6.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)
'@storybook/preset-react-webpack': 8.6.0(@storybook/test@8.6.0(storybook@8.6.0(prettier@3.3.3)))(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)(react-dom@19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305))(react@19.1.0-canary-e03ac20f-20250305)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)
'@storybook/react': 8.6.0(@storybook/test@8.6.0(storybook@8.6.0(prettier@3.3.3)))(react-dom@19.1.0-canary-e03ac20f-20250305(react@19.1.0-canary-e03ac20f-20250305))(react@19.1.0-canary-e03ac20f-20250305)(storybook@8.6.0(prettier@3.3.3))(typescript@5.8.2)
react: 19.1.0-canary-e03ac20f-20250305
......@@ -23755,7 +23749,7 @@ snapshots:
babel-plugin-macros@3.1.0:
dependencies:
'@babel/runtime': 7.22.5
cosmiconfig: 7.0.0
cosmiconfig: 7.1.0
resolve: 1.22.8
babel-plugin-polyfill-corejs2@0.4.11(@babel/core@7.22.5):
......@@ -24965,7 +24959,7 @@ snapshots:
postcss: 8.4.31
postcss-selector-parser: 6.0.11
css-loader@6.11.0(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)):
css-loader@6.11.0(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)):
dependencies:
icss-utils: 5.1.0(postcss@8.4.49)
postcss: 8.4.49
......@@ -24976,7 +24970,6 @@ snapshots:
postcss-value-parser: 4.2.0
semver: 7.6.3
optionalDependencies:
'@rspack/core': '@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15)'
webpack: 5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)
css-prefers-color-scheme@6.0.3(postcss@8.4.31):
......@@ -26678,7 +26671,7 @@ snapshots:
ajv: 6.12.6
chalk: 4.1.2
cross-spawn: 7.0.3
debug: 4.3.7
debug: 4.4.0
escape-string-regexp: 4.0.0
eslint-scope: 8.1.0
eslint-visitor-keys: 4.1.0
......@@ -28006,7 +27999,7 @@ snapshots:
transitivePeerDependencies:
- supports-color
html-webpack-plugin@5.6.3(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)):
html-webpack-plugin@5.6.3(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)):
dependencies:
'@types/html-minifier-terser': 6.1.0
html-minifier-terser: 6.1.0
......@@ -28014,7 +28007,6 @@ snapshots:
pretty-error: 4.0.0
tapable: 2.2.1
optionalDependencies:
'@rspack/core': '@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15)'
webpack: 5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)
htmlparser2@3.10.1:
......@@ -32045,8 +32037,6 @@ snapshots:
dependencies:
find-up: 3.0.0
platform@1.3.6: {}
playwright-chromium@1.48.0:
dependencies:
playwright-core: 1.48.0
......@@ -33965,11 +33955,10 @@ snapshots:
safer-buffer@2.1.2: {}
sass-loader@15.0.0(@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15))(sass@1.77.8)(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)):
sass-loader@15.0.0(sass@1.77.8)(webpack@5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)):
dependencies:
neo-async: 2.6.2
optionalDependencies:
'@rspack/core': '@rspack-canary/core@1.2.0-canary-37cc738d-20250207113050(@swc/helpers@0.5.15)'
sass: 1.77.8
webpack: 5.98.0(@swc/core@1.9.3(@swc/helpers@0.5.15))(esbuild@0.24.2)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment