add boxes / containers in mermaid sequence diagram

Viewed 1392

I'm making sequence diagrams with Mermaid, and I find the loop feature very cool, drawing a labeled rectangle around a loop with this code chunk:

sequenceDiagram
    loop Title
        Alice->>Bob: Hello John, how are you?
        Bob->>Alice: Answer
            loop Title
                Bob->>Bob: Thinks
            end
    end

Rendering like this: enter image description here

My question is: Can I use this rectangle container element for something else than a loop, for just grouping things, and naming it whatever i want, other than "loop" (it doesn't work if I change the keyword loop). It seems that there are only 'loop', 'opt' and 'alt' authorized keywords?

2 Answers

alt or opt can help you achieve this:

sequenceDiagram
    Alice->>Bob: Hello Bob, how are you?
    alt is sick
        Bob->>Alice: Not so good :(
    else is well
        Bob->>Alice: Feeling fresh like a daisy
    end
    opt Extra response
        Bob->>Alice: Thanks for asking
    end

See documentation

For now we cannot achieve this with current mermaid version. Hope they will update this in a newer version. See github issue.

Related