1.0.37 • Published 7 years ago

media-query-facade v1.0.37

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

media-query-facade

build status coverage status npm version npm downloads

Do stuff via JavaScript when the media queries on a document change. For efficiency it uses window.matchMedia under the hood.

Usage

import MQFacade from 'media-query-facade'

const mq = new MQFacade({
  small: 'only screen and (max-width: 480px)',
  medium: 'only screen and (min-width: 480px) and (max-width: 720px)'
})

const changeColour = colour => () => {
  document.body.style.backgroundColor = colour
}

mq.on('small', changeColour('blue'))
mq.on('medium', changeColour('green'))
mq.on('only screen and (min-width: 720px)', changeColour('red'))

There is a working version of the above in the example dir. First run npm start, then point a browser at localhost:8080.

API

const mq = new MQFacade(aliases)

Initialise a new MQFacade. Media query aliases may also be provided up front.

mq.registerAlias(alias, query)

Register an alias for a query, or register a number of aliases at once via an object.

mq.registerAlias('small', '(max-width: 100px)')
mq.registerAlias({
  small: '(max-width: 100px)',
  medium: '(max-width: 200px)'
})

mq.on(query, callback, context)

Register a callback which will be executed with the given context on entry of the given query or alias. If context is not specified, it will default to the mq instance.

mq.on('(max-width: 400px)', () => {})
mq.on('smartphones', () => {}, {})

mq.off(query, callback, context)

Remove all callbacks for all queries:

mq.off()

Remove all callbacks for a query or alias:

mq.off('(max-width: 400px)')

Remove a callback for a query or alias:

mq.off('(max-width: 400px)', () => {})

Remove a callback with a context for a query or alias:

mq.off('(max-width: 400px)', () => {}, {})

Install

$ npm install media-query-facade --save

There are also UMD builds available via unpkg:

License

MIT

1.0.37

7 years ago

1.0.36

7 years ago

1.0.35

7 years ago

1.0.34

7 years ago

1.0.33

8 years ago

1.0.32

8 years ago

1.0.31

8 years ago

1.0.30

8 years ago

1.0.29

8 years ago

1.0.28

8 years ago

1.0.27

8 years ago

1.0.26

9 years ago

1.0.25

9 years ago

1.0.24

9 years ago

1.0.23

9 years ago

1.0.22

9 years ago

1.0.21

9 years ago

1.0.20

9 years ago

1.0.19

9 years ago

1.0.18

9 years ago

1.0.17

9 years ago

1.0.16

9 years ago

1.0.15

10 years ago

1.0.13

10 years ago

1.0.12

10 years ago

1.0.11

10 years ago

1.0.10

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago