-
Notifications
You must be signed in to change notification settings - Fork 4k
Allow configuring keyboard shortcuts on buttons #12975
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
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds keyboard shortcut support to button components in Streamlit, allowing users to trigger buttons using keyboard combinations like "Ctrl+K" or "Cmd+N". The implementation spans both the Python backend (shortcut normalization and validation) and the TypeScript frontend (shortcut registration and display).
- Adds a
shortcutparameter tost.button(),st.download_button(),st.link_button(), andst.form_submit_button() - Implements shortcut normalization in Python to ensure consistent formatting (lowercase, standardized modifier order)
- Creates a new React hook (
useRegisterShortcut) for keyboard event handling in the frontend - Displays shortcut hints as styled
<kbd>elements on button labels
Reviewed Changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| proto/streamlit/proto/Button.proto | Adds shortcut field (11) to Button message |
| proto/streamlit/proto/DownloadButton.proto | Adds shortcut field (12) to DownloadButton message |
| proto/streamlit/proto/LinkButton.proto | Adds id field (1) and shortcut field (11) to LinkButton message |
| lib/streamlit/elements/widgets/button.py | Implements shortcut normalization, validation, and serialization for all button types |
| lib/streamlit/elements/form.py | Adds shortcut parameter to form submit button |
| lib/tests/streamlit/elements/button_test.py | Adds Python unit tests for shortcut normalization and reserved key validation |
| frontend/lib/src/hooks/useRegisterShortcut.ts | Implements keyboard shortcut registration hook with input field filtering |
| frontend/lib/src/hooks/useRegisterShortcut.test.tsx | Unit tests for the shortcut hook |
| frontend/lib/src/components/widgets/Button/Button.tsx | Integrates shortcut hook and display for regular buttons |
| frontend/lib/src/components/widgets/DownloadButton/DownloadButton.tsx | Integrates shortcut hook and display for download buttons |
| frontend/lib/src/components/elements/LinkButton/LinkButton.tsx | Integrates shortcut hook and display for link buttons |
| frontend/lib/src/components/elements/LinkButton/BaseLinkButton.tsx | Converts to forwardRef to support programmatic clicks via ref |
| frontend/lib/src/components/widgets/Form/FormSubmitButton.tsx | Integrates shortcut hook and display for form submit buttons |
| frontend/lib/src/components/shared/BaseButton/DynamicButtonLabel.tsx | Adds shortcut display logic and formatting |
| frontend/lib/src/components/shared/BaseButton/DynamicButtonLabel.test.tsx | Unit tests for shortcut display formatting |
| frontend/lib/src/components/shared/BaseButton/styled-components.ts | Adds styled components for shortcut badge display |
| e2e_playwright/st_button.py | Adds test button with shortcut |
| e2e_playwright/st_button_test.py | E2E tests for button shortcut display and activation |
| e2e_playwright/st_download_button.py | Adds test download button with shortcut |
| e2e_playwright/st_download_button_test.py | E2E tests for download button shortcut display and activation |
| e2e_playwright/st_link_button.py | Adds test link button with shortcut |
| e2e_playwright/st_link_button_test.py | E2E tests for link button shortcut display and activation |
| e2e_playwright/st_form.py | Adds test form submit button with shortcut |
| e2e_playwright/st_form_test.py | E2E tests for form submit button shortcut display and activation |
📉 Frontend coverage change detectedThe frontend unit test (vitest) coverage has decreased by 0.0000%
✅ Coverage change is within normal range. |
|
I removed the "Closes ..." from the GitHub issue since it is about keyboard shortcuts in general, not just for buttons. I think we should keep that issue open a bit longer. |
I would suggest to create a new issue after merging this PR instead to get a better insight on the actual interest in adding more generic hotkeys. Otherwise, we have the same issue as with #5370 where the interest is mixed between the already released header grouping vs the not yet released row spanning. I guess hotkeys on buttons will resolve the majority of use cases already. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 32 out of 50 changed files in this pull request and generated 1 comment.
| // The ID is only needed if a shortcut is provided. | ||
| string id = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Can you confirm that 1 has never been used in this proto message before? If it has, then this is not a safe change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, it was never used based on the proto history.
Describe your changes
Adds support for configuring shortcuts/hotkeys to trigger buttons (
st.button,st.download_button,st.link_button,st.form_submit_button)Screenshot or video (only for visual changes)
GitHub Issue Link (if applicable)
Testing Plan
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.