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"]
column / columnWith: stack children top to bottom
columnExample =
columnWith (gap 8) $
mapM_ (void . buttonWith fillW) ["One", "Two", "Three"]
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]
panel / panelWith: a container with a background and border
panelExample =
panelWith (fixedW 300 . padAll 12 . gap 6) $ do
heading "Deployments"
muted "3 services running"
card: a full-width panel with a minimum width
cardExample =
card $ do
heading "Build #482"
muted "Passed in 1m 42s"
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.
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"
center: center content on both axes
centerExample =
panelWith (fillW . fillH) $
center (label "Centered on both axes")
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:
The same view at 760 px 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")
separator: draw a one-pixel divider rule
separatorExample =
columnWith (fillW . gap 8) $ do
label "Above the rule"
separator
label "Below the rule"
spacer: insert flexible space between siblings
spacerExample =
rowWith (fillW . alignMiddle) $ do
label "Left"
spacer (Grow 1) Fit
label "Right"
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")
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")
grow: fill both axes (fillW . fillH)
growExample =
rowWith (grow . gap 8) $ do
panelWith (fixedW 120) (label "fixedW 120")
panelWith grow (label "grow")
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")
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")
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")
minW: set a minimum width
minWExample =
rowWith (fillW . gap 8) $ do
void (buttonWith (minW 200) "minW 200")
void (button "natural")
maxW: cap width at an upper bound
maxWExample =
rowWith (fillW . gap 8) $ do
void (buttonWith (fillW . maxW 160) "maxW 160")
void (buttonWith fillW "fillW")
minH: set a minimum height
minHExample =
rowWith (fillW . gap 8) $ do
panelWith (minH 80 . fillW) (label "minH 80")
panelWith fillW (label "natural")
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")
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")
aspect: preserve a width-to-height ratio
aspectExample =
panelWith (fillW . aspect (16 / 9)) $
center (label "16 : 9")
Spacing
padAll: equal padding on all four sides
padAllExample =
panelWith (fixedWH 260 120 . padAll 20) $
panelWith (fillW . fillH) (label "padAll 20")
padXY: horizontal padding first, then vertical
padXYExample =
panelWith (fixedWH 280 100 . padXY 32 10) $
panelWith (fillW . fillH) (label "padXY 32 10")
padLeft: left padding
padLeftExample =
panelWith (fixedWH 260 100 . padLeft 48) $
panelWith (fillW . fillH) (label "padLeft 48")
padRight: right padding
padRightExample =
panelWith (fixedWH 260 100 . padRight 48) $
panelWith (fillW . fillH) (label "padRight 48")
padTop: top padding
padTopExample =
panelWith (fixedWH 260 120 . padTop 40) $
panelWith (fillW . fillH) (label "padTop 40")
padBottom: bottom padding
padBottomExample =
panelWith (fixedWH 260 120 . padBottom 40) $
panelWith (fillW . fillH) (label "padBottom 40")
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")
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"]
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"
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")
alignCenter: center horizontally
alignCenterExample =
panelWith (fixedWH 300 110 . padAll 10) $
void (buttonWith (alignCenter . fixedW 140) "alignCenter")
alignEnd: align to the right edge
alignEndExample =
panelWith (fixedWH 300 110 . padAll 10) $
void (buttonWith (alignEnd . fixedW 140) "alignEnd")
alignTop: align to the top edge
alignTopExample =
panelWith (fixedWH 300 110 . padAll 10) $
rowWith (fillW . fillH . gap 8) $
void (buttonWith (alignTop . fixedW 140) "alignTop")
alignMiddle: center vertically
alignMidExample =
panelWith (fixedWH 300 110 . padAll 10) $
rowWith (fillW . fillH . gap 8) $
void (buttonWith (alignMiddle . fixedW 140) "alignMiddle")
alignBottom: align to the bottom edge
alignBottomExample =
panelWith (fixedWH 300 110 . padAll 10) $
rowWith (fillW . fillH . gap 8) $
void (buttonWith (alignBottom . fixedW 140) "alignBottom")
alignBaseline: line up on the first text baseline
alignBaselineExample =
rowWith (gap 12 . fixedW 340) $ do
labelWith (fontSize 24 . alignBaseline) "Title"
labelWith alignBaseline "baseline text"
wrap: wrap overflowing children onto the next line
wrapExample =
rowWith (wrap . gap 6 . fixedW 280) $
mapM_ (void . button) ["haskell", "immediate", "mode", "layout", "sdl", "widgets"]
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
lineAlign: align wrapped lines across the container
lineAlignExample =
rowWith (wrap . lineAlign LinesCenter . gap 6 . fixedW 210) $
mapM_ (void . button) ["alpha", "beta", "gamma", "delta", "epsilon"]
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"
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"
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"
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.
scrollWithscrolls vertically and returns its body’s result;scrollWith'also returns the scroller’sResponse(includingrespId).scroll2DWithandscroll2DWith'scroll both horizontally and vertically.scrollConfiguredandscrollConfigured'accept aScrollConfig: choose per-axisScrollPolicy(ScrollAuto,ScrollAlways,ScrollNone,ScrollHidden, orScrollOverlayfor a floating thumb without a reserved gutter), setscrollBare = Trueto omit the well background and border, or setscrollFocusable = Truefor keyboard-driven list views.- A full-viewport scroller (
scrollWith grow) drops its bordered input well and fills withpageBackground(themeWindowby default). Wrap the view instyled panelPageif 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.