Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support React Native 0.77 #3278

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ android {
// codegen turned off
srcDirs += 'paper/src/main/java'
}

if (REACT_NATIVE_MINOR_VERSION >= 77) {
// With RN 0.77, ViewManager related functions in the package has different signatures as they
// are no longer nullable
srcDirs += 'package77/src/main/java'
} else {
// It's safe to delete this block once we drop support for RN 0.76
srcDirs += 'packageDeprecated/src/main/java'
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.swmansion.gesturehandler

import com.facebook.react.BaseReactPackage
import com.facebook.react.ViewManagerOnDemandReactPackage
import com.facebook.react.bridge.ModuleSpec
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.module.annotations.ReactModuleList
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider
import com.facebook.react.uimanager.ViewManager
import com.swmansion.gesturehandler.react.RNGestureHandlerButtonViewManager
import com.swmansion.gesturehandler.react.RNGestureHandlerModule
import com.swmansion.gesturehandler.react.RNGestureHandlerRootViewManager

@ReactModuleList(
nativeModules = [
RNGestureHandlerModule::class
]
)
class RNGestureHandlerPackage : BaseReactPackage(), ViewManagerOnDemandReactPackage {
private val viewManagers: Map<String, ModuleSpec> by lazy {
mapOf(
RNGestureHandlerRootViewManager.REACT_CLASS to ModuleSpec.viewManagerSpec {
RNGestureHandlerRootViewManager()
},
RNGestureHandlerButtonViewManager.REACT_CLASS to ModuleSpec.viewManagerSpec {
RNGestureHandlerButtonViewManager()
}
)
}

override fun createViewManagers(reactContext: ReactApplicationContext) =
listOf<ViewManager<*, *>>(
RNGestureHandlerRootViewManager(),
RNGestureHandlerButtonViewManager()
)

override fun getViewManagerNames(reactContext: ReactApplicationContext) =
viewManagers.keys.toList()

override fun getViewManagers(reactContext: ReactApplicationContext): MutableList<ModuleSpec> =
viewManagers.values.toMutableList()

override fun createViewManager(
reactContext: ReactApplicationContext,
viewManagerName: String
) = viewManagers[viewManagerName]?.provider?.get() as? ViewManager<*, *>

override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return if (name == RNGestureHandlerModule.NAME) {
RNGestureHandlerModule(reactContext)
} else {
null
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
try {
val reactModuleInfoProviderClass =
Class.forName("com.swmansion.gesturehandler.RNGestureHandlerPackage$\$ReactModuleInfoProvider")
return reactModuleInfoProviderClass.getDeclaredConstructor().newInstance() as ReactModuleInfoProvider
} catch (e: ClassNotFoundException) {
return ReactModuleInfoProvider {
val reactModule: ReactModule = RNGestureHandlerModule::class.java.getAnnotation(ReactModule::class.java)!!

mutableMapOf(
RNGestureHandlerModule.NAME to ReactModuleInfo(
reactModule.name,
RNGestureHandlerModule::class.java.name,
reactModule.canOverrideExistingModule,
reactModule.needsEagerInit,
reactModule.isCxxModule,
true
)
)
}
} catch (e: InstantiationException) {
throw RuntimeException("No ReactModuleInfoProvider for RNGestureHandlerPackage$\$ReactModuleInfoProvider", e)
} catch (e: IllegalAccessException) {
throw RuntimeException("No ReactModuleInfoProvider for RNGestureHandlerPackage$\$ReactModuleInfoProvider", e)
}
}
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@
"jsSrcsDir": "./src/specs",
"android": {
"javaPackageName": "com.swmansion.gesturehandler"
},
"ios": {
"componentProvider": {
"RNGestureHandlerButton": "RNGestureHandlerButtonComponentView"
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
"packageManager": "[email protected]"
Expand Down
10 changes: 8 additions & 2 deletions src/getShadowNodeFromRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ export function getShadowNodeFromRef(ref: unknown) {
// Load findHostInstance_DEPRECATED lazily because it may not be available before render
if (findHostInstance_DEPRECATED === undefined) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
const ReactFabric = require('react-native/Libraries/Renderer/shims/ReactFabric');
// Since RN 0.77 ReactFabric exports findHostInstance_DEPRECATED in default object so we're trying to
// access it first, then fallback on named export
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
findHostInstance_DEPRECATED =
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
require('react-native/Libraries/Renderer/shims/ReactFabric').findHostInstance_DEPRECATED;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
ReactFabric?.default?.findHostInstance_DEPRECATED ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
ReactFabric?.findHostInstance_DEPRECATED;
} catch (e) {
findHostInstance_DEPRECATED = (_ref: unknown) => null;
}
Expand Down
Loading