Giter VIP home page Giter VIP logo

calvellido / focus-options-polyfill Goto Github PK

View Code? Open in Web Editor NEW
46.0 3.0 7.0 149 KB

JavaScript polyfill for the WHATWG spec of focusOptions, that enables a set of options to be passed to the focus method

Home Page: https://html.spec.whatwg.org/multipage/interaction.html#dom-focus

License: MIT License

JavaScript 43.75% HTML 56.25%
javascript polyfill javascript-polyfill focus whatwg whatwg-dom options preventscroll

focus-options-polyfill's Introduction

Hi there ๐Ÿ‘‹

My name is Juan Valencia Calvellido and I'm a front-end developer focused on UI development through JavaScript technologies.

decorative image

focus-options-polyfill's People

Contributors

calvellido avatar dependabot[bot] avatar kennethormandy avatar lukastillmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

focus-options-polyfill's Issues

Available on NPM?

Hi, nice polyfill! Is this available through NPM? I can't seem to find it from looking around

Make `patchedFocus` synchronous

We've recently run into a bug related to patchedFocus() behaving differently than native focus(), where focusing an element is not executed synchronously, and document.activeElement is not immediately the element focused. Is there a reason setTimeout(..., 0) is used here? Given the typeof setTimeout === 'function' check, and subsequent fallback, it would seem that dropping the asynchronous method should probably be the default.

Thanks for this polyfill, and let me know if you'd like me to make a PR for this!

Support iOS Safari?

I have not been able to make this polyfill work in iOS Safari - has it been tested there?

Many thanks.

Maybe we should check for != null instead of typeof "undefined"

I'm having an annoying issue where this all works locally but running with js-dom in travisCI, it can't find document (it's null), so this package breaks the tests, unfortunately.

Error: Uncaught [TypeError: Cannot read property 'scrollingElement' of null]

at calcScrollableElements (/home/travis/build/rpearce/react-medium-image-zoom/node_modules/focus-options-polyfill/index.js:41:18)

https://github.com/calvellido/focus-options-polyfill/blob/master/index.js#L3-L9

It might be a good idea to change those to be != null instead of typeof ... === "undefined". Using != null covers both undefined and null use cases. Another option would be simply to use the ! operator in front of each one.

What do you think?

Minify dist versions

Current published versions aren't minified, and while current workflows might minify the code in the end, it would be a good option to publish the polyfill minified so when used through services like unpkg, the weight is a bit less.

Suggestion: only prevent scroll if browser will do it

scroll happens when element isn't in viewport, so let's check if it's in viewport or not

function isElementInViewport (el) {
    var rect = el.getBoundingClientRect();

    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
}

this should work for all browsers, i think this polyfill is doing too much work even if nothing has scrolled

Property document.scrollingElement fallback

If document.scrollingElement doesn't exist, this will throw an error. Example would be in Internet Explorer or when testing with jest or anything that uses jsdom.

@calvellido As document.scrollingElement is document.documentElement by default (https://developer.mozilla.org/en-US/docs/Web/API/document/scrollingElement), would it make sense to fall back to documentElement when scrollingElement is not found?

parent = document.scrollingElement || document.documentElement

For jest users, place this in your test setup file:

document.scrollingElement = document.documentElement

Originally posted by @rpearce in #11

Add usage bits to README

The repo README file can be improved by adding some bits about how the polyfill can be used, being that through an npm stack, or on a HTML document directly.

There can be a lot more scrolled than just the window element.

I implemented something in a very old project. Here is a snippet:

    var calcScrollableElements = function(){
            var parent = element.parentNode;
            while(parent){
                if(parent.offsetHeight < parent.scrollHeight || parent.offsetWidth < parent.scrollWidth){
                    scrollableElements.push([parent, parent.scrollTop, parent.scrollLeft]);
                }
                parent = parent.parentNode;
            }
            parent = document.scrollingElement;
            scrollableElements.push([parent, parent.scrollTop, parent.scrollLeft]);
        };

        var restoreScrollPosition = function(){
            var i;

            for(i = 0; i < scrollableElements.length; i++){
                scrollableElements[i][0].scrollTop = scrollableElements[i][1];
                scrollableElements[i][0].scrollLeft = scrollableElements[i][2];
            }
            scrollableElements = [];
        };

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.