|
| 1 | +--- |
| 2 | +title: How to Close the Dialog on ESC When its Content is Focused |
| 3 | +description: Learn how to enable closing the Dialog on ESC key after focusing the content by adding a focusable wrapper. |
| 4 | +type: how-to |
| 5 | +page_title: How to Close the Dialog on ESC When its Content is Focused |
| 6 | +tags: blazor, dialog, keyboard |
| 7 | +slug: dialog-kb-close-on-esc |
| 8 | +--- |
| 9 | + |
| 10 | +## Description |
| 11 | + |
| 12 | +This knowledge base article answers to the following questions: |
| 13 | + |
| 14 | +* How to make Dialog responsive to keyboard events when content is clicked? |
| 15 | +* Why doesn't ESC key work after clicking inside Dialog content? |
| 16 | +* How to maintain keyboard functionality in Dialog after focus changes? |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +## Solution |
| 21 | + |
| 22 | +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. |
| 23 | + |
| 24 | +>caption Example of Implementing a Focusable Wrapper with Visual Indication |
| 25 | +
|
| 26 | +````RAZOR |
| 27 | +<TelerikDialog @bind-Visible="@DialogVisible" |
| 28 | + Width="500px" |
| 29 | + Height="300px" |
| 30 | + Class="focusable-content"> |
| 31 | + <DialogContent> |
| 32 | + <div tabindex="0" class="dialog-content-wrapper"> |
| 33 | + <p>Click anywhere in this content area, then press ESC to close the dialog.</p> |
| 34 | + </div> |
| 35 | + </DialogContent> |
| 36 | + <DialogButtons> |
| 37 | + <TelerikButton OnClick="@CloseDialog" ThemeColor="@ThemeConstants.Button.ThemeColor.Primary">Close</TelerikButton> |
| 38 | + </DialogButtons> |
| 39 | +</TelerikDialog> |
| 40 | +
|
| 41 | +<TelerikButton OnClick="@OpenDialog">Open Dialog</TelerikButton> |
| 42 | +
|
| 43 | +<style> |
| 44 | + /* Remove default Dialog content padding to let wrapper handle it */ |
| 45 | + .focusable-content .k-dialog-content { |
| 46 | + padding: 0; |
| 47 | + } |
| 48 | +
|
| 49 | + /* Focusable wrapper that fills the entire Dialog content area */ |
| 50 | + .focusable-content .dialog-content-wrapper { |
| 51 | + padding: 1rem; |
| 52 | + outline: none; |
| 53 | + height: 100%; |
| 54 | + box-sizing: border-box; |
| 55 | + transition: background-color 0.2s ease; |
| 56 | + } |
| 57 | +
|
| 58 | + /* Optional visual indication when the wrapper is focused */ |
| 59 | + .focusable-content .dialog-content-wrapper:focus-within { |
| 60 | + background-color: rgba(0, 123, 255, 0.05); |
| 61 | + } |
| 62 | +</style> |
| 63 | +
|
| 64 | +@code { |
| 65 | + private bool DialogVisible { get; set; } = true; |
| 66 | + private string SampleText { get; set; } = string.Empty; |
| 67 | +
|
| 68 | + private void OpenDialog() |
| 69 | + { |
| 70 | + DialogVisible = true; |
| 71 | + } |
| 72 | +
|
| 73 | + private void CloseDialog() |
| 74 | + { |
| 75 | + DialogVisible = false; |
| 76 | + } |
| 77 | +} |
| 78 | +```` |
0 commit comments