import { AdaptPortalContents, useAdaptIsActive } from '@tamagui/adapt' import { AnimatePresence } from '@tamagui/animate-presence' import { useComposedRefs } from '@tamagui/compose-refs' import { isWeb, useIsomorphicLayoutEffect } from '@tamagui/constants' import { createStyledHOC, splitStyleProps, styled, View } from '@tamagui/core' import { needsPortalRepropagation } from '@tamagui/portal' import { YStack } from '@tamagui/stacks' import * as React from 'react' import { VIEWPORT_NAME } from './constants' import { ForwardSelectContext, useSelectContext, useSelectItemParentContext, } from './context' import type { SelectViewportExtraProps, SelectViewportProps } from './types' import { getSelectListboxProps } from './selectionController' /* ------------------------------------------------------------------------------------------------- * SelectViewport * -----------------------------------------------------------------------------------------------*/ export const SelectViewportFrame = styled(View, { displayName: VIEWPORT_NAME, position: 'relative', }) const needsRepropagation = needsPortalRepropagation() // the viewport's styles dress the floating list (a capped height, a card // background, border and shadow). in a sheet the sheet is the surface, so only // the element props carry over and the items fill the sheet. a styled() skin // hands its styles to this HOC as class names (web) or a style (native) const getSheetFrameProps = (props: Record) => { const { className, style, 'data-tamagui-hoc-class': _hocClassNames, ...rest } = props return splitStyleProps(rest)[1] } export const SelectViewport = createStyledHOC( SelectViewportFrame, function SelectViewport(propsIn: SelectViewportExtraProps, forwardedRef) { const props: SelectViewportProps = propsIn const { scope, children, disableScroll, ...viewportProps } = props const context = useSelectContext(scope) const itemContext = useSelectItemParentContext(scope) const isAdapted = useAdaptIsActive(context.adaptScope) const viewportRef = React.useRef(null) const registeredItemCount = itemContext.registry.getItems().length // lazy mount keeps the items out of the tree until the first open, but it // has to happen in the render that opens, not in an effect after it: mounting // a frame later paints an empty popover, then floating-ui positions that empty // box and has to jump once the real height arrives. once mounted it stays. const hasOpenedRef = React.useRef(!context.lazyMount) if (context.open) hasOpenedRef.current = true const lazyMounted = hasOpenedRef.current React.useEffect(() => { if (!isWeb || !isAdapted || !context.open) return const frame = requestAnimationFrame(() => { const index = context.activeIndexRef.current ?? itemContext.registry.firstEnabledIndex() const activeItem = (viewportRef.current?.querySelector( '[role="option"][tabindex="0"]' ) as HTMLElement | null) ?? (index >= 0 ? itemContext.listRef?.current[index] : null) activeItem?.focus() }) return () => cancelAnimationFrame(frame) }, [context.open, isAdapted, registeredItemCount]) const composedRefs = useComposedRefs( // @ts-ignore react 19 ref type mismatch forwardedRef, viewportRef, context.floatingContext?.refs.setFloating as any ) useIsomorphicLayoutEffect(() => { if (context.update) { context.update() } }, [isAdapted]) // items register in layout effects, so the inner middleware can only position // against the selected one once the registry has settled React.useEffect(() => { if (context.lazyMount && context.open && context.update) { const frame = requestAnimationFrame(context.update) return () => cancelAnimationFrame(frame) } }, [context.open, registeredItemCount]) if (itemContext.shouldRenderWebNative) { return {children} } if (isAdapted || !isWeb) { const frameProps = isAdapted ? getSheetFrameProps(viewportProps) : viewportProps let content = ( {lazyMounted ? children : null} ) if (needsRepropagation) { content = ( {content} ) } return ( {content} ) } if (!context.interactions) { return null } const floatingProps = context.interactions.getFloatingProps() // FloatingFocusManager removed — SelectContent already wraps with FocusScope // that handles focus trapping and auto-focus return ( <> {!disableScroll && (