diff --git a/README.md b/README.md index bbb54cf..5fbb5f1 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Provides a React component that renders a masked view. - [x] iOS - [x] Android -- [ ] Web +- [x] Web ## Getting Started @@ -94,6 +94,13 @@ The following image demonstrates that you can put almost anything behind the mas
+### Web Usage + +you need to install moden-screenshot package for web usage: +```sh +yarn add modern-screenshot +``` + ### Props - [View props...](https://github.com/facebook/react-native-website/blob/master/docs/view.md#props) diff --git a/js/MaskedView.web.js b/js/MaskedView.web.js index 4916e2b..f748f1c 100644 --- a/js/MaskedView.web.js +++ b/js/MaskedView.web.js @@ -1,8 +1,53 @@ -import React from 'react'; -import { View } from 'react-native'; +import React, { ReactNode, useEffect, useRef, useState } from "react"; +import { View } from "react-native"; +import { domToPng } from "modern-screenshot"; +const MaskedView = ({ + children, + maskElement, + style, + ...rest +}) => { + const maskRef = useRef(null); + const [mask, setMask] = useState(""); + const snapShot = () => { + if (!maskRef.current) return; + domToPng(maskRef.current).then((dataUrl) => { + setMask(dataUrl); + }); + }; + useEffect(() => { + const observer = new ResizeObserver(snapShot); + observer.observe(maskRef.current!); + return () => { + observer.disconnect(); + }; + }, [maskElement]); -function MaskedView({ maskElement, ...props }) { - return React.createElement(View, props, maskElement); -} + return ( + <> +