Home / Class/ ViewTransitionStyleSheet Class — astro Architecture

ViewTransitionStyleSheet Class — astro Architecture

Architecture documentation for the ViewTransitionStyleSheet class in transition.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  d7cc381c_9294_21bb_a466_854d22d9bf61["ViewTransitionStyleSheet"]
  f4a9c12d_07bd_6de6_237f_8553c55f6fef["transition.ts"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|defined in| f4a9c12d_07bd_6de6_237f_8553c55f6fef
  75390ef9_f1c6_2996_aa81_87855a9a7679["constructor()"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|method| 75390ef9_f1c6_2996_aa81_87855a9a7679
  2527a613_a18b_f09f_d953_5ec7867c60a5["toString()"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|method| 2527a613_a18b_f09f_d953_5ec7867c60a5
  22c90603_4e12_ff50_9628_35770ebd45bf["layer()"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|method| 22c90603_4e12_ff50_9628_35770ebd45bf
  80c6564a_bf5c_0a79_226b_9b6d8ff8c1e3["addRule()"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|method| 80c6564a_bf5c_0a79_226b_9b6d8ff8c1e3
  27976e4d_cb29_8c73_4a03_b05d48d34065["addAnimationRaw()"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|method| 27976e4d_cb29_8c73_4a03_b05d48d34065
  066351a7_c283_6d13_ba2b_260df1d0fe47["addModern()"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|method| 066351a7_c283_6d13_ba2b_260df1d0fe47
  bddee0a0_6f9b_881a_da47_a6afb3379489["addFallback()"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|method| bddee0a0_6f9b_881a_da47_a6afb3379489
  1920fb61_b9e6_5573_7885_73058c25bccf["addAnimationPair()"]
  d7cc381c_9294_21bb_a466_854d22d9bf61 -->|method| 1920fb61_b9e6_5573_7885_73058c25bccf

Relationship Graph

Source Code

packages/astro/src/runtime/server/transition.ts lines 132–199

class ViewTransitionStyleSheet {
	private modern: string[] = [];
	private fallback: string[] = [];

	constructor(
		private scope: string,
		private name: string,
	) {}

	toString() {
		const { scope, name } = this;
		const [modern, fallback] = [this.modern, this.fallback].map((rules) => rules.join(''));
		return [
			`[data-astro-transition-scope="${scope}"] { view-transition-name: ${name}; }`,
			this.layer(modern),
			fallback,
		].join('');
	}

	private layer(cssText: string) {
		return cssText ? `@layer astro { ${cssText} }` : '';
	}

	private addRule(target: 'modern' | 'fallback', cssText: string) {
		this[target].push(cssText);
	}

	addAnimationRaw(image: 'old' | 'new' | 'group', animation: string) {
		this.addModern(image, animation);
		this.addFallback(image, animation);
	}

	addModern(image: 'old' | 'new' | 'group', animation: string) {
		const { name } = this;
		this.addRule('modern', `::view-transition-${image}(${name}) { ${animation} }`);
	}

	addFallback(image: 'old' | 'new' | 'group', animation: string) {
		const { scope } = this;
		this.addRule(
			'fallback',
			// Two selectors here, the second in case there is an animation on the root.
			`[data-astro-transition-fallback="${image}"] [data-astro-transition-scope="${scope}"],
			[data-astro-transition-fallback="${image}"][data-astro-transition-scope="${scope}"] { ${animation} }`,
		);
	}

	addAnimationPair(
		direction: 'forwards' | 'backwards' | string,
		image: 'old' | 'new',
		rules: TransitionAnimation | TransitionAnimation[],
	) {
		const { scope, name } = this;
		const animation = stringifyAnimation(rules);
		const prefix =
			direction === 'backwards'
				? `[data-astro-transition=back]`
				: direction === 'forwards'
					? ''
					: `[data-astro-transition=${direction}]`;
		this.addRule('modern', `${prefix}::view-transition-${image}(${name}) { ${animation} }`);
		this.addRule(
			'fallback',
			`${prefix}[data-astro-transition-fallback="${image}"] [data-astro-transition-scope="${scope}"],
			${prefix}[data-astro-transition-fallback="${image}"][data-astro-transition-scope="${scope}"] { ${animation} }`,
		);
	}
}

Domain

Frequently Asked Questions

What is the ViewTransitionStyleSheet class?
ViewTransitionStyleSheet is a class in the astro codebase, defined in packages/astro/src/runtime/server/transition.ts.
Where is ViewTransitionStyleSheet defined?
ViewTransitionStyleSheet is defined in packages/astro/src/runtime/server/transition.ts at line 132.

Analyze Your Own Codebase

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

Try Supermodel Free