App Shells
These whole-screen layouts combine the techniques from the other pages.
Each root fills the screen
and is split with flexbox:
fixed parts take a length or their content's size, and the part that fills takes fill(1).
When that part holds more than fits, such as a log, a transcript or a long document,
make it a scroll container with overflow_y_scroll,
so its content scrolls inside it while the fixed parts stay put.
Splitting space shows the same splits with grid,
and Choosing a layout model builds a whole shell both ways.
Header, body and footer
The header and footer take their content's height, and the body takes the rest.
@component
def header_body_footer() -> Div:
return Div(
style=col,
children=(
Text(style=border | border_heavy | text_justify_center, content="header: content height"),
Text(style=fill(1) | border, content="body: fill(1)"),
Text(style=text_bg("slate", 700), content=" footer: one row, no border"),
),
)
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ header: content height ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
┌──────────────────────────────────────────────────────────┐
│body: fill(1) │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────┘
footer: one row, no border
Sidebar, main pane and status bar
A row that fills the rest of a column holds the sidebar and the main pane, and the status bar takes one row below them.
@component
def sidebar_status() -> Div:
return Div(
style=col,
children=(
Div(
style=row | fill(1),
children=(
Text(style=length(20) | border, content="sidebar:\nlength(20)"),
Text(style=fill(1) | border, content="main: fill(1)"),
),
),
Text(style=text_bg("slate", 700), content=" status bar: one row, no border"),
),
)
Three panes
Fixed panes on both sides, and a middle column split 2:1 between two panes.
border_collapse on both containers makes neighbors share their borders.
@component
def three_panes() -> Div:
return Div(
style=row | border_collapse,
children=(
Text(style=length(20) | border, content="files:\nlength(20)"),
Div(
style=col | fill(1) | border_collapse,
children=(
Text(style=fill(2) | border, content="editor: fill(2)"),
Text(style=fill(1) | border, content="terminal: fill(1)"),
),
),
Text(style=length(20) | border, content="outline:\nlength(20)"),
),
)
┌──────────────────┬────────────────────────────────────────┬──────────────────┐
│files: │editor: fill(2) │outline: │
│length(20) │ │length(20) │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ ├────────────────────────────────────────┤ │
│ │terminal: fill(1) │ │
│ │ │ │
│ │ │ │
│ │ │ │
└──────────────────┴────────────────────────────────────────┴──────────────────┘
Hiding a pane
A pane that comes and goes, such as a sidebar the user can collapse, can be hidden two ways,
and they differ in what happens to its components.
display_none takes an element out of the layout, as CSS's display: none does:
it takes no space and draws nothing, and its siblings fill in where it was.
Its components stay mounted, so they keep their state and their effects keep running.
Leaving the pane out of its parent's children instead, as React's conditional rendering does,
unmounts its components, which start afresh, with new state, when it comes back.
Here each side pane shows how long it has been mounted.
h hides the left one with display_none, and l leaves the right one out:
@component
def pane(name: str) -> Text:
mounted_for = use_clock(timedelta(seconds=1)).since_mount
return Text(
style=length(18) | border,
content=f"{name}\n\nmounted for\n{mounted_for // timedelta(seconds=1)} s",
)
@component
def hiding() -> Div:
is_hidden, set_hidden = use_state(False)
is_left_out, set_left_out = use_state(False)
def on_input(event: InputEvent, _: Receiver) -> InputResponse:
match event:
case KeyPressed(key="h"):
set_hidden(toggle)
return ()
case KeyPressed(key="l"):
set_left_out(toggle)
return ()
case _:
return None
return Div(
style=col,
on_input=on_input,
children=(
Div(
style=row | fill(1),
children=(
# Hidden, the pane takes no space and draws nothing, but stays mounted.
Div(style=display_none if is_hidden else default, children=(pane("display_none"),)),
Text(style=fill(1) | border, content="main: fill(1)"),
# Left out, the pane unmounts, and mounts afresh when it comes back.
*(() if is_left_out else (pane("left out"),)),
),
),
Text(style=text_bg("slate", 700), content=" h: hide the left pane l: leave out the right pane"),
),
)
With both gone, the main pane fills the row:
Brought back after a while, the hidden pane has been mounted all along, while the pane that was left out has only just mounted again:
┌────────────────┐┌────────────────────────────────┐┌────────────────┐
│display_none ││main: fill(1) ││left out │
│ ││ ││ │
│mounted for ││ ││mounted for │
│10 s ││ ││2 s │
│ ││ ││ │
└────────────────┘└────────────────────────────────┘└────────────────┘
h: hide the left pane l: leave out the right pane
Hide a pane with display_none when what's in it should survive being hidden,
such as a half-typed draft or a scroll position,
and leave it out when it should start over, or when it's expensive to keep running.