• This repository has been archived on 29/Dec/2017
  • Stars
    star
    295
  • Rank 135,660 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library to make creating CSS polyfills much easier.

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.

More Repositories

1

flexbugs

A community-curated list of flexbox issues and cross-browser workarounds for them.
13,644
star
2

solved-by-flexbox

A showcase of problems once hard or impossible to solve with CSS alone, now made trivially easy with Flexbox.
CSS
13,023
star
3

html-inspector

HTML Inspector is a code quality tool to help you and your team write better markup. It's written in JavaScript and runs in the browser, so testing your HTML has never been easier.
JavaScript
2,317
star
4

analyticsjs-boilerplate

Examples and best practices for using analytics.js
JavaScript
1,124
star
5

responsive-components

A modern approach to styling elements based on the size of their container
JavaScript
584
star
6

webpack-esnext-boilerplate

JavaScript
516
star
7

rollup-native-modules-boilerplate

A demo app showcasing the use of real JavaScript modules in production—complete with cross-browser fallbacks for legacy browsers.
JavaScript
223
star
8

blog

The source code for https://philipwalton.com
JavaScript
180
star
9

private-parts

A simple and intuitive way to shim private methods and properties in JavaScript.
JavaScript
105
star
10

mozart

A full-featured, classical inheritance library for Node.js and the browser.
JavaScript
84
star
11

easy-sauce

Easily run JavaScript unit tests on the Sauce Labs cloud.
JavaScript
73
star
12

talks

HTML
46
star
13

dom-utils

A small, modular DOM utility library
JavaScript
35
star
14

import-maps-caching-demos

Demos showing how to use Import Maps to prevent cascading cache invalidations
JavaScript
21
star
15

selectable

Easily get and set the text selection with an HTML element.
JavaScript
15
star
16

router

A simple router that binds URLs paths and patterns to functions.
JavaScript
7
star
17

rollup-built-in-modules

A demo of using rollup with built-in modules
JavaScript
7
star
18

shimr

A proof-of-concept for building CSS polyfills
JavaScript
6
star
19

dotfiles

OS X dotfiles, config, bash, git, etc.
Vim Script
6
star
20

google-analytics-browser-stats

JavaScript
5
star
21

deep-watch

Exactly like fs.watch, but with sub-directory support.
JavaScript
5
star
22

ingen

JavaScript
5
star
23

slide-deck

HTML
4
star
24

handbrake

JavaScript
3
star
25

dev

HTML
3
star
26

navigation-event-proposal

3
star
27

solarized-dark-minimalist-syntax

CSS
3
star
28

deeplinker

JavaScript
2
star
29

usage-trends

JavaScript
2
star
30

github-stars

JavaScript
1
star
31

yore

Async and error handling sugar on top of the HTML5 History api.
JavaScript
1
star
32

rollup-3245-repro

JavaScript
1
star
33

photo-validator

JavaScript
1
star
34

philipwalton.github.io

GitHub pages root
HTML
1
star