Skip to content

Commit 14df526

Browse files
authored
Initial Web support (#224)
* Add web support Add initial web support with `modern-screenshot` library and new CSS `mask` api * Update README.md Add web support instructions
1 parent 34ddff1 commit 14df526

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Provides a React component that renders a masked view.
1111

1212
- [x] iOS
1313
- [x] Android
14-
- [ ] Web
14+
- [x] Web
1515

1616
## Getting Started
1717

@@ -94,6 +94,13 @@ The following image demonstrates that you can put almost anything behind the mas
9494

9595
<div align="center"><img src="img/example.png" width="200"></img></div>
9696

97+
### Web Usage
98+
99+
you need to install moden-screenshot package for web usage:
100+
```sh
101+
yarn add modern-screenshot
102+
```
103+
97104
### Props
98105

99106
- [View props...](https://github.com/facebook/react-native-website/blob/master/docs/view.md#props)

js/MaskedView.web.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,53 @@
1-
import React from 'react';
2-
import { View } from 'react-native';
1+
import React, { ReactNode, useEffect, useRef, useState } from "react";
2+
import { View } from "react-native";
3+
import { domToPng } from "modern-screenshot";
4+
const MaskedView = ({
5+
children,
6+
maskElement,
7+
style,
8+
...rest
9+
}) => {
10+
const maskRef = useRef(null);
11+
const [mask, setMask] = useState("");
12+
const snapShot = () => {
13+
if (!maskRef.current) return;
14+
domToPng(maskRef.current).then((dataUrl) => {
15+
setMask(dataUrl);
16+
});
17+
};
18+
useEffect(() => {
19+
const observer = new ResizeObserver(snapShot);
20+
observer.observe(maskRef.current!);
21+
return () => {
22+
observer.disconnect();
23+
};
24+
}, [maskElement]);
325

4-
function MaskedView({ maskElement, ...props }) {
5-
return React.createElement(View, props, maskElement);
6-
}
26+
return (
27+
<>
28+
<View
29+
style={{
30+
position: "absolute",
31+
transform: [{ translateX: "-100%" }, { translateY: "-100%" }],
32+
}}
33+
>
34+
<div ref={maskRef}>{maskElement}</div>
35+
</View>
36+
37+
<View
38+
style={[
39+
style,
40+
{
41+
//@ts-ignore
42+
mask: `url(${mask}) center no-repeat`,
43+
},
44+
]}
45+
{...rest}
46+
>
47+
{children}
48+
</View>
49+
</>
50+
);
51+
};
752

853
export default MaskedView;

0 commit comments

Comments
 (0)