Problem/Motivation
Views uses an automatic "scroll to top" behaviour when using AJAX. While it can be useful in some situations (eg. switching to the next page of a long list) in other situations it can be annoying (eg. Views block with exposed filter in the middle of a page).
Steps to reproduce
Create Views block with exposed filter that uses AJAX and place it in the middle of a page. Use the filter. The page will scroll to the top, above the view.
Proposed resolution
Add a setting option to Views AJAX UI to disable the "scroll to top" behaviour. It should be possible to set at the display level.
Remaining tasks
…
User interface changes
The Views UI will have a setting option for AJAX on every display to disable the "scroll to top" behaviour.
API changes
…
Data model changes
…
Release notes snippet
…
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 3273068-d11-with-test.patch | 4.91 KB | b_sharpe |
| #4 | 3273068-4.patch | 2.58 KB | skyredwang |
| #3 | 3273068-3.patch | 2.58 KB | skyredwang |
Issue fork drupal-3273068
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
skyredwangComment #3
skyredwangFixed path in the patch
Comment #4
skyredwangFixed a typo in a comment
Comment #6
chucksimply commented#4 patch works for me on 10.1.1. This is a small but necessary option.
Comment #7
lauriiiComment #8
bduell commentedThis is great - thank you @skyredwang
Comment #9
thidd commentedThat's cool, thanks @skyreddwang,
Tested on D9.5.8 /PHP 8.1/MariaDB 10.3
Comment #10
bobi-mel commentedHi @skyredwang
I applied the patch #4. Works well.
Thanks
Comment #11
znak commentedI think it's reviewed and tested by community, so I think we can change status of this issue
Comment #12
nod_Thanks for working on this, the patch should be moved to a Merge Request so that tests can run. Talking about tests, a test should be added per #7 to make sure we don't break that feature down the line.
Thanks!
Comment #15
sahana _n commentedHi ,
I tried to reproduce the issue issue is replaceable in 11. x.
And I created the MR for 11. x and tests are passed.
RTBC++
Please let me know if I missed anything happy to take suggestions.
Thank You!!
Comment #16
smustgrave commentedPreviously tagged for tests which are still needed.
Did not review.
Comment #18
b_sharpe commentedAdded unit test to prove new functionality.
Attaching patch simply for composer.
Comment #19
smustgrave commentedSo I imagine with this config change we will need a schema update for the new setting.
Existing view config will have to probably be updated.
Also imagine there will have to be a post_update hook to set the new key for existing views (should be batch)
Comment #20
oily commentedComment #21
oily commentedA unit test is in place. Test-only test fails as it should. Removing 'Needs tests' tag. If further test coverage is needed, please update.
Comment #22
chris burge commented+1 for this addition to core. In the meantime, you can also accomplish this with an event subscriber:
In my case, I wanted to modify the selector to the views'
.view-contentclass, which is only a minor change:It'd be worthwhile to consider allowing a site builder to set the
scrollTopselector instead of adding a toggle. It would meet my use case, which is necessitated by responsive design (where the exposed filters stack atop the content on mobile).Comment #23
phreaknerd commentedReviewed and tested patch #4 for several month on D10 and it's working fine.
Well done! Thanks!
Comment #24
smustgrave commented#19 still applies
Comment #26
andyf commentedThanks all. I added the schema and while doing that think I spotted an issue: IIUC in
\Drupal\views\Plugin\views\display\DisplayPluginBase::defineOptions()instead of providing a default value, we marked it as non-defaultable (ie it can't be reverted to default). I've updated it so that it is defaultable and also set the default value to off.It still needs a config update to be written (more details in #3447755: Document ViewsConfigUpdater) and presumably existing core views to be updated.
Comment #27
andyf commentedNot a views expert, but it looks to me like we should also add a method to
\Drupal\views\Plugin\views\display\DisplayPluginInterface. I was about to do that when I (at the huge risk of bike-shedding) wondered if it doesn't make more sense to have ascroll_to_topoption that is set to enabled by default, rather than adisable_scroll_to_top. This just avoids things like the double negative ofif (!$view->display_handler->getOption('disable_scroll_to_top')). Thanks!Comment #29
mlzr+1
I my case this "scroll to top" was annoying. I was verry pleased there was a solution! Thanks!
I hope this is going to land in Drupal core.
Thanks,
Marcel
Comment #31
samitk commentedI have rebased the PR to address the PHPCS and PHPStan pipeline issues. All tests are passing in CI.
The current failure appears to be related to the JUnit artifact upload limits (phpunit-*.xml), not an actual test failure.