0
# Confirmation and Toggle Prompts
1
2
Binary choice prompts for yes/no confirmations, toggle switches, and list input. These prompts handle simple binary decisions and comma-separated list input with custom formatting options.
3
4
```javascript
5
const prompts = require('prompts');
6
```
7
8
## Capabilities
9
10
### Confirm Prompt
11
12
Classic yes/no confirmation prompt. Users can press Y/N keys or use arrow keys to select Yes/No options.
13
14
```javascript { .api }
15
{
16
/** Prompt type identifier */
17
type: 'confirm',
18
/** Property name for response object */
19
name: string,
20
/** Display message for user */
21
message: string,
22
/** Default boolean value. Defaults to false */
23
initial?: boolean,
24
/** Custom formatter for boolean value */
25
format?: (value: boolean, values: Answers) => any,
26
/** Render callback with kleur styling */
27
onRender?: (kleur: any) => void,
28
/** State change callback */
29
onState?: (state: { value: boolean; aborted: boolean }) => void,
30
/** Input stream */
31
stdin?: NodeJS.ReadableStream,
32
/** Output stream */
33
stdout?: NodeJS.WritableStream
34
}
35
```
36
37
**Usage Examples:**
38
39
```javascript
40
const prompts = require('prompts');
41
42
// Basic confirmation
43
const response = await prompts({
44
type: 'confirm',
45
name: 'continue',
46
message: 'Do you want to continue?',
47
initial: true
48
});
49
50
// Confirmation with custom formatting
51
const response = await prompts({
52
type: 'confirm',
53
name: 'subscribe',
54
message: 'Subscribe to newsletter?',
55
initial: false,
56
format: value => value ? 'Subscribed' : 'Not subscribed'
57
});
58
59
// Confirmation in a sequence
60
const questions = [
61
{
62
type: 'text',
63
name: 'name',
64
message: 'Enter your name:'
65
},
66
{
67
type: 'confirm',
68
name: 'isCorrect',
69
message: (prev, values) => `Is "${values.name}" correct?`,
70
initial: true
71
}
72
];
73
74
const response = await prompts(questions);
75
```
76
77
### Toggle Prompt
78
79
Interactive toggle/switch prompt with custom labels for active and inactive states. Users can use arrow keys, Tab, or Space to switch between states.
80
81
```javascript { .api }
82
{
83
/** Prompt type identifier */
84
type: 'toggle',
85
/** Property name for response object */
86
name: string,
87
/** Display message for user */
88
message: string,
89
/** Default boolean value. Defaults to false */
90
initial?: boolean,
91
/** Text for active/true state. Defaults to 'on' */
92
active?: string,
93
/** Text for inactive/false state. Defaults to 'off' */
94
inactive?: string,
95
/** Custom formatter for boolean value */
96
format?: (value: boolean, values: Answers) => any,
97
/** Render callback with kleur styling */
98
onRender?: (kleur: any) => void,
99
/** State change callback */
100
onState?: (state: { value: boolean; aborted: boolean }) => void,
101
/** Input stream */
102
stdin?: NodeJS.ReadableStream,
103
/** Output stream */
104
stdout?: NodeJS.WritableStream
105
}
106
```
107
108
**Usage Examples:**
109
110
```javascript
111
// Basic toggle prompt
112
const response = await prompts({
113
type: 'toggle',
114
name: 'notifications',
115
message: 'Enable notifications?',
116
initial: true,
117
active: 'enabled',
118
inactive: 'disabled'
119
});
120
121
// Toggle with custom labels
122
const response = await prompts({
123
type: 'toggle',
124
name: 'mode',
125
message: 'Development mode?',
126
initial: false,
127
active: 'development',
128
inactive: 'production'
129
});
130
131
// Toggle with formatting
132
const response = await prompts({
133
type: 'toggle',
134
name: 'privacy',
135
message: 'Make profile public?',
136
initial: false,
137
active: 'public',
138
inactive: 'private',
139
format: (value, values) => ({
140
isPublic: value,
141
visibility: value ? 'public' : 'private',
142
username: values.username
143
})
144
});
145
```
146
147
## Prompt Behavior
148
149
### Confirm Prompt Controls
150
- **Y key**: Select Yes (true)
151
- **N key**: Select No (false)
152
- **Arrow keys**: Navigate between Yes/No
153
- **Enter**: Submit current selection
154
- **Escape**: Cancel prompt
155
156
### Toggle Prompt Controls
157
- **Arrow keys**: Switch between active/inactive states
158
- **Tab**: Switch between active/inactive states
159
- **Space**: Switch between active/inactive states
160
- **Enter**: Submit current state
161
- **Escape**: Cancel prompt
162
163
### Return Values
164
- **Confirm**: Returns boolean value (true/false)
165
- **Toggle**: Returns boolean value (true/false)
166
167
Both prompts return the boolean value directly, or the result of the format function if provided.
168
169
### Visual Display
170
171
**Confirm Prompt:**
172
```
173
? Do you want to continue? › (Y/n)
174
? Do you want to continue? › (y/N) // when initial: false
175
```
176
177
**Toggle Prompt:**
178
```
179
? Enable notifications? › enabled / disabled
180
? Development mode? › development / production
181
```
182
183
### Formatting Examples
184
185
```javascript
186
// Confirm with custom format
187
{
188
type: 'confirm',
189
name: 'terms',
190
message: 'Accept terms and conditions?',
191
format: value => ({
192
accepted: value,
193
timestamp: value ? new Date().toISOString() : null
194
})
195
}
196
197
// Toggle with status mapping
198
{
199
type: 'toggle',
200
name: 'feature',
201
message: 'Enable beta features?',
202
active: 'enabled',
203
inactive: 'disabled',
204
format: value => value ? 'FEATURE_ON' : 'FEATURE_OFF'
205
}
206
```
207
208
These prompts are ideal for:
209
- User confirmations before destructive actions
210
- Feature toggles and settings
211
- Binary configuration options
212
- Consent and agreement prompts
213
- Mode selection (development/production, public/private, etc.)