Intern commands API, powered by Leadfoot, is powerful and easy to use. However, it’s missing some handful methods. Let’s see how to extend it with custom commands.
The key part is the JS wrapper setup. You need to require the leadfoot/Command module and then extend its prototype with additional methods. Your custom command could be a simple shorthand for a series of other commands, or it can be something more complex.
This is not the ideal solution because we are extending the original Command prototype. However, Intern does not allow you to extend its custom command object, so pay attention to not override Leadfoot/Intern methods.
Finally, keep in mind that Command is different from Element, so your last method in the queue needs to return a Command instance (if you use find() then add end() before returning) or your code will fail.
Implementing window scrollTo command
Implementation example of window.scrollTo command that animates nicely, like users do.
Implementing window scrollIntoView command
Implementation example of window.scrollIntoView command that animates nicely, like users do.
In both scroll custom commands, the duration can be fixed, passed as an argument, or it can be calculated by the command itself based on the distance from the current scroll position.