Skip to content

Notifications: transient toasts and a persistent notice stack #347

Open

Description

@samitbasu

Both consumers hand-build toasts.

  • One consumer's toast: a single slot where a newer toast replaces the old; a 2.4 s dwell on egui's clock with a repaint booked for expiry; placement that shifts with the view mode. It has no live-region announcement, no fade and no pause on hover (~30 lines of view + ~60 of timing).
  • Next to it, a persistent notice stack (~150 lines): cards with a sentence, inline actions and a × to dismiss; no timeout; its rect is reported so other floating UI avoids it.

Proposal: one notification model.

  • Transient toasts: a configurable stack or replace policy and a dwell as a Duration on a clock tests can step. Paused while hovered or focused. A polite live region (role=status). An optional enter/exit motion via the motion tokens.
  • Persistent notices: dismissible, with action links that have proper roles and names.
  • Shared: placement as a corner/edge anchor with an inset the app can change, and a queried rect for avoidance.

Must keep: one-slot replace, a deterministic dwell, view-dependent placement. Design note first; looks go to the user as candidates.

Activity

  1. added
    v0.1
    consumer
    on Nov 18, 2026
  2. samitbasu commented on Nov 18, 2026

    @samitbasu
    OwnerAuthor

    Design note: Notifications (transient toasts and persistent notices)

    Status: proposal for review. No code yet. The API shape and every look decision are yours to make; I give a recommendation for each.

    Goal (#351): one notification widget replaces the hand-built toast (about 30 lines of view and 60 of timing) and the notice stack (about 150 lines). It must keep the one-slot replace, a deterministic dwell, and placement that depends on the view.

    Where it lives: crates/eggshell-ui-extras, behind a notify feature, built only on core's public API. Its style lives in extras, with the shadcn values behind the shadcn crate's extras feature (the Markdown pattern). The public API can carry it today. The only core addition I recommend is optional: a one-method NodeHandle::live(..) (section 3).


    1. Proposed public API

    The model is one type for both kinds. Whether a stack is transient is a property of the stack, not of each item (Q2).

    use core::time::Duration;
    use eggshell_ui::Distance;
    use eggshell_ui_extras::notify::{Dismissed, Lifetime, Notice, NotifyExt, ScreenAnchor};
    
    #[derive(Clone, Copy)]
    enum Act { CopyDetails, Open }
    
    struct App {
        toast: Option<Notice>,          // one slot: replace = assign
        notices: Vec<Notice<Act>>,      // a stack: push / retain
        next_notice: u64,               // ids are the app's
        compact: bool,                  // a view mode that moves the toast
    }
    
    fn view<'f>(b: &mut Builder<'_, 'f, App>, app: &'f App) {
        // The transient toast, bottom-centre, 28 px up (41 px in one view mode).
        b.notifications("toast", app.toast.as_slice())
            .at(ScreenAnchor::BottomCenter, Distance::new(if app.compact { 41.0 } else { 28.0 }))
            .lifetime(Lifetime::Dwell(Duration::from_millis(2400)))
            .on_dismiss(|app, _id, _why| app.toast = None)
            .add();
    
        // Persistent notices, top-right, with actions and a × button.
        b.notifications("notices", &app.notices)
            .at(ScreenAnchor::TopRight, Distance::new(16.0))
            .lifetime(Lifetime::UntilDismissed)
            .on_action(|app, id, act| app.run(id, act))
            .on_dismiss(|app, id, _why| app.notices.retain(|n| n.id != id))
            .add();
    }
    
    // Posting: plain data, no library call.
    app.toast = Some(Notice::new(app.next_id(), "Copied"));
    app.notices.push(Notice::new(app.next_id(), "Export failed.")
        .action("Copy details", Act::CopyDetails)
        .action("Open log", Act::Open));
    pub struct Notice<Act = NoAction> {
        pub id: NoticeId,                  // a new id restarts the dwell
        pub text: String,
        pub actions: Vec<NoticeAction<Act>>,
    }
    pub enum Lifetime { Dwell(Duration), UntilDismissed }
    pub enum Dismissed { Expired, Closed, Escape }   // why `on_dismiss` ran
    pub enum ScreenAnchor { TopLeft, TopCenter, TopRight, BottomLeft, BottomCenter, BottomRight }

    Who owns state. The queue is app state: an Option<Notice> or a Vec<Notice> in the app's plain fields. The library never adds to it or removes from it. Expiry, ×, Escape and actions each come back to the app through a handler, as on_dismiss already does for overlays. This rule also settles the replace-or-stack question without any setting. The policy is the app's data structure: Option means replace and Vec means stack. A cap on visible items is truncate. Two settings that could disagree with the data simply don't exist.

    The library keeps only timing: the dwell left on each visible id. It is keyed by the stack's node id and the NoticeId, and lives in egui temp data, which extras can reach publicly. This mirrors the tooltip, whose hover-intent state is library-side while the app owns what is shown. A dwell is not app data. The app would otherwise have to tick it itself, which is the 60 lines this widget deletes.

    2. Behaviour

    Aspect Proposal
    Replace vs stack It follows the data: Option replaces and Vec stacks, newest nearest the anchor (Q3). A replacement carries a new id, so its dwell starts fresh and is never inherited.
    Dwell Lifetime::Dwell(Duration), a core::time::Duration. The clock is egui's input.time, converted once into Duration as tooltip::end does. A pure Dwell::tick(now, Hold) -> Remaining(Duration) | Expired follows the precedent of Tooltips::tick(now, ..). It is unit-tested with explicit Durations, and kittest steps it through step_dt. While a dwell runs, the widget books request_repaint_after(remaining), so a reactive app wakes at expiry without other input, as today.
    Pause Each dwell holds while the pointer is over the stack, while focus is within it (has_focus_within), and while the window is unfocused (!input.focused). That last case is sonner's "document hidden": a toast that expires while you're in another window was never read. All visible items pause together (sonner's behaviour, and the simplest rule). Time held is not counted, and the dwell resumes where it left off.
    Escape With focus inside a notice, Esc dismisses that notice (Dismissed::Escape). Esc anywhere else is left alone, because the page's dialogs and menus own it (Q5). The overlay is DismissOn::Never, so core's overlay Esc never fires for it.
    × dismiss It is a real button with Role::Button, named "Dismiss" plus the notice text, and it gives Dismissed::Closed. Whether a transient toast shows × is a look and behaviour choice (Q6).
    Actions Each action is a Role::Link (or Button, Q7) named by its label. Activating one runs on_action(app, id, act). The notice does not dismiss itself, because a "Copy details" action should leave it up. The app removes it in the handler if it wants that.
    Live region The stack's container is Role::Status with accesskit live = Polite. accesskit 0.25 has Live::{Off, Polite, Assertive} and Node::set_live, and egui 0.37 re-exports accesskit. Errors could be Role::Alert / Assertive (Q8). This needs a spike: whether egui's adapters actually announce a live-region change on AT-SPI, macOS and Windows has to be checked by hand with a screen reader before we rely on it.
    Keyboard reach Overlays come after the page in Tab order (docs/overlays.md), so Tab reaches notices without a hotkey. The overlay is Focus::None: posting a notice never steals focus. Sonner's Alt+T hotkey is Q9.
    Placement ScreenAnchor plus a Distance inset is a thin constructor of core's existing Placement: Anchor::Screen, Placement::edge(side).align(align), with the inset as Boundary::Window.inset(Sides { .. }). For full control, .placement(Placement) takes core's type directly. A view-dependent inset is just a value the app computes each frame.
    Band It renders as an overlay in Band::Popup, declared after the page so it stacks above an open dialog and stays reachable (Q10). Outside::PassThrough: a press beside a toast belongs to the page.
    Motion shown(!items.is_empty()) with an entrance from the extras style's KindMotion (Entrance::Slide(side) or Pop). It goes through core's presence, so reduced motion (animation_time == 0) already zeroes it. Core never fades a box (group opacity is missing; see presence.rs), so "fade" is not on the menu unless that changes. In v1 the stack as a whole enters and exits. An item arriving in or leaving a non-empty stack is instant (Q4).
    Rect for avoidance This is already public: placed.overlay("notices").map(|o| o.rect()) after layout. A view that must avoid the stack at build time keeps last frame's rect in a field, as the consumer does now, or passes it to its own overlay's Placement::boundary(Boundary::Rect(..).inset(..)). No new API (Q11).

    3. Reuse, and what core must make public

    Reused, with no parallel implementation:

    • Overlay (b.overlay, Anchor::Screen, Placement::edge, Boundary::inset, Band, Focus::None, DismissOn::Never).
    • Presence and motion (shown, entrance, KindMotion, reduced motion).
    • The owned button for × and for the actions. A link look is a named button Variant (shadcn's link and ghost), not new paint code.
    • The tooltip's Duration-based tick pattern.
    • has_focus_within and NodeUi::node_response for the pause.
    • Placed::overlay(..).rect() for avoidance.

    Verdict: it can live in extras on core's public API as it stands. The live region can be set from extras through egui's public accesskit_node_builder in a draw closure, which is how core's NodeHandle::expanded works. A role only reaches accesskit on a sensing node, so the container gets sense_hover(), which is public.

    The one optional core primitive: NodeHandle::live(Live), about 15 lines mirroring expanded(). I recommend it (Q1). It is not required for extras to work. The reason is that "announce changes here" is a general need: status lines, Spinner #348 and progress messages all have it. Without it, each extras widget reaches into accesskit separately.

    4. Look decisions for a render round (no picks made)

    I'll render each candidate at real size, in light and dark, over a busy page:

    1. Card surface: fill (sonner/shadcn use popover), border, radius (--radius), shadow.
    2. Width: fixed (sonner 356 px; the consumer's notice stack is fixed) or fit to content with a max.
    3. Padding and text: a single sentence or title plus description, with typography roles.
    4. Gap between stacked items (sonner 14 px) and the maximum visible.
    5. Stacking style: a plain list, or sonner's collapsed pile that expands on hover.
    6. Tone: neutral only, or success/info/warning/error variants with lucide icons (shadcn's sonner wires CircleCheck, Info, TriangleAlert, OctagonX, plus a spinning Loader2), and whether "rich colours" tint the card.
    7. × button: shown on toasts or only on notices; its position (sonner puts it top-left, many apps top-right) and its size and icon.
    8. Action links: link-styled text or small ghost or outline buttons; their placement (inline after the sentence, or a footer row).
    9. Entrance: slide from the anchor edge, pop (zoom 95% plus a short slide), or none; plus durations and easing.
    10. Default insets per anchor (consumer: 28 px bottom; sonner 24 px desktop, 16 px mobile).
    11. Toast vs notice: one look, or a visual distinction between transient and persistent items.

    5. Breaking changes and PR split

    Breaking changes: none. Everything is additive.

    PR Contents Size
    1 (core, optional) NodeHandle::live(Live), with a test reading the accesskit tree. XS (~40 lines)
    2 (extras) The notify feature: the model, the pure Dwell with unit tests, the widget (overlay, placement, pause, Esc, ×, actions, live region), and kittest tests through the full path. Tests step the clock past the dwell, check that hover and focus hold it, check that a replacement restarts it, and check that Esc only acts with focus inside. M (~600)
    3 (extras + shadcn) NotifyStyle after the render round, its shadcn fill behind extras, and an example. A manual screen-reader check of the live region. You try the build before the PR is approved. S–M (~250)

    The consumer migrations happen in the consumers' own repos afterwards.

    6. Questions for you

    # Question My recommendation
    Q1 Add NodeHandle::live(Live) to core, or set the live region from extras through egui's accesskit hook? Add it. It is tiny and general (Spinner #348 wants it too).
    Q2 Lifetime per stack (.lifetime(..)) or per notice? Per stack. Both consumer stacks are uniform, and a mixed stack has no user yet.
    Q3 Order in a stack: newest nearest the anchor (sonner), or appended in reading order? Newest nearest the anchor.
    Q4 Per-item enter and exit motion inside a non-empty stack? It costs keeping a departed item's content in library state, as the tooltip does. Not in v1. Whole-stack motion only; revisit after the render round.
    Q5 Esc: dismiss only the focused notice, or the newest toast wherever focus is? Focused only. A global Esc would fight dialogs and menus.
    Q6 Do transient toasts show ×? No by default, with an option. The persistent notices always show it.
    Q7 Actions as Role::Link or Role::Button? Button. They act and don't navigate, even when styled as links.
    Q8 Politeness: all polite, or errors assertive (Role::Alert)? All polite in v1. Assertive interrupts and is easy to overuse.
    Q9 A hotkey to focus the stack (sonner's Alt+T)? No. Tab order reaches it. Add it through app commands (#325) if a consumer asks.
    Q10 Band: Popup (above dialogs, reachable during a modal) or Floating (with the page, under popups and scrims)? Popup. A toast posted by a dialog's action must be visible.
    Q11 Rect for avoidance: the existing placed.overlay(key).rect() only, or an .on_rect(|app, rect|) callback that writes it into app state? The existing query. It needs no new API and keeps derived layout out of app fields unless the app chooses to store it.
    Q12 Pause while the window is unfocused? Yes, as sonner does.
  3. samitbasu commented on Nov 18, 2026

    @samitbasu
    OwnerAuthor

    Decisions (user, 2026-11-18):

    • The app holds the queue (Option replaces, Vec stacks); eggshell only times each notice's dwell.
    • Escape dismisses only a focused notice.
    • Toasts have no × by default (an option adds one); persistent notices always have ×.
    • Notices float above dialogs; the newest sits nearest the anchor.
    • The dwell pauses while hovered or focused and while the window is unfocused.
    • All announcements are polite in v1, with no per-item entrance animation.
    • The rest of the note's recommendations stand: core live(), the lifetime per stack, actions as buttons, no hotkey, the rect via the existing query.

    Placement: the widgets crate (#366), extras until the split. The look goes to a render round (11 items).

  4. samitbasu commented on Nov 18, 2026

    @samitbasu
    OwnerAuthor

    Looks decided (user, 2026-11-18): all 11 recommendations accepted:

    1. popover fill, a 1px border and sonner's shadow;
    2. a fixed 356 pt width;
    3. 16 pt padding with a 13 px sentence;
    4. a 14 pt gap, 3 visible;
    5. a plain list;
    6. neutral tone in v1;
    7. × as a trailing ghost button;
    8. actions as small outline buttons inline;
    9. a 24 pt inset;
    10. one shared look for toasts and notices;
    11. no entrance animation.

    Before finalising PR 3, check each sonner reference value against sonner's actual source; the note wrote them from memory. Then build the style, the shadcn values and the example, and hand it over for a user trial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    consumerv0.1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions