Skip to content

Commit

Permalink
fix: add url prop
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Feb 1, 2025
1 parent 3912d8c commit 6649284
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 43 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,7 @@
"i18n-ally.displayLanguage": "en",
"i18n-ally.localesPaths": ["locales"],
"i18n-ally.namespace": true,
"i18n-ally.pathMatcher": "{namespaces}/{locale}.json"
"i18n-ally.pathMatcher": "{namespaces}/{locale}.json",
"lldb.library": "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB",
"lldb.launch.expressions": "native"
}
5 changes: 2 additions & 3 deletions apps/mobile/packages/follow-native/example/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { SharedWebView, SharedWebViewModule } from "follow-native"
import { SharedWebView } from "follow-native"
import { ScrollView, View } from "react-native"

SharedWebViewModule.preload("https://follow.is")
export default function App() {
return (
<ScrollView>
<SharedWebView />
<SharedWebView url="https://innei.in" />

<View style={{ height: 1000, backgroundColor: "red" }} />
</ScrollView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,6 @@ import ExpoModulesCore
import SwiftUI
import WebKit

class WebViewProps: ExpoSwiftUI.ViewProps {
@Field var url: String
}

struct WebViewComponentView: ExpoSwiftUI.View {
@EnvironmentObject var props: WebViewProps
@StateObject private var webViewState = WebViewManager.state

let webView: WKWebView = SharedWebViewModule.sharedWebView!

var body: some View {
WebViewComponent(webView: webView)
.frame(
height: webViewState.contentHeight
)

.onChange(of: props.url, initial: false) { _, newValue in
if let nextUrl = URL(string: newValue) {
let request = URLRequest(url: nextUrl)
webView.load(request)
}
}
#if DEBUG
.border(.tint)
#endif
}
}

class WebViewView: ExpoView {
private var cancellable: AnyCancellable?

Expand All @@ -39,6 +11,12 @@ class WebViewView: ExpoView {
required init(appContext: AppContext? = nil) {
super.init(appContext: appContext)
addSubview(rctView)

#if DEBUG
rctView.borderStyle = .solid
rctView.borderWidth = 1
rctView.borderColor = .tintColor
#endif
rctView.addSubview(SharedWebViewModule.sharedWebView!)

clipsToBounds = true
Expand Down Expand Up @@ -71,13 +49,7 @@ class WebViewView: ExpoView {
rctView.frame = rect

onContentHeightChange(["height": Float(rect.height)])
// if let superview = self.superview {
// superview.frame = rect
//
// if let superSuperview = superview.superview {
// superSuperview.frame = rect
// }
// }

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ public class SharedWebViewModule: Module {
}

View(WebViewView.self) {
Events(
"onContentHeightChange"
)
Events("onContentHeightChange")

Prop("url") { (_: UIView, urlString: String) in
if let webView = SharedWebViewModule.sharedWebView {
if let url = URL(string: urlString) {
if url == webView.url {
return
}
webView.load(URLRequest(url: url))
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ enum WebViewManager {


#if DEBUG
if #available(iOS 16.4, *) {

webView.isInspectable = true
}
#endif


Expand Down
4 changes: 3 additions & 1 deletion apps/mobile/packages/follow-native/src/SharedWebView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { View } from "react-native"

const NativeView: React.ComponentType<{
onContentHeightChange?: (e: { nativeEvent: { height: number } }) => void
url: string
}> = requireNativeView("FOSharedWebView")

export default function SharedWebView() {
export default function SharedWebView({ url }: { url: string }) {
const [contentHeight, setContentHeight] = useState(0)

return (
Expand All @@ -16,6 +17,7 @@ export default function SharedWebView() {
onContentHeightChange={(e) => {
setContentHeight(e.nativeEvent.height)
}}
url={url}
/>
</View>
)
Expand Down

0 comments on commit 6649284

Please sign in to comment.