Skip to content

Add support for ShadowRoot API #34

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 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- uses: purescript-contrib/setup-purescript@main
with:
purescript: "0.14.0-rc3"
purescript: "0.14.0-rc5"

- uses: actions/setup-node@v1
with:
Expand Down
19 changes: 19 additions & 0 deletions src/Web/DOM/ShadowRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

exports._attachShadow = function(props) {
return function (el) {
return function() {
return el.attachShadow(props);
};
};
};

exports._mode = function (el) {
return el.mode;
};

exports.host = function (el) {
return function() {
return el.host;
};
};
47 changes: 47 additions & 0 deletions src/Web/DOM/ShadowRoot.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Web.DOM.ShadowRoot
( ShadowRoot
, ShadowRootMode (..)
, attachShadow
, host
, mode
, toNode
) where

import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Unsafe.Coerce (unsafeCoerce)
import Web.DOM.Internal.Types (Element, Node)

foreign import data ShadowRoot :: Type

data ShadowRootMode = Open | Closed

instance showShadowRootMode :: Show ShadowRootMode where
show Open = "open"
show Closed = "closed"

attachShadow :: ShadowRootMode -> Element -> Effect ShadowRoot
attachShadow = _attachShadow <<< modeToProps
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest this belongs in Web.DOM.Element, since the member is part of the Element interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Gary, thank you for the suggestion. I have moved the function and related stuff to Web.DOM.Element and updated it to accept an argument of type ShadowRootInit type as per the specs.


mode :: ShadowRoot -> Maybe ShadowRootMode
mode = modeFromString <<< _mode

foreign import host :: ShadowRoot -> Effect Element

toNode :: ShadowRoot -> Node
toNode = unsafeCoerce

type ShadowRootProps = { mode :: String }

foreign import _attachShadow :: ShadowRootProps -> Element -> Effect ShadowRoot
foreign import _mode :: ShadowRoot -> String

modeToProps :: ShadowRootMode -> ShadowRootProps
modeToProps m = { mode: (show m) }

modeFromString :: String -> Maybe ShadowRootMode
modeFromString m = case m of
"open" -> Just Open
"closed" -> Just Closed
_ -> Nothing