diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..526ddfd --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["@babel/preset-env"] +} diff --git a/.editorconfig b/.editorconfig index e717f5e..9aac58c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,8 +2,8 @@ root = true [*] -indent_style = space -indent_size = 2 +indent_style = tab +indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true diff --git a/.eslintrc b/.eslintrc index 3b82de0..319fbc4 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,29 +1,37 @@ { - "env": { - "browser": true, - "node": true - }, - "globals": { - "DocumentFragment": true - }, - "rules": { - "indent": [2, 2], - "valid-jsdoc": 0, - "brace-style": [1, "stroustrup"], - "no-constant-condition": 1, - "no-underscore-dangle": 0, - "no-use-before-define": 1, - "func-names": 0, - "semi": [2, "always"], - "no-new": 0, - "new-parens": 2, - "no-ternary": 0, - "new-cap": 0, - "no-unused-vars": [1, {"vars": "local", "args": "none"}], - "quotes": [2, "single"], - "one-var": 0, - "space-infix-ops": 0, - "strict": 0, - "camelcase": [2, {"properties": "never"}] - } + "env": { + "browser": true, + "node": true, + "es6": true + }, + "extends": ["plugin:prettier/recommended"], + "parserOptions": { + "ecmaFeatures": { + "modules": true + }, + "sourceType": "module" + }, + "globals": { + "DocumentFragment": true + }, + "rules": { + "indent": [1, "tab"], + "valid-jsdoc": 0, + "brace-style": [1, "1tbs"], + "no-constant-condition": 1, + "no-underscore-dangle": 0, + "no-use-before-define": 1, + "func-names": 0, + "semi": [1, "never"], + "no-new": 0, + "new-parens": 1, + "no-ternary": 0, + "new-cap": 0, + "no-unused-vars": [1, { "vars": "local", "args": "none" }], + "quotes": [1, "single"], + "one-var": 0, + "space-infix-ops": 0, + "strict": 0, + "camelcase": 0 + } } diff --git a/.gitignore b/.gitignore index 6f83170..8a4bf85 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ build tmp .grunt .DS_Store -file-loader.pug +auto-imports.pug docs/scripts/time-input-polyfill.* diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..697bd8e --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "singleQuote": true, + "trailingComma": "es5", + "useTabs": true, + "tabWidth": 4, + "semi": false +} diff --git a/README.md b/README.md index 0ec7fd4..94808dc 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,18 @@ An accessible polyfill for `` elements. -- ✔️ Modeled after the Chrome 78 and Firefox 70 desktop implementations. -- ✔️ Fully keyboard and screen reader accessible. -- ✔️ Submits the same values to servers as real time inputs (24 hour time). -- ✔️ Only downloads the full polyfill code in the browsers that need it. -- ✔️ Zero dependencies. +- ✔️ Modeled after the Chrome 78 and Firefox 70 desktop implementations. +- ✔️ Fully keyboard and screen reader accessible. +- ✔️ Submits the same values to servers as real time inputs (24 hour time). +- ✔️ Only downloads the full polyfill code in the browsers that need it. +- ✔️ Zero dependencies. Demo available here: https://dan503.github.io/time-input-polyfill/
- The recommended version is 1.0.9 or higher. + The recommended version is 1.0.10 or higher. -

If the recommended version in this documentation is out of sync with the npm version, this is because npm only allows readme edits to be committed through full releases. To prevent needless cache invalidation, I'll only update the recommended version number when there are actual changes to the polyfill code. The current recommended version is 1.0.9. As long as you are using a version that is equal to or higher than that, you are using the latest version of the polyfill.

+

If the recommended version in this documentation is out of sync with the npm version, this is because npm only allows readme edits to be committed through full releases. To prevent needless cache invalidation, I'll only update the recommended version number when there are actual changes to the polyfill code. The current recommended version is 1.0.10. As long as you are using a version that is equal to or higher than that, you are using the latest version of the polyfill.

@@ -23,8 +23,8 @@ Demo available here: https://dan503.github.io/time-input-polyfill/ To make it easier to implement this polyfill into your projects, I have some pre-built component versions of it that you might find easier to use. -- [React component](https://www.npmjs.com/package/react-time-input-polyfill) -- [Vue component](https://www.npmjs.com/package/vue-time-input-polyfill) (not built yet) +- [React component](https://www.npmjs.com/package/react-time-input-polyfill) +- [Vue component](https://www.npmjs.com/package/vue-time-input-polyfill) (not built yet) ## Fastest and easiest way to implement @@ -44,10 +44,10 @@ Then require it in your main JavaScript file like so: ```js // ES5 -require('time-input-polyfill/auto'); +require('time-input-polyfill/auto') // ES6 -import 'time-input-polyfill/auto'; +import 'time-input-polyfill/auto' ``` That's all you need to do. @@ -59,39 +59,40 @@ You didn't load the actual polyfill onto the page, you loaded a much smaller aut 1. The initialiser checks if the browser supports `input[type="time"]` elements. 2. If it **does**, it skips the rest of the functionality. 3. If it does **not**, it will: - 1. load `https://cdn.jsdelivr.net/npm/time-input-polyfill@1.0.9/dist/time-input-polyfill.min.js` (the actual polyfill). - 2. Collect all existing `input[type="time"]` elements on the page. - 3. Loop through each `input[type="time"]` element and apply the polyfill to it. - + 1. load `https://cdn.jsdelivr.net/npm/time-input-polyfill@1.0.10/dist/time-input-polyfill.min.js` (the actual polyfill). + 2. Collect all existing `input[type="time"]` elements on the page. + 3. Loop through each `input[type="time"]` element and apply the polyfill to it. ## I need more control -The following downloads the full polyfill in all browsers, take a look at the [auto.js](https://github.com/Dan503/time-input-polyfill/blob/master/auto.js) file if you want to see how it loads the polyfill dynamically. +The following downloads the full polyfill in all browsers, take a look at the [auto.mjs](https://github.com/Dan503/time-input-polyfill/blob/master/auto.mjs) file if you want to see how it loads the polyfill dynamically. ### npm First check for `input[type="time"]` support. ```js -var supportsTime = require('time-input-polyfill/supportsTime'); +import supportsTime from 'time-input-polyfill/supportsTime' if (!supportsTime) { - //Apply polyfill here + //Apply polyfill here } ``` Then gather a list of all `input[type="time"]` elements on the page, and loop through them to apply the polyfill. ```js -var supportsTime = require('time-input-polyfill/supportsTime'); -var TimePolyfill = require('time-input-polyfill'); +import supportsTime from 'time-input-polyfill/supportsTime' +import TimePolyfill from 'time-input-polyfill' if (!supportsTime) { - // Converting to an array for IE support - var $$inputs = [].slice.call( document.querySelectorAll('input[type="time"]') ); - $$inputs.forEach(function($input){ - new TimePolyfill($input); - }) + // Converting to an array for IE support + const $$inputs = [].slice.call( + document.querySelectorAll('input[type="time"]') + ) + $$inputs.forEach(function ($input) { + new TimePolyfill($input) + }) } ``` @@ -102,32 +103,59 @@ if (!supportsTime) { First check for `input[type="time"]` support. ```html - + ``` + ```js if (!supportsTime) { - //Apply polyfill here + //Apply polyfill here } ``` Then gather a list of all `input[type="time"]` elements on the page, and loop through them to apply the polyfill. ```html - - + + ``` + ```js if (!supportsTime) { - // Converting to an array for IE support - var $$inputs = [].slice.call( document.querySelectorAll('input[type="time"]') ); - $$inputs.forEach(function($input){ - new TimePolyfill($input); - }) + // Converting to an array for IE support + const $$inputs = [].slice.call( + document.querySelectorAll('input[type="time"]') + ) + $$inputs.forEach(function ($input) { + new TimePolyfill($input) + }) } ``` This will add a global `TimePolyfill` function to the page. +## Shadow DOM + +When your code is inside a component that resides in the Shadow DOM, the polyfill will not be able to find your label +element. For this case, you can pass your label element in directly. + +```html + + +``` + +```js +import timePolyfill from 'time-input-polyfill' + +// The following element must not be in a shadow DOM +const componentRootElem = document.getElementById( + 'idOfYourShadowDomComponentRootElement' +) + +const timeLabelElem = componentRootElem.shadowRoot.getElementById('myLabel') +const timeInputElem = componentRootElem.shadowRoot.getElementById('timeInput') +timePolyFill(timeInputElem, timeLabelElem) +``` + ## Major limitations Note that I refer to an `input[type="time"]` element that has had the polyfill initialized on it as an `$input` in this section. @@ -147,13 +175,14 @@ I couldn't find any reliable way to track when a user uses `$input.value = '13:3 Any time you update the value of the time input element through JavaScript, check that `$input.polyfill` exists, and if it does, call `$input.polyfill.update()`. ```html - + ``` + ```js -var $input = document.getElementByID('example'); -$input.value = '13:30'; +const $input = document.getElementByID('example') +$input.value = '13:30' // call the update() method whenever the value is updated through JS -if ($input.polyfill) $input.polyfill.update(); +if ($input.polyfill) $input.polyfill.update() ``` The `update()` method will return the input element that it was called on so it can be chained if you want. @@ -164,31 +193,30 @@ The polyfill will fail if the `$input` is missing a label. The following is a list of ways you can add a label to the `$input`. The list is in order from the **best** method to the **worst** method: -1. Nesting the `$input` inside a `