Skip to content

Commit

Permalink
RNVisitableView: introduce progressViewOffset property (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
pklatka committed Apr 26, 2024
1 parent 3c03f9d commit 5b7fe1c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/turbo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ The amount by which the web view content is inset from the edges of the scroll v

Note: available only on iOS.

### `progressViewOffset`

The refresh indicator starting and resting position is always positioned near the top of the refreshing content. This position is a consistent location, but can be adjusted in either direction based on whether or not there is a header or other content that should be visible when the refresh indicator is shown.

Note: available only on Android.

### `stradaComponents`

`VisitableView` supports defining [Strada components](https://strada.hotwired.dev/) that receive and reply to messages from web components that are present on the page within one session. This prop accepts an array of Strada components that will be registered in the webview.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.core.view.isVisible
import androidx.lifecycle.findViewTreeLifecycleOwner
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.RCTEventEmitter
import dev.hotwire.turbo.views.TurboView
Expand Down Expand Up @@ -53,6 +54,17 @@ class RNVisitableView(context: Context) : LinearLayout(context), SessionSubscrib
field = value
setPullToRefresh(value)
}
var progressViewOffset: ReadableMap? = null
set(value) {
field = value
progressViewOffset?.let {
turboView.webViewRefresh?.setProgressViewOffset(
it.getBoolean("scale"),
it.getInt("start"),
it.getInt("end")
)
}
}

// Session
private var _session: RNSession? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.reactnativeturbowebview

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
Expand Down Expand Up @@ -62,6 +63,11 @@ class RNVisitableViewManager(
view.scrollEnabled = scrollEnabled
}

@ReactProp(name = "progressViewOffset")
fun setProgressViewOffset(view: RNVisitableView, progressViewOffset: ReadableMap?) {
view.progressViewOffset = progressViewOffset
}

override fun getCommandsMap(): MutableMap<String, Int> = RNVisitableViewCommand.values()
.associate {
it.jsCallbackName to it.ordinal
Expand Down
2 changes: 2 additions & 0 deletions packages/turbo/src/RNVisitableView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
FormSubmissionEvent,
ContentProcessDidTerminateEvent,
ContentInsetObject,
ProgressViewOffsetObject,
} from './types';

// Interface should match RNVisitableView exported properties in native code
Expand All @@ -30,6 +31,7 @@ export interface RNVisitableViewProps {
pullToRefreshEnabled: boolean;
scrollEnabled: boolean;
contentInset: ContentInsetObject;
progressViewOffset?: ProgressViewOffsetObject;
onLoad?: (e: NativeSyntheticEvent<LoadEvent>) => void;
onMessage?: (e: NativeSyntheticEvent<MessageEvent>) => void;
onError?: (e: NativeSyntheticEvent<ErrorEvent>) => void;
Expand Down
4 changes: 4 additions & 0 deletions packages/turbo/src/VisitableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import type {
FormSubmissionEvent,
ContentProcessDidTerminateEvent,
ContentInsetObject,
ProgressViewOffsetObject,
} from './types';
import { nextEventLoopTick } from './utils/nextEventLoopTick';

Expand All @@ -52,6 +53,7 @@ export interface Props {
pullToRefreshEnabled?: boolean;
scrollEnabled?: boolean;
contentInset?: ContentInsetObject;
progressViewOffset?: ProgressViewOffsetObject;
renderLoading?: RenderLoading;
renderError?: RenderError;
onVisitProposal: (proposal: VisitProposal) => void;
Expand Down Expand Up @@ -83,6 +85,7 @@ const VisitableView = React.forwardRef<RefObject, React.PropsWithRef<Props>>(
pullToRefreshEnabled = true,
scrollEnabled = true,
contentInset = { top: 0, left: 0, right: 0, bottom: 0 },
progressViewOffset,
renderLoading,
renderError,
onLoad,
Expand Down Expand Up @@ -221,6 +224,7 @@ const VisitableView = React.forwardRef<RefObject, React.PropsWithRef<Props>>(
pullToRefreshEnabled={pullToRefreshEnabled}
scrollEnabled={scrollEnabled}
contentInset={contentInset}
progressViewOffset={progressViewOffset}
onError={onErrorCombinedHandlers}
onVisitProposal={handleVisitProposal}
onMessage={handleOnMessage}
Expand Down
6 changes: 6 additions & 0 deletions packages/turbo/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export type ContentInsetObject = {
top?: number;
};

export type ProgressViewOffsetObject = {
scale: boolean;
start: number;
end: number;
};

export type StradaMessage = {
component: string;
event: string;
Expand Down

0 comments on commit 5b7fe1c

Please sign in to comment.