Skip to content

Commit 818c68d

Browse files
committed
feat(pat navigation): Implement scroll-trigger-selector option.
Define the CSS selector which is used to find navigation links with hash URLs. The default is "a[href^='#'].scroll-marker" which would find all anchor elements which href starts with a "#" sign and have the class scroll-marker. The restriction on scroll-marker allows for other hash-urls in the same navigation - e.g. a pat-tooltip which references a local content. If you set it to "none" the scroll marker functionality is not activated.
1 parent 678aee5 commit 818c68d

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/pat/navigation/documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ The available options are:
3636
| scrill-item-side | `top` | `top`, `bottom`, `middle`, `auto` | Side of element that scrolls. This is used to calculate the current item. The defined side of the element will be used to calculate the distance baseline. If this is set to "auto" then one of the "top" or "bottom" positions are used, depending on which one is nearer to the distance baseline. |
3737
| scrill-item-distance | `50%` | CSS length (px, %, vw, vh, vmin or vmax) | Distance from side of scroll box. any amount in px, %, vw, vh, vmin or vmax. This is used to calculate the current item. The nearest element to the distance-baseline measured from the top of the container will get the current class. |
3838
| scrill-item-visibility | | `none`, `most-visible` | Visibility of element in scroll box. This is used to calculate the current item. If "most-visible" is set, the element which is most visible in the scroll container gets the current class. If more elements have the same visibility ratio, the other conditions are used to calculate the current one. |
39+
| scroll-trigger-selector | `a[href^='#'].scroll-marker` | CSS selector, `none` | Selects the element within the pat-navigation container that should get a class current while scrolling, when applicable. |
3940

src/pat/navigation/navigation.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ parser.addArgument("current-content-class", "navigation-current");
2020
parser.addArgument("scroll-item-side", "top", ["top", "bottom", "middle", "auto"]);
2121
parser.addArgument("scroll-item-distance", "50%");
2222
parser.addArgument("scroll-item-visibility", null, [null, "none", "most-visible"]);
23+
parser.addArgument("scroll-trigger-selector", "a[href^='#'].scroll-marker");
2324

2425
class Pattern extends BasePattern {
2526
static name = "navigation";
@@ -34,25 +35,28 @@ class Pattern extends BasePattern {
3435
this.init_listeners();
3536
this.init_markings();
3637

37-
this.scroll_marker = new ScrollMarker(this.el, {
38-
"current-class": this.options["current-class"],
39-
"current-content-class": this.options["current-content-class"],
40-
"in-view-class": this.options["in-view-class"],
41-
"side": this.options["scroll-item-side"],
42-
"distance": this.options["scroll-item-distance"],
43-
"visibility": this.options["scroll-item-visibility"],
44-
});
38+
if (utils.is_option_truthy(this.options["scroll-trigger-selector"])) {
39+
this.scroll_marker = new ScrollMarker(this.el, {
40+
"current-class": this.options["current-class"],
41+
"current-content-class": this.options["current-content-class"],
42+
"in-view-class": this.options["in-view-class"],
43+
"side": this.options["scroll-item-side"],
44+
"distance": this.options["scroll-item-distance"],
45+
"visibility": this.options["scroll-item-visibility"],
46+
"selector": this.options["scroll-trigger-selector"],
47+
});
4548

46-
this.debounced_scroll_marker_enabler = utils.debounce(() => {
47-
log.debug("Enable scroll-marker.");
48-
this.scroll_marker.set_current_disabled = false;
49-
events.remove_event_listener(
50-
this.scroll_marker.scroll_container === window
51-
? document
52-
: this.scroll_marker.scroll_container,
53-
"pat-navigation__scroll_marker_enable"
54-
);
55-
}, 200);
49+
this.debounced_scroll_marker_enabler = utils.debounce(() => {
50+
log.debug("Enable scroll-marker.");
51+
this.scroll_marker.set_current_disabled = false;
52+
events.remove_event_listener(
53+
this.scroll_marker.scroll_container === window
54+
? document
55+
: this.scroll_marker.scroll_container,
56+
"pat-navigation__scroll_marker_enable"
57+
);
58+
}, 200);
59+
}
5660
}
5761

5862
/**
@@ -72,6 +76,12 @@ class Pattern extends BasePattern {
7276
// Mark the current item
7377
this.mark_current(ev.target);
7478

79+
if (
80+
!utils.is_option_truthy(this.options["scroll-trigger-selector"])
81+
) {
82+
// Don't do the scroll-marker stuff below, if it is disabled by the settings.
83+
return;
84+
}
7585
// Disable scroll marker to set the current class after
7686
// clicking in the menu and scrolling to the target.
7787
log.debug("Disable scroll-marker.");

0 commit comments

Comments
 (0)