Coder Social home page Coder Social logo

Comments (6)

wessberg avatar wessberg commented on May 29, 2024

Hey there. Are you attaching a shadow root to your scrolling element? In any case, there should be no issues since this polyfill is built with full Shadow DOM support. Is there a way for you to provide me with a simple reproduction of this problem that occurs so I can investigate it? scrollIntoView at least under some circumstances should work just fine in IE 11, so I'd like a repro to work from ☺️

from scroll-behavior-polyfill.

enijar avatar enijar commented on May 29, 2024

I'm using this inside of a React-based application, so no shadow DOM, just the normal DOM of the page. I can confirm the element is mounted before I load the polyfill. I'm on mobile right now, but I'll share a CodeSandbox link when I'm on my home machine. Note that scrollTo works with this polyfill; it's only scrollIntoView that's not working as expected.

from scroll-behavior-polyfill.

wessberg avatar wessberg commented on May 29, 2024

Oh, alright. You mentioned it was a custom element, so I figured you might be using Shadow DOM. Anyway, that sounds great. I'll take a look at it when you have it. And thanks for reporting this bug!

from scroll-behavior-polyfill.

enijar avatar enijar commented on May 29, 2024

Here is a reproducable error: https://codesandbox.io/s/crazy-hopper-nfp2l

Let me know if you can see the issue on IE11.

Edit: I just tried to load CodeSandbox in IE 11 on Browserstack and it doesn't work 😬

from scroll-behavior-polyfill.

SeinopSys avatar SeinopSys commented on May 29, 2024

Hello, I believe I ran into the same issue, this polyfill was included in a project where a non-functional scrollIntoView call was reported as a bug when using IE11, I suspect it might have to do with our specific layout, but I created an example barebones react app that reproduces the broken behavior. Due to none of the code editors I found online actually working in IE11 I'm πŸ“Ž attaching the app as a zip, extract it then run

npm install
npm install -g serve
npm build
serve -s build

If you open the URL the last command prints in current stable Chrome/Firefox and IE11 you can see that pressing the button to scroll to the element at the 500th index does nothing in IE while it works in other browsers. In my experience this is because an element in the list of parents actually appears scrollable, but when you set its scrollTop then try to look at it immediately after it remains unchanged at 0. Before I discovered this polyfill was already pulled in I wrote my own solution in TypeScript which seemed to do the trick, just trying to scroll all parents until one of them actually accepts the change. Hopefully this helps in finding a proper solution:

/**
 * This is a polyfill for the HTMLElement.scrollIntoView function for IE11
 * Based on https://github.com/tidal-engineering/ie11-scroll-into-view
 */
export const applyScrollIntoViewPolyfill = () => {
    const isIE = typeof window !== 'undefined' && window.navigator.userAgent.indexOf('Trident/') > 0;

    if (!isIE) return;

    const getComputedStyle = (el: HTMLElement): CSSStyleDeclaration => window.getComputedStyle(el, null);
    const overflowScrollable = (text: string) => text === 'scroll' || text === 'auto' || text === 'visible';
    const isXScrollable = (elem: HTMLElement) => (
        elem.clientWidth < elem.scrollWidth
        && overflowScrollable(getComputedStyle(elem).overflowX)
    );
    const isYScrollable = (elem: HTMLElement) => (
        elem.clientHeight < elem.scrollHeight
        && overflowScrollable(getComputedStyle(elem).overflowY)
    );
    const hasScrollbar = (elem: HTMLElement) => isYScrollable(elem) || isXScrollable(elem);
    const offsetTop = (el: HTMLElement) => el.getBoundingClientRect().top;

    /** @return A list of parent elements that are likely to be scrollable */
    function findScrollableParents(el: HTMLElement): HTMLElement[] {
        let checkElement: HTMLElement = el;
        const scrollableParents = [];

        while (checkElement !== null) {
            if (hasScrollbar(checkElement))
                scrollableParents.push(checkElement);

            checkElement = checkElement.parentNode as HTMLElement;
        }

        return scrollableParents;
    }

    /**
     * Turns out IE11 just "gives up" when the native scrollIntoView method is called with
     * the current layout, so we need to manually try to scroll all scrollable parents
     * until one of them bites.
     */
    const ieScrollIntoView = (el: HTMLElement) => findScrollableParents(el).some(scrollContainer => {
        const elOffsetTop = offsetTop(el);
        // Stop scrolling if element is at the top
        if (elOffsetTop < 1.2) return true;
        const containerScrollTop = scrollContainer.scrollTop;
        const containerOffsetTop = offsetTop(scrollContainer);
        const newScrollTop = containerScrollTop - containerOffsetTop + elOffsetTop;
        scrollContainer.scrollTo(scrollContainer.scrollLeft, newScrollTop);
    });

    HTMLElement.prototype.scrollIntoView = function () {
        ieScrollIntoView(this);
    };
};

from scroll-behavior-polyfill.

elambro avatar elambro commented on May 29, 2024

This also doesn't work on Chrome for Android, at least on versions 38.0.2125 through 68.0.3440

from scroll-behavior-polyfill.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.