cps-dialog: let closeOnEscape be scoped to focus inside the dialog (for non-modal dialogs)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- angular, typescript
- Ambito
- frontend
Direzione di ricerca
Start in projects/cps-ui-kit/src/lib/services/cps-dialog/utils/cps-dialog-config.ts (the closeOnEscape type, around line 102), then cps-dialog.component.ts: bindGlobalListeners() (around line 840) and bindDocumentEscapeListener() (around line 868). Done when closeOnEscape accepts 'focus-within', Esc inside the dialog (including the frame) closes only the topmost dialog, Esc outside is left unprevented, and unit tests plus a Playwright spec in playwright/cps-ui-kit/components/ cover those cases.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
cps-dialog can only close on Esc in two ways: anywhere on the page (closeOnEscape: true, the default) or never (closeOnEscape: false). A non-modal dialog that sits next to an application (a tools or inspector panel, a side panel) needs a third behaviour: close on Esc only when focus is inside the dialog, and only when the dialog is the topmost overlay.
Today an application has to choose false and write its own Esc handler. That handler lives outside the kit's stacking logic, so it cannot follow the rule the kit applies everywhere else ("Esc closes the topmost overlay"), and it misses some cases.
Proposal: let closeOnEscape also accept a focus-scoped mode, for example 'focus-within'. true and false keep their current meaning.
Current behaviour
CpsDialogConfig.closeOnEscapeis a plain boolean: "Specifies if pressing escape key should hide the dialog." (projects/cps-ui-kit/src/lib/services/cps-dialog/utils/cps-dialog-config.ts, around line 102.)- In
cps-dialog.component.ts,bindGlobalListeners()(around line 840) bindsbindDocumentEscapeListener()whencloseOnEscape !== false. That listener (around line 868) is a document-levelkeydownlistener. It hides the dialog whenparseInt(this.container.style.zIndex) === ZIndexUtils.getCurrent(), that is, when the dialog is the topmost overlay, wherever focus is. Since 21.33.0 it also callsevent.preventDefault()for the keypress it handled. - The notification service (
cps-notification.service.ts,_onKeydown, around line 262) clears notifications on Esc unlessevent.defaultPrevented, and only if a notification containerisOnTop(). - Checked in 21.32.0, 21.33.0 and 22.3.0: the dialog's Esc logic is the same apart from the
preventDefault().
Why neither value works for a non-modal dialog
An application whose dialog stays open beside the page (it is not modal and the user keeps working in the app) sees this:
closeOnEscape: true: pressing Esc in an unrelated field of the page closes the dialog. This was measured in an application: with focus in a text field outside the dialog, Esc closed it. Because the kit also callspreventDefault(), other handlers on the page that respectdefaultPreventedtreat that Esc as already handled.closeOnEscape: false: the application has to listen for Esc itself, only inside the dialog, to close it. That has three problems:- It ignores the kit's stacking rule. If a notification (or any overlay) is on top, the kit's own rule is "dismiss that first, close the dialog on the next Esc". An application cannot read the stack, so it closes the dialog while the notification is on top, or closes both.
- The application's handler has to mark the event handled (
preventDefault()) by hand, otherwise the notification service also clears the notifications on the same keypress. This is easy to forget, and the convention is not documented for dialogs. - Focus can be on the dialog frame itself (
div.cps-dialog[tabindex="-1"]), for example after clicking the header. An application listener attached to the dialog's content does not receive the keydown, so Esc does nothing at all there. (WithcloseOnEscape: trueit closes, as expected.)
The kit already contains everything needed to get this right (the z-index stack, the handled-event convention). It only lacks a way to scope the existing dialog listener to focus.
Proposed behaviour
Extend the type:
closeOnEscape?: boolean | 'focus-within';
true(default): unchanged.false: unchanged.'focus-within': the dialog closes on Esc only if- the key event's target is inside the dialog container (the content, the header, or the frame itself), and
- the dialog is the topmost overlay, using the same z-index check as today, and
- the event is handled the way it is today (
preventDefault()when the dialog closes).
An Esc from outside the dialog is left alone: it is not prevented, so the application and other overlays handle it as they do now. Overlays opened from inside the dialog (menus and tooltips were checked) already take the Esc first through the stack check and their own handlers, so nothing changes for them.
Suggested implementation: in bindDocumentEscapeListener(), when the option is 'focus-within', add this.container.contains(event.target as Node) to the existing condition. bindGlobalListeners() already tests closeOnEscape !== false, so it keeps binding the listener for the new value.
Acceptance criteria
- With
closeOnEscape: 'focus-within':- Esc pressed in an element inside the dialog closes it.
- Esc pressed on the dialog frame itself closes it (for example after a click on the header).
- Esc pressed in a field outside the dialog does not close it, and the event is not
defaultPrevented. - With a notification on top of the dialog, the first Esc inside the dialog dismisses the notification and leaves the dialog open, and the next Esc closes the dialog.
- With a menu opened from the dialog, the first Esc closes the menu, not the dialog.
closeOnEscape: trueandfalsebehave exactly as before. The existing dialog specs still pass.- Unit tests for each case above, and a Playwright test next to the existing dialog specs (
playwright/cps-ui-kit/components/) for the "Esc in a field outside the dialog" and "Esc on the frame" cases. - The dialog documentation describes the three values, with a note that
'focus-within'is meant for non-modal dialogs.
Related
- #948: the focus ring on the dialog frame covers the dialog and blocks clicks. It is reached by the same steps (click the header, then press a key), and the frame-focus case above is where the two meet.
- #943 (draft): Esc ordering between tooltips, notifications and tree-select. This request follows the same direction, for dialogs.
Alternatives considered
- Documenting that apps should call
preventDefault()in their own handler. This avoids the double close but still cannot follow the stacking rule and still misses the frame-focus case. - Making notifications ignore an Esc that starts inside a dialog. This would change the behaviour for every dialog, including ones that have disabled closing, where Esc should still dismiss notifications.
- Lingua principale
- TypeScript
- Stelle
- 9
- Fork
- 8
- Merge medio
- 1g 6h
- PR unite (30g)
- 25
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di AbsaOSS/cps-shared-ui
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
AbsaOSS/cps-shared-ui#948 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
AbsaOSS/cps-shared-ui#875 ·
I maintainer di solito rispondono entro 1 giorno
-
InputComponent - expand type beyond text/number/passwordForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Apertaenhancement library
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
AbsaOSS/cps-shared-ui#644 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 62/100
AbsaOSS/cps-shared-ui#950 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 Mezza giornata Idoneità per principianti 55/100
AbsaOSS/cps-shared-ui#949 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di AbsaOSS/cps-shared-ui
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
vercel-labs/ui#540 ·
I maintainer di solito rispondono entro 1 giorno
-
accepted ai-dev ai-implementing
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
livesport/homer#218 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
normalizePropName strips `#` characters from Figma property names instead of only the `#id` suffixAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
figma/code-connect#419 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Negative values skip k/M abbreviation and medium decimal format on line, stacked-area, and tooltipAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
britecharts/britecharts#1255 ·
I maintainer di solito rispondono entro 1 giorno