Home / Function/ bind_value() — svelte Function Reference

bind_value() — svelte Function Reference

Architecture documentation for the bind_value() function in input.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  5c05338d_77e4_261b_050c_69794590bc6f["bind_value()"]
  2254d980_5794_d0a4_3609_60f21d662ff4["input.js"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|defined in| 2254d980_5794_d0a4_3609_60f21d662ff4
  140e2114_da40_4679_bc9f_599a89c67e4e["listen_to_event_and_reset_event()"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|calls| 140e2114_da40_4679_bc9f_599a89c67e4e
  e05d5341_3016_672c_19c4_c6a1e50c4133["bind_invalid_checkbox_value()"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|calls| e05d5341_3016_672c_19c4_c6a1e50c4133
  6527d0bb_4ede_1d16_1231_00e8ef19d573["is_numberlike_input()"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|calls| 6527d0bb_4ede_1d16_1231_00e8ef19d573
  d8e54ac6_b5a2_959b_3711_0d62625dd214["to_number()"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|calls| d8e54ac6_b5a2_959b_3711_0d62625dd214
  a53cae39_471c_2887_0e0b_eedb385d245a["tick()"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|calls| a53cae39_471c_2887_0e0b_eedb385d245a
  a08b6cc5_af73_1be4_d02f_3113cf8a8305["get()"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|calls| a08b6cc5_af73_1be4_d02f_3113cf8a8305
  a814b193_e12a_4037_c3c8_dfd45f3bd0bb["untrack()"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|calls| a814b193_e12a_4037_c3c8_dfd45f3bd0bb
  7494b934_a3b8_689e_91b6_8435e26461c5["render_effect()"]
  5c05338d_77e4_261b_050c_69794590bc6f -->|calls| 7494b934_a3b8_689e_91b6_8435e26461c5
  style 5c05338d_77e4_261b_050c_69794590bc6f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/dom/elements/bindings/input.js lines 19–121

export function bind_value(input, get, set = get) {
	var batches = new WeakSet();

	listen_to_event_and_reset_event(input, 'input', async (is_reset) => {
		if (DEV && input.type === 'checkbox') {
			// TODO should this happen in prod too?
			e.bind_invalid_checkbox_value();
		}

		/** @type {any} */
		var value = is_reset ? input.defaultValue : input.value;
		value = is_numberlike_input(input) ? to_number(value) : value;
		set(value);

		if (current_batch !== null) {
			batches.add(current_batch);
		}

		// Because `{#each ...}` blocks work by updating sources inside the flush,
		// we need to wait a tick before checking to see if we should forcibly
		// update the input and reset the selection state
		await tick();

		// Respect any validation in accessors
		if (value !== (value = get())) {
			var start = input.selectionStart;
			var end = input.selectionEnd;
			var length = input.value.length;

			// the value is coerced on assignment
			input.value = value ?? '';

			// Restore selection
			if (end !== null) {
				var new_length = input.value.length;
				// If cursor was at end and new input is longer, move cursor to new end
				if (start === end && end === length && new_length > length) {
					input.selectionStart = new_length;
					input.selectionEnd = new_length;
				} else {
					input.selectionStart = start;
					input.selectionEnd = Math.min(end, new_length);
				}
			}
		}
	});

	if (
		// If we are hydrating and the value has since changed,
		// then use the updated value from the input instead.
		(hydrating && input.defaultValue !== input.value) ||
		// If defaultValue is set, then value == defaultValue
		// TODO Svelte 6: remove input.value check and set to empty string?
		(untrack(get) == null && input.value)
	) {
		set(is_numberlike_input(input) ? to_number(input.value) : input.value);

		if (current_batch !== null) {
			batches.add(current_batch);
		}
	}

	render_effect(() => {
		if (DEV && input.type === 'checkbox') {
			// TODO should this happen in prod too?
			e.bind_invalid_checkbox_value();
		}

		var value = get();

		if (input === document.activeElement) {
			// we need both, because in non-async mode, render effects run before previous_batch is set
			var batch = /** @type {Batch} */ (previous_batch ?? current_batch);

			// Never rewrite the contents of a focused input. We can get here if, for example,
			// an update is deferred because of async work depending on the input:
			//
			// <input bind:value={query}>
			// <p>{await find(query)}</p>
			if (batches.has(batch)) {
				return;

Domain

Subdomains

Frequently Asked Questions

What does bind_value() do?
bind_value() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/dom/elements/bindings/input.js.
Where is bind_value() defined?
bind_value() is defined in packages/svelte/src/internal/client/dom/elements/bindings/input.js at line 19.
What does bind_value() call?
bind_value() calls 8 function(s): bind_invalid_checkbox_value, get, is_numberlike_input, listen_to_event_and_reset_event, render_effect, tick, to_number, untrack.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free