Skip to content

Commit b2adbe0

Browse files
committed
OTP tokens
1 parent e8f92ac commit b2adbe0

2 files changed

Lines changed: 46 additions & 28 deletions

File tree

scss/forms/_otp-input.scss

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
@use "../config" as *;
22
@use "../variables" as *;
33
@use "form-variables" as *;
4-
5-
// scss-docs-start otp-input-variables
6-
$otp-input-size: 3rem !default;
7-
$otp-input-size-sm: 2.25rem !default;
8-
$otp-input-size-lg: 3.5rem !default;
9-
$otp-input-font-size: $font-size-lg !default;
10-
$otp-input-font-size-sm: $font-size-base !default;
11-
$otp-input-font-size-lg: $font-size-lg * 1.25 !default;
12-
$otp-input-gap: .5rem !default;
13-
// scss-docs-end otp-input-variables
14-
15-
@layer forms {
4+
@use "../mixins/border-radius" as *;
5+
6+
// stylelint-disable custom-property-no-missing-var-function
7+
// scss-docs-start otp-tokens
8+
$otp-tokens: (
9+
--otp-size: var(--btn-input-lg-min-height),
10+
--otp-font-size: var(--btn-input-font-size),
11+
--otp-gap: .5rem,
12+
) !default;
13+
// scss-docs-end otp-tokens
14+
// stylelint-enable custom-property-no-missing-var-function
15+
16+
// scss-docs-start otp-sizes
17+
$otp-sizes: sm, lg !default;
18+
// scss-docs-end otp-sizes
19+
20+
@layer components {
1621
.otp {
17-
--otp-size: #{$otp-input-size};
18-
--otp-font-size: #{$otp-input-font-size};
19-
--otp-gap: #{$otp-input-gap};
22+
@each $prop, $value in $otp-tokens {
23+
#{$prop}: #{$value};
24+
}
2025

2126
display: inline-flex;
2227
gap: var(--otp-gap);
@@ -29,6 +34,7 @@ $otp-input-gap: .5rem !default;
2934
font-weight: 500;
3035
line-height: 1;
3136
text-align: center;
37+
@include border-radius(var(--otp-border-radius, var(--btn-input-border-radius)));
3238

3339
// Remove default number spinners
3440
&::-webkit-outer-spin-button,
@@ -87,13 +93,11 @@ $otp-input-gap: .5rem !default;
8793
user-select: none;
8894
}
8995

90-
.otp-sm {
91-
--otp-size: #{$otp-input-size-sm};
92-
--otp-font-size: #{$otp-input-font-size-sm};
93-
}
94-
95-
.otp-lg {
96-
--otp-size: #{$otp-input-size-lg};
97-
--otp-font-size: #{$otp-input-font-size-lg};
96+
@each $size in $otp-sizes {
97+
.otp-#{$size} {
98+
--otp-size: var(--btn-input-#{$size}-min-height);
99+
--otp-font-size: var(--btn-input-#{$size}-font-size);
100+
--otp-border-radius: var(--btn-input-#{$size}-border-radius);
101+
}
98102
}
99103
}

site/src/content/docs/forms/otp-input.mdx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Wrap your inputs in a container with `.otp` and add `data-bs-otp` to enable the
3131

3232
## Connected inputs
3333

34-
Add `.input-group` to visually connect the inputs into a single cohesive field, leveraging Bootstrap's [input group](/docs/forms/input-group/) styles.
34+
Add `.input-group` to visually connect the inputs into a single cohesive field, leveraging Bootstrap's [input group](/docs/forms/input-group/) styles. We override the `width` to prevent stretching the inputs.
3535

3636
<Example code={`<div class="otp input-group" data-bs-otp>
3737
<input type="text" class="form-control" aria-label="Digit 1">
@@ -152,9 +152,9 @@ Add `.is-valid` or `.is-invalid` to the container to show validation states.
152152

153153
Use a form label and help text for better accessibility.
154154

155-
<Example code={`<div class="mb-3">
155+
<Example code={`<div class="vstack gap-2">
156156
<label class="form-label" id="otpLabel">Verification code</label>
157-
<div class="otp input-group" data-bs-otp aria-labelledby="otpLabel" aria-describedby="otpHelp">
157+
<div class="otp" data-bs-otp aria-labelledby="otpLabel" aria-describedby="otpHelp">
158158
<input type="text" class="form-control" aria-label="Digit 1">
159159
<input type="text" class="form-control" aria-label="Digit 2">
160160
<input type="text" class="form-control" aria-label="Digit 3">
@@ -193,19 +193,23 @@ const otpInput = new bootstrap.OtpInput(otpElement)
193193

194194
Options can be passed via data attributes or JavaScript:
195195

196+
<BsTable>
196197
| Name | Type | Default | Description |
197198
| --- | --- | --- | --- |
198199
| `mask` | boolean | `false` | If `true`, inputs will use `type="password"` to hide the entered values. |
200+
</BsTable>
199201

200202
### Methods
201203

204+
<BsTable>
202205
| Method | Description |
203206
| --- | --- |
204207
| `getValue()` | Returns the complete OTP value as a string. |
205208
| `setValue(value)` | Sets the OTP value, distributing characters across inputs. |
206209
| `clear()` | Clears all inputs and focuses the first one. |
207210
| `focus()` | Focuses the first empty input, or the last input if all are filled. |
208211
| `dispose()` | Destroys the component instance. |
212+
</BsTable>
209213

210214
```js
211215
const otpElement = document.querySelector('.otp')
@@ -223,10 +227,12 @@ otpInput.clear()
223227

224228
### Events
225229

230+
<BsTable>
226231
| Event | Description |
227232
| --- | --- |
228233
| `complete.bs.otp` | Fired when all inputs are filled. The event's `value` property contains the complete code. |
229234
| `input.bs.otp` | Fired on each input change. Includes `value` (current combined value) and `index` (changed input index). |
235+
</BsTable>
230236

231237
```js
232238
const otpElement = document.querySelector('.otp')
@@ -251,6 +257,14 @@ otpElement.addEventListener('input.bs.otp', event => {
251257

252258
## CSS
253259

254-
### Sass variables
260+
### Variables
255261

256-
<ScssDocs name="otp-input-variables" file="scss/forms/_otp-input.scss" />
262+
<CSSVariables component="OTP input" className="otp" />
263+
264+
<ScssDocs name="otp-tokens" file="scss/forms/_otp-input.scss" />
265+
266+
### Sass list
267+
268+
We use a Sass list to define the sizes of the OTP input, so you can easily add or modify sizes by fetching the associated root variables for each size. This list should generally match the sizes defined in the `$form-control-sizes` Sass map in `_form-control.scss`.
269+
270+
<ScssDocs name="otp-sizes" file="scss/forms/_otp-input.scss" />

0 commit comments

Comments
 (0)