drawer

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package drawer is a click/key-triggered overlay holding arbitrary (possibly multi-line) content, anchored to an edge of the base view (left/right/top/bottom) rather than centered over it (unlike dialog.Model) or anchored to a point (unlike popover.Model). Its open/dismiss lifecycle mirrors popover.Model/dialog.Model exactly. This is a static anchored panel unless SlideDuration is set, in which case SlideIn and SlideOut animate it with motion.Tween.

Sliding

Set SlideDuration (and optionally Ease) and call SlideIn or SlideOut from Update, returning the Cmd they give back. The drawer moves through its own edge, and only the part that has come through is drawn. Its position comes from the time each tick carries, so it is where the clock says and the slide ends exactly one SlideDuration after the first tick. Enter and Esc slide it out, and DismissedMsg is delivered when a slide out finishes. Show and Hide stay immediate and cancel a slide. See the Example.

Reduced motion

The slide is an animation, so it honours reduced motion: with Motion set to motion.Reduced, SlideIn and SlideOut complete at once, draw no intermediate frame and schedule no tick (SlideOut still delivers DismissedMsg). Take the value from motion.Detect(), which is Reduced when the NO_ANIMATION environment variable is set, or from the Program:

d.Motion = motion.Normal
if p.ReducedMotion() { // WithReducedMotion(true), NO_ANIMATION, or WithAccessible
	d.Motion = motion.Reduced
}
Example

A drawer that slides in from its edge. In a real program SlideIn's Cmd is returned from Update and the Program delivers each tick back to the drawer's Update; here the ticks are fed by hand at fixed times, so the output does not depend on the clock. The first tick fixes the start; the drawer is fully in place exactly one SlideDuration later.

package main

import (
	"fmt"
	"strings"
	"time"

	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/motion"
)

// A drawer that slides in from its edge. In a real program SlideIn's Cmd is
// returned from Update and the Program delivers each tick back to the
// drawer's Update; here the ticks are fed by hand at fixed times, so the
// output does not depend on the clock. The first tick fixes the start; the
// drawer is fully in place exactly one SlideDuration later.
func main() {
	base := strings.TrimRight(strings.Repeat(strings.Repeat(".", 24)+"\n", 5), "\n")

	m := New("menu")
	m.Hide()
	m.Edge = EdgeRight
	m.Width = 8
	m.SlideDuration = 100 * time.Millisecond
	m.Ease = motion.Linear

	m.SlideIn() // in a program: return this Cmd from Update

	start := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
	for _, ms := range []int{0, 50, 100} {
		m, _ = m.Update(slideTickMsg{gen: m.gen, at: start.Add(time.Duration(ms) * time.Millisecond)})
		fmt.Printf("t=%dms, %d%% in:\n%s\n\n", ms, int(m.shown()*100+0.5), plainRows(m.Render(base)))
	}

}

// plainRows strips the styling and trailing spaces so the output reads cleanly.
func plainRows(s string) string {
	rows := strings.Split(ansi.StripANSI(s), "\n")
	for i, r := range rows {
		rows[i] = strings.TrimRight(r, " ")
	}
	return strings.Join(rows, "\n")
}
Output:
t=0ms, 0% in:
........................
........................
........................
........................
........................

t=50ms, 50% in:
..................┌─────
..................│
..................│ menu
..................│
..................└─────

t=100ms, 100% in:
............┌──────────┐
............│          │
............│ menu     │
............│          │
............└──────────┘

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DismissedMsg

type DismissedMsg struct{}

DismissedMsg is delivered (via the Cmd Update returns) when the drawer is dismissed with Enter or Esc, or when SlideOut finishes.

type Edge

type Edge int

Edge is the side of the base view a Drawer is anchored to.

const (
	// EdgeRight anchors the drawer to the right edge of base — the zero
	// value, matching termcn's own default.
	EdgeRight Edge = iota
	// EdgeLeft anchors the drawer to the left edge of base.
	EdgeLeft
	// EdgeTop anchors the drawer to the top edge of base.
	EdgeTop
	// EdgeBottom anchors the drawer to the bottom edge of base.
	EdgeBottom
)

type KeyMap

type KeyMap struct {
	Dismiss keymap.Binding // close (or slide out) the drawer and deliver DismissedMsg
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns the keys a Model used before KeyMap existed.

type Model

type Model struct {
	// Content holds arbitrary, possibly multi-line text rendered inside
	// the drawer's bordered box, unwrapped and unsplit (like
	// popover.Model's Content, unlike dialog.Model's Title/Message
	// split).
	Content string
	Theme   theme.Theme

	// Edge is the side of base the drawer is anchored to. The zero value
	// is EdgeRight.
	Edge Edge

	// Width is the drawer's fixed content width, used when Edge is
	// EdgeLeft or EdgeRight. Height is its fixed content height, used
	// when Edge is EdgeTop or EdgeBottom.
	Width, Height int

	// SlideDuration is how long SlideIn and SlideOut take to move the drawer
	// all the way in or out. Zero (the default) means no animation: the drawer
	// appears and disappears in one frame, exactly as it always has.
	SlideDuration time.Duration
	// Ease shapes the slide; the zero value is motion.Linear.
	Ease motion.Ease
	// Motion set to motion.Reduced skips the animation (see motion.Detect and
	// Program.ReducedMotion): SlideIn and SlideOut then complete at once.
	Motion motion.Preference

	// KeyMap holds the keys that dismiss the drawer. New fills it with
	// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
	// behaves as if it held DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent while the drawer
	// is open: a left click outside the drawer dismisses it exactly as Esc
	// does (sliding it out when SlideDuration is set) and a click inside it is
	// swallowed. Off (the default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle of the base view the drawer is rendered
	// over; the drawer's own rectangle is worked out from it as Render does,
	// fully slid in.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is an edge-anchored drawer. Render composites it over a base view when open; when closed, Render returns base unchanged, so a parent can unconditionally call it every frame without checking Open itself.

func New

func New(content string) Model

New returns an already-open Model with Content, Theme: theme.DarkTheme(), Edge: EdgeRight and sane default Width/Height — the common case is showing a drawer immediately in response to some trigger, not constructing one ahead of time and opening it later (though Show/Hide support that too).

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions the drawer currently honours, with descriptions, for help text. A closed drawer honours none.

func (*Model) Hide

func (m *Model) Hide()

Hide closes the drawer at once without dismissing it via Update (no DismissedMsg is sent), cancelling any slide in progress.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode returns the drawer's bordered box as an ordinary layout.Node, sized by the layout it is placed in rather than by Width, Height or Edge. While the drawer is closed it measures as nothing and renders blank. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the drawer as plain text for accessible output (see tui.Linearizer): "Drawer, <edge> edge" and then its content, or "" while it is closed.

func (Model) Open

func (m Model) Open() bool

Open reports whether the drawer is currently shown.

func (Model) Rect

func (m Model) Rect() hittest.Rect

Rect is the screen rectangle the drawer occupies, fully slid in, when rendered over a base view filling Bounds. It is the zero Rect while the drawer is closed.

func (*Model) Release

func (m *Model) Release(r focus.Ring) focus.Ring

Release hides the drawer and pops the scope Trap pushed, returning the Ring with focus back on the widget that had it before the drawer opened. Call it when DismissedMsg arrives, then Sync the Ring so that widget is told it has focus again. On a Ring with no open scope it only hides the drawer.

func (Model) Render

func (m Model) Render(base string) string

Render composites the drawer, anchored to Edge, over base. If the drawer is closed, it returns base unchanged. While a slide is running only the part of the drawer that has come through its edge is drawn (see SlideIn).

EdgeLeft/EdgeRight size the box to a fixed Width and place it flush against the left/right edge of base, vertically centered. EdgeTop/ EdgeBottom size the box to a fixed Height and place it flush against the top/bottom edge of base, horizontally centered.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (*Model) Show

func (m *Model) Show()

Show opens the drawer at once, cancelling any slide in progress.

func (*Model) SlideIn

func (m *Model) SlideIn() tui.Cmd

SlideIn opens the drawer and slides it in from its edge over SlideDuration, shaped by Ease, and returns the Cmd that drives the animation: run it (return it from Update). Open reports true from the start. With no SlideDuration, or with Motion reduced, the drawer is in place at once and the Cmd is nil. Called on an open, resting drawer it does nothing; called during a slide out it reverses from where the drawer is, without a jump.

func (*Model) SlideOut

func (m *Model) SlideOut() tui.Cmd

SlideOut slides the drawer out through its edge over SlideDuration, shaped by Ease, and returns the Cmd that drives the animation. When the slide finishes the drawer is closed (Open reports false) and a DismissedMsg is delivered once. With no SlideDuration, or with Motion reduced, the drawer closes at once and the Cmd only delivers the DismissedMsg. Called on a closed drawer it does nothing; called during a slide in it reverses from where the drawer is, without a jump.

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentDrawer, ...) and then by WithTokens.

func (*Model) Trap

func (m *Model) Trap(r focus.Ring, items int) focus.Ring

Trap shows the drawer and traps focus in it: it pushes a focus scope of items focusable items (at least 1) over r and returns the new Ring. While the scope is open Tab and Shift+Tab cycle among those items only, never reaching the widgets behind the drawer. Call Release with the Ring when the drawer closes.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update dismisses the drawer on Enter or Esc while open; it's a no-op (including for Enter/Esc) once closed, so a parent doesn't need to guard forwarding to it on Model.Open(). With a SlideDuration set (and motion not reduced) Enter and Esc slide the drawer out instead of closing it at once. Update also advances a running slide on its ticks.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL