MCPcopy Create free account
hub / github.com/ember-learn/ember-api-docs / scrollToHash

Function scrollToHash

app/utils/url-hash-polyfill.js:26–66  ·  view source on GitHub ↗
(hash)

Source from the content-addressed store, hash-verified

24}
25
26export function scrollToHash(hash) {
27 let selector = `[name="${hash}"]`;
28 let element =
29 document.getElementById(hash) || document.querySelector(selector);
30
31 if (!element) {
32 warn(
33 `Tried to scroll to element with id or name "${hash}", but it was not found`,
34 {
35 id: 'no-hash-target',
36 },
37 );
38
39 return;
40 }
41
42 /**
43 * NOTE: the ember router does not support hashes in the URL
44 * https://github.com/emberjs/rfcs/issues/709
45 *
46 * this means that when testing hash changes in the URL,
47 * we have to assert against the window.location, rather than
48 * the self-container currentURL helper
49 *
50 * NOTE: other ways of changing the URL, but without the smoothness:
51 * - window[.top].location.replace
52 */
53
54 element.scrollIntoView({ behavior: 'smooth' });
55
56 if (hash !== window.location.hash) {
57 let withoutHash = location.href.split('#')[0];
58 let nextUrl = `${withoutHash}#${hash}`;
59 // most browsers ignore the title param of pushState
60 let titleWithoutHash = document.title.split(' | #')[0];
61 let nextTitle = `${titleWithoutHash} | #${hash}`;
62
63 history.pushState({}, nextTitle, nextUrl);
64 document.title = nextTitle;
65 }
66}
67
68function isLoadingRoute(routeName) {
69 return routeName.endsWith('_loading') || routeName.endsWith('.loading');

Callers 1

eventuallyTryScrollingToFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected