You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

media-hooks

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

media-hooks

Media API using Hooks

0.3.1
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Media Hooks CI/CD

Two screens with

Media Hooks

Use Media API with React Hooks

Table of Content

Installation:

You can install using:

yarn add media-hooks

// or

npm install media-hooks

How to use:

Media hooks provides two ways to use.

First way:

Use provider with patterns. Current support one operator (AND | OR).

App.js

import MediaProvider from 'media-hooks';

const App = () => {
  const patterns = {
    mobile: '(max-width: 768px)', // or { minWidth: 768 }
    tablet: '(min-width: 769px) and (max-width: 1023px)', // or { minWidth: 769, operator: 'and', maxWidth: 1023 }
    desktop: '(min-width: 1024px)' // or { maxWidth: 1024 }
  }
  
  return (
    <MediaProvider patterns={patterns}>
      <Component />
    </MediaProvider>
  );
}

export default App;

Component.js

import { useMedia } from 'media-hooks';

const Component = () => {
  const isMobile = useMedia('mobile');
  const isTablet = useMedia('tablet');
  const isDesktop = useMedia('desktop');
  
  return (
    <>
      {isMobile && <MobileComponent />}
      {isTablet && <TabletComponent />}
      {isDesktop && <DesktopComponent />}
    </>
  );
}

export default Component;

Second way:

Use custom CSS Media Query.

Component.js

import { useCustomMedia } from 'media-hooks';

const Component = () => {
  const isMobile = useCustomMedia('(max-width: 768px)');
  const isTablet = useCustomMedia('(min-width: 769px) and (max-width: 1023px)');
  const isDesktop = useCustomMedia('(min-width: 1024px)');
  
  return (
    <>
      {isMobile && <MobileComponent />}
      {isTablet && <TabletComponent />}
      {isDesktop && <DesktopComponent />}
    </>
  );
}

export default Component;

Definition:

<MediaProvider />

PropDescriptionExample
patternsobject patterns.<MediaProvider patterns={{ mobile: '(min-width: 768px)' }}>...</MediaProvider>
patternsobject patterns.<MediaProvider patterns={{ mobile: { minWidth: 768 } }}>...</MediaProvider>

useMedia()

ParamDescriptionExample
patternstring pattern key in patterns.const isMobile = useMedia('mobile')
configobject configuration objectconst isMobile = useMedia('mobile', { default: true })
config.defaultbool fallback used when Media API is unavailableconst isMobile = useMedia('mobile', { default: true })

useCustomMedia()

ParamDescriptionExample
querystring CSS Media Query.const isMobile = useCustomMedia('max-width: 768px')
configobject configuration objectconst isMobile = useCustomMedia('max-width: 768px', { default: true })
config.defaultbool fallback used when Media API is unavailableconst isMobile = useCustomMedia('max-width: 768px', { default: true })

P.S: If Media API is unavailable and default value is not provided the hooks will return undefined;

MIT License

Copyright (c) 2021 Matheus Pelegrinetti

Keywords

media-hooks

FAQs

Package last updated on 26 Jul 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.