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:
- jQuery dependency for what is now a 10-line vanilla behavior.
js/scroll-to-top.jsdepends oncore/jqueryand optionallycore/jquery.ui.effects.core. Nativewindow.scrollTo({ top: 0, behavior: 'smooth' })plusscroll+resizelisteners cover the full behavior without jQuery. - No
prefers-reduced-motionsupport. jQuery'sanimate()ignores the user's motion preference. Users who opt into reduced motion still get the animated scroll. easing(swing/linear) only makes sense with jQuery UI. If we drop jQuery, the config field becomes meaningless.durationis a free-form textfield. Schema saysintegerbut the form accepts any string. Should be#type=numberwithmin/max/step.position: 'static'is a misleading option label. The CSS propertyposition: staticis unrelated to what this setting controls. Something like Inline (rendered where the block is placed) reads more clearly.offset: 0is a hidden "always visible" shortcut.build()addsbs-lib-scroll-to-top--showat render time whenoffset == 0. Only has a visible effect inposition: fixedand the branching is undocumented. Either drop it or make it an explicit "Always visible" checkbox.$this.click(...)is a deprecated jQuery shorthand. Moot if we go vanilla.- Form descriptions don't explain the rendered markup.
textis wrapped in<span class="label">so site builders can apply thehide-labelutility 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.labelwrapper without reading source. - No template override.
build()returns#type=linkdirectly. Themes that want to change the markup (SVG icon, different structure) have to subclass the plugin or post-process render arrays. Abs-lib-scroll-to-top.html.twigtemplate 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
durationto#type=numberwith#min,#step,#default_value, and a reasonable max. - Convert
offsetandoffset_opacityto#type = number; updatebs_lib.schema.ymlfromstringtointegerwith a post_update casting existing values. - Reword the
textfield description to mention the.labelwrapper and thehide-labelhelper. - Reword
positionoption labels; keep stored values asstatic/fixedfor 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.twigrendering the default markup. - Rewrite
BSLibScrollToTopBlock::build()to return a themed render array instead of#type=link. - Add
hook_theme()inbs_lib.module.
Commit 3 — Vanilla JS rewrite + reduced motion
- Rewrite
js/scroll-to-top.jswithout jQuery. Usecore/oncefor attach,window.scrollTowithbehavior: 'smooth' / 'auto'branched onprefers-reduced-motion, passivescrolllistener +requestAnimationFramefor the show/hide toggle, andIntersectionObserveronposition_fixed_disable_elementfor the fixed → absolute toggle (more efficient and accurate than the currentgetBoundingClientRect()polling). - Drop the
easingconfig field; post_update removes it from existing blocks; schema removes it. - Drop the conditional
core/jquery.ui.effects.corelibrary dep. - Keep the
durationfield 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/-ycustom properties in the module README. - (Optional) Add
--bs-lib-scroll-to-top-offset-inline/-blocklogical-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 optionallyduration). Ship ahook_post_update_NNNN()that loads eachbs_lib_scroll_to_top_block, removes the deprecated keys, and saves. - Downstream sites that override
js/scroll-to-top.jswill 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: staticon small / big screens;position: fixedwith and withoutposition_fixed_disable_element;offset: 0replacement path;prefers-reduced-motion: reducehonored on click; RTL. - Update
README.mdwith 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_wrappersetting routes classes onto the outer<div class="block …">via#attributes, so visibility utilities likehide-size-bigcollapse the whole block instead of only the inner<a>. Composes withblock_class.
Issue fork bs_lib-3333020
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
Comment #2
pivica commentedComment #3
pivica commentedComment #6
pivica commentedDone.