Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
seng-device-state-tracker
Advanced tools
Readme
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:
window.SengDeviceStateTracker
View CONTRIBUTING.md
View CHANGELOG.md
View AUTHORS.md
MIT © MediaMonks
FAQs
DeviceStateTracker is a utility class that tracks which media query is currently active using the matchMedia API.
The npm package seng-device-state-tracker receives a total of 250 weekly downloads. As such, seng-device-state-tracker popularity was classified as not popular.
We found that seng-device-state-tracker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.