Text & Textarea Inputs

October 24, 2025 · View on GitHub

Text inputs allow users to input, edit, and select text.

<script type="module">
  import '@brightspace-ui/core/components/inputs/input-group.js';
  import '@brightspace-ui/core/components/inputs/input-text.js';
  import '@brightspace-ui/core/components/inputs/input-textarea.js';
</script>
<d2l-input-group>
  <d2l-input-text label="Name"></d2l-input-text>
  <d2l-input-textarea label="Description" max-rows="4" rows="4"></d2l-input-textarea>
</d2l-input-group>

Best Practices

  • Make sure you include an obvious indication of what the field is for. Usually this means a label.
  • Design the length of the text input to give the user a scent of how long the expected data should be.
  • Ensure the label remains visible when a user focuses on the input using their mobile device. Often this means using a top-aligned label, but a left-aligned label with a very short text input can work also.
  • Don’t use placeholder text - support for this property is being removed. Find an alternative method of communicating text input instructions.
  • Don’t use different font sizes. Text should always be Compact.

Text Input [d2l-input-text]

The <d2l-input-text> element is a simple wrapper around the native <input type="text"> tag. It's intended primarily for inputting generic text, email addresses and URLs.

<script type="module">
  import '@brightspace-ui/core/components/inputs/input-text.js';
</script>
<d2l-input-text label="Label"></d2l-input-text>

Properties

PropertyTypeDescription
labelString, requiredLabel for the input
aria-haspopupStringIndicates that the input has a popup menu
aria-invalidStringIndicates that the input value is invalid
autocompleteStringSpecifies which types of values can be autofilled by the browser
autofocusBooleanWhen set, will automatically place focus on the input
descriptionStringA description to be added to the input for accessibility
disabledBooleanDisables the input
input-widthString, default: 100%Restricts the maximum width of the input box without restricting the width of the label
instructionsStringAdditional information relating to how to use the component
label-hiddenBooleanHides the label visually (moves it to the input's aria-label attribute)
labelled-byStringHTML id of an element in the same shadow root which acts as the input's label
maxStringFor number inputs, maximum value
maxlengthNumberImposes an upper character limit
minStringFor number inputs, minimum value
minlengthNumberImposes a lower character limit
nameStringName of the form control. Submitted with the form as part of a name/value pair.
novalidateBooleanDisables the built-in validation
patternStringRegular expression pattern to validate the value
pattern-failure-textStringText to display when input fails validation against the pattern. If a list of characters is included in the message, use LocalizeMixin's localizeCharacter.
prevent-submitBooleanPrevents pressing ENTER from submitting forms
readonlyBooleanMakes the input read-only
requiredBooleanIndicates that a value is required
sizeNumberSize of the input
stepStringFor number inputs, sets the step size
typeString, default: textCan be one of text, email, password, tel, url. Type number is deprecated, use d2l-input-number instead.
unitStringUnit associated with the input value, displayed next to input and announced as part of the label
valueString, default: ''Value of the input

Events

The <d2l-input-text> dispatches the change event when an alteration to the value is committed (typically after focus is lost) by the user. To be notified immediately of changes made by the user, use the input event.

// dispatched when value changes are committed
input.addEventListener('change', (e) => {
  console.log(input.value);
});

// dispatched whenever value changes occur
input.addEventListener('input', (e) => {
  console.log(input.value);
});

Slots

  • left: Slot within the input on the left side. Useful for an icon or button-icon.
  • right: Slot within the input on the right side. Useful for an icon or button-icon.
  • inline-help: Help text that will appear below the input. Use this only when other helpful cues are not sufficient, such as a carefully-worded label.

Accessibility Properties

To make your usage of <d2l-input-text> accessible, use the following properties when applicable:

AttributeDescription
aria-haspopupIndicate clicking the input opens a menu.
aria-invalidIndicate that the input value is invalid
descriptionUse when label on input does not provide enough context.
labelREQUIRED Acts as a primary label on the input. Visible unless label-hidden is also used.
label-hiddenUse if label should be visually hidden but available for screen reader users
labelled-byUse when another visible element should act as the label
unitUse to render the unit (offscreen) as part of the label.

Textarea Input [d2l-input-textarea]

The <d2l-input-textarea> is a wrapper around the native <textarea> element that provides auto-grow and validation behaviours. It's intended for inputting unformatted multi-line text.

<script type="module">
  import '@brightspace-ui/core/components/inputs/input-textarea.js';
</script>
<style>
  d2l-input-textarea {
    width: 100%;
  }
</style>
<d2l-input-textarea label="Description"></d2l-input-textarea>

Properties

PropertyTypeDescription
labelString, requiredLabel for the textarea
aria-invalidStringIndicates that the textarea value is invalid
descriptionStringA description to be added to the textarea for accessibility
disabledBooleanDisables the textarea
label-hiddenBooleanHides the label visually (moves it to the textarea's aria-label attribute)
labelled-byStringHTML id of an element in the same shadow root which acts as the input's label
max-rowsNumber, default: 11Maximum number of rows before scrolling. Less than 1 allows textarea to grow infinitely.
maxlengthNumberImposes an upper character limit
minlengthNumberImposes a lower character limit
nameStringName of the form control. Submitted with the form as part of a name/value pair.
no-borderBooleanHides the border
no-paddingBooleanRemoves left/right padding
requiredBooleanIndicates that a value is required
rowsNumber, default: 5Minimum number of rows. If rows and max-rows are equal then auto-grow will be disabled.
valueString, default: ''Value of the textarea

Events

The <d2l-input-textarea> dispatches the change event when an alteration to the value is committed (typically after focus is lost) by the user. To be notified immediately of changes made by the user, use the input event.

// dispatched when value changes are committed
textarea.addEventListener('change', (e) => {
  console.log(textarea.value);
});

// dispatched whenever value changes occur
textarea.addEventListener('input', (e) => {
  console.log(textarea.value);
});

Accessibility Properties

To make your usage of d2l-input-textarea accessible, use the following properties when applicable:

AttributeDescription
aria-invalidIndicate that the textarea value is invalid
descriptionUse when label on textarea does not provide enough context.
labelREQUIRED Acts as a primary label on the textarea. Visible unless label-hidden is also used.
label-hiddenUse if label should be visually hidden but available for screen reader users
labelled-byUse when another visible element should act as the label

Methods

  • focus(): Places focus in the textarea
  • select(): Selects the contents of the textarea