-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ballcat-projects/dev
0.3.0
- Loading branch information
Showing
4 changed files
with
88 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { CSSProperties } from 'react'; | ||
import { useState, useCallback, useEffect } from 'react'; | ||
import Icon from '../Icon'; | ||
|
||
export type FullScreenProps = { | ||
full?: boolean; | ||
onFullChange?: (flag: boolean) => void; | ||
iconStyle?: CSSProperties; | ||
dom?: HTMLElement; | ||
}; | ||
|
||
export const openFullScreen = (dom?: HTMLElement) => { | ||
(dom || document.getElementsByTagName('body')[0]).requestFullscreen(); | ||
}; | ||
|
||
export const exitFullScreen = () => document.exitFullscreen(); | ||
|
||
const FullScreen = ({ full, onFullChange, iconStyle, dom }: FullScreenProps) => { | ||
const [isFull, setIsFull] = useState(false); | ||
|
||
const changeFull = useCallback( | ||
(flag: boolean) => { | ||
setIsFull(flag); | ||
if (onFullChange) { | ||
onFullChange(flag); | ||
} | ||
|
||
// 切换成全屏 且 当前没有全屏元素 | ||
if (flag && !document.fullscreenElement) { | ||
openFullScreen(dom); | ||
} | ||
// 取消全屏 且 当前有全屏元素 | ||
else if (!flag && document.fullscreenElement) { | ||
document.exitFullscreen(); | ||
} | ||
}, | ||
[dom, onFullChange], | ||
); | ||
|
||
useEffect(() => { | ||
changeFull(!!full); | ||
}, [full]); | ||
|
||
useEffect(() => { | ||
const fullscreenchangeListener = () => { | ||
const flag = !!document.fullscreenElement; | ||
setIsFull(flag); | ||
if (onFullChange) { | ||
onFullChange(flag); | ||
} | ||
}; | ||
document.addEventListener('fullscreenchange', fullscreenchangeListener); | ||
return () => { | ||
document.removeEventListener('fullscreenchange', fullscreenchangeListener); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<Icon | ||
type={isFull ? 'fullscreen-exit' : 'fullscreen'} | ||
style={{ fontSize: '16px', ...iconStyle }} | ||
onClick={() => { | ||
// 外部自定义触发是否全屏 | ||
if (full === undefined && onFullChange === undefined) { | ||
changeFull(!isFull); | ||
} | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
FullScreen.open = openFullScreen; | ||
|
||
FullScreen.exit = exitFullScreen; | ||
|
||
export default FullScreen; |
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
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