1
1
import '../styles/index.css' ;
2
2
3
- import React , { FunctionComponent , useState } from 'react' ;
3
+ import React , { FunctionComponent , MouseEvent } from 'react' ;
4
4
import { AppProps } from 'next/app' ;
5
5
import Div100vh from 'react-div-100vh' ;
6
6
import NProgress from 'nprogress' ;
7
+ import usePortal from 'react-useportal' ;
7
8
import Router from 'next/router' ;
8
9
import Head from 'next/head' ;
9
10
@@ -19,8 +20,8 @@ Router.events.on('routeChangeComplete', () => NProgress.done());
19
20
Router . events . on ( 'routeChangeError' , ( ) => NProgress . done ( ) ) ;
20
21
21
22
const App : FunctionComponent < AppProps > = ( { Component, pageProps } ) => {
22
- const [ showFallbackShare , setShowFallbackShare ] = useState ( false ) ;
23
- function openShareDialog ( ) {
23
+ const { openPortal , closePortal , isOpen , Portal } = usePortal ( ) ;
24
+ function openShareDialog ( e : MouseEvent < HTMLButtonElement > ) {
24
25
// try to open native share dialog
25
26
if ( window . navigator . share ) {
26
27
const title = document . title ;
@@ -30,23 +31,25 @@ const App: FunctionComponent<AppProps> = ({ Component, pageProps }) => {
30
31
return window . navigator . share ( { title, text : `${ text } ${ url } ` } ) ;
31
32
}
32
33
// fallback to custom native share dialog
33
- return setShowFallbackShare ( true ) ;
34
+ return openPortal ( e ) ;
34
35
}
35
- function closeShareDialog ( ) {
36
- setShowFallbackShare ( false ) ;
36
+ function closeShareDialog ( e : MouseEvent < HTMLButtonElement > ) {
37
+ closePortal ( e ) ;
37
38
}
38
39
return (
39
40
< >
40
41
< Head >
41
42
{ /* Import CSS for nprogress */ }
42
43
< link rel = "stylesheet" type = "text/css" href = "/nprogress.css" />
43
44
</ Head >
44
- < ShareContext . Provider value = { { openShareDialog, closeShareDialog, isOpen : showFallbackShare } } >
45
- < Div100vh className = "relative" >
45
+ < ShareContext . Provider value = { { openShareDialog, closeShareDialog, isOpen : isOpen } } >
46
+ < Div100vh >
46
47
< Component { ...pageProps } />
47
- < FallbackShare />
48
48
< MobileMenu />
49
49
</ Div100vh >
50
+ < Portal >
51
+ < FallbackShare />
52
+ </ Portal >
50
53
</ ShareContext . Provider >
51
54
</ >
52
55
) ;
0 commit comments