-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate selector before calling
scrollIntoView
Fix #4
- Loading branch information
1 parent
6906b78
commit f2cd6f7
Showing
2 changed files
with
22 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/** | ||
* Smooth scrolling onClick event handler | ||
* @param {string} selector argument will be passed to `querySelector`, usually an HTML id | ||
* @returns {boolean} false if `document.querySelector` doesn't find a match, otherwise true | ||
*/ | ||
declare const scrollTo: (selector: string) => void; | ||
declare const scrollTo: (selector: string) => boolean; | ||
export default scrollTo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
/** | ||
* Smooth scrolling onClick event handler | ||
* @param {string} selector argument will be passed to `querySelector`, usually an HTML id | ||
* @returns {boolean} false if `document.querySelector` doesn't find a match, otherwise true | ||
*/ | ||
const scrollTo = (selector) => { | ||
document.querySelector(selector).scrollIntoView({ | ||
behavior: 'smooth', | ||
block: 'start', | ||
}); | ||
const element = document.querySelector(selector); | ||
|
||
if (element) { | ||
element.scrollIntoView({ | ||
behavior: 'smooth', | ||
block: 'start', | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn( | ||
"gatsby-plugin-smoothscroll:\n The selector: '%s' wasn't found in the document.\n Make sure you pass in a valid CSS selector string.", | ||
selector | ||
); | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export default scrollTo; |