Skip to content

Commit

Permalink
Concatenate multiple classes together (#607)
Browse files Browse the repository at this point in the history
* Concatenate multiple classes together

- Remove unnecessary 'import Data.Monoid'
- Update iso logic for example

* Branch on mountPoint child.
  • Loading branch information
dmjio authored Jul 5, 2020
1 parent 4fcd792 commit b462467
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 0 additions & 1 deletion ghc-src/Miso/Html/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ module Miso.Html.Internal (

import Data.Aeson (Value(..), ToJSON(..))
import qualified Data.Map as M
import Data.Monoid
import Data.Proxy
import Data.String (IsString(..))
import qualified Data.Text as T
Expand Down
5 changes: 5 additions & 0 deletions ghcjs-ffi/Miso/FFI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,13 @@ syncPoint = pure ()

-- | Set property on object
set :: ToJSVal v => JSString -> v -> OI.Object -> IO ()
set "class" v obj = toJSVal v >>= appendClass obj
set k v obj = toJSVal v >>= \x -> OI.setProp k x obj

-- | Only used for 'class', guaranteed to be a MisoString
foreign import javascript unsafe "if ('class' in $1) { $1['class'] += ' ' + $2; } else { $1['class'] = $2; }"
appendClass :: OI.Object -> JSVal -> IO ()

foreign import javascript unsafe "$1.addEventListener($2, $3);"
addEventListener' :: JSVal -> JSString -> Callback (JSVal -> IO ()) -> IO ()

Expand Down
12 changes: 12 additions & 0 deletions jsaddle-ffi/Miso/FFI.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE LambdaCase #-}
-----------------------------------------------------------------------------
-- |
Expand Down Expand Up @@ -80,6 +81,17 @@ objectToJSVal = toJSVal

-- | Set property on object
set :: ToJSVal v => JSString -> v -> OI.Object -> JSM ()
set (unpack -> "class") v obj = do
classSet <- ((pack "class") `elem`) <$> listProps obj
if classSet
then do
classStr <- fromJSValUnchecked =<< getProp (pack "class") obj
vStr <- fromJSValUnchecked =<< toJSVal v
v' <- toJSVal (classStr <> pack " " <> vStr)
setProp (pack "class") v' obj
else do
v' <- toJSVal v
setProp (pack "class") v' obj
set k v obj = do
v' <- toJSVal v
setProp k v' obj
Expand Down
8 changes: 6 additions & 2 deletions jsbits/isomorphic.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ window['copyDOMIntoVTree'] = function copyDOMIntoVTree(logLevel,mountPoint, vtre
} else if (mountPoint.childNodes.length === 0) {
node = mountPoint.appendChild (doc.createElement('div'));
} else {
while (mountPoint.childNodes[mountChildIdx].localName === 'script' || mountPoint.childNodes[mountChildIdx].nodeType === Node.TEXT_NODE){
while (mountPoint.childNodes[mountChildIdx] && (mountPoint.childNodes[mountChildIdx].nodeType === Node.TEXT_NODE || mountPoint.childNodes[mountChildIdx].localName === 'script')){
mountChildIdx++;
}
node = mountPoint.childNodes[mountChildIdx];
if (!mountPoint.childNodes[mountChildIdx]) {
node = doc.body.appendChild (doc.createElement('div'));
} else {
node = mountPoint.childNodes[mountChildIdx];
}
}

if (!window['walk'](logLevel,vtree, node, doc)) {
Expand Down

0 comments on commit b462467

Please sign in to comment.