Skip to content

Commit

Permalink
Introduce contentInset property (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
pklatka authored Apr 26, 2024
1 parent fe34a1a commit 3c03f9d
Show file tree
Hide file tree
Showing 6 changed files with 29 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 @@ -91,6 +91,12 @@ Enables pull to refresh functionality. Default value is `true`.

Enables scrolling in the webview. Default value is `true`.

### `contentInset`

The amount by which the web view content is inset from the edges of the scroll view.

Note: available only on iOS.

### `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
9 changes: 9 additions & 0 deletions packages/turbo/ios/RNVisitableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class RNVisitableView: UIView, RNSessionSubscriber {
configureWebView()
}
}
@objc var contentInset: [String: CGFloat] = [:] {
didSet {
configureWebView()
}
}
@objc var onMessage: RCTDirectEventBlock?
@objc var onVisitProposal: RCTDirectEventBlock?
@objc var onOpenExternalUrl: RCTDirectEventBlock?
Expand Down Expand Up @@ -80,6 +85,10 @@ class RNVisitableView: UIView, RNSessionSubscriber {
}

webView!.scrollView.isScrollEnabled = scrollEnabled
webView!.scrollView.contentInset = UIEdgeInsets(top: contentInset["top"] ?? 0,
left: contentInset["left"] ?? 0,
bottom: contentInset["bottom"] ?? 0,
right: contentInset["right"] ?? 0)
}

override func willMove(toWindow newWindow: UIWindow?) {
Expand Down
1 change: 1 addition & 0 deletions packages/turbo/ios/RNVisitableViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ @interface RCT_EXTERN_MODULE(RNVisitableViewManager, NSObject)
RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
RCT_EXPORT_VIEW_PROPERTY(pullToRefreshEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(contentInset, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(onVisitProposal, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onOpenExternalUrl, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
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 @@ -19,6 +19,7 @@ import type {
OpenExternalUrlEvent,
FormSubmissionEvent,
ContentProcessDidTerminateEvent,
ContentInsetObject,
} from './types';

// Interface should match RNVisitableView exported properties in native code
Expand All @@ -28,6 +29,7 @@ export interface RNVisitableViewProps {
applicationNameForUserAgent?: string;
pullToRefreshEnabled: boolean;
scrollEnabled: boolean;
contentInset: ContentInsetObject;
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 @@ -40,6 +40,7 @@ import type {
StradaComponent,
FormSubmissionEvent,
ContentProcessDidTerminateEvent,
ContentInsetObject,
} from './types';
import { nextEventLoopTick } from './utils/nextEventLoopTick';

Expand All @@ -50,6 +51,7 @@ export interface Props {
stradaComponents?: StradaComponent[];
pullToRefreshEnabled?: boolean;
scrollEnabled?: boolean;
contentInset?: ContentInsetObject;
renderLoading?: RenderLoading;
renderError?: RenderError;
onVisitProposal: (proposal: VisitProposal) => void;
Expand Down Expand Up @@ -80,6 +82,7 @@ const VisitableView = React.forwardRef<RefObject, React.PropsWithRef<Props>>(
stradaComponents,
pullToRefreshEnabled = true,
scrollEnabled = true,
contentInset = { top: 0, left: 0, right: 0, bottom: 0 },
renderLoading,
renderError,
onLoad,
Expand Down Expand Up @@ -217,6 +220,7 @@ const VisitableView = React.forwardRef<RefObject, React.PropsWithRef<Props>>(
applicationNameForUserAgent={resolvedApplicationNameForUserAgent}
pullToRefreshEnabled={pullToRefreshEnabled}
scrollEnabled={scrollEnabled}
contentInset={contentInset}
onError={onErrorCombinedHandlers}
onVisitProposal={handleVisitProposal}
onMessage={handleOnMessage}
Expand Down
7 changes: 7 additions & 0 deletions packages/turbo/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export enum SystemStatusCode {
UNKNOWN = -4,
}

export type ContentInsetObject = {
bottom?: number;
left?: number;
right?: number;
top?: number;
};

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

0 comments on commit 3c03f9d

Please sign in to comment.