11import { html , nothing , type TemplateResult } from "lit" ;
22import { state } from "lit/decorators.js" ;
3+ import { styleMap } from "lit/directives/style-map.js" ;
4+ import { icons } from "../components/icons.ts" ;
35import { t } from "../i18n/index.ts" ;
46import { OpenClawLightDomContentsElement } from "../lit/openclaw-element.ts" ;
57import { formatUiExternalText } from "./format-error.ts" ;
@@ -10,13 +12,18 @@ export type ToastOptions = {
1012 /** A template lets a message name a destination the operator can actually open,
1113 * instead of spelling out a settings path the toast then makes them find. */
1214 message : string | TemplateResult ;
15+ /** Positions a compact toast at the top center of the owning surface. */
16+ anchor ?: Element ;
17+ anchorTopOffset ?: number ;
18+ icon ?: TemplateResult ;
1319 actionLabel ?: string ;
1420 onAction ?: ( ) => void ;
1521 onDismiss ?: ( reason : ToastDismissReason ) => void ;
1622 durationMs ?: number ;
1723} ;
1824
1925const DEFAULT_TOAST_DURATION_MS = 6_000 ;
26+ const TOAST_EXIT_FALLBACK_MS = 450 ;
2027
2128function activeModalToastLayer ( ) {
2229 return [ ...( document . openClawModalToastLayers ?? [ ] ) ] . findLast (
@@ -31,7 +38,10 @@ let queuedToast: ToastOptions | null = null;
3138
3239class OpenClawToastHost extends OpenClawLightDomContentsElement {
3340 @state ( ) private toast : ToastOptions | null = null ;
41+ @state ( ) private active = false ;
3442 private dismissTimer : ReturnType < typeof globalThis . setTimeout > | null = null ;
43+ private exitTimer : ReturnType < typeof globalThis . setTimeout > | null = null ;
44+ private exitReason : ToastDismissReason | null = null ;
3545
3646 override connectedCallback ( ) {
3747 super . connectedCallback ( ) ;
@@ -56,8 +66,10 @@ class OpenClawToastHost extends OpenClawLightDomContentsElement {
5666 connectedMoveCallback ( ) { }
5767
5868 show ( options : ToastOptions ) {
59- this . dismiss ( "replaced" ) ;
69+ this . finishDismiss ( this . exitReason ?? "replaced" ) ;
6070 this . toast = options ;
71+ this . active = true ;
72+ this . exitReason = null ;
6173 this . dismissTimer = globalThis . setTimeout (
6274 ( ) => this . dismiss ( "timeout" ) ,
6375 options . durationMs ?? DEFAULT_TOAST_DURATION_MS ,
@@ -69,22 +81,85 @@ class OpenClawToastHost extends OpenClawLightDomContentsElement {
6981 globalThis . clearTimeout ( this . dismissTimer ) ;
7082 this . dismissTimer = null ;
7183 }
84+ if ( this . exitTimer !== null ) {
85+ globalThis . clearTimeout ( this . exitTimer ) ;
86+ this . exitTimer = null ;
87+ }
7288 }
7389
74- private dismiss ( reason : ToastDismissReason ) {
90+ private finishDismiss ( reason : ToastDismissReason ) {
7591 const toast = this . toast ;
7692 this . clearDismissTimer ( ) ;
93+ this . active = false ;
94+ this . exitReason = null ;
7795 this . toast = null ;
7896 toast ?. onDismiss ?.( reason ) ;
7997 }
8098
99+ private dismiss ( reason : ToastDismissReason ) {
100+ const toast = this . toast ;
101+ if ( ! toast ) {
102+ return ;
103+ }
104+ this . clearDismissTimer ( ) ;
105+ const reducedMotion = globalThis . matchMedia ?.( "(prefers-reduced-motion: reduce)" ) . matches ;
106+ const anchorRect = toast . anchor ?. isConnected ? toast . anchor . getBoundingClientRect ( ) : null ;
107+ const anchored = anchorRect !== null && anchorRect . width > 0 ;
108+ if (
109+ ( reason !== "dismiss" && reason !== "timeout" ) ||
110+ reducedMotion ||
111+ ! this . isConnected ||
112+ ! anchored
113+ ) {
114+ this . finishDismiss ( reason ) ;
115+ return ;
116+ }
117+ this . active = false ;
118+ this . exitReason = reason ;
119+ this . exitTimer = globalThis . setTimeout ( ( ) => {
120+ if ( this . toast === toast ) {
121+ this . finishDismiss ( reason ) ;
122+ }
123+ } , TOAST_EXIT_FALLBACK_MS ) ;
124+ }
125+
81126 override render ( ) {
82127 const toast = this . toast ;
83128 if ( ! toast ) {
84129 return nothing ;
85130 }
131+ const anchorRect = toast . anchor ?. isConnected ? toast . anchor . getBoundingClientRect ( ) : null ;
132+ const anchored = anchorRect !== null && anchorRect . width > 0 ;
86133 return html `
87- < div class ="app-toast " role ="status " aria-live ="polite " aria-atomic ="true ">
134+ < div
135+ class ="app-toast ${ anchored ? "app-toast--anchored" : "" } "
136+ data-active =${ this . active ? "true" : "false" }
137+ style =${ styleMap (
138+ anchored
139+ ? {
140+ "--app-toast-anchor-center" : `${ anchorRect . left + anchorRect . width / 2 } px` ,
141+ "--app-toast-anchor-top" : `${ anchorRect . top + ( toast . anchorTopOffset ?? 0 ) } px` ,
142+ "--app-toast-anchor-width" : `${ anchorRect . width } px` ,
143+ }
144+ : { } ,
145+ ) }
146+ role="status"
147+ aria-live="polite"
148+ aria-atomic="true"
149+ @transitionend=${ ( event : TransitionEvent ) => {
150+ if (
151+ event . target === event . currentTarget &&
152+ event . propertyName === "opacity" &&
153+ ! this . active &&
154+ this . exitReason
155+ ) {
156+ this . finishDismiss ( this . exitReason ) ;
157+ }
158+ } }
159+ >
160+ ${ toast . icon
161+ ? html `< span class ="app-toast__icon " aria-hidden ="true "> ${ toast . icon } </ span > `
162+ : nothing }
88163 < span class ="app-toast__message "
89164 > ${ typeof toast . message === "string"
90165 ? formatUiExternalText ( toast . message )
@@ -110,7 +185,7 @@ class OpenClawToastHost extends OpenClawLightDomContentsElement {
110185 aria-label =${ t ( "common.dismiss" ) }
111186 @click =${ ( ) => this . dismiss ( "dismiss" ) }
112187 >
113- ×
188+ ${ icons . x }
114189 </ button >
115190 </ div >
116191 ` ;
0 commit comments