@@ -43,6 +43,11 @@ module Web.DOM.Element
4343 , DOMRect
4444 , ShadowRootInit
4545 , attachShadow
46+ , scrollIntoView
47+ , scrollIntoViewWithOptions
48+ , ScrollBehavior (..)
49+ , ScrollLogicalPosition (..)
50+ , ScrollIntoViewOptions
4651 ) where
4752
4853import Prelude
@@ -188,3 +193,56 @@ initToProps init =
188193 }
189194
190195foreign import _attachShadow :: ShadowRootProps -> Element -> Effect ShadowRoot
196+
197+ -- | Scrolls the element's ancestor containers so that the element is visible,
198+ -- | using the default alignment (`element.scrollIntoView()`).
199+ foreign import scrollIntoView :: Element -> Effect Unit
200+
201+ -- | How the scroll is animated.
202+ data ScrollBehavior = Auto | Instant | Smooth
203+
204+ derive instance Eq ScrollBehavior
205+
206+ -- | Where the element is aligned within the scrollport.
207+ data ScrollLogicalPosition = Start | Center | End | Nearest
208+
209+ derive instance Eq ScrollLogicalPosition
210+
211+ type ScrollIntoViewOptions =
212+ { behavior :: ScrollBehavior
213+ , block :: ScrollLogicalPosition
214+ , inline :: ScrollLogicalPosition
215+ }
216+
217+ -- | Scrolls the element into view with the given behavior and alignment
218+ -- | (`element.scrollIntoView(options)`).
219+ scrollIntoViewWithOptions :: ScrollIntoViewOptions -> Element -> Effect Unit
220+ scrollIntoViewWithOptions = _scrollIntoViewWithOptions <<< optionsToProps
221+
222+ type ScrollIntoViewProps =
223+ { behavior :: String
224+ , block :: String
225+ , inline :: String
226+ }
227+
228+ optionsToProps :: ScrollIntoViewOptions -> ScrollIntoViewProps
229+ optionsToProps options =
230+ { behavior: behaviorToString options.behavior
231+ , block: logicalPositionToString options.block
232+ , inline: logicalPositionToString options.inline
233+ }
234+
235+ behaviorToString :: ScrollBehavior -> String
236+ behaviorToString = case _ of
237+ Auto -> " auto"
238+ Instant -> " instant"
239+ Smooth -> " smooth"
240+
241+ logicalPositionToString :: ScrollLogicalPosition -> String
242+ logicalPositionToString = case _ of
243+ Start -> " start"
244+ Center -> " center"
245+ End -> " end"
246+ Nearest -> " nearest"
247+
248+ foreign import _scrollIntoViewWithOptions :: ScrollIntoViewProps -> Element -> Effect Unit
0 commit comments