Displaying HTML web components
ThoseHTML web components I made for date inputsare very simple. All they do is slightly extend the behaviour of the existinginput
elements.
This would be the ideal use-case fortheis
attribute:
<input is= "input-date-future" type= "date" >
Alas, Apple have gone on record to say thatthey willnevership support for customized built-in elements.
So instead we have to makeHTML web componentsby wrapping existing elements in new custom elements:
<input-date-future>
<input type= "date" >
<input-date-future>
The end result is the same. Mostly.
Because there’s now an additional element in the DOM, there could be unexpected styling implications. Like, suppose the original element was direct child of a flex or grid container. Now that will no longer be true.
So something I’ve started doing with HTML web components like these is adding something like this inside theconnectedCallback
method:
connectedCallback() {
this.style.display = 'contents';
…
}
This tells the browser that, as far as styling is concerned, there’s nothing to see here. Move along.
Or you could (and probably should) do it in your stylesheet instead:
input-date-future {
display: contents;
}
Just to be clear, you should only usedisplay: contents
if your HTML web component is augmenting what’swithinit. If you add any behaviours or styling to the custom element itself, then don’t add this style declaration.
It’s a bit of a hack to work around the lack of universal support for theis
attribute, but it’ll do.