Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Layout & Scrolling

Every layout container arranges the widgets declared inside its do block. Plain forms like row and column use the default layout, while rowWith and columnWith take a Layout -> Layout modifier function first. Modifiers compose with (.), and when two modifiers set the same property, the leftmost one wins.

settingsPanel :: NanoUI ()
settingsPanel =
  columnWith (gap 8 . padAll 12 . fillW) $ do
    label "Settings"
    whenM (buttonWith (fixedW 120) "Apply") apply

All coordinates and sizes use logical pixels with the origin at the top-left and y increasing downward. Backends handle DPI scaling automatically.

Expand any example below to inspect its Haskell source alongside the rendered output. Each frame adds padAll 14 and fills the preview window so padding and alignment are easy to see.

Containers

A container wraps a NanoUI action and positions its children. Use the plain form (row, column, panel) for defaults, or the ...With variant to apply modifiers.

row / rowWith: arrange children left to right
rowExample =
  rowWith (gap 8) $
    mapM_ (void . button) ["One", "Two", "Three"]
Three buttons in a horizontal row.
column / columnWith: stack children top to bottom
columnExample =
  columnWith (gap 8) $
    mapM_ (void . buttonWith fillW) ["One", "Two", "Three"]
Three full-width buttons stacked vertically.
grid / gridWith: place children in equal-width columns
gridExample =
  gridWith 3 (gap 8 . fillW) $
    mapM_
      (\i -> void (buttonWith fillW ("Cell " <> T.pack (show (i :: Int)))))
      [1 .. 6]
Six buttons in a three-column grid.
panel / panelWith: a container with a background and border
panelExample =
  panelWith (fixedW 300 . padAll 12 . gap 6) $ do
    heading "Deployments"
    muted "3 services running"
A panel with a heading and a muted line of text.
card: a full-width panel with a minimum width
cardExample =
  card $ do
    heading "Build #482"
    muted "Passed in 1m 42s"
A full-width card showing a build result.
callout / calloutTone: a panel tinted with a semantic color
calloutExample = do
  warnCol <- themeWarning <$> uiTheme
  callout warnCol $ do
    heading "Certificate expiring"
    muted "TLS certificate expires in 3 days."

calloutTone Warning body resolves a theme Tone directly instead of taking an explicit Color.

An amber callout warning about an expiring certificate.
layers: stack children in one box, later children on top
layersExample =
  layers $ do
    let badgeRed = colorRGB 204 64 64
    void (buttonWith (fixedWH 170 40) "Inbox")
    styled (panelStyle (background badgeRed . borderColor badgeRed)) $
      panelWith (alignEnd . alignTop . padXY 6 1) $
        labelWith (fontColor colorWhite) "3"
A button with a small red count badge stacked at its top right.
center: center content on both axes
centerExample =
  panelWith (fillW . fillH) $
    center (label "Centered on both axes")
A label centered in a large panel.
responsive / responsiveRowCol / container queries: switch layouts at a breakpoint

responsive breakpoint wide narrow body chooses between two containers based on window width; responsiveRowCol switches between a row and a column. For layouts inside split panes or sidebars that depend on the enclosing container’s own inner width rather than the window, use responsiveIn, responsiveRowColIn, withContainerQuery, withContainerWidth, or withContainerSize. withContainerQuery projects the inner Size to any Eq value and only schedules a follow-up frame when that projected value changes.

responsiveExample =
  responsiveRowCol 640 (tight . gap 8 . fillW) $ do
    panelWith (fillW . padAll 12) (label "Sidebar")
    panelWith (fillW . padAll 12) (label "Content")

At 420 px wide:

Sidebar and Content stacked vertically at 420 pixels wide.

The same view at 760 px wide:

Sidebar and Content side by side at 760 pixels wide.
mouseArea: track hover and clicks across a whole region
mouseAreaExample = do
  (_, area) <-
    panelWith fillW $
      mouseArea (fillW . padAll 10 . gap 4) $ do
        label "build.log"
        muted "Click anywhere in this card"
  when (respClicked area) (muted "opening build.log")
A card containing two lines, wrapped in a mouse area.
separator: draw a one-pixel divider rule
separatorExample =
  columnWith (fillW . gap 8) $ do
    label "Above the rule"
    separator
    label "Below the rule"
A horizontal rule between two labels.
spacer: insert flexible space between siblings
spacerExample =
  rowWith (fillW . alignMiddle) $ do
    label "Left"
    spacer (Grow 1) Fit
    label "Right"
Left and Right labels pushed to opposite ends of a row.

Modifiers

Modifiers are plain Layout -> Layout functions chained with (.). Because the leftmost function runs last in (f . g) x, it overrides anything to its right: tight . padAll 14 zeroes padding out, while padAll 14 . tight keeps the 14 px padding.

Sizing

fillW: take all remaining horizontal space
fillWExample =
  rowWith (fillW . gap 8) $ do
    void (buttonWith (fixedW 110) "fixedW 110")
    void (buttonWith fillW "fillW")
A fixed-width button beside a button filling the rest of the row.
fillH: take all remaining vertical space
fillHExample =
  rowWith (fillW . gap 8 . fixedH 96) $ do
    panelWith (fixedW 120 . fillH) (label "fillH")
    panelWith (fixedW 120) (label "natural")
One panel filling a fixed-height row and one panel at its natural height.
grow: fill both axes (fillW . fillH)
growExample =
  rowWith (grow . gap 8) $ do
    panelWith (fixedW 120) (label "fixedW 120")
    panelWith grow (label "grow")
A small fixed panel beside a panel growing to fill the remaining space.
fixedW: set an exact width
fixedWExample =
  rowWith (gap 8) $ do
    void (buttonWith (fixedW 60) "60")
    void (buttonWith (fixedW 110) "110")
    void (buttonWith (fixedW 170) "170")
Three buttons at 60, 110, and 170 pixels wide.
fixedH: set an exact height
fixedHExample =
  rowWith (gap 8 . fillW) $ do
    void (buttonWith (fixedH 30) "fixedH 30")
    void (buttonWith (fixedH 56) "fixedH 56")
    void (buttonWith (fixedH 82) "fixedH 82")
Three buttons at 30, 56, and 82 pixels tall.
fixedWH: set an exact width and height
fixedWHExample =
  rowWith (gap 8) $ do
    void (buttonWith (fixedWH 120 36) "120 x 36")
    void (buttonWith (fixedWH 200 72) "200 x 72")
A small button and a larger button with explicit sizes.
minW: set a minimum width
minWExample =
  rowWith (fillW . gap 8) $ do
    void (buttonWith (minW 200) "minW 200")
    void (button "natural")
A wide minimum-width button beside a natural-width button.
maxW: cap width at an upper bound
maxWExample =
  rowWith (fillW . gap 8) $ do
    void (buttonWith (fillW . maxW 160) "maxW 160")
    void (buttonWith fillW "fillW")
A capped button beside an uncapped fill-width button.
minH: set a minimum height
minHExample =
  rowWith (fillW . gap 8) $ do
    panelWith (minH 80 . fillW) (label "minH 80")
    panelWith fillW (label "natural")
A panel forced to 80 pixels tall beside one at its natural height.
maxH: cap height at an upper bound
maxHExample =
  rowWith (fillW . gap 8 . fixedH 100) $ do
    panelWith (maxH 50 . fillW) (label "maxH 50")
    panelWith (fillW . fillH) (label "fillH")
A panel capped at 50 pixels beside one filling a 100-pixel row.
percent: take a percentage of the parent's width
percentExample =
  rowWith (fillW . gap 8) $ do
    panelWith (percent 30) (label "percent 30")
    panelWith (percent 60) (label "percent 60")
Two panels sized to 30 and 60 percent of their row.
aspect: preserve a width-to-height ratio
aspectExample =
  panelWith (fillW . aspect (16 / 9)) $
    center (label "16 : 9")
A wide panel with a 16 by 9 aspect ratio.

Spacing

padAll: equal padding on all four sides
padAllExample =
  panelWith (fixedWH 260 120 . padAll 20) $
    panelWith (fillW . fillH) (label "padAll 20")
A panel inset by 20 pixels on every side inside another panel.
padXY: horizontal padding first, then vertical
padXYExample =
  panelWith (fixedWH 280 100 . padXY 32 10) $
    panelWith (fillW . fillH) (label "padXY 32 10")
A panel with wide horizontal padding and shallow vertical padding.
padLeft: left padding
padLeftExample =
  panelWith (fixedWH 260 100 . padLeft 48) $
    panelWith (fillW . fillH) (label "padLeft 48")
A panel inset 48 pixels from the left.
padRight: right padding
padRightExample =
  panelWith (fixedWH 260 100 . padRight 48) $
    panelWith (fillW . fillH) (label "padRight 48")
A panel inset 48 pixels from the right.
padTop: top padding
padTopExample =
  panelWith (fixedWH 260 120 . padTop 40) $
    panelWith (fillW . fillH) (label "padTop 40")
A panel inset 40 pixels from the top.
padBottom: bottom padding
padBottomExample =
  panelWith (fixedWH 260 120 . padBottom 40) $
    panelWith (fillW . fillH) (label "padBottom 40")
A panel inset 40 pixels from the bottom.
padLRTB: individual left, right, top, and bottom padding
padLRTBExample =
  panelWith (fixedWH 280 120 . padLRTB 40 8 30 12) $
    panelWith (fillW . fillH) (label "padLRTB 40 8 30 12")
A panel with different padding on each side.
gap: spacing between adjacent children
gapExample =
  columnWith (fillW . gap 12) $ do
    rowWith (gap 4) $ mapM_ (void . button) ["gap 4", "B", "C"]
    rowWith (gap 24) $ mapM_ (void . button) ["gap 24", "B", "C"]
Two rows with tight and wide spacing between their buttons.
tight: zero container padding while keeping its gap
tightExample =
  rowWith (gap 16 . fillW) $ do
    panelWith (padAll 14 . gap 6 . fillW) $ do
      muted "padAll 14"
      label "default padding"
    panelWith (tight . padAll 14 . gap 6 . fillW) $ do
      muted "tight"
      label "no padding"
Two panels comparing normal padding with tight.

Alignment, distribution, and wrapping

alignStart, alignCenter, and alignEnd position a child horizontally inside a column. alignTop, alignMiddle, alignBottom, and alignBaseline position a child vertically inside a row.

Every align* modifier applies to the node it is placed on, not to that node’s children. Putting alignMiddle on a row centers the row inside its parent. To align the children inside a container across its cross axis, put itemsStart, itemsCenter, itemsEnd, itemsBaseline, or itemsStretch (or itemsAlign) on the container itself: rowWith (itemsCenter . gap 8) vertically centers every child that does not set its own vertical alignment, columnWith itemsCenter centers fit-width children horizontally without affecting left-aligned text inside fillW labels, and itemsStretch stretches fit-sized children across the cross axis. To distribute leftover space along a container’s main axis, use justifyStart, justifyCenter, justifyEnd, or justifyBetween (or justify). Any explicit align* modifier on a child (including alignTop or alignStart) overrides the container default, and itemsAlign never leaks into nested containers or pinned children.

Additional sizing and flow helpers include fitW / fitH, growW / growH, percentH, spacerW / spacerH, direction, and clipBox (clips children without painting a panel border or fill).

alignStart: align to the left edge
alignStartExample =
  panelWith (fixedWH 300 110 . padAll 10) $
    void (buttonWith (alignStart . fixedW 140) "alignStart")
A button aligned to the left edge of a panel.
alignCenter: center horizontally
alignCenterExample =
  panelWith (fixedWH 300 110 . padAll 10) $
    void (buttonWith (alignCenter . fixedW 140) "alignCenter")
A button centered horizontally in a panel.
alignEnd: align to the right edge
alignEndExample =
  panelWith (fixedWH 300 110 . padAll 10) $
    void (buttonWith (alignEnd . fixedW 140) "alignEnd")
A button aligned to the right edge of a panel.
alignTop: align to the top edge
alignTopExample =
  panelWith (fixedWH 300 110 . padAll 10) $
    rowWith (fillW . fillH . gap 8) $
      void (buttonWith (alignTop . fixedW 140) "alignTop")
A button aligned to the top of a tall row.
alignMiddle: center vertically
alignMidExample =
  panelWith (fixedWH 300 110 . padAll 10) $
    rowWith (fillW . fillH . gap 8) $
      void (buttonWith (alignMiddle . fixedW 140) "alignMiddle")
A button centered vertically in a tall row.
alignBottom: align to the bottom edge
alignBottomExample =
  panelWith (fixedWH 300 110 . padAll 10) $
    rowWith (fillW . fillH . gap 8) $
      void (buttonWith (alignBottom . fixedW 140) "alignBottom")
A button aligned to the bottom of a tall row.
alignBaseline: line up on the first text baseline
alignBaselineExample =
  rowWith (gap 12 . fixedW 340) $ do
    labelWith (fontSize 24 . alignBaseline) "Title"
    labelWith alignBaseline "baseline text"
A large and a small label lined up on their baselines.
wrap: wrap overflowing children onto the next line
wrapExample =
  rowWith (wrap . gap 6 . fixedW 280) $
    mapM_ (void . button) ["haskell", "immediate", "mode", "layout", "sdl", "widgets"]
Buttons wrapped onto two lines.
lineGap: spacing between wrapped lines
lineGapExample =
  columnWith (fillW . gap 16) $ do
    let labels = ["haskell", "immediate", "mode", "layout", "sdl", "widgets"]
    muted "lineGap 2"
    rowWith (wrap . lineGap 2 . gap 6 . fixedW 280) $ mapM_ (void . button) labels
    muted "lineGap 18"
    rowWith (wrap . lineGap 18 . gap 6 . fixedW 280) $ mapM_ (void . button) labels
Two wrapped rows comparing a small and a large line gap.
lineAlign: align wrapped lines across the container
lineAlignExample =
  rowWith (wrap . lineAlign LinesCenter . gap 6 . fixedW 210) $
    mapM_ (void . button) ["alpha", "beta", "gamma", "delta", "epsilon"]
Wrapped buttons with the final line centered.

Positioning and pointer behavior

pinAt pulls a child out of normal flow and positions it at an offset from its alignment corner inside its parent. pointer controls whether an overlapping element intercepts clicks (PointerBlock) or lets them pass through to widgets underneath (PointerPass). In the pointer examples below, the cursor is parked directly over the overlay.

pinAt: position absolutely inside the parent
pinAtExample =
  panelWith (fixedWH 280 110 . padAll 12) $ do
    heading "Inbox"
    muted "The badge is pinned, not stacked."
    let badgeCol = colorRGB 204 64 64
    styled (panelStyle (background badgeCol . borderColor badgeCol)) $
      panelWith (pinAt 6 (-6) . alignEnd . alignTop . padXY 6 1) $
        labelWith (fontColor colorWhite) "12"
A count badge pinned to the top-right corner of a panel.
pointer PointerBlock: absorb pointer events over a region
pointerBlockExample =
  layers $ do
    void (buttonWith (fixedWH 200 44) "Open logs")
    styled (panelStyle (background (colorRGBA 0 0 0 150) . borderWidth 0)) $
      panelWith (pinAt 0 0 . grow . pointer PointerBlock . padAll 6) $
        muted "scrim"
A dimming scrim over a button labeled Open logs.
pointer PointerPass: let pointer events pass through
pointerPassExample =
  layers $ do
    void (buttonWith (fixedWH 200 44) "Open logs")
    labelWith (pinAt 8 (-6) . pointer PointerPass) "decorative label"
A decorative label over a button that still receives hover.

Scrolling

A scroll container must have a bounded viewport along its scroll axis:

itemList :: [Text] -> NanoUI ()
itemList items =
  scrollWith (fixedH 300 . fillW) $
    column $ for_ items label

If none of the scroller’s ancestors constrain its size, grow leaves it with nothing to measure against.

  • scrollWith scrolls vertically and returns its body’s result; scrollWith' also returns the scroller’s Response (including respId).
  • scroll2DWith and scroll2DWith' scroll both horizontally and vertically.
  • scrollConfigured and scrollConfigured' accept a ScrollConfig: choose per-axis ScrollPolicy (ScrollAuto, ScrollAlways, ScrollNone, ScrollHidden, or ScrollOverlay for a floating thumb without a reserved gutter), set scrollBare = True to omit the well background and border, or set scrollFocusable = True for keyboard-driven list views.
  • A full-viewport scroller (scrollWith grow) drops its bordered input well and fills with pageBackground (themeWindow by default). Wrap the view in styled panelPage if you want the page background to match the panel surface instead.

Virtualization

For lists with tens of thousands of rows, build only the rows currently inside the viewport and pad above and below them with spacers. Grab the scroller’s ID first and read its previous-frame layout with getScrollMetrics:

virtualizedList :: [Item] -> NanoUI ()
virtualizedList items = do
  sid <- currentId
  scrollWith (fixedH 500 . fillW) $ do
    metrics <- getScrollMetrics sid
    let rowH = 28
        total = length items
        (offsetY, viewH) = case metrics of
          Nothing -> (0, 0)
          Just m  -> (v2Y (scrollOffset m), rectH (scrollViewport m))
        firstVisible = max 0 (floor (offsetY / rowH))
        lastVisible  = min total (firstVisible + ceiling (viewH / rowH) + 2)
        topH    = fromIntegral firstVisible * rowH
        bottomH = fromIntegral (total - lastVisible) * rowH
        visibleRows = take (lastVisible - firstVisible) (drop firstVisible items)
    columnWith (tight . fillW) $ do
      when (topH > 0) (spacer Fit (Fixed topH))
      for_ visibleRows $ \item ->
        withKey (itemId item) $
          labelWith (fixedH rowH . fillW) (itemTitle item)
      when (bottomH > 0) (spacer Fit (Fixed bottomH))

Use scrollViewport rather than the outer container rectangle for the visible height, since scrollViewport already subtracts padding and the scrollbar lane. scrollOffset gives the current scroll position. Calling getScrollMetrics registers a layout dependency, so resizing the window triggers a fresh frame automatically.

For complete examples, look at packages/nano-ui-demo/examples/LongList.hs (100,000 rows) and Logs.hs (auto-scrolling log tail).

Programmatic scrolling and mouse-wheel step

reveal :: WidgetId -> Float -> Int -> Bool -> NanoUI ()
reveal sid rowH index shouldReveal =
  when shouldReveal $
    scrollRectIntoView sid (Rect 0 (fromIntegral index * rowH) 1 rowH) ScrollNearest ScrollInstant

Inside a view, you can drive scrolling with scrollTo, scrollBy, scrollPages, scrollToStart, scrollToEnd, scrollIntoView, scrollRectIntoView, setScrollOffset, and setScrollStep.

For custom keyboard-driven lists without a full table or tree, set scrollFocusable = True in ScrollConfig so the scroller joins the Tab order and takes focus when passive content inside it is clicked. Pair its WidgetId with useListNav (uniform row height) or useListNavWith (custom per-row rectangles) and selectableRow to handle Up, Down, PageUp, PageDown, Home, End, Enter, and Space with automatic scrollRectIntoView.

By default, one mouse-wheel notch scrolls 60 logical pixels. For uniform lists, match the wheel step to your row height so one notch advances exactly one row:

oneRowPerNotch :: Float -> NanoUI ()
oneRowPerNotch rowHeight =
  setScrollTuning defaultScrollTuning
    {scrollWheelStep = rowHeight, scrollSmoothTime = 0}

You can also override the wheel step for a single scroller with setScrollStep sid px; passing 0 restores the context default.

Nesting and chrome

scrollArea, panel, and card each paint their own background, border, and corner radius. Nesting one inside a popup or modal produces an awkward box-within-a-box. Keep the frame at one level: either strip the inner surface with styled (inputStyle (background colorTransparent . borderWidth 0)), or set padAll 0 on the outer dialog and put the padding on the inner body instead.

See Styling Gotchas and Troubleshooting for more examples.