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
Pressing Tab blurs the editor #238
Comments
See also https://codemirror.net/6/examples/tab/ This is by design, since tab is an essential part of keyboard web navigation, and trapping it harms accessibility. I will in the future look if I can find a way of trapping tab in a way that provides keyboard users with an obvious way to escape the editor, but the default behavior will remain leaving it alone. |
I was a little surprised by this behaiour because In all editors I have come across, tab just adds 4 spaces |
Could it be an option to use Esc to blur the editor and focus the parent element. |
I'm not sure how discoverable that would be... and there isn't always a (reasonable) focusable parent element (and tab order isn't hierarchical, so it's not unlikely that, after moving focus out of the editor, your next tab press will move it back in). |
@nmanandhar I'm curious, what editors on the Web have you come across where the tab key indents? |
|
The escape option mirrors how Jupyter behaves. We could lean further into that: when tab-navigating a page, don't focus into the CodeMirror, but focus the overall element and require the user to press "enter" to start typing. I think that makes the escape-to-exit functionality more obvious. I (personally) think that using tab for indenting is a necessary evil. I'm willing to bet that the current behavior will result in decreased accessibility as people constantly accidentally exit the code area (since sooooo many people are used to tabbing behavior). Edit:
I think re-focusing the outer element makes sense here (probably just a |
Just in case people are wondering, you can override the default behavior if you choose to do so: import { defaultKeymap, indentMore, indentLess } from "@codemirror/next/commands";
EditorState.create({
extensions: [
keymap([
...defaultKeymap,
{
key: "Tab",
preventDefault: true,
run: indentMore,
},
{
key: "Shift-Tab",
preventDefault: true,
run: indentLess,
},
]),
],
}); |
Not really. Tab changes the focused element regardless of web or non-web environment. Documents are one exception to this general rule, which includes code files, which are the main contents of CodeMirror, aren't they? I can see this "the web world has to behave very differently than the native world because reasons" movement every now and then, and I'm not a fan of it. All it does is creating an inconsistent UX. Imagine pressing tab in a word processor or your code editor and it blurs the document/file instead of indenting the text/code. Code editors are most likely used in applications, not in web forms. The latter would justify "press tab to change focus" but this is not where I see much use in CodeMirror.
Again, the "on the web" should not make a difference. It's about use-case and context. If you ask me which editors in general I've came across where the tab key does not indent, the answer is: none. All editors indent your code whether it's native (Scintilla/Notepad++, IntelliJ, Xcode, VS...) or web (Monaco/VS Code, Ace, CodeJar). Because it makes sense in this context. |
It absolutely does. This component aims to be web-native and accessible, and is going to set its defaults accordingly. If you have a problem with that you can configure it differently, but see for example the content of a keyboard trap for why that is user-hostile and inaccessible. |
You basically just said that keyboard traps and poor accessibility is not a problem in non-web environments. This is what I mean with "it doesn't matter". Everything you make should be accessible, whether it's a website, web app or native app. But there are different approaches to how this is achieved. Applications normally use keyboard shortcuts to access different parts of them, which automatically removes focus from the document ( On a side note: Even in a keyboard trap you can still move focus to the Browser by pressing But anyway: The Monaco Editor uses Assuming the default won't change nonetheless, could you provide an additional |
https://gist.github.com/ (which uses CodeMirror) is a pretty big one. |
I do strongly think that tab should be indent - it's what practically every developer expects. Also, other editors (like document editors) use tab for indent (e.g., create a sub-bullet). This issue has come up elsewhere, and the escape-to-exit-edit-mode suggestion has come up at least once: chakra-ui/chakra-ui#5 I know it's customizable in "user space," but the default seems strange. |
As an emergency escape-from-keyboard-trap mechanism when tab is bound to another command. Issue #238
FEATURE: The new `insertTab` command inserts a tab when nothing is selected, and defers to `indentMore` otherwise. FEATURE: The package now exports a `defaultTabBinding` object that provides a recommended binding for tab (if you must bind tab). Issue codemirror/dev#238
Attached patches add more information about handling tab to the website, plus an example tab binding to the commands package. I hope people will find that, if not, I guess I'll use it as a link to point people to when the inevitable issues are opened. |
Pressing tab loses the focus from the editor and moves focus to other elements
The text was updated successfully, but these errors were encountered: