-
Notifications
You must be signed in to change notification settings - Fork 81
chore(Dialog): add kb for focusable content #3210
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d42f64
chore(Dialog): add kb for focusable content
Tsvetomir-Hr 05c87ec
Update knowledge-base/dialog-close-on-esc.md
Tsvetomir-Hr 2075637
Update knowledge-base/dialog-close-on-esc.md
Tsvetomir-Hr f269698
Update knowledge-base/dialog-close-on-esc.md
Tsvetomir-Hr 948fce0
Update knowledge-base/dialog-close-on-esc.md
Tsvetomir-Hr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| --- | ||
| title: How to Close the Dialog on ESC When its Content is Focused | ||
| description: Learn how to enable closing the Dialog on ESC key after focusing the content by adding a focusable wrapper. | ||
| type: how-to | ||
| page_title: How to Close the Dialog on ESC When its Content is Focused | ||
| tags: Dialog, ESC, Keyboard, Focus | ||
| slug: dialog-kb-close-on-esc | ||
| --- | ||
|
|
||
| ## Description | ||
|
|
||
| This knowledge base article answers to the following questions: | ||
|
|
||
| * How to make Dialog responsive to keyboard events when content is clicked? | ||
| * Why doesn't ESC key work after clicking inside Dialog content? | ||
| * How to maintain keyboard functionality in Dialog after focus changes? | ||
|
|
||
| When you click or focus inside the Dialog content area, the focus moves away from the Dialog component. As a result, the Dialog does not receive keyboard events, and pressing the `ESC` key does not close it. This behavior occurs because the keydown events are not invoked for the Dialog when the focus is on another element or the body tag. | ||
|
|
||
| ## Solution | ||
|
|
||
| To ensure the Dialog closes on `ESC` even after focusing the content, wrap the Dialog content in a `div` with `tabindex="0"`. This makes the content wrapper focusable and allows keyboard events to be captured. Add custom styling to handle the focus state and preserve the existing content padding. | ||
|
|
||
| >caption Example of Implementing a Focusable Wrapper with Visual Indication | ||
|
|
||
| ````RAZOR | ||
| <TelerikDialog @bind-Visible="@DialogVisible" | ||
| Width="500px" | ||
| Height="300px" | ||
| Class="focusable-content"> | ||
| <DialogContent> | ||
| <div tabindex="0" class="dialog-content-wrapper"> | ||
| <p>Click anywhere in this content area, then press ESC to close the dialog.</p> | ||
| </div> | ||
| </DialogContent> | ||
| <DialogButtons> | ||
| <TelerikButton OnClick="@CloseDialog" ThemeColor="@ThemeConstants.Button.ThemeColor.Primary">Close</TelerikButton> | ||
| </DialogButtons> | ||
| </TelerikDialog> | ||
|
|
||
| <TelerikButton OnClick="@OpenDialog">Open Dialog</TelerikButton> | ||
|
|
||
| <style> | ||
| /* Remove default Dialog content padding to let wrapper handle it */ | ||
| .focusable-content .k-dialog-content { | ||
| padding: 0; | ||
| } | ||
|
|
||
| /* Focusable wrapper that fills the entire Dialog content area */ | ||
| .focusable-content .dialog-content-wrapper { | ||
| padding: 1rem; | ||
| outline: none; | ||
| height: 100%; | ||
| box-sizing: border-box; | ||
| transition: background-color 0.2s ease; | ||
| } | ||
|
|
||
| /* Visual indication when the wrapper is focused */ | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .focusable-content .dialog-content-wrapper:focus { | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| background-color: rgba(0, 123, 255, 0.05); | ||
| } | ||
| </style> | ||
|
|
||
| @code { | ||
| private bool DialogVisible { get; set; } | ||
Tsvetomir-Hr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private string SampleText { get; set; } = string.Empty; | ||
|
|
||
| private void OpenDialog() | ||
| { | ||
| DialogVisible = true; | ||
| } | ||
|
|
||
| private void CloseDialog() | ||
| { | ||
| DialogVisible = false; | ||
| } | ||
| } | ||
| ```` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.