Skip to content

feat: minimumContentPadding prop on KeyboardChatScrollView#1344

Merged
kirillzyusko merged 33 commits intokirillzyusko:mainfrom
trcoffman:feature/blank-size
Mar 10, 2026
Merged

feat: minimumContentPadding prop on KeyboardChatScrollView#1344
kirillzyusko merged 33 commits intokirillzyusko:mainfrom
trcoffman:feature/blank-size

Conversation

@trcoffman
Copy link
Copy Markdown
Contributor

@trcoffman trcoffman commented Mar 5, 2026

📜 Description

Added new minimumContentPadding prop to KeyboardChatScrollView. This is to provide a mechanism for positioning the latest user message at the top of the screen when building AI chat apps with streaming responses.

💡 Motivation and Context

Schematically all the process can be shown as this:

image

The main idea is that minimumContentPadding adds a scrollable range in ScrollView, so that "just sent" message can be scrolled up and be visible from the top (typical functionality in AI apps).

However we need to define a strategy how new property reacts when keyboard appears/disappears. For that we define two stategies:

  • minimumContentPadding > extraContentPadding + keyboardHeight - content position is not changing in this case
  • minimumContentPadding ><extraContentPadding + keyboardHeight - content position is changes by delta between left/right operand.

This is a typical AI flow. Both ChatGPT/Claude works in this way, so I don't think we need to customize it somehow (maybe in future, but not now).

Closes #1333

📢 Changelog

Docs

  • added docs about new props;

JS

  • added applyWorkaroundForContentInsetHitTestBug prop;
  • added minimumContentPadding prop;
  • cover new functionality with unit tests;
  • supply separate indicatorInsets props to have a better control of scroll indicator position.

iOS

  • implemented code applyWorkaroundForContentInsetHitTestBug prop;

Android

  • added no-op setter for applyWorkaroundForContentInsetHitTestBug` prop;

🤔 How Has This Been Tested?

Manually testing on iOS and Android with the AI LegendList Chat example that I included in this PR.

You can type 'a', 'b', 'c', or 'd' in the chat to get shorter vs longer replies from the AI to test how things work with the blankSize.

Tested on:

  • Pixel 7 Pro;
  • iPhone 17 Pro (iOS 26.2).

📸 Screenshots (if appropriate):

Simulator.Screen.Recording.-.iPhone.16.Pro.-.2026-03-03.at.16.31.29.mp4
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2026-03-03.at.16.32.00.mp4
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2026-03-03.at.16.32.50.mp4
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2026-03-03.at.16.33.04.mp4
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2026-03-03.at.16.34.52.mp4

📝 Checklist

  • CI successfully passed
  • I added new mocks and corresponding unit-tests if library API was changed

@kirillzyusko kirillzyusko self-assigned this Mar 5, 2026
@kirillzyusko kirillzyusko self-requested a review March 5, 2026 19:44
@kirillzyusko kirillzyusko added enhancement New feature or request KeyboardChatScrollView 💬 Anything about chat functionality labels Mar 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 5, 2026

📊 Package size report

Current size Target Size Difference
299974 bytes 286926 bytes 13048 bytes 📈

@trcoffman trcoffman force-pushed the feature/blank-size branch 2 times, most recently from 06cc264 to 629b694 Compare March 5, 2026 22:35
Comment thread ios/views/ClippingScrollViewDecoratorViewManager.mm
Comment thread src/components/KeyboardChatScrollView/useChatKeyboard/__fixtures__/testUtils.ts Outdated
Comment thread src/components/KeyboardChatScrollView/index.tsx Outdated
Comment thread src/components/KeyboardChatScrollView/index.tsx Outdated
Comment thread src/components/KeyboardChatScrollView/types.ts
Comment thread src/components/ScrollViewWithBottomPadding/index.tsx Outdated
Comment thread FabricExample/src/screens/Examples/AILegendListChat/index.tsx Outdated
Comment thread FabricExample/src/screens/Examples/AILegendListChat/index.tsx Outdated
Comment thread FabricExample/package.json Outdated
@kirillzyusko kirillzyusko added the 🍎 iOS iOS specific label Mar 6, 2026
@trcoffman trcoffman force-pushed the feature/blank-size branch 2 times, most recently from fb9d88f to 50a444c Compare March 6, 2026 23:59
@kirillzyusko kirillzyusko marked this pull request as ready for review March 7, 2026 22:48
@kirillzyusko kirillzyusko added documentation Improvements or additions to documentation 🤖 android Android specific labels Mar 7, 2026
@argos-ci
Copy link
Copy Markdown

argos-ci Bot commented Mar 7, 2026

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
default (Inspect) ⚠️ Changes detected (Review) 1 changed Mar 10, 2026, 9:55 AM

Comment thread docs/docs/api/components/keyboard-chat-scroll-view.mdx Outdated
Comment thread docs/docs/api/components/keyboard-chat-scroll-view.mdx Outdated
Comment thread src/components/KeyboardChatScrollView/useChatKeyboard/__fixtures__/testUtils.ts Outdated
Comment thread src/components/KeyboardChatScrollView/useChatKeyboard/index.ios.ts Outdated
Comment thread src/components/KeyboardChatScrollView/useChatKeyboard/index.ios.ts Outdated
Comment thread src/components/KeyboardChatScrollView/useChatKeyboard/index.ts Outdated
Comment thread src/components/KeyboardChatScrollView/useChatKeyboard/index.ts Outdated
Comment thread src/components/KeyboardChatScrollView/useChatKeyboard/index.ts
Comment thread src/components/KeyboardChatScrollView/index.tsx Outdated
Comment thread FabricExample/src/screens/Examples/KeyboardChatScrollView/config.tsx Outdated
@kirillzyusko kirillzyusko changed the title DRAFT: Feature: blankSize prop on KeyboardChatScrollView feat: minimumContentPadding prop on KeyboardChatScrollView Mar 7, 2026
trcoffman added 12 commits March 9, 2026 10:10
Add a blankSize prop that will ensure that it's possible
to put the latest user message right at the top of the screen
when building an AI chat app.

KeyboardChatLegendList: Fix imports after reverting to beta.32
Fix Android keyboard close/open asymmetry and apply binary blank absorption

Two changes to the Android useChatKeyboard hook:

1. Apply the same binary blank absorption from the iOS fix: only absorb
   blank space when fully visible (visibleFraction >= 1), otherwise
   shift content by the full keyboard amount. Previously Android used
   linear scaling which caused partial occlusion when blank was
   partially visible.

2. Fix close animation overshooting on non-inverted lists. The close
   path was computing the theoretical scroll displacement (keyboard
   height minus blank absorption) to undo the open shift. But when
   the open shift was clamped by maxScroll, the theoretical value
   exceeded the actual shift, causing close to move content down
   more than open moved it up.

   Record the actual scroll displacement at the end of the open
   animation (scroll.value - offsetBeforeScroll) and use that for
   the close calculation. This makes open/close symmetric regardless
   of clamping.
…lView

Previously, scrollIndicatorInsets matched contentInset, which included
blankSize. This caused the scroll indicator to end halfway down the
screen when blankSize was large.

Add a scrollIndicatorPadding prop to ScrollViewWithBottomPadding that
computes indicator insets from keyboard + extraContentPadding only,
excluding blankSize. User-supplied scrollIndicatorInsets.bottom/top
are now additive adjustments (previously silently ignored).

Update AILegendListChat example to pass a negative safe area bottom
adjustment for apps that render into the unsafe area.
… iOS

Implements a runtime swizzling fix for React Native 0.81+ bug where
ScrollView's contentInset area fails to respond to touch events. This
prevents users from initiating scroll gestures by touching the inset
padding area, which is critical for chat UX patterns where contentInset
positions messages at the top of the screen.

## Problem

React Native's RCTScrollViewComponentView.betterHitTest returns the
container view (self) instead of the underlying UIScrollView instance
when the touch is in the contentInset area. This causes touch events
to be intercepted by the container, preventing the scroll view from
receiving gestures.

Upstream issue: facebook/react-native#54123

## Solution

Uses Objective-C runtime method swizzling to override hitTest:withEvent:
on the ScrollView's container view. The fix mirrors the upstream patch:

- Calls the original hitTest:withEvent: implementation
- When the result is self (the container) - which is the bug - dynamically
  finds and returns the UIScrollView child instead
- Otherwise preserves the original result (subviews, nil, etc.)

Key implementation detail: the UIScrollView is looked up dynamically at
hit-test time rather than captured during swizzling. This ensures the
fix works across React Native hot reloads and full refreshes where the
view hierarchy is recreated but the swizzled class persists.

Implementation follows the same pattern as PR kirillzyusko#1336's scrollRectToVisible
fix: dynamic subclass creation with idempotent prefix check, applied lazily
in didMoveToWindow.

## Benefits

✅ Users can now scroll by touching contentInset area
✅ Interactive content (Pressables, buttons, inputs) continues to work normally
✅ Survives React Native hot reloads and full refreshes
✅ Fixes chat UX where messages need top positioning via contentInset
✅ Non-breaking: defensive implementation returns early if structure unexpected
✅ Compatible with existing scrollRectToVisible noop fix
✅ Zero-cost for apps not using ClippingScrollView
✅ Provides immediate relief while waiting for upstream React Native fix

## Performance Considerations

The dynamic scrollView lookup only executes when:
- The original hitTest returns the container (empty space/contentInset area)
- NOT when touching actual content (messages, buttons, etc.)

Cost: iterating through container's direct subviews (typically 1-3 views)
- UIScrollView + maybe scroll indicators
- Just pointer comparisons and class checks
- Negligible performance impact

## Risks and Considerations

⚠️ Runtime swizzling: Fragile if React Native's view hierarchy changes
   Mitigation: Defensive checks, silently returns if container not found

⚠️ Dynamic lookup on every hit test in contentInset area: Small overhead
   Mitigation: Only 1-3 subviews to check, only runs for empty space touches

⚠️ React Native version variations: Different RN versions may have different hierarchies
   Mitigation: Graceful degradation - no fix applied but doesn't break

⚠️ Calling original implementation: Assumes it's safe to invoke
   Mitigation: Standard ObjC pattern, same lifetime guarantees as the view

## Testing Recommendations

- Test contentInset with top/bottom/left/right values
- Verify touch in contentInset area initiates scroll
- Verify interactive elements (Pressables, buttons, inputs) remain functional
- Test with inverted ScrollViews (chat pattern)
- Verify behavior persists after hot reload
- Verify behavior persists after full refresh
- Test keyboard interactions remain functional
- Verify no console warnings
- Test on iOS 17+ with React Native 0.81+

## Technical Details

Implementation in ClippingScrollViewDecoratorViewManager.mm:
- Added KCApplyFixedHitTest() function that swizzles hitTest:withEvent:
- Modified didMoveToWindow() to apply both fixes
- Captures original IMP and calls it via function pointer
- Conditional: if (result == self) lookup and return UIScrollView child
- Dynamic lookup avoids stale references across RN refreshes
- Uses KC_FixedHitTest_ prefix for idempotent check
@trcoffman trcoffman force-pushed the feature/blank-size branch from a7f010f to 1b2d38e Compare March 9, 2026 18:26
@kirillzyusko kirillzyusko added the tests You added or changed unit tests label Mar 10, 2026
@kirillzyusko
Copy link
Copy Markdown
Owner

This is amazing! I can't express how grateful I am to you for making this a reality! ❤️

@kirillzyusko kirillzyusko merged commit 7f32767 into kirillzyusko:main Mar 10, 2026
34 of 36 checks passed
@kirillzyusko kirillzyusko added the 🚨 requires API changes 🚨 Changes that requires changes in library API label Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖 android Android specific documentation Improvements or additions to documentation enhancement New feature or request 🍎 iOS iOS specific KeyboardChatScrollView 💬 Anything about chat functionality 🚨 requires API changes 🚨 Changes that requires changes in library API tests You added or changed unit tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants