Skip to content

Commit f2414f7

Browse files
committed
field auto scroll option
1 parent 129d3da commit f2414f7

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

ghcjs/delivery-calculator/src/App/Jsm.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
module App.Jsm (fetchBlobUris) where
1+
module App.Jsm
2+
( fetchBlobUris,
3+
)
4+
where
25

36
import App.Types
47
import qualified Data.ByteString.Lazy as BL

ghcjs/miso-functora/src/Functora/Miso/Jsm/Generic.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Functora.Miso.Jsm.Generic
2121
saveFileThen,
2222
fetchUrlAsRfc2397,
2323
setValue,
24+
scrollTo,
2425
)
2526
where
2627

@@ -302,3 +303,18 @@ setValue uid value = do
302303
$ htmlUid uid
303304
is <- ghcjsPure $ JS.isTruthy el
304305
when is $ el ^. JS.jss ("value" :: Unicode) value
306+
307+
scrollTo :: Uid -> JSM ()
308+
scrollTo uid = do
309+
el <-
310+
getElementById
311+
. either impureThrow id
312+
. decodeUtf8Strict
313+
. unTagged
314+
$ htmlUid uid
315+
is <- ghcjsPure $ JS.isTruthy el
316+
jarg <- JS.toJSVal $ toJSON arg
317+
when is . void $ el ^. JS.js1 ("scrollIntoView" :: Unicode) jarg
318+
where
319+
arg :: Map Unicode Unicode
320+
arg = [("behavior", "smooth"), ("block", "center")]

ghcjs/miso-functora/src/Functora/Miso/Widgets/Field.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ data Opts model action t f = Opts
4949
optsLabel :: Maybe Unicode,
5050
optsOnFocus :: Field t f -> Update model -> Update model,
5151
optsFullWidth :: Bool,
52+
optsAutoScroll :: Bool,
5253
optsPlaceholder :: Unicode,
5354
optsOnInputAction :: Maybe (Update model -> action),
5455
optsTrailingWidgets ::
@@ -71,6 +72,7 @@ defOpts =
7172
optsLabel = Nothing,
7273
optsOnFocus = const id,
7374
optsFullWidth = False,
75+
optsAutoScroll = True,
7476
optsPlaceholder = mempty,
7577
optsOnInputAction = Nothing,
7678
optsTrailingWidgets = defTrailingWidgets,
@@ -320,7 +322,13 @@ field full@Full {fullArgs = args, fullParser = parser, fullViewer = viewer} opts
320322
.~ Blurred
321323
onFocusAction =
322324
action
323-
. ( maybe id (optsOnFocus opts) $ st ^? cloneTraversal optic
325+
. ( maybe id (optsOnFocus opts)
326+
$ st
327+
^? cloneTraversal optic
328+
)
329+
. ( if optsAutoScroll opts
330+
then addEffect $ Jsm.scrollTo uid
331+
else id
324332
)
325333
. PureUpdate
326334
$ cloneTraversal optic

0 commit comments

Comments
 (0)