vaadin / flow-components

Java counterpart of Vaadin Web Components
101 stars 66 forks source link

Scroller: add support for programmatic scrolling #6283

Open mvysny opened 4 months ago

mvysny commented 4 months ago

Describe your motivation

I'm migrating an app from Vaadin 8 and searching for a Panel replacement. Scroller is a natural candidate for that, unfortunately it doesn't support setScrollTop() nor setScrollLeft().

Describe the solution you'd like

Add Scroller.setScrollTop(int) and Scroller.setScrollLeft(int) which would accept a non-negative value in pixels to set the scrolling offset on the client-side.

Describe alternatives you've considered

No response

Additional context

No response

mvysny commented 4 months ago

Workaround: call element.scroll():

public class VovScroller extends Scroller implements Scrollable {
    @Override
    public void scroll(@Nullable Integer top, @Nullable Integer left) {
        if (top != null && top < 0) {
            throw new IllegalArgumentException("Parameter top: invalid value " + top + ": must be 0 or greater");
        }
        if (left != null && left < 0) {
            throw new IllegalArgumentException("Parameter top: invalid value " + left + ": must be 0 or greater");
        }
        // https://developer.mozilla.org/en-US/docs/Web/API/Element/scroll
        List<String> options = new ArrayList<>();
        if (top != null) {
            options.add("top: " + top);
        }
        if (left != null) {
            options.add("left: " + left);
        }
        if (!options.isEmpty()) {
            getElement().executeJs("this.scroll({" + String.join(",", options) + "});");
        }
    }
}