-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
34ddff1
commit 14df526
Showing
2 changed files
with
58 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |