-
Notifications
You must be signed in to change notification settings - Fork 116
Anchor slide & scroll button #3477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { registry } from "@web/core/registry"; | ||
| import { Interaction } from "@website/core/interaction"; | ||
| import { scrollTo } from "@web_editor/js/common/scrolling"; | ||
|
|
||
| export class AnchorSlide extends Interaction { | ||
| static selector = "a[href^='/'][href*='#'], a[href^='#']"; | ||
| static dynamicContent = { | ||
| "_root": { | ||
| "t-on-click": "animateClick", | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * @param {DOMElement} el the element to scroll to. | ||
| * @param {string} [scrollValue='true'] scroll value | ||
| * @returns {Promise} | ||
| */ | ||
| scrollTo(el, scrollValue="true") { | ||
| return scrollTo(el, { | ||
| duration: scrollValue === "true" ? 500 : 0, | ||
| extraOffset: this.computeExtraOffset(), | ||
| }); | ||
| } | ||
| /** | ||
| * To be overridden. | ||
| */ | ||
| computeExtraOffset() { | ||
| return 0; | ||
| } | ||
| /** | ||
| */ | ||
| animateClick(ev) { | ||
| const ensureSlash = path => path.endsWith("/") ? path : path + "/"; | ||
| if (ensureSlash(this.el.pathname) !== ensureSlash(window.location.pathname)) { | ||
| return; | ||
| } | ||
| // Avoid flicker at destination in case of ending "/" difference. | ||
| if (this.el.pathname !== window.location.pathname) { | ||
| this.el.pathname = window.location.pathname; | ||
| } | ||
| let hash = this.el.hash; | ||
| if (!hash.length) { | ||
| return; | ||
| } | ||
| // Escape special characters to make the selector work. | ||
| // TODO Not rely on jQuery to escape anchor name for selector. | ||
| hash = "#" + $.escapeSelector(hash.substring(1)); | ||
| const anchorEl = this.el.ownerDocument.querySelector(hash); | ||
| const scrollValue = anchorEl?.dataset.anchor; | ||
| if (!anchorEl || !scrollValue) { | ||
| return; | ||
| } | ||
|
|
||
| const offcanvasEl = this.el.closest(".offcanvas.o_navbar_mobile"); | ||
| if (offcanvasEl && offcanvasEl.classList.contains("show")) { | ||
| // Special case for anchors in offcanvas in mobile: we can't just | ||
| // scrollTo() after preventDefault because preventDefault would | ||
| // prevent the offcanvas to be closed. The choice is then to close | ||
| // it ourselves manually and once it's fully closed, then start our | ||
| // own smooth scrolling. | ||
| ev.preventDefault(); | ||
| Offcanvas.getInstance(offcanvasEl).hide(); | ||
| offcanvasEl.addEventListener("hidden.bs.offcanvas", | ||
| () => { | ||
| this.manageScroll(hash, anchorEl, scrollValue); | ||
| }, | ||
| // the listener must be automatically removed when invoked | ||
| { once: true } | ||
| ); | ||
| } else { | ||
| ev.preventDefault(); | ||
| this.manageScroll(hash, anchorEl, scrollValue); | ||
| } | ||
| } | ||
| /** | ||
| * | ||
| * @param {string} hash | ||
| * @param {DOMElement} anchorEl the element to scroll to. | ||
| * @param {string} [scrollValue='true'] scroll value | ||
| */ | ||
| manageScroll(hash, anchorEl, scrollValue = "true") { | ||
| if (hash === "#top" || hash === "#bottom") { | ||
| // If the anchor targets #top or #bottom, directly call the | ||
| // "scrollTo" function. The reason is that the header or the footer | ||
| // could have been removed from the DOM. By receiving a string as | ||
| // parameter, the "scrollTo" function handles the scroll to the top | ||
| // or to the bottom of the document even if the header or the | ||
| // footer is removed from the DOM. | ||
| scrollTo(hash, { | ||
| duration: 500, | ||
| extraOffset: this.computeExtraOffset(), | ||
| }); | ||
| } else { | ||
| this.scrollTo(anchorEl, scrollValue); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| registry.category("website.active_elements").add("website.anchor_slide", AnchorSlide); | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { registry } from "@web/core/registry"; | ||
| import { isVisible } from "@web/core/utils/ui"; | ||
| import { AnchorSlide } from "@website/interactions/anchor_slide"; | ||
|
|
||
| export class ScrollButton extends AnchorSlide { | ||
| static selector = ".o_scroll_button"; | ||
|
|
||
| animateClick(ev) { | ||
| ev.preventDefault(); | ||
| // Scroll to the next visible element after the current one. | ||
| const currentSectionEl = this.el.closest("section"); | ||
| let nextEl = currentSectionEl.nextElementSibling; | ||
| while (nextEl) { | ||
| if (isVisible(nextEl)) { | ||
| this.scrollTo(nextEl); | ||
| return; | ||
| } | ||
| nextEl = nextEl.nextElementSibling; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| registry.category("website.active_elements").add("website.scroll_button", ScrollButton); |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.