Skip to content

Commit 678aee5

Browse files
committed
feat(pat scroll-marker): Implement selector option.
Define the CSS selector which is used to find navigation links with hash URLs. The default is "a[href^='#']" which would find all anchor elements which href starts with a "#" sign.
1 parent 456e05a commit 678aee5

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/pat/scroll-marker/documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ Here is a complete example:
5656
| 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. |
5757
| 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. |
5858
| 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. |
59+
| selector | `a[href^='#']` | CSS selector, `none` | Selects the element within the pat-navigation container that should get a class current while scrolling, when applicable. |
5960

src/pat/scroll-marker/scroll-marker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ parser.addArgument("current-content-class", "scroll-marker-current");
1616
parser.addArgument("side", "top", ["top", "bottom", "middle", "auto"]);
1717
parser.addArgument("distance", "50%");
1818
parser.addArgument("visibility", null, [null, "none", "most-visible"]);
19+
parser.addArgument("selector", "a[href^='#']");
1920

2021
class Pattern extends BasePattern {
2122
static name = "scroll-marker";
@@ -31,7 +32,7 @@ class Pattern extends BasePattern {
3132
init() {
3233
// Get all elements that are referenced by links in the current page.
3334
this.observables = new Map(
34-
[...dom.querySelectorAllAndMe(this.el, "a[href^='#']")]
35+
[...dom.querySelectorAllAndMe(this.el, this.options.selector)]
3536
.map(
3637
// Create the structure:
3738
// id: {link, target}

src/pat/scroll-marker/scroll-marker.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,30 @@ describe("pat-scroll-marker", () => {
232232
expect(id2.classList.contains("scroll-marker-current")).toBe(true);
233233
expect(id3.classList.contains("scroll-marker-current")).toBe(false);
234234
});
235+
236+
it("1.5: The selector option can exclude some items", async () => {
237+
// With the selector option you can include/exclude some items from
238+
// being included in the scroll-marker functionality.
239+
240+
const { nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
241+
await create_scroll_marker({
242+
options: {
243+
selector: "a[href^='#']:not([href='#id3'])",
244+
},
245+
});
246+
247+
expect(nav_id1.classList.contains("in-view")).toBe(false);
248+
expect(nav_id2.classList.contains("in-view")).toBe(true);
249+
expect(nav_id3.classList.contains("in-view")).toBe(false);
250+
251+
expect(nav_id1.classList.contains("current")).toBe(false);
252+
expect(nav_id2.classList.contains("current")).toBe(true);
253+
expect(nav_id3.classList.contains("current")).toBe(false);
254+
255+
expect(id1.classList.contains("scroll-marker-current")).toBe(false);
256+
expect(id2.classList.contains("scroll-marker-current")).toBe(true);
257+
expect(id3.classList.contains("scroll-marker-current")).toBe(false);
258+
});
235259
});
236260

237261
describe("2: Test on element as scroll container", () => {

0 commit comments

Comments
 (0)