Skip to content

Commit

Permalink
Rewrite CloseButton
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-github committed Jul 31, 2024
1 parent 0b8ff48 commit 517f092
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/components/Controls/CloseButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,44 @@
*/
import { h } from "preact"
import { useState, useEffect } from "preact/hooks"

import { useUiContext, useUiContextFn } from "../../contexts"

const isFullScreen = (element) => {
return document.fullscreenElement === element
}

const CloseButton = ({panelId, hideOnFullScreen, onclick }) => {
return null
const CloseButton = ({elementId, hideOnFullScreen, onclick }) => {
const { panels } = useUiContext()
const [isFullScreenMode, setIsFullScreenMode] = useState(false)

//at each render, check if the element is fullscreen
useEffect(() => {
const handleFullscreenChange = () => {
setIsFullScreenMode(isFullScreen(panelRef.current))
//Handle fullscreen change event
const handleFullScreenChange = () => {
const element =document.getElementById(elementId)
if ( document.getElementById(elementId)) {
console.log("Button close Fullscreen state changed for " + elementId)
setIsFullScreenMode(document.fullscreenElement==element)
}
}

document.addEventListener("fullscreenchange", handleFullscreenChange)

//Add event listener to handle fullscreen change
document.addEventListener("fullscreenchange", handleFullScreenChange)
//Remove event listener on unmount
return () => {
document.removeEventListener(
"fullscreenchange",
handleFullscreenChange
handleFullScreenChange
)
}
}, [panelRef])

})
//Hide the button if fullscreen mode is active and hideOnFullScreen is true
if (hideOnFullScreen && isFullScreenMode) {
return null
}

//display the button according to the props
return (
<span
class="btn btn-clear btn-close m-1"
aria-label="Close"
onclick={(e) => {
useUiContextFn.haptic()
panels.hide(panelId)
panels.hide(elementId)
if (onclick) {
onclick()
}
Expand Down

0 comments on commit 517f092

Please sign in to comment.