Home / File/ media-query.js — svelte Source File

media-query.js — svelte Source File

Architecture documentation for media-query.js, a javascript file in the svelte codebase. 3 imports, 0 dependents.

File javascript SharedInternal DOMUtils 3 imports 1 classes

Entity Profile

Dependency Diagram

graph LR
  1d26e65a_8f51_acd9_1974_56655d57e188["media-query.js"]
  dbd5109f_e29b_bbbf_8573_b47d1cf23d5a["index.js"]
  1d26e65a_8f51_acd9_1974_56655d57e188 --> dbd5109f_e29b_bbbf_8573_b47d1cf23d5a
  01ad7075_03ac_8603_15c0_1261b1448369["reactive-value.js"]
  1d26e65a_8f51_acd9_1974_56655d57e188 --> 01ad7075_03ac_8603_15c0_1261b1448369
  b486621e_e5a2_e818_93d0_84358fa8b8a7["ReactiveValue"]
  1d26e65a_8f51_acd9_1974_56655d57e188 --> b486621e_e5a2_e818_93d0_84358fa8b8a7
  style 1d26e65a_8f51_acd9_1974_56655d57e188 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { on } from '../events/index.js';
import { ReactiveValue } from './reactive-value.js';

const parenthesis_regex = /\(.+\)/;

// these keywords are valid media queries but they need to be without parenthesis
//
// eg: new MediaQuery('screen')
//
// however because of the auto-parenthesis logic in the constructor since there's no parenthesis
// in the media query they'll be surrounded by parenthesis
//
// however we can check if the media query is only composed of these keywords
// and skip the auto-parenthesis
//
// https://github.com/sveltejs/svelte/issues/15930
const non_parenthesized_keywords = new Set(['all', 'print', 'screen', 'and', 'or', 'not', 'only']);

/**
 * Creates a media query and provides a `current` property that reflects whether or not it matches.
 *
 * Use it carefully — during server-side rendering, there is no way to know what the correct value should be, potentially causing content to change upon hydration.
 * If you can use the media query in CSS to achieve the same effect, do that.
 *
 * ```svelte
 * <script>
 * 	import { MediaQuery } from 'svelte/reactivity';
 *
 * 	const large = new MediaQuery('min-width: 800px');
 * </script>
 *
 * <h1>{large.current ? 'large screen' : 'small screen'}</h1>
 * ```
 * @extends {ReactiveValue<boolean>}
 * @since 5.7.0
 */
export class MediaQuery extends ReactiveValue {
	/**
	 * @param {string} query A media query string
	 * @param {boolean} [fallback] Fallback value for the server
	 */
	constructor(query, fallback) {
		let final_query =
			parenthesis_regex.test(query) ||
			// we need to use `some` here because technically this `window.matchMedia('random,screen')` still returns true
			query.split(/[\s,]+/).some((keyword) => non_parenthesized_keywords.has(keyword.trim()))
				? query
				: `(${query})`;
		const q = window.matchMedia(final_query);
		super(
			() => q.matches,
			(update) => on(q, 'change', update)
		);
	}
}

Subdomains

Classes

Frequently Asked Questions

What does media-query.js do?
media-query.js is a source file in the svelte codebase, written in javascript. It belongs to the SharedInternal domain, DOMUtils subdomain.
What does media-query.js depend on?
media-query.js imports 3 module(s): ReactiveValue, index.js, reactive-value.js.
Where is media-query.js in the architecture?
media-query.js is located at packages/svelte/src/reactivity/media-query.js (domain: SharedInternal, subdomain: DOMUtils, directory: packages/svelte/src/reactivity).

Analyze Your Own Codebase

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

Try Supermodel Free