Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add api for switching parts of the UI to fullscreen (#237)
- Loading branch information
1 parent
4f33be3
commit 37d42df
Showing
4 changed files
with
149 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable no-alert */ | ||
import { | ||
createContext, | ||
ReactNode, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useReducer, | ||
useRef, | ||
} from 'react'; | ||
|
||
interface FullscreenProps { | ||
children: ReactNode; | ||
} | ||
interface FullscreenState { | ||
isFullScreen: boolean; | ||
} | ||
|
||
interface ContextType extends FullscreenState { | ||
toggle: () => void; | ||
} | ||
const fullscreenContextInit = { | ||
isFullScreen: false, | ||
toggle: () => {}, | ||
}; | ||
const FullscreenContext = createContext<ContextType>(fullscreenContextInit); | ||
|
||
export function useFullscreen() { | ||
return useContext(FullscreenContext); | ||
} | ||
export function FullScreenProvider(props: FullscreenProps) { | ||
const [isFullScreen, toggle] = useReducer((value: boolean) => !value, false); | ||
|
||
const value = useMemo( | ||
() => ({ isFullScreen, toggle }), | ||
[isFullScreen, toggle], | ||
); | ||
return ( | ||
<FullscreenContext.Provider value={value}> | ||
<FullscreenInner {...props} /> | ||
</FullscreenContext.Provider> | ||
); | ||
} | ||
function FullscreenInner(props: FullscreenProps) { | ||
const { children } = props; | ||
const { isFullScreen } = useFullscreen(); | ||
const ref = useRef<HTMLDivElement>(null); | ||
useEffect(() => { | ||
if (isFullScreen && ref.current) { | ||
ref.current.requestFullscreen().catch(() => { | ||
alert('Fullscreen is not supported'); | ||
}); | ||
} else if ( | ||
!isFullScreen && | ||
ref.current && | ||
document.fullscreenElement !== null && | ||
document.exitFullscreen | ||
) { | ||
document.exitFullscreen().catch(() => { | ||
alert("Can't exit fullscreen"); | ||
}); | ||
} | ||
}, [isFullScreen]); | ||
return ( | ||
<div | ||
ref={ref} | ||
style={{ | ||
width: '100%', | ||
height: '100%', | ||
backgroundColor: 'white', | ||
}} | ||
> | ||
{children} | ||
</div> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Button, FullScreenProvider, useFullscreen } from '@/components'; | ||
|
||
export default { | ||
title: 'Components / Fullscreen', | ||
}; | ||
export function basic() { | ||
return ( | ||
<div> | ||
<FullScreenProvider> | ||
<div | ||
style={{ border: 'solid 1px black', padding: '10px', margin: '10px' }} | ||
> | ||
Page 1 | ||
<FullScreenProvider> | ||
<div | ||
style={{ | ||
border: 'solid 1px red', | ||
padding: '10px', | ||
margin: '10px', | ||
}} | ||
> | ||
Page 2 | ||
<FullScreenProvider> | ||
<div | ||
style={{ | ||
border: 'solid 1px blue', | ||
padding: '10px', | ||
margin: '10px', | ||
}} | ||
> | ||
Page 3 | ||
<Content i="3" /> | ||
</div> | ||
</FullScreenProvider> | ||
<Content i="2" /> | ||
</div> | ||
</FullScreenProvider> | ||
<Content i="1" /> | ||
</div> | ||
</FullScreenProvider> | ||
</div> | ||
); | ||
} | ||
function Content({ i }) { | ||
const { toggle } = useFullscreen(); | ||
return ( | ||
<div> | ||
<Button onClick={toggle}>Toggle fullscreen {i}</Button> | ||
</div> | ||
); | ||
} |