seng-device-state-tracker
DeviceStateTracker is a utility class that tracks which media query is currently active using the matchMedia API.
Installation
yarn / npm
yarn add seng-device-state-tracker
npm i -S seng-device-state-tracker
other
We also have browser, amd, commonjs, umd, systemjs and es6 versions of
this module available attached to the Github Releases.
manual
Check the build section below to see your you can build for all the
targets yourself.
Usage information
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.
Example configuration for deviceStateTracker
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,
},
showStateIndicator: true,
reverseDeviceStateOrder: false,
};
Usage TypeScript
Configuration
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)',
};
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,
});
const { state, name } = deviceStateTracker.currentDeviceState;
deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, (event:DeviceStateEvent) => {
if (event.data.state === DeviceState.SMALL) {
console.log('Matched', event.data.name);
}
});
Usage JavaScript
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)',
};
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,
});
const { state, name } = deviceStateTracker.currentDeviceState;
deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, (event) => {
if (event.data.state === DeviceState.SMALL) {
console.log('Matched', event.data.name);
}
});
Documentation
View the generated documentation.
Building
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
yarn dev
yarn generate
yarn typings
yarn test:unit
yarn validate
yarn lint
yarn doc
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
Contribute
View CONTRIBUTING.md
Changelog
View CHANGELOG.md
Authors
View AUTHORS.md
LICENSE
MIT © MediaMonks