Giter Club home page Giter Club logo

polyfill's Introduction

UPDATE (2016-12-22): I am no longer supporting this library for all the reasons I address in my post The Dark Side of Polyfilling CSS. If you choose to use this library, please make sure you read the post, so you fully understand the challenges and limitations involved in writing CSS polyfills.


Polyfill.js

Polyfill.js is a library designed to make writing CSS polyfills much, much easier. It's an abstraction library that takes care of the boilerplate, so you can focus on what your polyfill actually does.

For most CSS polyfills, the hardest part is not the polyfill logic itself, it's the boring stuff, the stuff that the browser is supposed to do for you: downloading the CSS, parsing it, and finding the parts you care about. If the CSS contains media queries, you need to deal with them, detect when they apply, and manually listen for changes.

Furthermore, on the Web today, most polyfills exist isolated from each other, which means they all repeat the same expensive tasks. Polyfill.js solves this problem. It provides a common API for Polyfill authors to hook in to, so all the hard work happens only once at most. The stylesheets are downloaded, parsed, and stored in a cache so additional requests don't do double work.

How It Works

Polyfill.js makes writing your own CSS Polyfill easy by breaking it down into the following three steps:

1) Include the Polyfill.js library on your page.

It doesn't really matter where you put it, as long as it appears after the stylesheet(s) containing the rules you want to polyfill.

<script src="path/to/polyfill.js"></script>

2) Create a new Polyfill Instance

You create a new instance of the Polyfill object by passing in one or more keywords representing the CSS features you want to polyfill. The keywords can be declaration keywords (property-value pairs) or selector keywords.

The following expression creates an instance to polyfill the :local-link CSS pseudo-class:

var localLinkPolyfill = Polyfill({ selectors: [":local-link"] })

3) Register Event Callbacks

Once you have your polyfill instance, you simply register two callbacks: doMatched() and undoUnmatched(). When the page first loads and Polyfill.js has done all its work behind the scenes, the doMatched() callback is invoked and is passed a list of CSS rules that contain the specified keywords and match the current media.

If the media values change (usually by resizing the browser window) and new rules match, the doMatched() callback will be invoked again, each time being passed the newly matched rules.

If the media value changes and some rules no longer match, the undoUnmatched() callback is invoked and passed a list of rules that previously matched but no longer do.

Demos

  • Local Link: Local links (:local-link) is a new CSS pseudo-class for styling anchor tags that point to URLs within the current domain.
  • Position Sticky: "Sticky" is a new CSS position value to allow elements to stick in place only after a specified scroll position is met. This is most commonly used for navigation elements to stick in place after you start scrolling down the page.

Running the Tests

Polyfill.js includes a Jasmine test suite and uses an embedded iframe to test the media queries and resize events. Polyfill.js has been tested on the latest Chrome, Firefox, Safari, Opera, and IE 7-10.

If you use a browser other than these, please run the test suite in your browser and report back the results.

API

The full API can be found on project home page.

polyfill's People

Contributors

mortonfox avatar philipwalton 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

polyfill's Issues

Element should stop scrolling with viewport at the bottom of its parent container

This functionality might already be in the polyfill, but I was unable to get it working.

As the parent element of an element with position:sticky; approaches the top of the browser viewport (as the user scrolls down), the `position:sticky' element should stop its 'sticky' behavior as the parent element goes out of the viewport.

In the HTML5Rocks demo page, the blue blog headers stop at the bottom of their parent elements.

Possible error in `position:sticky` demo?

Should you be passing one object to the constructor? Can't see where a second arg would be used.

  Polyfill({
    declarations:["position:sticky"],
    include: ["position-sticky"]
  })
  .doMatched(doMatched)
  .undoUnmatched(undoUnmatched)

Update README

Include:

  • Introduction
  • Getting started
  • Link to Demos
  • Testing Info
  • Browser Support
  • Link to API

Add .stuck class

Hey,
I thought it would be helpful to add a .stuck class to make it possible to style stuck elements.

I've never really used github before, but seeing as I got it working with only one line, I don't think its worth doing a pull/fork/push whatever its supposed to be.

Anyway, after line 38 in polyfill.position-sticky.js, add

    $(data.$clone).addClass('stuck')

then you can style the element with .element.stuck {

It's not a perfect solution due to the way the polyfill works, but it works for small effects.

Thanks,
euan.

Demo: rems, vh, vw, vmax & vmin

This can probably be accomplished by adding an additional <style> element afterward, duplicating the selectors, and simply replacing the occurrences of these units with their pixel equivalents.

The v[x] units will need to update on window.resize.

Release v0.1.0 (work in progress)

Remaining tasks:

  • Combine keywords and options into a single parameter for Polyfill()
  • Add Google Analytics to all demo pages
  • Check the docs and demo pages in old IE
  • Add content to local-link demo
  • Final proof of everything

You can't change the `top` property of a stuck element later in the cascade

I'm trying to implement a sticky navigation that gets stuck below the WordPress admin bar, rather than appearing underneath it. The solution to this is simple:

.navigation_container.-sticky {
    position: sticky;
    top: 0;
    width: 100% !important; // polyfill miscalculates, should always be 100%
}

@media screen and (min-width: 601px) {
    .admin-bar .navigation_container.-sticky {
        top: 46px;
    }
}

@media screen and (min-width: 783px) {
    .admin-bar .navigation_container.-sticky {
        top: 32px;
    }
}

Unfortunately, this doesn't appear to work. It always thinks top:0; is what should be set. I'm not sure if this is due to the added .admin-bar class, making it think that it's an entirely different stuck element, or if it's simply that I'm trying to change the top property at all.

In this use case, where I just want it to be stuck to the top of the body element, it doesn't make sense to add an inline style for top at all, preferably there should be a way to override that.

Permit insertion of stylesheet contents

I'm using Dojo Toolkit's dijit/Editor which is a rich-text editor, with its content contained within an iframe. I have added dynamically various scripts/stylesheets to the iframe, but I also need to run polyfill against it, and unfortunately Dojo gives the iframe a src attribute of "javascript:*". This means that any CSS script loading fails as a cross-domain request.
Would it be possible to add an extra parameter (perhaps "stylesheetContents") to the initial options, that would then cause the program to check for provided stylesheet content, prior to attempting a (potentially) XSS attempt to retrieve them?
I will fork the project and try and find the time to implement this in the fork.

Chrome iOS issue

Hi,
We've just got to the bottom of a strange bug in Chrome on iOS 6 (i know, who uses that, its not even a proper browser as it uses WebViews etc!). The page just hangs and crashes the browser when polyfill.js is included. We've removed polyfill.js and the page loads fine. We use requirejs to load everything too.

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.