1.0.8 • Published 6 years ago

@reacthooks.org/use-match-media v1.0.8

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
6 years ago

Install

$ npm install @reacthooks.org/use-match-media

useMatchMedia

React (State and Effect) Hook to check any media matches.

This IS NOT JUST .matchMedia() though since we also provide easy to use built-in matches such as the following:

  • useMatchMedia.prefersColorScheme() // returns 'light' or 'dark' (or null)

Synopsis

In your React component:

import useMatchMedia from "use-match-media";

function MediaMatchSmall() {
  const small = useMediaMatch("(max-width: 600px)")

  return <code>Small Device = { small }</code>
}

There is no need to subscribe or unsubscribe to a 'window.matchMedia()'MediaQueryList event since this hook does it for you.

.usePrefersColorScheme()

We provide a simple predefined way to obtain whether the user prefer the 'light' or 'dark' color scheme. If this isn't supported by the browser, then it'll always return null.

import useMatchMedia from "use-match-media";

function ColorScheme() {
  const scheme = useMediaMatch.usePrefersColorScheme()

  return <p>You prefer the <strong>{ scheme }</strong> color scheme.</p>
}

Other Hooks

Please see all of the other reacthooks.org hooks:

Author

$ npx chilts

   ╒════════════════════════════════════════════════════╕
   │                                                    │
   │   Andrew Chilton (Personal)                        │
   │   -------------------------                        │
   │                                                    │
   │          Email : andychilton@gmail.com             │
   │            Web : https://chilts.org                │
   │        Twitter : https://twitter.com/andychilton   │
   │         GitHub : https://github.com/chilts         │
   │         GitLab : https://gitlab.org/chilts         │
   │                                                    │
   │   Apps Attic Ltd (My Company)                      │
   │   ---------------------------                      │
   │                                                    │
   │          Email : chilts@appsattic.com              │
   │            Web : https://appsattic.com             │
   │        Twitter : https://twitter.com/AppsAttic     │
   │         GitLab : https://gitlab.com/appsattic      │
   │                                                    │
   │   Node.js / npm                                    │
   │   -------------                                    │
   │                                                    │
   │        Profile : https://www.npmjs.com/~chilts     │
   │           Card : $ npx chilts                      │
   │                                                    │
   ╘════════════════════════════════════════════════════╛

(Ends)

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago