Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass in label to support the shadow DOM #31

Merged
merged 5 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ require('time-input-polyfill/auto');

// ES6
import 'time-input-polyfill/auto';

// TypeScript
import * as timePolyFill from 'time-input-polyfill';
russcarver marked this conversation as resolved.
Show resolved Hide resolved
```

That's all you need to do.
Expand Down Expand Up @@ -128,6 +131,29 @@ if (!supportsTime) {

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.

```
<label id="myLabel" for="timeInput></label>
<input type="time" id="timeInput">
```

```
import timePolyfill from 'time-input-polyfill';

someMethod() {
// The following element must not be in a shadow DOM
var componentRootElem = document.getElementById('idOfYourShadowDomComponentRootElement');

var timeLabelElem = componentRootElem.shadowRoot.getElementById('myLabel');
var 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.
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import create_a11y_block from './core/accessibility/create_a11y_block'
var accessibility_block_created = false
var $a11y

function TimePolyfill($input) {
function TimePolyfill($input, $label) {
$input.setAttribute('autocomplete', 'off')

// Prevent screen reader from announcing the default stuff
Expand All @@ -21,7 +21,7 @@ function TimePolyfill($input) {
accessibility_block_created = true
}

var label = get_label($input)
const label = $label ? $label.textContent : get_label($input)

$input.polyfill = {
$a11y: $a11y,
Expand Down