@@ -13,13 +13,15 @@ export type HapticType =
1313export type HapticOptions = {
1414 enableVibrateFallback ?: boolean ;
1515 ignoreAndroidSystemSettings ?: boolean ;
16- androidPattern ?: number [ ] ;
16+ pattern ?: number [ ] ;
17+ increaseIosIntensity ?: boolean ;
1718} ;
1819
1920const defaultOptions : HapticOptions = {
2021 enableVibrateFallback : true ,
2122 ignoreAndroidSystemSettings : false ,
22- androidPattern : [ 50 , 100 , 50 ] ,
23+ pattern : [ 50 , 100 , 50 ] ,
24+ increaseIosIntensity : true ,
2325} ;
2426
2527/**
@@ -35,31 +37,111 @@ export const buttonTap = impactLight;
3537export const cancelTap = selectionChange ;
3638export const confirmTap = impactMedium ;
3739
40+ // Custom feedback events
41+ // consistent light feedback at a steady interval
42+ export const feedbackProgress = ( ) => {
43+ if ( Platform . OS === 'android' ) {
44+ triggerFeedback ( 'custom' , {
45+ pattern : [ 0 , 50 , 450 , 50 , 450 , 50 ] ,
46+ } ) ;
47+ return ;
48+ }
49+
50+ setTimeout ( ( ) => {
51+ triggerFeedback ( 'impactLight' , {
52+ increaseIosIntensity : false ,
53+ } ) ;
54+ } , 500 ) ;
55+ setTimeout ( ( ) => {
56+ triggerFeedback ( 'impactLight' , {
57+ increaseIosIntensity : false ,
58+ } ) ;
59+ } , 1000 ) ;
60+ setTimeout ( ( ) => {
61+ triggerFeedback ( 'impactLight' , {
62+ increaseIosIntensity : false ,
63+ } ) ;
64+ } , 1500 ) ;
65+ } ;
66+
67+ // light -> medium -> heavy intensity in sequence
68+ export const feedbackSuccess = ( ) => {
69+ if ( Platform . OS === 'android' ) {
70+ triggerFeedback ( 'custom' , {
71+ pattern : [ 500 , 50 , 200 , 100 , 150 , 150 ] ,
72+ } ) ;
73+ return ;
74+ }
75+
76+ setTimeout ( ( ) => {
77+ triggerFeedback ( 'impactLight' , {
78+ increaseIosIntensity : false ,
79+ } ) ;
80+ } , 500 ) ;
81+ setTimeout ( ( ) => {
82+ triggerFeedback ( 'impactMedium' , {
83+ increaseIosIntensity : false ,
84+ } ) ;
85+ } , 750 ) ;
86+ setTimeout ( ( ) => {
87+ triggerFeedback ( 'impactHeavy' , {
88+ increaseIosIntensity : false ,
89+ } ) ;
90+ } , 1000 ) ;
91+ } ;
92+
93+ // heavy -> medium -> light intensity in sequence
94+ export const feedbackUnsuccessful = ( ) => {
95+ if ( Platform . OS === 'android' ) {
96+ triggerFeedback ( 'custom' , {
97+ pattern : [ 500 , 150 , 100 , 100 , 150 , 50 ] ,
98+ } ) ;
99+ return ;
100+ }
101+
102+ setTimeout ( ( ) => {
103+ triggerFeedback ( 'impactHeavy' , {
104+ increaseIosIntensity : false ,
105+ } ) ;
106+ } , 500 ) ;
107+ setTimeout ( ( ) => {
108+ triggerFeedback ( 'impactMedium' , {
109+ increaseIosIntensity : false ,
110+ } ) ;
111+ } , 750 ) ;
112+ setTimeout ( ( ) => {
113+ triggerFeedback ( 'impactLight' , {
114+ increaseIosIntensity : false ,
115+ } ) ;
116+ } , 1000 ) ;
117+ } ;
118+
38119/**
39120 * Triggers haptic feedback or vibration based on platform.
40121 * @param type - The haptic feedback type.
41122 * @param options - Custom options (optional).
42123 */
43124export const triggerFeedback = (
44- type : HapticType ,
125+ type : HapticType | 'custom' ,
45126 options : HapticOptions = { } ,
46127) => {
47128 const mergedOptions = { ...defaultOptions , ...options } ;
48-
49- if ( Platform . OS === 'ios' ) {
50- // increase feedback intensity for iOS
51- if ( type === 'impactLight' ) {
52- type = 'impactMedium' ;
53- } else if ( type === 'impactMedium' ) {
54- type = 'impactHeavy' ;
129+ if ( Platform . OS === 'ios' && type !== 'custom' ) {
130+ if ( mergedOptions . increaseIosIntensity ) {
131+ if ( type === 'impactLight' ) {
132+ type = 'impactMedium' ;
133+ } else if ( type === 'impactMedium' ) {
134+ type = 'impactHeavy' ;
135+ }
55136 }
137+
56138 ReactNativeHapticFeedback . trigger ( type , {
57139 enableVibrateFallback : mergedOptions . enableVibrateFallback ,
58140 ignoreAndroidSystemSettings : mergedOptions . ignoreAndroidSystemSettings ,
59141 } ) ;
60142 } else {
61- if ( mergedOptions . androidPattern ) {
62- Vibration . vibrate ( mergedOptions . androidPattern , false ) ;
143+ if ( mergedOptions . pattern ) {
144+ Vibration . vibrate ( mergedOptions . pattern , false ) ;
63145 } else {
64146 Vibration . vibrate ( 100 ) ;
65147 }
0 commit comments