# Modal

- Entry point: `@koobiq/components/modal`
- NgModule: `KbqModalModule`
- API: https://koobiq.io/api/modal.md
- Component tokens: https://koobiq.io/tokens/modal.md
- Page: https://koobiq.io/en/components/modal/overview

## Overview

A modal dialog opens as a window on top of the page, dims the underlying layer, and blocks all interaction with it. The modal dialog focuses the user's attention on its content.

[**Example: Modal component** (`modal-component`)](https://koobiq.io/examples/modal-component.md)

### When to use

Use modal dialogs for short-term tasks and user-triggered operations (you can open a short form, settings). It can be convenient to show hidden content in a modal window. Modals help accomplish tasks efficiently without losing context of the main page. Despite their versatility, modal windows are [not suitable for all situations](https://koobiq.io/en/components/modal.md#related-components).

### Component structure

- Header. Dialog title
- Close action
- Body. Contains message text, a question, controls, and input fields
- Footer. Action buttons, option to cancel the operation or close the dialog

Many of these parts may be absent. The body and the ability to close the dialog must remain. The required way to close the dialog is the × (cross) in the header or the "Cancel" ("Close") button in the footer.

#### Simple dialog without a header

Resembles a browser alert. Use when the system asks a single question or delivers a short message, to avoid redundant text duplication in the title and body of the modal window. Such dialogs do not have a close × icon in the top right corner.

There is no title to name the dialog for a screen reader here, so pass `kbqAriaLabel` — otherwise the dialog is announced unnamed.

[**Example: Modal** (`modal-overview`)](https://koobiq.io/examples/modal-overview.md)

#### Dialog with a title and caption

Use a caption to provide additional context below the dialog title — for example, a brief description of the action or its scope. The caption is limited to two lines and truncates with an ellipsis if the text is longer.

[**Example: Modal with caption** (`modal-caption`)](https://koobiq.io/examples/modal-caption.md)

### How it works

The page is dimmed and cannot be interacted with: buttons cannot be clicked, there is no hover, scrolling, etc. On top of the dim, the modal dialog window opens.

A dialog may open from a user action. The system can also block the user with a modal dialog, but such abrupt interruption should be avoided whenever possible.

#### How to close the dialog

- Performing the primary or secondary dialog actions (buttons in the footer)
- The "Cancel" or "Close" button in the footer
- The × in the header — only when the dialog heading is unavoidable
- Clicking outside the modal window. By default, modal windows do not close this way. However, closing by clicking outside the window can be useful when there is no risk of data loss and the system is not awaiting user input (for example, when there are no terminal buttons in the window).
- The Esc key (equivalent to the "Cancel" button)

The dialog closes on a left mouse button click only: the right and middle buttons open the browser's context menu or trigger its own actions and leave the window alone. The [side panel](https://koobiq.io/en/components/sidepanel.md) behaves the same way.

All of these close the dialog the same way whether it was opened with `KbqModalService` or declared as `<kbq-modal [(kbqVisible)]>`, and each runs the OK or the Cancel flow first.

Each flow has an input and an output, and you bind one or the other:

| Member                              | What it does                                                                                          |
| ----------------------------------- | ----------------------------------------------------------------------------------------------------- |
| `[kbqOkClick]` / `[kbqCancelClick]` | Decides. Returning `false` — or a promise of `false` — keeps the dialog open; anything else closes it |
| `(kbqOnOk)` / `(kbqOnCancel)`       | Notifies. The dialog closes either way                                                                |

Binding both on the same dialog does not combine them: the handler decides the close, so the event is never emitted. Development mode reports it.

Do not close a modal window automatically by timeout, as the user may not have had time to read the information inside.

Sometimes the system cannot allow the user to return to interacting with the page. Even in this situation, the modal window should not be a dead end in the use case — action options must be provided. For example, if the system shows a window about an expired session, returning to the page in the background is not possible — but a link to the login page should be offered.

#### Remembering user data

If any data has been entered in the window, it must be remembered and shown again when the dialog is reopened. For example, when a create form was being filled out. If an edit form was opened in a modal, changes were started but not saved, and the modal was closed, then unsaved data should not be pre-filled when reopening.

#### Scrolling

The page background does not scroll. The height of the modal window is determined by its content. The window stretches in height and occupies the full available screen height, minus 48px padding on each side.

Only the central part (body) of the dialog scrolls. The header and footer remain visible. They cast a shadow. The scrollbar appears inside the modal content block. It should be flush with the right edge of the window, without any offset.

A long modal is a clear sign that a [different component or a separate page](https://koobiq.io/en/components/modal.md#related-components) should be used.

[**Example: Modal scroll** (`modal-scroll`)](https://koobiq.io/examples/modal-scroll.md)

#### Focus and keyboard navigation

When a modal dialog is open, focus moves only through elements inside the window, including the × button and the footer buttons. Focus does not leave the modal.

##### Focus when opening a modal dialog

When a dialog opens, focus moves to an element inside the dialog. The focus placement depends on the nature and size of the content.

- Focus is placed on the first tabbable element of the dialog, unless otherwise specified. For a dialog with a header that is the × button, so mark the element you want instead with `autofocus` or `cdkFocusInitial` — either wins over the default
- If the dialog contains a form, focus should be placed in the first input field
- If the dialog window contains an important irreversible action, it is better to place focus on a different, less destructive element
- If the dialog contains only terminal buttons, it is useful to place focus on the button the user is most likely to press ("OK" or "Cancel", depending on the situation)
- If the modal window contains a lot of content with scrolling, placing focus on the first available element may automatically scroll the window content and hide the beginning. Set `kbqAutoFocus: 'dialog'` for that case: focus lands on the dialog itself, which is reachable by keyboard but does not scroll the body. `'first-heading'` focuses the title instead, and `false` leaves focus alone entirely — the caller then has to move it into the dialog

A dialog with no focusable control at all still takes focus off the trigger behind it: focus goes to the dialog element.

##### Focus after closing the modal window

When the dialog is closed, focus returns by default to the element that triggered it.

If the trigger element no longer exists, focus returns to another element appropriate to the interaction logic.

In some situations, focus may intentionally return to a different element than the one that originally opened the dialog:

- It is unlikely that the user will want to immediately open the same dialog again
- The task performed in the dialog is directly related to the next step in the workflow. For example, a table has a toolbar with an "Add rows" button. The button opens a dialog asking for the number of new rows. After the dialog is closed, focus is placed in the first cell of the first new row.

##### Keyboard navigation

| Key      | Action                                                                                                                                                               |
| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Ctrl + ↵ | Performs the primary dialog action                                                                                                                                   |
| Esc      | Close the dialog, exactly as the Cancel button does — the `kbqCancelClick` handler runs first and can veto the close. Set `kbqCloseByESC: false` to turn the key off |

### Validation

A form may be placed in the window body. Terminal submit buttons must be located in the window footer. [Validation follows general rules](https://koobiq.io/en/other/validation.md).

### Design and animation

#### Position

The window opens centered on screen with a 48px offset from the top edge. If the page was scrolled, there should be no horizontal shift when the dialog opens (this often occurs when the scrollbar appears and disappears when opening or closing a modal).

#### Size

There is no horizontal scroll; content in the panel must fit within the width.

| Preset | Width         |
| ------ | ------------- |
| Small  | 400           |
| Medium | 640 (default) |
| Large  | 960           |

[**Example: Modal sizes** (`modal-sizes`)](https://koobiq.io/examples/modal-sizes.md)

#### Button panel

Terminal buttons are located in the lower right corner of the footer. Order: primary action, secondary action, cancel. Additional action buttons are located in the lower left corner.

[**Example: Modal template** (`modal-template`)](https://koobiq.io/examples/modal-template.md)

#### Overlapping modal windows

Opening one modal dialog from another is incorrect. But if it happens (as an economy measure or forced workaround), only one dim overlay should remain. The page background and the parent modal sit beneath it. Multiple dim overlays is a mistake.

[**Example: Modal multiple** (`modal-multiple`)](https://koobiq.io/examples/modal-multiple.md)

#### Fully custom content

When a modal is opened via `KbqModalService.open` with a `kbqComponent`, use the `kbq-modal-title`, `kbq-modal-body`, and `kbq-modal-footer` directives to compose the dialog. To add a caption, place `kbq-modal-caption` inside `kbq-modal-title` — it is rendered in the dialog header, below the title. These directives keep the standard layout and scroll shadows, so a long body still casts a shadow onto the header and footer, exactly as in a modal created via `KbqModalService.create`.

The title and the caption also name and describe the dialog itself, through `aria-labelledby` and `aria-describedby`, so a composed dialog is announced the same way a dialog created via `KbqModalService.create` is.

For a control that belongs next to the heading rather than in the footer — a settings button, a counter, a link to the documentation — mark it `kbqModalTitleActions` and place it inside `kbq-modal-title`. It is rendered beside the heading, outside the two-line clamp the title and caption are subject to, so a long title does not push it out of view:

```html
<kbq-modal-title>
    Title
    <button kbq-button kbqModalTitleActions>…</button>
</kbq-modal-title>
```

[**Example: Modal full custom** (`modal-full-custom`)](https://koobiq.io/examples/modal-full-custom.md)

### Related components

A modal dialog abruptly concentrates attention on itself, causes the user to lose focus, and prevents interaction with the page beneath it. Therefore, in most cases you should try to avoid modality. These approaches will help you treat the user considerately and avoid modal window stacking:

A [Popover](https://koobiq.io/en/components/popover.md) captures the user's attention less disruptively. It does not interfere with interacting with the rest of the page and does not limit freedom. A popover is easier to accidentally close. If the system is waiting for an important response, or it is unclear what will happen if the popover is closed, use the trusty modal dialog.

A [Side panel](https://koobiq.io/en/components/sidepanel.md) is something between a new page and a modal window. It stretches the full height of the browser window; long and narrow pages look better in it than in a modal window, while wide and low pages look worse. Side panels stack on each other well, while modal windows should not be used with such stacking.

**Inline messages and inline forms** help show new content on the same layer of the page and do so less intrusively. This technique is actively used to display errors for the entire form — or to warn the user in advance about something important. Inline forms help avoid modal modes, new screens, and nesting in complex forms. For example, when you need to add a new object consisting of several fields (first name, last name, middle name) to a collection.

**Accordion**, "Show more" pattern, [tooltip](https://koobiq.io/en/components/tooltip.md) will help when you simply need to show additional content. This option is suitable for less important things than inline messages.

A toast notification will briefly report the result of an operation or show a completely unimportant message. Toast notifications are used when it is acceptable for the person to miss the message, when the message can quickly become irrelevant, or when there is another way to read it (for example, in the notification center).

**A new page** replaces a modal window when a lot of content, a long form, or a list needs to be shown. This approach allows content to be presented fully, with focus and without loss of functionality (for example, navigation).

**The "Undo changes" pattern.** Instead of a "Are you sure?" modal dialog, it is often better to immediately perform the operation — and give the ability to undo it.

### Close interception

Any close attempt — a backdrop click, Escape, the close button, or a call from code — can be intercepted to ask for confirmation before unsaved changes are lost. The guard can be asynchronous: while the decision is pending, repeated close attempts are ignored.

You can set the guard when the dialog opens or replace it afterwards — the latter takes precedence. It works alongside the existing close veto from the button click handlers: either mechanism can keep the dialog open. Programmatic closing goes through the guard too: it sees the close reason and can, for example, ask no confirmation when the dialog is closed from code. When closing by backdrop or Escape is disabled, the matching trigger never reaches the guard.

[**Example: Modal that stays open on a form error** (`modal-keep-open-on-error`)](https://koobiq.io/examples/modal-keep-open-on-error.md)

## Examples

### Custom Injector for Modal Component

A component passed as `kbqComponent` is created from the **root** environment injector, not from the component that called `KbqModalService`. Anything the caller provides itself is therefore invisible inside the modal: component-level services, tokens from a route or a lazily loaded module, and providers that reach the caller only through NgModules listed in its standalone `imports` — `DateAdapter`, `KBQ_DATE_FORMATS` and `DateFormatter` among them.

To make them available, pass the caller's `injector` to `modalService.open`; it becomes the parent of the modal content's injection hierarchy. Content passed as a `TemplateRef` is unaffected — it keeps the injector of the component that declared it.

A missing dependency surfaces as `NullInjectorError: No provider for …`, thrown while the modal content is being created.

[**Example: Modal component With Injector** (`modal-component-with-injector`)](https://koobiq.io/examples/modal-component-with-injector.md)

### Buttons in the loading state

A footer button renders a spinner and is disabled while the connection check is running. The server address is validated: on error, a hint appears under the field.

[**Example: Modal buttons in the loading state** (`modal-button-loading`)](https://koobiq.io/examples/modal-button-loading.md)
