Skip to content

Commit 9b50c08

Browse files
Add Element.scrollIntoView and scrollIntoViewWithOptions (#64)
Binds element.scrollIntoView() and the options form, with typed ScrollBehavior (Auto/Instant/Smooth) and ScrollLogicalPosition (Start/Center/End/Nearest), converted to the wire record by dedicated encoders (not Show).
1 parent 326c125 commit 9b50c08

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Breaking changes:
1515
New features:
1616
- `AttrName`, `ClassName`, and `PropName` types have been added, migrated from [web-html](https://github.com/purescript-web/purescript-web-html). (#58 by @nsaunders)
1717
- A new `ElementId` type, representing the value of an `id` property/attribute, has been added. (#58 by @nsaunders)
18+
- `scrollIntoView` and `scrollIntoViewWithOptions` (with typed `ScrollBehavior` and `ScrollLogicalPosition`) have been added. (#64 by @i-am-the-slime)
1819

1920
Bugfixes:
2021

src/Web/DOM/Element.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,17 @@ export function _attachShadow(props) {
210210
};
211211
};
212212
}
213+
214+
export function scrollIntoView(el) {
215+
return function () {
216+
el.scrollIntoView();
217+
};
218+
}
219+
220+
export function _scrollIntoViewWithOptions(props) {
221+
return function (el) {
222+
return function () {
223+
el.scrollIntoView(props);
224+
};
225+
};
226+
}

src/Web/DOM/Element.purs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4853
import Prelude
@@ -188,3 +193,56 @@ initToProps init =
188193
}
189194

190195
foreign 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

Comments
 (0)