Problem/Motivation

The bs_lib_scroll_to_top_block plugin and its companion JS/CSS carry legacy design decisions from the jQuery era and surface a few papercuts when used in a modern Drupal 11 site:

  1. jQuery dependency for what is now a 10-line vanilla behavior. js/scroll-to-top.js depends on core/jquery and optionally core/jquery.ui.effects.core. Native window.scrollTo({ top: 0, behavior: 'smooth' }) plus scroll + resize listeners cover the full behavior without jQuery.
  2. No prefers-reduced-motion support. jQuery's animate() ignores the user's motion preference. Users who opt into reduced motion still get the animated scroll.
  3. easing (swing / linear) only makes sense with jQuery UI. If we drop jQuery, the config field becomes meaningless.
  4. duration is a free-form textfield. Schema says integer but the form accepts any string. Should be #type=number with min / max / step.
  5. position: 'static' is a misleading option label. The CSS property position: static is unrelated to what this setting controls. Something like Inline (rendered where the block is placed) reads more clearly.
  6. offset: 0 is a hidden "always visible" shortcut. build() adds bs-lib-scroll-to-top--show at render time when offset == 0. Only has a visible effect in position: fixed and the branching is undocumented. Either drop it or make it an explicit "Always visible" checkbox.
  7. $this.click(...) is a deprecated jQuery shorthand. Moot if we go vanilla.
  8. Form descriptions don't explain the rendered markup. text is wrapped in <span class="label"> so site builders can apply the hide-label utility to visually hide the label while keeping it for screen readers. The form description just says "Text of a scroll to top element." — people don't discover the .label wrapper without reading source.
  9. No template override. build() returns #type=link directly. Themes that want to change the markup (SVG icon, different structure) have to subclass the plugin or post-process render arrays. A bs-lib-scroll-to-top.html.twig template would be a cleaner extension point.

Proposed resolution

Split into logical commits so each can be reviewed independently. Items ordered from lowest risk to biggest behavioral change.

Commit 1 — Form & schema polish (low risk)

  • Convert duration to #type=number with #min, #step, #default_value, and a reasonable max.
  • Convert offset and offset_opacity to #type = number; update bs_lib.schema.yml from string to integer with a post_update casting existing values.
  • Reword the text field description to mention the .label wrapper and the hide-label helper.
  • Reword position option labels; keep stored values as static / fixed for BC.

Commit 2 — Template override

  • Add a theme hook (bs_lib_scroll_to_top) with variables { attributes, link_attributes, label, url }.
  • Add templates/bs-lib-scroll-to-top.html.twig rendering the default markup.
  • Rewrite BSLibScrollToTopBlock::build() to return a themed render array instead of #type=link.
  • Add hook_theme() in bs_lib.module.

Commit 3 — Vanilla JS rewrite + reduced motion

  • Rewrite js/scroll-to-top.js without jQuery. Use core/once for attach, window.scrollTo with behavior: 'smooth' / 'auto' branched on prefers-reduced-motion, passive scroll listener + requestAnimationFrame for the show/hide toggle, and IntersectionObserver on position_fixed_disable_element for the fixed → absolute toggle (more efficient and accurate than the current getBoundingClientRect() polling).
  • Drop the easing config field; post_update removes it from existing blocks; schema removes it.
  • Drop the conditional core/jquery.ui.effects.core library dep.
  • Keep the duration field but mark it deprecated (native smooth scroll doesn't expose a duration knob). Alternative: drop it too with a post_update — decide during review.

Commit 4 — CSS polish

  • Document the --bs-lib-scroll-to-top-offset-x / -y custom properties in the module README.
  • (Optional) Add --bs-lib-scroll-to-top-offset-inline / -block logical-property aliases for LTR/RTL / writing-mode support.

Backward compatibility

  • Commits 1 and 2 are additive; existing block configs keep working.
  • Commit 3 removes easing (and optionally duration). Ship a hook_post_update_NNNN() that loads each bs_lib_scroll_to_top_block, removes the deprecated keys, and saves.
  • Downstream sites that override js/scroll-to-top.js will need to migrate; call this out in the CHANGELOG.

Remaining tasks

  • Commit 1 — Form & schema polish
  • Commit 2 — Theme hook + template override
  • Commit 3 — Vanilla JS rewrite, reduced-motion, drop easing
  • Commit 4 — CSS var documentation + logical-property aliases
  • Manual QA matrix: position: static on small / big screens; position: fixed with and without position_fixed_disable_element; offset: 0 replacement path; prefers-reduced-motion: reduce honored on click; RTL.
  • Update README.md with all user-facing changes.

Related prior work

Two prerequisite rounds have already landed on the 2.0.x branch:

  • CSS custom properties for the fixed offset (--bs-lib-scroll-to-top-offset-x / -y / -z-index), with the consuming theme's wrapper rules scoped to small screens.
  • Wrapper CSS classes: a second css_class_wrapper setting routes classes onto the outer <div class="block …"> via #attributes, so visibility utilities like hide-size-big collapse the whole block instead of only the inner <a>. Composes with block_class.

Issue fork bs_lib-3333020

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pivica created an issue. See original summary.

pivica’s picture

Title: Use vanilla JS for scroll-top-top.js » Use vanilla JS for scroll-to-top.js
Issue summary: View changes
pivica’s picture

Title: Use vanilla JS for scroll-to-top.js » Scroll to top modernization
Version: 8.x-1.x-dev » 2.0.x-dev
Issue summary: View changes

  • pivica committed 8718fc85 on 2.0.x
    fix: #3333020 Modernize scroll to top block. 
    
    - Rewrites the scroll...
pivica’s picture

Status: Active » Fixed

Done.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.