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

Add support for Custom Element #6652

Closed
4 tasks done
vtsvetkov-splunk opened this issue Jan 15, 2024 · 8 comments
Closed
4 tasks done

Add support for Custom Element #6652

vtsvetkov-splunk opened this issue Jan 15, 2024 · 8 comments
Labels
bug Issue reports a bug resolved Issue is resolved, yet unreleased if open

Comments

@vtsvetkov-splunk
Copy link
Contributor

vtsvetkov-splunk commented Jan 15, 2024

Context

I added Custom Elements with shadow root via custom elements because I don't want the styles of mkdocs has influence on forms I am adding. When I have a form inside my custom component, the global keyboard handler catches some keys I am typing like s and takes focus to search.
The reason is if there is input focused inside the shadow root, the parent's document.activeElement would point to the custom element itself, not to it's underlying element.

Since 'the bug' is caused by extra javascript, I cannot mark it as a bug, so I am asking about adding support for custom elements.

Description

While having additional functionality via extra_javascript is a great feature, it lacks a full support for adding custom elements. I was able to add the custom element, but the keyboard shortcut is not aware about such type of element.
There is a check for activeElement which works differently for elements with shadowRoot. I am asking to add support for such kind of elements here:

export function getActiveElement(): HTMLElement | undefined {
return document.activeElement instanceof HTMLElement
? document.activeElement || undefined
: undefined
}

The active element of shadowRoot can be accessed via document.activeElement.shadowRoot.activeElement.

Related links

Use Cases

It will allow to have forms inside custom elements

Visuals

No response

Before submitting

@squidfunk squidfunk added the needs investigation Issue must be investigated by the maintainers label Jan 16, 2024
@squidfunk
Copy link
Owner

Thanks for reporting. Please provide a reproduction that showcases the issues. We cannot merge a PR without knowing what it impacts and for that we need to see it in action.

@squidfunk squidfunk added needs reproduction Issue lacks a minimal reproduction .zip file and removed needs investigation Issue must be investigated by the maintainers labels Jan 17, 2024
@vtsvetkov-splunk
Copy link
Contributor Author

@humitos
Copy link

humitos commented Jan 17, 2024

I have a pretty similar issue and I asked about a possibility to disable the hotkeys in Material for MkDocs but I was told the only way to do it currently it by removing that chunk of code and re-compile the theme 😞 : #4273

I'm also working with Custom Elements that contain inputs/forms inside them and this is a problem since every time that p, n or other keys are pressed, Material for MkDocs captures them and perform an action that it's not expected.

@squidfunk
Copy link
Owner

We can make hotkeys configurable in the future, but it is currently not a priority for us, so no ETA.

@vtsvetkov-splunk
Copy link
Contributor Author

@humitos,

I'm also working with Custom Elements that contain inputs/forms inside them

If you use open mode when attaching shadow root, then my PR would mitigate that issue.

@vtsvetkov-splunk
Copy link
Contributor Author

To workaround the issue, I added a keyDown event listener for every input inside the custom element and called stopPropagation. I hope that would help who also experiences the issue.

@squidfunk squidfunk added bug Issue reports a bug resolved Issue is resolved, yet unreleased if open and removed needs reproduction Issue lacks a minimal reproduction .zip file labels Jan 24, 2024
@squidfunk
Copy link
Owner

Fixed in be95f49 (superseding #6653) and released as part of 9.5.5.

@humitos
Copy link

humitos commented Jan 24, 2024

I can confirm that this fix works for me. I created a small example at https://test-builds.readthedocs.io/en/mkdocs-material/ and opening the search modal (CustomElement) and typing n or p inside the text input doesn't trigger the Next or Previous MkDocs hotkey 🎉 . Thanks @vtsvetkov-splunk and @squidfunk for the fix 💪🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue reports a bug resolved Issue is resolved, yet unreleased if open
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants