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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//! InputOTP: a one-time code entry. Carbon has none; the reference
//! (`.cu-otp*` in `src/custom.css`) draws Carbon field slots over one hidden
//! native input.
//!
//! - **Behavior** is Kit's: one transparent [`InputState`] editor covers the
//! slots and owns typing, paste, IME, deletion and focus. The component
//! installs a validator that keeps the text at most `length` characters,
//! each matching `pattern` (digits by default). Read the code from the state
//! (subscribe to its `InputEvent::Change` for `onValueChange`).
//! - **Appearance**: 40×48 slots (`size-md` × `size-lg`) on `field` with a
//! 1px `border-strong` rule, 1px apart, the characters in IBM Plex Mono
//! 20px; groups are split by an en dash in `text-secondary`. While focused
//! the next empty slot (or the last when full) gets the 2px inset `focus`
//! outline and an empty one shows a 1×20px caret blinking 1s in steps
//! (`@keyframes cu-blink`; steady under reduced motion). `invalid` outlines
//! every slot in `support-error`; `disabled` dims the text and drops the rule.
//!
//! ```ignore
//! let state = cx.new(|cx| InputState::new(window, cx).default_value("482"));
//! InputOtp::new("code", &state).length(6).groups([3, 3]).label("One-time code")
//! ```
use super::form::{self, rem};
use super::input::InputState;
use crate::{
motion,
theme::color,
tokens::{self, ColorRole, TypeStyle},
typography::{self, TypeStyled as _},
};
use gpui_kit::{
Animation, AnimationExt as _, App, CursorStyle, ElementId, Entity, Focusable as _, Hsla,
InteractiveElement as _, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window,
component::{Sizable as _, Size, input::Input as KitInput},
div,
prelude::FluentBuilder as _,
};
use std::time::Duration;
/// Slot width (`width: var(--size-md)`).
pub const SLOT_WIDTH: f32 = 40.;
/// Slot height (`height: var(--size-lg)`).
pub const SLOT_HEIGHT: f32 = 48.;
/// The index of the slot that shows focus: the next empty one, or the last
/// when the code is full (`k === v.length || (k === length - 1 && v.length === length)`).
pub fn active_slot(len: usize, length: usize) -> usize {
len.min(length.saturating_sub(1))
}
/// Whether `text` is an acceptable (partial) code.
pub fn accepts(text: &str, length: usize, pattern: fn(char) -> bool) -> bool {
text.chars().count() <= length && text.chars().all(pattern)
}
/// The colors of one slot.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SlotLook {
/// Fill (`field`).
pub background: Hsla,
/// Bottom rule (`border-strong`, transparent when disabled).
pub border: Hsla,
/// Character color.
pub text: Hsla,
/// Outline: `support-error` when invalid, else `focus` on the active slot.
pub outline: Option<Hsla>,
}
impl SlotLook {
/// The look of a slot (`.cu-otp-slot*`).
pub fn of(active: bool, invalid: bool, disabled: bool) -> Self {
Self {
background: color(ColorRole::Field01),
border: if disabled {
Hsla::transparent_black()
} else {
color(ColorRole::BorderStrong01)
},
text: color(if disabled {
ColorRole::TextDisabled
} else {
ColorRole::TextPrimary
}),
// `.cu-otp--invalid .cu-otp-slot` comes after `.cu-otp-slot--active`, so it wins.
outline: if invalid {
Some(color(ColorRole::SupportError))
} else if active {
Some(color(ColorRole::Focus))
} else {
None
},
}
}
}
/// A one-time code input. See the module docs.
#[derive(IntoElement)]
pub struct InputOtp {
id: ElementId,
state: Entity<InputState>,
length: usize,
groups: Option<Vec<usize>>,
pattern: fn(char) -> bool,
invalid: bool,
disabled: bool,
label: Option<SharedString>,
}
impl InputOtp {
/// A 6-digit code input over the caller's `state`.
pub fn new(id: impl Into<ElementId>, state: &Entity<InputState>) -> Self {
Self {
id: id.into(),
state: state.clone(),
length: 6,
groups: None,
pattern: |c| c.is_ascii_digit(),
invalid: false,
disabled: false,
label: None,
}
}
/// `length` (default 6).
pub fn length(mut self, length: usize) -> Self {
self.length = length.max(1);
self
}
/// `groups`, e.g. `[3, 3]` (default one group of `length`).
pub fn groups(mut self, groups: impl IntoIterator<Item = usize>) -> Self {
self.groups = Some(groups.into_iter().collect());
self
}
/// `pattern`: which characters are allowed (default ASCII digits).
pub fn pattern(mut self, pattern: fn(char) -> bool) -> Self {
self.pattern = pattern;
self
}
/// `invalid`.
pub fn invalid(mut self, invalid: bool) -> Self {
self.invalid = invalid;
self
}
/// `disabled`.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// `label`: the accessible name of the hidden input.
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
}
impl RenderOnce for InputOtp {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let length = self.length;
let pattern = self.pattern;
// Install the validator once per (state, length, pattern).
let key = (self.state.entity_id(), length, pattern as usize);
let applied = window.use_keyed_state((id.clone(), "validator"), cx, |_, _| None);
if *applied.read(cx) != Some(key) {
applied.update(cx, |a, _| *a = Some(key));
self.state.update(cx, |s, cx| {
s.set_validator(move |text, _| accepts(text, length, pattern), cx)
});
}
// The caret marks the next slot (`input-otp` places it on focus), so
// Tab into the slots does not select the whole code.
super::form::keep_caret_on_tab_in(&self.state);
let value: Vec<char> = self.state.read(cx).value().chars().collect();
let focus = self.state.read(cx).focus_handle(cx);
let focused = focus.is_focused(window) && !self.disabled;
let active = active_slot(value.len(), length);
let blink = !motion::reduced_motion(cx);
let groups = self
.groups
.clone()
.filter(|g| !g.is_empty())
.unwrap_or_else(|| vec![length]);
let mut index = 0;
let mut parts = Vec::new();
for (gi, size) in groups.iter().enumerate() {
if gi > 0 {
// `.cu-otp-sep { color: var(--text-secondary) }`.
parts.push(
div()
.text_color(color(ColorRole::TextSecondary))
.child("–")
.into_any_element(),
);
}
let slots = (0..*size).map(|_| {
let k = index;
index += 1;
let is_active = focused && k == active;
let look = SlotLook::of(is_active, self.invalid, self.disabled);
let ch = value.get(k).copied();
div()
.relative()
.flex()
.items_center()
.justify_center()
.w(rem(SLOT_WIDTH))
.h(rem(SLOT_HEIGHT))
.bg(look.background)
.pb(gpui_kit::px(form::FIELD_RULE))
.font(typography::font(tokens::FontFamilyKey::Mono, 400))
.text_size(rem(20.))
.text_color(look.text)
.when_some(ch, |this, ch| {
this.child(SharedString::from(ch.to_string()))
})
.when(is_active && ch.is_none(), |this| {
let caret = div()
.w(gpui_kit::px(1.))
.h(rem(20.))
.bg(color(ColorRole::TextPrimary));
if blink {
this.child(caret.with_animation(
(id.clone(), "caret"),
Animation::new(Duration::from_secs(1)).repeat(),
// `@keyframes cu-blink { 50% { opacity: 0 } }` with `steps(1)`.
|caret, t| caret.opacity(if t < 0.5 { 1. } else { 0. }),
))
} else {
this.child(caret)
}
})
.child(form::bottom_rule(look.border))
.when_some(look.outline, |this, outline| {
this.child(form::inset_outline(2., outline))
})
});
// `.cu-otp-group { display: flex; gap: 1px }`.
parts.push(
div()
.flex()
.flex_row()
.gap(gpui_kit::px(1.))
.children(slots)
.into_any_element(),
);
}
let editor = KitInput::new(&self.state)
.appearance(false)
.with_size(Size::XSmall)
.disabled(self.disabled)
.when_some(self.label.clone(), |this, label| this.aria_label(label))
.absolute()
.top_0()
.left_0()
.size_full()
.opacity(0.);
div()
.id(id.clone())
.relative()
.flex()
.flex_row()
.items_center()
.type_style(TypeStyle::BodyCompact01)
.cursor(if self.disabled {
gpui_kit::CursorStyle::OperationNotAllowed
} else {
CursorStyle::IBeam
})
// `onClick={() => ref.current.focus()}`: a click anywhere focuses the
// hidden editor, which then takes the text at its end.
.when(!self.disabled, |this| {
let state = self.state.clone();
this.on_mouse_down(gpui_kit::MouseButton::Left, move |_, window, cx| {
focus.focus(window, cx);
state.update(cx, |s, cx| {
let end = s.value().len();
s.set_selected_range(end..end, cx);
});
})
})
// `.cu-otp { gap: var(--spacing-03) }` between the groups only.
.child(
div()
.flex()
.flex_row()
.items_center()
.gap(tokens::SPACING_03)
.children(parts),
)
// `.cu-otp-input { position: absolute; inset: 0 }`: Kit's input keeps an
// in-flow root, so an absolute box holds it and the row stays as wide
// as its slots.
.child(div().absolute().top_0().left_0().size_full().child(editor))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn active_slot_follows_the_reference() {
assert_eq!(active_slot(0, 6), 0);
assert_eq!(active_slot(3, 6), 3);
// Full: the last slot stays active.
assert_eq!(active_slot(6, 6), 5);
}
#[test]
fn validator_limits_length_and_pattern() {
let digits: fn(char) -> bool = |c| c.is_ascii_digit();
assert!(accepts("482", 6, digits));
assert!(!accepts("48a", 6, digits));
assert!(!accepts("1234567", 6, digits));
}
#[test]
fn slot_colors_follow_cu_otp_css() {
crate::theme::tests_set(CarbonTheme::White);
let active = SlotLook::of(true, false, false);
assert_eq!(active.outline, Some(color(ColorRole::Focus)));
let invalid = SlotLook::of(true, true, false);
assert_eq!(invalid.outline, Some(color(ColorRole::SupportError)));
let disabled = SlotLook::of(false, false, true);
assert_eq!(disabled.border.a, 0.);
assert_eq!(SLOT_WIDTH, tokens::SIZE_MD.px_value());
assert_eq!(SLOT_HEIGHT, tokens::SIZE_LG.px_value());
}
}