Visual Studio Code Text Field

March 8, 2023 · View on GitHub

The vscode-text-field is a web component implementation of a text field element.

Text field hero

Usage

❌ Don't✅ Do
Text field with overflowing textText field with a short value
Don't use a text field for inputs that are greater than a single line of text. Use a text-area component instead.Use text fields for short input values.
❌ Don't✅ Do
Text field without a formal labelText label with a descriptive label
Don't use a placeholder value instead of a label unless absolutely necessary.Use descriptive labels to help users understand the text fields purpose.
❌ Don't✅ Do
Text field with important information as a placeholder valueText field with helper text below
Don't include critical information in a placeholder value.Use helper text if necessary to provide more information about the purpose of the text field.
❌ Don't✅ Do
Text field with an icon providing little valueText field with a search icon
Don't use decorative icons in a text field.Use icons to help users quickly identify the purpose of a text field—especially if no label is used.

Implementation

Interactive component examples

Attributes

AttributeTypeDescription
autofocusbooleanIndicates that this component should get focus after the page finishes loading.
disabledbooleanPrevents the user from interacting with the button––it cannot be pressed or focused.
maxlengthnumberThe maximum number of characters a user can enter.
namestringThe name of the component.
placeholderstringSets the placeholder value of the component, generally used to provide a hint to the user.
readonlybooleanWhen true, the control will be immutable by any user interaction.
sizenumberSets the width of the element to a specified number of characters.
typestringSets the text field type.
valuestringThe value (i.e. content) of the text field.

Basic Text Field

<vscode-text-field>Text Field Label</vscode-text-field>

Autofocus Attribute

<vscode-text-field autofocus>Text Field Label</vscode-text-field>

Disabled Attribute

<vscode-text-field disabled>Text Field Label</vscode-text-field>

Max Length Attribute

<vscode-text-field maxlength="10">Text Field Label</vscode-text-field>

Name Attribute

<vscode-text-field name="example-vscode-text-field">Text Field Label</vscode-text-field>

Placeholder Attribute

<vscode-text-field placeholder="Placeholder Text">Text Field Label</vscode-text-field>

Read Only Attribute

<vscode-text-field readonly>Text Field Label</vscode-text-field>

Size Attribute

<vscode-text-field size="50">Text Field Label</vscode-text-field>

Type Attribute

Sets the text field type. Valid options include: "email", "password", "tel", "text", and "url". The default value is "text".

<vscode-text-field type="email">Text Field Label</vscode-text-field>
<vscode-text-field type="password">Text Field Label</vscode-text-field>
<vscode-text-field type="tel">Text Field Label</vscode-text-field>
<vscode-text-field type="text">Text Field Label</vscode-text-field>
<vscode-text-field type="url">Text Field Label</vscode-text-field>

Start Icon

An icon can be added to the left of the text field by adding an element with the attribute slot="start".

<!-- Note: Using Visual Studio Code Codicon Library -->

<vscode-text-field>
  Text Field Label
  <span slot="start" class="codicon codicon-git-merge"></span>
</vscode-text-field>

End Icon

An icon can be added to the right of the text field by adding an element with the attribute slot="end".

<!-- Note: Using Visual Studio Code Codicon Library -->

<vscode-text-field>
  Text Field Label
  <span slot="end" class="codicon codicon-chevron-right"></span>
</vscode-text-field>

Multiple Icons

Building on top of the icon examples above, multiple icons can also be inserted into the start and end slots of a text field with the slot="start" and slot="end" attributes.

The below example demonstrates how the native search text field from VS Code might be visually implemented with a section tag that wraps a few vscode-buttons with an icon appearance applied (please note that further JavaScript is needed to make this example functional, however).

<!-- Note: Using Visual Studio Code Codicon Library -->

<vscode-text-field>
  Text Field Label
  <section slot="end" style="display:flex; align-items: center;">
    <vscode-button appearance="icon" aria-label="Match Case">
      <span class="codicon codicon-case-sensitive"></span>
    </vscode-button>
    <vscode-button appearance="icon" aria-label="Match Whole Word">
      <span class="codicon codicon-whole-word"></span>
    </vscode-button>
    <vscode-button appearance="icon" aria-label="Use Regular Expression">
      <span class="codicon codicon-regex"></span>
    </vscode-button>
  </section>
</vscode-text-field>