Requires iOS 17 or later. On older versions, the modifier is a no-op.
Track the leading scroll target id from JavaScript and scroll to a target by writing to the state. Mark each scroll target with the id modifier, wrap the content container in scrollTargetLayout, and apply the scrollPosition modifier to the ScrollView. The optional onChange callback fires on the JS thread when the leading target changes.
The scrollPosition modifier also works on other scrollable containers like LazyVStack and LazyHStack.
Writes to state.value must run on the UI runtime. Wrap the write in scheduleOnUI from react-native-worklets, or call them from inside a 'worklet' function. Writes from the JS runtime trip Main Thread Checker, Xcode's runtime tool that flags UIKit calls made from a background thread.
ScrollViewSharedPositionExample.tsx
import{Button,Host,ScrollView,Text,VStack,
useNativeState,}from'@expo/ui/swift-ui';import{
id,
padding,
scrollPosition,
scrollTargetLayout,}from'@expo/ui/swift-ui/modifiers';import{ scheduleOnUI }from'react-native-worklets';exportdefaultfunctionScrollViewSharedPositionExample(){const activeID =useNativeState<string|null>(null);return(<Hoststyle={{ flex:1}}><VStackspacing={12}><ScrollViewmodifiers={[scrollPosition(activeID,{onChange: newID =>{console.log('[JS thread] leading target:', newID);},}),]}><VStackmodifiers={[scrollTargetLayout()]}>{Array.from({ length:30},(_, i)=>(<Textkey={`item-${i}`}modifiers={[id(`item-${i}`),padding({ horizontal:16, vertical:12}),]}>{`Item ${i}`}</Text>))}</VStack></ScrollView><Buttonlabel="Scroll to item 10 from worklet"onPress={()=>{scheduleOnUI(()=>{'worklet';
activeID.value='item-10';});}}/></VStack></Host>);}
Unclipped scroll content
Requires iOS 17 or later. On older versions, the modifier is a no-op.
A scroll view clips its content to its own bounds, which cuts off anything drawn past them, such as a shadow or a card scaled up beyond the edge. Apply the scrollClipDisabled modifier to keep that content visible.
SwiftUI ScrollView wrapper. To control scroll position, pair this with the
scrollPosition(state, { onChange }) modifier and a useNativeState-backed
id. Write state.value = targetId for an instant scroll, or wrap the write
in withAnimation(...) from @expo/ui/swift-ui for an animated one.