Namespace: SpawnDev.SpawnJS.JSObjects
Inherits: EventTarget
Source: JSObjects/MediaQueryList.cs
MDN Reference: MediaQueryList on MDN
A MediaQueryList object stores information on a media query applied to a document, with support for both immediate and event-driven matching against the state of the document. https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList
| Signature | Description |
|---|---|
MediaQueryList(SpawnJSObjectReference _ref) |
Deserialization constructor |
| Property | Type | Access | Description |
|---|---|---|---|
Matches |
bool |
get | A boolean value that returns true if the document currently matches the media query list, or false if not. |
Media |
string |
get | A string representing a serialized media query. |
| Event | Type | Description |
|---|---|---|
OnChange |
ActionEvent<MediaQueryListEvent> |
Sent to the MediaQueryList when the result of running the media query against the document changes. For example, if the media query is (min-width: 400px), the change event is fired any time the width of the document's viewport changes such that its width moves across the 400px boundary in either direction. |
SpawnDev.SpawnJS Mapping Note: The JavaScript examples below show the standard Web API usage. In SpawnDev.SpawnJS, the same API is available with C# conventions:
- Properties and methods use PascalCase (e.g.,
readyStatebecomesReadyState)- Events use ActionEvent with
+=/-=(e.g.,addEventListener("click", fn)becomesOnClick += handler)- Async methods return
Task<T>instead ofPromise<T>- Objects should be disposed with
usingstatements- Access via
JS.Get<MediaQueryList>(...)or constructornew MediaQueryList(...)
const para = document.querySelector("p");
const mql = window.matchMedia("(width <= 600px)");
function screenTest(e) {
if (e.matches) {
/* the viewport is 600 pixels wide or less */
para.textContent = "This is a narrow screen — less than 600px wide.";
document.body.style.backgroundColor = "red";
} else {
/* the viewport is more than 600 pixels wide */
para.textContent = "This is a wide screen — more than 600px wide.";
document.body.style.backgroundColor = "blue";
}
}
mql.addEventListener("change", screenTest);