Skip to content

Add page visibility state functionality #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Web/HTML/HTMLDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export function _readyState(doc) {
};
}

export function _visibilityState(doc) {
return function () {
return doc.readyState;
};
}

export function _activeElement(doc) {
return function () {
return doc.activeElement;
Expand Down
8 changes: 8 additions & 0 deletions src/Web/HTML/HTMLDocument.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Web.HTML.HTMLDocument
, head
, body
, readyState
, visibilityState
, activeElement
, currentScript
, referrer
Expand All @@ -34,6 +35,8 @@ import Web.DOM.ParentNode (ParentNode)
import Web.Event.EventTarget (EventTarget)
import Web.HTML.HTMLDocument.ReadyState (ReadyState)
import Web.HTML.HTMLDocument.ReadyState as ReadyState
import Web.HTML.HTMLDocument.VisibilityState (VisibilityState)
import Web.HTML.HTMLDocument.VisibilityState as VisibilityState
import Web.HTML.HTMLElement (HTMLElement)
import Web.HTML.HTMLHtmlElement (HTMLHtmlElement)
import Web.HTML.HTMLScriptElement (HTMLScriptElement)
Expand Down Expand Up @@ -91,6 +94,11 @@ foreign import _readyState :: HTMLDocument -> Effect String
readyState :: HTMLDocument -> Effect ReadyState
readyState = map (fromMaybe ReadyState.Loading <<< ReadyState.parse) <<< _readyState

foreign import _visibilityState :: HTMLDocument -> Effect String

visibilityState :: HTMLDocument -> Effect VisibilityState
visibilityState = map (fromMaybe VisibilityState.Visible <<< VisibilityState.parse) <<< _visibilityState

foreign import _activeElement :: HTMLDocument -> Effect (Nullable HTMLElement)

activeElement :: HTMLDocument -> Effect (Maybe HTMLElement)
Expand Down
27 changes: 27 additions & 0 deletions src/Web/HTML/HTMLDocument/VisibilityState.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Web.HTML.HTMLDocument.VisibilityState where

import Prelude
import Data.Maybe (Maybe(..))

data VisibilityState
= Visible
| Hidden

derive instance eqVisibilityState :: Eq VisibilityState
derive instance ordVisibilityState :: Ord VisibilityState

instance showVisibilityState :: Show VisibilityState where
show = case _ of
Visible -> "Visible"
Hidden -> "Hidden"

print :: VisibilityState -> String
print = case _ of
Visible -> "loading"
Hidden -> "interactive"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify where this is stated in the doc you referenced?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, shouldn’t this be “hidden” and “visible”? Reading through the spec I didn’t see mention of these wrt visibility state.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahem uhm err, I definitely didn't copy and paste this from ReadyState and forget to update these 🤦‍♂️


parse :: String -> Maybe VisibilityState
parse = case _ of
"visible" -> Just Visible
"hidden" -> Just Hidden
_ -> Nothing