Skip to content

Commit 302f413

Browse files
Nils Lindemanngdotdesign
andauthored
Add jump() function (#616)
Co-authored-by: Szikszai Gusztáv <[email protected]>
1 parent a037f8c commit 302f413

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

core/source/Window.mint

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ module Window {
163163
window.port == current.port)
164164
}
165165

166+
/*
167+
Like `Window.navigate()`, but also triggers a jump to the start of the
168+
document or the hash, if it exists.
169+
170+
Window.jump("/new-url")
171+
*/
172+
fun jump (url : String) : Promise(Void) {
173+
`_navigate(
174+
#{url},
175+
/* dispatch */ true,
176+
/* triggerJump */ true,
177+
/* routeInfo */ null
178+
)`
179+
}
180+
166181
/*
167182
Returns `true` if the given media query matches.
168183
@@ -173,16 +188,22 @@ module Window {
173188
}
174189

175190
/*
176-
Navigates to the given URL.
191+
Sets the URL of the window. If there is a route defined for this URL, runs
192+
its handler. Updates the navigation history.
177193
178-
Window.navigate("https://www.example.com")
194+
Window.navigate("/new-url")
179195
*/
180196
fun navigate (url : String) : Promise(Void) {
181-
`_navigate(#{url})`
197+
`_navigate(
198+
#{url},
199+
/* dispatch */ true,
200+
/* triggerJump */ false,
201+
/* routeInfo */ null
202+
)`
182203
}
183204

184205
/*
185-
Opens the given url in a new window.
206+
Opens the given url in a new window or tab.
186207
187208
Window.open("https://www.google.com")
188209
*/
@@ -303,12 +324,17 @@ module Window {
303324
}
304325

305326
/*
306-
Sets the URL of the window without navigating to it.
327+
Sets the URL of the window.
307328
308-
Window.setUrl("https://www.example.com")
329+
Window.setUrl("/new-url")
309330
*/
310331
fun setUrl (url : String) : Promise(Void) {
311-
`_navigate(#{url}, false)`
332+
`_navigate(
333+
#{url},
334+
/* dispatch */ false,
335+
/* triggerJump */ false,
336+
/* routeInfo */ null
337+
)`
312338
}
313339

314340
/*
@@ -321,15 +347,17 @@ module Window {
321347
}
322348

323349
/*
324-
Triggers the hash location jump on the page.
350+
Triggers a jump to the current location on the page.
325351
326-
When a page loads and the current url has a hash `#anchor-name` the browser
327-
automatically jumps to the matching anchor tag `<a name="anchor-name">`, but
328-
this behavior does not happen when the history is manipulated.
352+
When a page loads and the current url has a hash `#anchor-name`, the browser
353+
automatically jumps to the element with the matching id or to the anchor tag
354+
with the matching name `<a name="anchor-name">`. This behavior does not happen
355+
when the history is manipulated manually.
329356
330-
This function triggers that behavior.
357+
This function triggers that behavior. When there is a hash in the current URL,
358+
it jumps to it, otherwise it jumps to the start of the document.
331359
*/
332-
fun triggerHashJump : Promise(Void) {
360+
fun triggerJump : Promise(Void) {
333361
`requestAnimationFrame(() => {
334362
if (window.location.hash) {
335363
window.location.href = window.location.hash

src/assets/runtime.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)