forked from Kureev/react-native-blur
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Kureev#1 from Serraview/ts-migration
Migrate code to Typescript
- Loading branch information
Showing
13 changed files
with
7,387 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"extends": "@react-native-community" | ||
"extends": [ | ||
"plugin:react/recommended", | ||
"@react-native-community", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"plugin:prettier/recommended" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all", | ||
"bracketSpacing": false, | ||
"bracketSpacing": true, | ||
"jsxBracketSameLine": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React, { forwardRef, PropsWithChildren, useEffect } from 'react'; | ||
import { | ||
View, | ||
requireNativeComponent, | ||
DeviceEventEmitter, | ||
StyleSheet, | ||
ViewProps, | ||
ViewStyle, | ||
} from 'react-native'; | ||
|
||
const OVERLAY_COLORS = { | ||
light: 'rgba(255, 255, 255, 0.2)', | ||
xlight: 'rgba(255, 255, 255, 0.75)', | ||
dark: 'rgba(16, 12, 12, 0.64)', | ||
}; | ||
|
||
export type BlurViewProps = PropsWithChildren & | ||
ViewProps & { | ||
blurAmount?: number; | ||
blurType?: 'dark' | 'light' | 'xlight'; | ||
blurRadius?: number; | ||
downsampleFactor?: number; | ||
overlayColor?: string; | ||
}; | ||
|
||
const BlurView = forwardRef<View, BlurViewProps>( | ||
({ | ||
downsampleFactor, | ||
blurRadius, | ||
blurAmount = 10, | ||
blurType = 'dark', | ||
overlayColor, | ||
children, | ||
style, | ||
...rest | ||
}) => { | ||
useEffect(() => { | ||
DeviceEventEmitter.addListener('ReactNativeBlurError', (message) => { | ||
throw new Error(`[ReactNativeBlur]: ${message}`); | ||
}); | ||
|
||
return () => { | ||
DeviceEventEmitter.removeAllListeners('ReactNativeBlurError'); | ||
}; | ||
}, []); | ||
|
||
const getOverlayColor = () => { | ||
if (overlayColor != null) { | ||
return overlayColor; | ||
} | ||
|
||
return OVERLAY_COLORS[blurType] || OVERLAY_COLORS.dark; | ||
}; | ||
|
||
const getBlurRadius = () => { | ||
if (blurRadius != null) { | ||
if (blurRadius > 25) { | ||
throw new Error( | ||
`[ReactNativeBlur]: blurRadius cannot be greater than 25! (was: ${blurRadius})`, | ||
); | ||
} | ||
return blurRadius; | ||
} | ||
|
||
// iOS seems to use a slightly different blurring algorithm (or scale?). | ||
// Android blurRadius + downsampleFactor is approximately 80% of blurAmount. | ||
const equivalentBlurRadius = Math.round(blurAmount * 0.8); | ||
|
||
if (equivalentBlurRadius > 25) { | ||
return 25; | ||
} | ||
return equivalentBlurRadius; | ||
}; | ||
|
||
const getDownsampleFactor = () => { | ||
if (downsampleFactor != null) { | ||
return downsampleFactor; | ||
} | ||
|
||
return blurRadius; | ||
}; | ||
|
||
return ( | ||
<NativeBlurView | ||
{...rest} | ||
blurRadius={getBlurRadius()} | ||
downsampleFactor={getDownsampleFactor()} | ||
overlayColor={getOverlayColor()} | ||
blurAmount={blurAmount} | ||
blurType={blurType} | ||
pointerEvents="none" | ||
style={StyleSheet.compose(styles.transparent, style)}> | ||
{children} | ||
</NativeBlurView> | ||
); | ||
}, | ||
); | ||
|
||
const styles = StyleSheet.create<{ transparent: ViewStyle }>({ | ||
transparent: { backgroundColor: 'transparent' }, | ||
}); | ||
|
||
const NativeBlurView = requireNativeComponent<BlurViewProps>('BlurView'); | ||
|
||
export default BlurView; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { forwardRef } from 'react'; | ||
import { | ||
requireNativeComponent, | ||
StyleSheet, | ||
ViewProps, | ||
ViewStyle, | ||
View, | ||
} from 'react-native'; | ||
|
||
type BlurType = | ||
| 'dark' | ||
| 'light' | ||
| 'xlight' | ||
| 'prominent' | ||
| 'regular' | ||
| 'extraDark' | ||
| 'chromeMaterial' | ||
| 'material' | ||
| 'thickMaterial' | ||
| 'thinMaterial' | ||
| 'ultraThinMaterial' | ||
| 'chromeMaterialDark' | ||
| 'materialDark' | ||
| 'thickMaterialDark' | ||
| 'thinMaterialDark' | ||
| 'ultraThinMaterialDark' | ||
| 'chromeMaterialLight' | ||
| 'materialLight' | ||
| 'thickMaterialLight' | ||
| 'thinMaterialLight' | ||
| 'ultraThinMaterialLight'; | ||
|
||
export type BlurViewProps = ViewProps & { | ||
blurType?: BlurType; | ||
blurAmount?: number; | ||
}; | ||
|
||
const BlurView = forwardRef<View, BlurViewProps>( | ||
({ blurType = 'dark', blurAmount = 10, style, ...rest }, ref) => ( | ||
<NativeBlurView | ||
ref={ref} | ||
style={StyleSheet.compose(styles.transparent, style)} | ||
blurType={blurType} | ||
blurAmount={blurAmount} | ||
{...rest} | ||
/> | ||
), | ||
); | ||
|
||
const styles = StyleSheet.create<{ transparent: ViewStyle }>({ | ||
transparent: { backgroundColor: 'transparent' }, | ||
}); | ||
|
||
const NativeBlurView = requireNativeComponent<BlurViewProps>('BlurView'); | ||
|
||
export default BlurView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.