-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useFullscreen): always listen to fullscreen change and dynamicall…
…y set the initial value (#1996) * fix: always listen to fullscreen change * refactor: restore original code * refactor: optimize code * docs: remove useless comment * fix: though closure * refactor: simplify getIsFullscreen * refactor: update * docs: remove extra comment * docs: add demo4 --------- Co-authored-by: 云泥 <[email protected]> Co-authored-by: liuyib <[email protected]>
- Loading branch information
1 parent
9d60990
commit 80f7226
Showing
5 changed files
with
94 additions
and
34 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,39 @@ | ||
/** | ||
* title: Coexist with other full screen operations | ||
* desc: The element's full screen may be modified by other scripts, don't worry, ahooks can work with them. | ||
* | ||
* title.zh-CN: 与其它全屏操作共存 | ||
* desc.zh-CN: 元素的全屏情况可能被其它脚本修改,不用担心,ahooks 可以与它们共存。 | ||
*/ | ||
|
||
import React, { useRef } from 'react'; | ||
import { useFullscreen } from 'ahooks'; | ||
|
||
function vanillaToggleFullscreen(element) { | ||
const isFullscreen = !!document.fullscreenElement; | ||
|
||
if (isFullscreen) { | ||
document.exitFullscreen(); | ||
} else { | ||
element.requestFullscreen(); | ||
} | ||
} | ||
|
||
export default () => { | ||
const ref = useRef(null); | ||
const [isFullscreen, { toggleFullscreen }] = useFullscreen(ref); | ||
|
||
return ( | ||
<div ref={ref} style={{ background: 'white' }}> | ||
<div style={{ marginBottom: 16 }}>{isFullscreen ? 'Fullscreen' : 'Not fullscreen'}</div> | ||
<div> | ||
<button style={{ marginRight: '8px' }} onClick={toggleFullscreen}> | ||
ahooks toggleFullscreen | ||
</button> | ||
<button onClick={() => vanillaToggleFullscreen(ref.current)}> | ||
vanilla toggleFullscreen | ||
</button> | ||
</div> | ||
</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
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