Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/core/components/autocomplete/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MouseEvent,
ReactNode,
Ref,
startTransition,
useCallback,
useEffect,
useImperativeHandle,
Expand Down Expand Up @@ -191,7 +192,16 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
const inputElementRef = useRef<HTMLInputElement | null>(null)
const listBoxElementRef = useRef<HTMLDivElement | null>(null)
// Element refs that need to be accessed during render
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null)
const [inputElement, _setInputElement] = useState<HTMLInputElement | null>(null)
/**
* The startTransition wrapper here is to avoid an issue when on React 18 where this error can happen:
* >Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
* This doesn't happen on React 19 due to automatic batching of all state updates, the startTransition wrapper here gives a type of batching for 18 users in a way that still works with 19.
* NOTE: The startTransition wrapper is not needed in UI v4, since the baseline there is React 19.
*/
const setInputElement = useCallback((node: HTMLInputElement | null) => {
startTransition(() => _setInputElement(node))
}, [])

// Value refs
const listFocusedRef = useRef(false)
Expand Down
14 changes: 12 additions & 2 deletions src/core/components/tree/treeItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ToggleArrowRightIcon} from '@sanity/icons'
import {ThemeFontWeightKey} from '@sanity/ui/theme'
import {useCallback, useEffect, useId, useMemo, useRef, useState} from 'react'
import {startTransition, useCallback, useEffect, useId, useMemo, useRef, useState} from 'react'
import {styled} from 'styled-components'

import {Box, BoxProps, Flex, Text} from '../../primitives'
Expand Down Expand Up @@ -65,7 +65,17 @@ export function TreeItem(
weight,
...restProps
} = props
const [rootElement, setRootElement] = useState<HTMLLIElement | null>(null)
const [rootElement, _setRootElement] = useState<HTMLLIElement | null>(null)
/**
* The startTransition wrapper here is to avoid an issue when on React 18 where this error can happen:
* >Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
* This doesn't happen on React 19 due to automatic batching of all state updates, the startTransition wrapper here gives a type of batching for 18 users in a way that still works with 19.
* NOTE: The startTransition wrapper is not needed in UI v4, since the baseline there is React 19.
*/
const setRootElement = useCallback((node: HTMLLIElement | null) => {
startTransition(() => _setRootElement(node))
}, [])

const treeitemRef = useRef<HTMLDivElement | null>(null)
const tree = useTree()
const {path, registerItem, setExpanded, setFocusedElement} = tree
Expand Down