Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbaroni committed Nov 13, 2024
0 parents commit 1446d8e
Show file tree
Hide file tree
Showing 150 changed files with 29,980 additions and 0 deletions.
78 changes: 78 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# OSX
#
.DS_Store

# XDE
.expo/

# VSCode
.vscode/
jsconfig.json

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IJ
#
.classpath
.cxx
.gradle
.idea
.project
.settings
local.properties
android.iml

# Cocoapods
#
example/ios/Pods

# Ruby
example/vendor/

# node.js
#
node_modules/
npm-debug.log
yarn-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
android/app/libs
android/keystores/debug.keystore

# Yarn
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Expo
.expo/

# Turborepo
.turbo/

# generated by bob
lib/
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 🌀 Turbo Haptics

Fast, **worklet-compatible** haptic feedback for React Native.

## Features

- 🏎️ Zero-delay haptic feedback, powered by JSI
- 🪄 Compatible with Reanimated and Gesture Handler worklets
- 🎯 9 different haptic patterns (impact, notification, selection)
- 📱 iOS and Android support
- 🪶 Lightweight, zero dependencies

## Installation

```sh
yarn add react-native-turbo-haptics
cd ios && pod install
```

## Usage

```ts
import { triggerHaptics } from 'react-native-turbo-haptics';

// In any JavaScript context:
triggerHaptics('selection');

// In worklets:
Gesture.Tap()
.onBegin(() => triggerHaptics('soft'));
```

```ts
// Available haptic types:
const HapticTypes = {
impactHeavy: 'impactHeavy',
impactLight: 'impactLight',
impactMedium: 'impactMedium',
notificationError: 'notificationError',
notificationSuccess: 'notificationSuccess',
notificationWarning: 'notificationWarning',
rigid: 'rigid',
selection: 'selection',
soft: 'soft',
};
```

## License

MIT
7 changes: 7 additions & 0 deletions package/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
arrowParens: 'avoid',
bracketSameLine: true,
bracketSpacing: true,
singleQuote: true,
trailingComma: 'all',
};
19 changes: 19 additions & 0 deletions package/TurboHaptics.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "json"

package = JSON.parse(File.read(File.join(__dir__, "package.json")))

Pod::Spec.new do |s|
s.name = "TurboHaptics"
s.version = package["version"]
s.summary = package["description"]
s.homepage = package["homepage"]
s.license = package["license"]
s.authors = package["author"]

s.platforms = { :ios => "13.0" }
s.source = { :git => "https://github.com/christianbaroni/react-native-turbo-haptics.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm}"

install_modules_dependencies(s)
end
4 changes: 4 additions & 0 deletions package/android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.22.1)
project("turbo-haptics")

add_subdirectory(src/main/jni)
64 changes: 64 additions & 0 deletions package/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}

if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}

android {
namespace "com.turbohaptics"
compileSdkVersion safeExtGet('compileSdkVersion', 33)

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 21)
targetSdkVersion safeExtGet('targetSdkVersion', 33)
buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
externalNativeBuild {
cmake {
cppFlags "-std=c++17"
}
}
}

sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += [
'src/fabric',
// Codegen output directory
project.file("${project.buildDir}/generated/source/codegen/java")
]
} else {
java.srcDirs += ['src/paper']
}
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

externalNativeBuild {
cmake {
path "src/main/jni/CMakeLists.txt"
}
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
compileOnly "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"
}
1 change: 1 addition & 0 deletions package/android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-keep class com.turbohaptics.** { *; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.turbohaptics

import androidx.annotation.NonNull
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.NativeModule

class TurboHapticsModule(context: ReactApplicationContext) :
NativeTurboHapticsSpec(context),
NativeModule {

companion object {
const val NAME = "TurboHaptics"

init {
System.loadLibrary("turbo-haptics")
}
}

override fun getName(): String = NAME

override fun initialize() {}

override fun invalidate() {}

override fun install(): Boolean {
try {
val catalystInstance = reactApplicationContext.catalystInstance
?: return false

val runtimePointer = catalystInstance.javaScriptContextHolder.get()
if (runtimePointer == 0L) {
return false
}

return nativeInstallHaptics(runtimePointer, reactApplicationContext)
} catch (e: Exception) {
e.printStackTrace()
return false
}
}

private external fun nativeInstallHaptics(runtimePointer: Long, context: ReactApplicationContext): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.turbohaptics

import com.facebook.react.TurboReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider

class TurboHapticsPackage : TurboReactPackage() {
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
if (name == TurboHapticsModule.NAME) {
return TurboHapticsModule(reactContext)
}
return null
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
val moduleInfos = HashMap<String, ReactModuleInfo>()
val isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
moduleInfos.apply {
put(
TurboHapticsModule.NAME,
ReactModuleInfo(
TurboHapticsModule.NAME,
TurboHapticsModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
isTurboModule // isTurboModule
)
)
}
moduleInfos
}
}
}
5 changes: 5 additions & 0 deletions package/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.turbohaptics">
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
24 changes: 24 additions & 0 deletions package/android/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.22.1)
project("turbo-haptics")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(NODE_MODULES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../node_modules")

add_library(turbo-haptics SHARED
TurboHapticsHostObject.cpp
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp")

target_include_directories(turbo-haptics PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi"
)

find_library(log-lib log)

target_link_libraries(turbo-haptics
android
${log-lib})
Loading

0 comments on commit 1446d8e

Please sign in to comment.