Unverified Commit 684c7c01 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[devtools] Use `useEffectEvent` instead of ignoring exhaustive deps (#83236)

parent fe19e10c
...@@ -17,7 +17,7 @@ function shouldIgnorePath(modulePath) { ...@@ -17,7 +17,7 @@ function shouldIgnorePath(modulePath) {
* @returns {webpack.Configuration} * @returns {webpack.Configuration}
*/ */
module.exports = ({ dev, ...rest }) => { module.exports = ({ dev, ...rest }) => {
const experimental = false const experimental = true
const bundledReactChannel = experimental ? '-experimental' : '' const bundledReactChannel = experimental ? '-experimental' : ''
......
...@@ -2,6 +2,7 @@ import { ...@@ -2,6 +2,7 @@ import {
createContext, createContext,
useCallback, useCallback,
useContext, useContext,
experimental_useEffectEvent as useEffectEvent,
useLayoutEffect, useLayoutEffect,
useState, useState,
type RefObject, type RefObject,
...@@ -71,8 +72,9 @@ export const ResizeProvider = ({ value, children }: ResizeProviderProps) => { ...@@ -71,8 +72,9 @@ export const ResizeProvider = ({ value, children }: ResizeProviderProps) => {
const storageKey = value.storageKey ?? STORE_KEY_SHARED_PANEL_SIZE const storageKey = value.storageKey ?? STORE_KEY_SHARED_PANEL_SIZE
const { resizeRef } = value
const applyConstrainedDimensions = useCallback(() => { const applyConstrainedDimensions = useCallback(() => {
if (!value.resizeRef.current) return if (!resizeRef.current) return
// this feels weird to read local storage on resize, but we don't // this feels weird to read local storage on resize, but we don't
// track the dimensions of the container, and this is better than // track the dimensions of the container, and this is better than
...@@ -96,11 +98,11 @@ export const ResizeProvider = ({ value, children }: ResizeProviderProps) => { ...@@ -96,11 +98,11 @@ export const ResizeProvider = ({ value, children }: ResizeProviderProps) => {
minHeight: minHeight ?? 80, minHeight: minHeight ?? 80,
}) })
value.resizeRef.current.style.width = `${width}px` resizeRef.current.style.width = `${width}px`
value.resizeRef.current.style.height = `${height}px` resizeRef.current.style.height = `${height}px`
return true return true
}, [ }, [
value.resizeRef, resizeRef,
draggingDirection, draggingDirection,
storageKey, storageKey,
minWidth, minWidth,
...@@ -108,11 +110,11 @@ export const ResizeProvider = ({ value, children }: ResizeProviderProps) => { ...@@ -108,11 +110,11 @@ export const ResizeProvider = ({ value, children }: ResizeProviderProps) => {
value.devToolsPanelSize, value.devToolsPanelSize,
]) ])
useLayoutEffect(() => { const fireInitialConstrainDimensions = useEffectEvent(() => {
const applied = applyConstrainedDimensions() const applied = applyConstrainedDimensions()
if ( if (
!applied && !applied &&
value.resizeRef.current && resizeRef.current &&
value.initialSize?.height && value.initialSize?.height &&
value.initialSize.width value.initialSize.width
) { ) {
...@@ -122,10 +124,14 @@ export const ResizeProvider = ({ value, children }: ResizeProviderProps) => { ...@@ -122,10 +124,14 @@ export const ResizeProvider = ({ value, children }: ResizeProviderProps) => {
minWidth: minWidth ?? 100, minWidth: minWidth ?? 100,
minHeight: minHeight ?? 80, minHeight: minHeight ?? 80,
}) })
value.resizeRef.current.style.width = `${width}px` resizeRef.current.style.width = `${width}px`
value.resizeRef.current.style.height = `${height}px` resizeRef.current.style.height = `${height}px`
} }
// eslint-disable-next-line react-hooks/exhaustive-deps })
useLayoutEffect(() => {
// eslint-disable-next-line react-hooks/rules-of-hooks -- eprh bug
fireInitialConstrainDimensions()
}, []) }, [])
useLayoutEffect(() => { useLayoutEffect(() => {
......
import { useEffect } from 'react' import { useEffect, experimental_useEffectEvent as useEffectEvent } from 'react'
export function useFocusTrap( export function useFocusTrap(
rootRef: React.RefObject<HTMLElement | null>, rootRef: React.RefObject<HTMLElement | null>,
...@@ -6,6 +6,13 @@ export function useFocusTrap( ...@@ -6,6 +6,13 @@ export function useFocusTrap(
active: boolean, active: boolean,
onOpenFocus?: () => void onOpenFocus?: () => void
) { ) {
const fireOpenFocus = useEffectEvent((rootNode: HTMLElement | null) => {
if (onOpenFocus) {
onOpenFocus()
} else {
rootNode?.focus()
}
})
useEffect(() => { useEffect(() => {
let rootNode: HTMLElement | null = null let rootNode: HTMLElement | null = null
...@@ -35,11 +42,7 @@ export function useFocusTrap( ...@@ -35,11 +42,7 @@ export function useFocusTrap(
// Grab this on next tick to ensure the content is mounted // Grab this on next tick to ensure the content is mounted
rootNode = rootRef.current rootNode = rootRef.current
if (active) { if (active) {
if (onOpenFocus) { fireOpenFocus(rootNode)
onOpenFocus()
} else {
rootNode?.focus()
}
rootNode?.addEventListener('keydown', onTab) rootNode?.addEventListener('keydown', onTab)
} else { } else {
const activeElement = getActiveElement(rootNode) const activeElement = getActiveElement(rootNode)
...@@ -56,8 +59,7 @@ export function useFocusTrap( ...@@ -56,8 +59,7 @@ export function useFocusTrap(
clearTimeout(id) clearTimeout(id)
rootNode?.removeEventListener('keydown', onTab) rootNode?.removeEventListener('keydown', onTab)
} }
// eslint-disable-next-line react-hooks/exhaustive-deps }, [active, rootRef, triggerRef])
}, [active])
} }
export function getActiveElement(node: HTMLElement | null) { export function getActiveElement(node: HTMLElement | null) {
......
import { useDevOverlayContext } from '../../dev-overlay.browser' import { useDevOverlayContext } from '../../dev-overlay.browser'
import { useClickOutsideAndEscape } from '../components/errors/dev-tools-indicator/utils' import { useClickOutsideAndEscape } from '../components/errors/dev-tools-indicator/utils'
import { import {
experimental_useEffectEvent as useEffectEvent,
useLayoutEffect, useLayoutEffect,
useRef, useRef,
createContext, createContext,
...@@ -134,14 +135,18 @@ export const DevtoolMenu = ({ ...@@ -134,14 +135,18 @@ export const DevtoolMenu = ({
} }
} }
) )
useLayoutEffect(() => { const fireInitialSelectMenuItem = useEffectEvent(() => {
menuRef.current?.focus() // allows keydown to be captured
selectMenuItem({ selectMenuItem({
index: selectedIndex === -1 ? 'first' : selectedIndex, index: selectedIndex === -1 ? 'first' : selectedIndex,
menuRef, menuRef,
setSelectedIndex, setSelectedIndex,
}) })
// eslint-disable-next-line react-hooks/exhaustive-deps })
useLayoutEffect(() => {
menuRef.current?.focus() // allows keydown to be captured
// eslint-disable-next-line react-hooks/rules-of-hooks -- eprh bug
fireInitialSelectMenuItem()
}, []) }, [])
const indicatorOffset = getIndicatorOffset(state) const indicatorOffset = getIndicatorOffset(state)
......
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