Shoelace + HTMX. TypeError: Cannot read properties of null (reading 'focus') #866
-
|
Hello! I'm using Shoelace with HTMX (https://htmx.org), and I seem to be getting an error relating to the focus property of an sl-button. This is the the HTMX code I am using Which gives me this error The line I used the browser dev tools debugger to blur the button before HTMX tries to swap the HTML with the server response and it seems to stop the error occurring, but then the button that launches the sl-dialog no longer does so after the swap. Any ideas for:
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 16 replies
-
|
I have zero experience with HTMX but this error leads me to believe you're calling
If you defer your logic, I bet you won't get that error anymore, e.g. await customElements.whenDefined('sl-button');
// apply focus here |
Beta Was this translation helpful? Give feedback.
-
|
@benopotamus you're a genius! your HTMX extension fix for forms with shoelace compon# ents works perfectly! I'm not sure whether it's a quirk of HTMX or something to look at in shoelace in how it presents values to a form. Link to fix for htmx: https://github.com/benopotamus/htmx-ext-shoelace |
Beta Was this translation helpful? Give feedback.
-
|
To overcome the error I mentioned in this discussion thread, I think (it was a while ago now) the solution was to change this line in htmx to: Just handling the error was enough for everything to proceed smoothly. This may be fixed in htmx by now though, I haven't checked in a while 😱 |
Beta Was this translation helpful? Give feedback.
-
|
@claviska I'm back with this problem again 😂. In the last 2 years ChatGPT became a thing and I asked it for a resolution and it came up with this (for WebAwesome mind you), which worked 😮 customElements.whenDefined('wa-button').then(() => {
const WaButton = customElements.get('wa-button');
WaButton.prototype.focus = function (opts) {
const inner = this.shadowRoot?.querySelector('button');
if (inner) inner.focus(opts);
};
});What do you think of adding something like this to WebAwesome (and Shoelace if appropriate)? |
Beta Was this translation helpful? Give feedback.
-
|
I do think that this problem here has to be solved somehow as it points out to an unhandled state of focusable elements. As shoelace aims to be a replacement for plain input elements (and plain input elements are focusable at all the times), I do think that this problem is a bug within shoelace. If the element has not been upgraded and someone already wants to put focus on it, the element itself should know what to do at this state, ideally wait until it has been upgraded and then continue focusing on the correct element inside the shadow dom. |
Beta Was this translation helpful? Give feedback.
-
|
As a side note for anyone if they still are interested in making this work properly without any workarounds. I have added a quick (and dirty) fix to this issue here- |
Beta Was this translation helpful? Give feedback.
I have zero experience with HTMX but this error leads me to believe you're calling
focus()before the custom element has upgraded.If you defer your logic, I bet you won't get that error anymore, e.g.