UNPKG

1.12 kBTypeScriptView Raw
1import * as React from 'react';
2import { View } from 'react-native';
3
4import { BlurProps } from './BlurView.types';
5import getBackgroundColor from './getBackgroundColor';
6
7class BlurView extends React.Component<BlurProps> {
8 render() {
9 const { tint = 'default', intensity = 50, style, ...props } = this.props;
10 const blurStyle = getBlurStyle({ tint, intensity });
11 return <View {...props} style={[style, blurStyle]} />;
12 }
13}
14
15function isBlurSupported(): boolean {
16 // https://developer.mozilla.org/en-US/docs/Web/API/CSS/supports
17 // https://developer.mozilla.org/en-US/docs/Web/CSS/backdrop-filter#Browser_compatibility
18 return (
19 typeof CSS !== 'undefined' &&
20 (CSS.supports('-webkit-backdrop-filter', 'blur(1px)') ||
21 CSS.supports('backdrop-filter', 'blur(1px)'))
22 );
23}
24
25function getBlurStyle({ intensity, tint }): Record<string, string> {
26 const style: Record<string, string> = {
27 backgroundColor: getBackgroundColor(intensity, tint),
28 };
29
30 if (isBlurSupported()) {
31 style.backdropFilter = `saturate(180%) blur(${intensity * 0.2}px)`;
32 }
33
34 return style;
35}
36
37export default BlurView;