Home / File/ window.js — svelte Source File

window.js — svelte Source File

Architecture documentation for window.js, a javascript file in the svelte codebase. 7 imports, 0 dependents.

File javascript ClientRuntime Hydration 7 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  8246ee55_416b_58b3_f7ac_d471f90be1e9["window.js"]
  1ae6fa4e_16ee_acdf_5e28_17eb0819fddb["effects.js"]
  8246ee55_416b_58b3_f7ac_d471f90be1e9 --> 1ae6fa4e_16ee_acdf_5e28_17eb0819fddb
  a985ae40_8ef8_7ef2_adad_116fbf97e70c["effect"]
  8246ee55_416b_58b3_f7ac_d471f90be1e9 --> a985ae40_8ef8_7ef2_adad_116fbf97e70c
  7494b934_a3b8_689e_91b6_8435e26461c5["render_effect"]
  8246ee55_416b_58b3_f7ac_d471f90be1e9 --> 7494b934_a3b8_689e_91b6_8435e26461c5
  20340432_01a2_6741_abf4_60ccab51cdb3["teardown"]
  8246ee55_416b_58b3_f7ac_d471f90be1e9 --> 20340432_01a2_6741_abf4_60ccab51cdb3
  af7441d2_339a_2db1_88df_90dba2875c10["shared.js"]
  8246ee55_416b_58b3_f7ac_d471f90be1e9 --> af7441d2_339a_2db1_88df_90dba2875c10
  da178604_a1e6_1104_9d60_daf043f014c9["listen"]
  8246ee55_416b_58b3_f7ac_d471f90be1e9 --> da178604_a1e6_1104_9d60_daf043f014c9
  e95d0513_ce71_430f_7ef3_577e736f42c1["without_reactive_context"]
  8246ee55_416b_58b3_f7ac_d471f90be1e9 --> e95d0513_ce71_430f_7ef3_577e736f42c1
  style 8246ee55_416b_58b3_f7ac_d471f90be1e9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { effect, render_effect, teardown } from '../../../reactivity/effects.js';
import { listen, without_reactive_context } from './shared.js';

/**
 * @param {'x' | 'y'} type
 * @param {() => number} get
 * @param {(value: number) => void} set
 * @returns {void}
 */
export function bind_window_scroll(type, get, set = get) {
	var is_scrolling_x = type === 'x';

	var target_handler = () =>
		without_reactive_context(() => {
			scrolling = true;
			clearTimeout(timeout);
			timeout = setTimeout(clear, 100); // TODO use scrollend event if supported (or when supported everywhere?)

			set(window[is_scrolling_x ? 'scrollX' : 'scrollY']);
		});

	addEventListener('scroll', target_handler, {
		passive: true
	});

	var scrolling = false;

	/** @type {ReturnType<typeof setTimeout>} */
	var timeout;
	var clear = () => {
		scrolling = false;
	};
	var first = true;

	render_effect(() => {
		var latest_value = get();
		// Don't scroll to the initial value for accessibility reasons
		if (first) {
			first = false;
		} else if (!scrolling && latest_value != null) {
			scrolling = true;
			clearTimeout(timeout);
			if (is_scrolling_x) {
				scrollTo(latest_value, window.scrollY);
			} else {
				scrollTo(window.scrollX, latest_value);
			}
			timeout = setTimeout(clear, 100);
		}
	});

	// Browsers don't fire the scroll event for the initial scroll position when scroll style isn't set to smooth
	effect(target_handler);

	teardown(() => {
		removeEventListener('scroll', target_handler);
	});
}

/**
 * @param {'innerWidth' | 'innerHeight' | 'outerWidth' | 'outerHeight'} type
 * @param {(size: number) => void} set
 */
export function bind_window_size(type, set) {
	listen(window, ['resize'], () => without_reactive_context(() => set(window[type])));
}

Domain

Subdomains

Frequently Asked Questions

What does window.js do?
window.js is a source file in the svelte codebase, written in javascript. It belongs to the ClientRuntime domain, Hydration subdomain.
What functions are defined in window.js?
window.js defines 2 function(s): bind_window_scroll, bind_window_size.
What does window.js depend on?
window.js imports 7 module(s): effect, effects.js, listen, render_effect, shared.js, teardown, without_reactive_context.
Where is window.js in the architecture?
window.js is located at packages/svelte/src/internal/client/dom/elements/bindings/window.js (domain: ClientRuntime, subdomain: Hydration, directory: packages/svelte/src/internal/client/dom/elements/bindings).

Analyze Your Own Codebase

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

Try Supermodel Free