detect-media
Live media-query facts as attributes — what CSS knows, Quark can now select on. One <detect-media> per query; is-matched follows window.matchMedia and flips as the query does.
Features
- Live
is-matchedFollowsMediaQueryList.matchesand itschangeevent - Any media query Color scheme, pointer type, motion, width — whatever
matchMediaaccepts - CSS / Quark selectable Gate content on
detect-media[is-matched], no listeners - Provision + event
.provisionis{ mediaQuery, isMatched };detect-media-changefires on every flip
Installation
This package is available in the
<script src="https://unpkg.com/@excom/kit-utils@0.3.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/neutron@0.2.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/detect-media@0.1.8/dist/index.umd.min.js"></script>npm install @excom/detect-media<!-- import path to `node_modules` will depend on your build setup -->
<script type="module" src="/node_modules/@excom/detect-media"></script>
<link rel="stylesheet" href="/node_modules/@excom/detect-media">import "@excom/detect-media";Peer dependencies (0)
Packages a consumer must install alongside this one. Workspace deps are bundled.
| Package | Version |
|---|---|
|
Usage
One element per query. media-query takes anything window.matchMedia does.
<detect-media media-query="(pointer: coarse)"></detect-media><detect-media media-query="(pointer: coarse)"></detect-media>Select on is-matched from CSS:
body:has(detect-media[media-query="(pointer: coarse)"][is-matched]) .hover-hint {
display: none;
}body:has(detect-media[media-query="(pointer: coarse)"][is-matched]) .hover-hint {
display: none;
}Or from Quark — the attribute, or the provision:
detect-media[media-query="(pointer: coarse)"][is-matched] ~ nav {
data-is-touch: "";
}
detect-media[media-query="(pointer: coarse)"]:not([is-matched]) ~ nav {
data-is-touch: none;
}
detect-media[media-query="(prefers-color-scheme: dark)"] {
$app-is-dark: prop("provision").isMatched;
}detect-media[media-query="(pointer: coarse)"][is-matched] ~ nav {
data-is-touch: "";
}
detect-media[media-query="(pointer: coarse)"]:not([is-matched]) ~ nav {
data-is-touch: none;
}
detect-media[media-query="(prefers-color-scheme: dark)"] {
$app-is-dark: prop("provision").isMatched;
}Missing / empty media-query leaves is-matched unset and provision null; changing it re-subscribes. detect-media-change fires on every flip after mount, never on mount — react to the attribute for the initial state.
API Reference
Attributes
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
Provision
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
Events
Type
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
Recognized Elements
Child or descendant elements are recognized by and relevant to its functionality.Styles
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
detect-media
Attributes (2)
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
media-query
window.matchMedia syntax: (prefers-color-scheme: dark), (pointer: coarse), (width < 600px). Required — missing / empty leaves is-matched unset and provision null. Changing it re-subscribes.
mediaQuery
string
nullis-matched
media-query matches. Live — follows the query's change event.
isMatched
boolean
falseProvision (1)
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
provision
{ mediaQuery, isMatched } — a new object on first evaluation and on every change; null without a query. Not reflected as an attribute.
provision
DetectMediaProvision
Events (1)
e.preventDefault() is not
synchronously called on the event.
Type
detect-media-change
event.detail is the .provision payload.
Type
DetectMediaChangeEvent
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
Recognized Elements (0)
Child or descendant elements are recognized by detect-media and relevant to its functionality.Styles (0)
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
Release notes (1)
0.1.1
- Ship only dist (and declared extras) in the npm tarball; drop build logs, tests and sources
Release notes
e.preventDefault() is not
synchronously called on the event.
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
Examples
Touch vs pointer views
Quark flips is-active on two <include-content> hosts from (pointer: coarse) — a true render, so the inactive view is not in the document. Toggle device emulation in DevTools to switch live.