1 | import * as React from 'react';
|
2 | import { View } from 'react-native';
|
3 |
|
4 | import { BlurProps } from './BlurView.types';
|
5 | import getBackgroundColor from './getBackgroundColor';
|
6 |
|
7 | class 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 |
|
15 | function isBlurSupported(): boolean {
|
16 |
|
17 |
|
18 | return (
|
19 | typeof CSS !== 'undefined' &&
|
20 | (CSS.supports('-webkit-backdrop-filter', 'blur(1px)') ||
|
21 | CSS.supports('backdrop-filter', 'blur(1px)'))
|
22 | );
|
23 | }
|
24 |
|
25 | function 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 |
|
37 | export default BlurView;
|