DeviceStateTracker is a utility class that tracks which media query is currently active using the matchMedia API.
yarn add seng-device-state-tracker
npm i -S seng-device-state-tracker
We also have browser, amd, commonjs, umd, systemjs and es6 versions of this module available attached to the Github Releases.
Check the build section below to see your you can build for all the targets yourself.
When using max-width media queries make sure to set the DeviceStateTracker in reverse order, set the reverseDeviceStateOrder boolean truthy when initializing the DeviceStateTracker class.
To keep track of the currently selected media query set the stateIndicator boolean truthy. This will add a unstyled div element to the DOM.
const config = {
mediaQueries: {
X_SMALL: '(max-width: 479px)',
SMALL: '(min-width: 480px)',
MEDIUM: '(min-width: 768px)',
LARGE: '(min-width: 1024px)',
},
deviceState: {
X_SMALL: 0,
SMALL: 1,
MEDIUM: 2,
LARGE: 3,
},
// When the keys below aren't set it will default to false for these options
showStateIndicator: true,
reverseDeviceStateOrder: false,
};
Configuration
/**
* Object with available media queries
*/
export const mediaQueries:IMediaQuery = {
X_SMALL: '(max-width: 479px)',
SMALL: '(min-width: 480px)',
MEDIUM: '(min-width: 768px)',
LARGE: '(min-width: 1024px)',
MEDIUM_ISOLATE: '(min-width: 768px) and (max-width: 1023px)',
};
/**
* This enum is used by the DeviceStateTracker class to determine which of the media queries in
* the mediaQueries object above are considered 'device states'. Names of this enum have to
* correspond with one of the keys in the mediaQueries object. When using the DeviceStateTracker,
* make sure you have enough device states so that there will always be one with a matching media query.
*/
export enum DeviceState {
X_SMALL,
SMALL,
MEDIUM,
LARGE,
}
Usage
import DeviceStateTracker, { DeviceStateEvent } from 'seng-device-state-tracker';
import { mediaQueries, DeviceState } from './path/to/config/deviceStateConfig';
const deviceStateTracker:DeviceStateTracker = new DeviceStateTracker({
mediaQueries,
deviceState: DeviceState,
});
// Retrieve the current state after initialisation
const { state, name } = deviceStateTracker.currentDeviceState;
deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, (event:DeviceStateEvent) => {
if (event.data.state === DeviceState.SMALL) {
console.log('Matched', event.data.name);
}
});
Configuration
export const mediaQueries = {
X_SMALL: '(max-width: 479px)',
SMALL: '(min-width: 480px)',
MEDIUM: '(min-width: 768px)',
LARGE: '(min-width: 1024px)',
MEDIUM_ISOLATE: '(min-width: 768px) and (max-width: 1023px)',
};
/**
* This enum is used by the DeviceStateTracker class to determine which of the media queries in
* the mediaQueries object above are considered 'device states'. Names of this enum have to
* correspond with one of the keys in the mediaQueries object. When using the DeviceStateTracker,
* make sure you have enough device states so that there will always be one with a matching media query.
*/
export const DeviceState = {
X_SMALL: 0,
SMALL: 1,
MEDIUM: 2,
LARGE: 3,
};
Usage
import DeviceStateTracker, { DeviceStateEvent } from 'seng-device-state-tracker';
import { mediaQueries, DeviceState } from './path/to/config/deviceStateConfig';
const deviceStateTracker = new DeviceStateTracker({
mediaQueries,
deviceState: DeviceState,
});
// Retrieve the current state after initialisation
const { state, name } = deviceStateTracker.currentDeviceState;
deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, (event) => {
if (event.data.state === DeviceState.SMALL) {
console.log('Matched', event.data.name);
}
});
View the generated documentation.
In order to build seng-device-state-tracker, ensure that you have Git and Node.js installed.
Clone a copy of the repo:
git clone https://github.com/mediamonks/seng-device-state-tracker.git
Change to the seng-device-state-tracker directory:
cd seng-device-state-tracker
Install dev dependencies:
yarn
Use one of the following main scripts:
yarn build # build this project
yarn dev # run dev-watch mode, serving example/index.html in the browser
yarn generate # generate all artifacts (compiles ts, webpack, docs and coverage)
yarn typings # install .d.ts dependencies (done on install)
yarn test:unit # run the unit tests
yarn validate # runs validation scripts, including test, lint and coverage check
yarn lint # run tslint on this project
yarn doc # generate typedoc documentation
When installing this module, it adds a pre-push hook, that runs the validate
script before committing, so you can be sure that everything checks out.
If you want to create the distribution files yourself, you can run the
build-dist
script, and the following files will get generated in the
dist
folder:
- /dist/seng-device-state-tracker.js: bundled with webpack, can be loaded from
a script tag, available as
window.SengDeviceStateTracker
- /dist/seng-device-state-tracker.min.js: same as above, but minified
- /dist/seng-device-state-tracker-amd.js: bundled with webpack, can be used with e.g. requirejs
- /dist/seng-device-state-tracker-commonjs.js: bundled with webpack, can be used in systems that support commonjs, but you should just use npm
- /dist/seng-device-state-tracker-umd.js: bundled with webpack, works in the browser, with requirejs, and in a commonjs system
- /dist/seng-device-state-tracker-umd.min.js: same as above, but minified
- /dist/seng-device-state-tracker-system.js: bundled with typescript, can be used in systems that support systemjs
- /dist/seng-device-state-tracker-es6.zip: transpiled with typescript, only types are removed from the source files
View CONTRIBUTING.md
View CHANGELOG.md
View AUTHORS.md
MIT © MediaMonks