Select On Focus

A directive that automatically selects input text on focus. Configurable to respond to specific focus origins.

UtilitiesExperimental

Usage

import { Component, ViewEncapsulation } from '@angular/core';import {  ScField,  ScInput,  ScLabel,  ScSelectOnFocus,} from '@semantic-components/ui';
@Component({  selector: 'app-select-on-focus-usage-demo',  imports: [ScSelectOnFocus, ScInput, ScField, ScLabel],  template: `    <div scField>      <label scLabel>Text</label>      <input scInput scSelectOnFocus value="Text to select" />    </div>  `,  host: { class: 'flex w-full justify-center' },  encapsulation: ViewEncapsulation.None,})export class SelectOnFocusUsageDemo {}

Anatomy

One directive on an input: its text is selected when it gets focus — from the keyboard, the mouse, a touch or the program, as you choose.

import { ScSelectOnFocus } from '@semantic-components/ui';
<input scSelectOnFocus /><input scSelectOnFocus="keyboard" />

How It Works

  • Uses FocusMonitor to detect the origin of focus events.
  • When the focus origin matches one of the configured origins and the input has a non-empty value, calls select() to highlight all text.
  • By default, responds to all focus origins (program, keyboard, mouse, touch).

Examples

Keyboard only

scSelectOnFocus="keyboard": Tab in and the text is selected; a click places the caret where you click.

import { Component, ViewEncapsulation } from '@angular/core';import {  ScField,  ScInput,  ScLabel,  ScSelectOnFocus,} from '@semantic-components/ui';
/** Selected on a keyboard focus only: a click places the caret. */@Component({

API reference

A behaviour only, on an input.

PartSelectorDescription
ScSelectOnFocusinput[scSelectOnFocus] [scSelectOnFocus]: the focus origins that select the text — 'program', 'keyboard', 'mouse', 'touch', one or a list; empty: all of them. Nothing happens while the input is empty.