gitstack

grindlemire/go-tui code browser

1.9 KB Go 63 lines 2026-04-03 · 2dcff81 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package tui

import "fmt"

// ModalOption configures a Modal component.
type ModalOption func(*Modal)

// WithModalOpen binds the modal's visibility to a boolean state.
func WithModalOpen(state *State[bool]) ModalOption {
	return func(m *Modal) {
		m.open = state
	}
}

// WithModalBackdrop sets the backdrop style: "dim" (default), "blank", or "none".
func WithModalBackdrop(b string) ModalOption {
	if b != "dim" && b != "blank" && b != "none" {
		panic(fmt.Sprintf("tui: invalid backdrop %q: must be \"dim\", \"blank\", or \"none\"", b))
	}
	return func(m *Modal) {
		m.backdrop = b
	}
}

// WithModalCloseOnEscape configures whether Escape closes the modal (default true).
func WithModalCloseOnEscape(v bool) ModalOption {
	return func(m *Modal) {
		m.closeOnEscape = v
	}
}

// WithModalCloseOnBackdropClick configures whether clicking the backdrop closes the modal (default true).
func WithModalCloseOnBackdropClick(v bool) ModalOption {
	return func(m *Modal) {
		m.closeOnBackdrop = v
	}
}

// WithModalTrapFocus configures whether Tab/Shift+Tab is restricted to modal children (default true).
func WithModalTrapFocus(v bool) ModalOption {
	return func(m *Modal) {
		m.trapFocus = v
	}
}

// WithModalKeyMap sets custom key bindings for the modal. These bindings fire
// after the built-in Escape/Tab/Enter handlers but before the catch-all blocker
// (when trapFocus is true). Use OnPreemptStop for bindings that should block
// parent handlers. When trapFocus is true, non-preemptive bindings (On, OnStop)
// will never fire because the AnyKey catch-all consumes events first.
func WithModalKeyMap(km KeyMap) ModalOption {
	return func(m *Modal) {
		m.customKeyMap = km
	}
}

// WithModalElementOptions passes through standard Element options to the modal's
// container element. Used by the code generator to apply class-derived layout options.
func WithModalElementOptions(opts ...Option) ModalOption {
	return func(m *Modal) {
		m.elementOpts = append(m.elementOpts, opts...)
	}
}