Skip to content

Commit

Permalink
Initial Web support (#224)
Browse files Browse the repository at this point in the history
* Add web support

Add initial web support with `modern-screenshot` library and new CSS `mask` api

* Update README.md

Add web support instructions
  • Loading branch information
arasrezaei authored Nov 4, 2024
1 parent 34ddff1 commit 14df526
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Provides a React component that renders a masked view.

- [x] iOS
- [x] Android
- [ ] Web
- [x] Web

## Getting Started

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

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

### 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)
Expand Down
55 changes: 50 additions & 5 deletions js/MaskedView.web.js
Original file line number Diff line number Diff line change
@@ -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 (
<>
<View
style={{
position: "absolute",
transform: [{ translateX: "-100%" }, { translateY: "-100%" }],
}}
>
<div ref={maskRef}>{maskElement}</div>
</View>

<View
style={[
style,
{
//@ts-ignore
mask: `url(${mask}) center no-repeat`,
},
]}
{...rest}
>
{children}
</View>
</>
);
};

export default MaskedView;

0 comments on commit 14df526

Please sign in to comment.