@reacthooks.org/use-match-media

1.0.8 • Public • Published

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)

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i @reacthooks.org/use-match-media

    Weekly Downloads

    0

    Version

    1.0.8

    License

    ISC

    Unpacked Size

    9.94 kB

    Total Files

    5

    Last publish

    Collaborators

    • chilts
    • chilts-appsattic