Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

gatsby-plugin-breakpoints

JimmyBeldone8.2kMIT1.3.11TypeScript support: definitely-typed

Gatsby plugin providing breakpoints using React Hooks

gatsby, gatsby-plugin, gatsby-plugin-breakpoints, breakpoints, media query, media queries, hooks, responsive

readme

gatsby-plugin-breakpoints

Gatsby plugin providing breakpoints using React Hooks

travis build travis build npm version NPM downloads dependencies status dev dependencies status

commitizen semantic-release prettier license

Install

npm i gatsby-plugin-breakpoints

or

yarn add gatsby-plugin-breakpoints

Include the plugin in your gatsby-config.js file :

/* gatsby-config.js */

module.exports = {
    plugins: ['gatsby-plugin-breakpoints'],
};

Usage

Functional Components

Import the useBreakpoint hook anywhere in your app.

This hook provides four default breakpoints as boolean :

name breakpoints
xs max-width: 320px
sm max-width: 720px
md max-width: 1024px
l max-width: 1536px
/* yourFunctionalComponentOrPage.js */

import { useBreakpoint } from 'gatsby-plugin-breakpoints';

import MobileOnlyComponent from './your/component/path';
// ...

const MyComponent = () => {
    const breakpoints = useBreakpoint();

    return (
        <AnyComponent>
            {/* Anything */}

            {/* <MobileOnlyComponent /> will only be displayed if max-width <= 320px  */}
            {breakpoints.xs ? <MobileOnlyComponent /> : null}
        </AnyComponent>
    );
};

export default MyComponent;

Class Components

Import the withBreakpoints Higher Order Component anywhere in your app.

This HOC adds a breakpoints props to your component, providing four default breakpoints as boolean :

name breakpoints
xs max-width: 320px
sm max-width: 720px
md max-width: 1024px
l max-width: 1536px
/* yourClassComponent.js */

import { withBreakpoints } from 'gatsby-plugin-breakpoints';

import MobileOnlyComponent from './your/component/path';
// ...

class Test extends React.Component {
    render() {
        const { breakpoints } = this.props;
        {
            /* <MobileOnlyComponent /> will only be displayed if max-width <= 320px  */
        }
        return breakpoints.xs ? (
            <MobileOnlyComponent />
        ) : (
            <div>Content hidden only on mobile</div>
        );
    }
}

export default withBreakpoints(Test);

Options

You can set your own queries like this :

// in gatsby-config.js

const myCustomQueries = {
    xs: '(max-width: 320px)',
    sm: '(max-width: 720px)',
    md: '(max-width: 1024px)',
    l: '(max-width: 1536px)',
    xl: ...,
    portrait: '(orientation: portrait)',
};

module.exports = {
    plugins: [
        {
            resolve: "gatsby-plugin-breakpoints",
            options: {
                queries: myCustomQueries,
            },
        },
    ],
}

Default values

const defaultQueries = {
    xs: '(max-width: 320px)',
    sm: '(max-width: 720px)',
    md: '(max-width: 1024px)',
    l: '(max-width: 1536px)',
};

Note (only for test)

If you need to import <BreakpointProvider /> for testing you can do it like so :

import { BreakpointProvider } from 'gatsby-plugin-breakpoints';

In case you need full context, you can import it too :

import { BreakpointContext } from 'gatsby-plugin-breakpoints';

Contributing

Contributions are welcome ! See contributing guidelines

License

MIT

Copyright (c) 2019 Jimmy Beldone

changelog

1.3.11 (2023-11-16)

Bug Fixes

  • 🐛 update dependencies (3105405)

1.3.10 (2023-05-03)

Bug Fixes

  • 🐛 Gatsby v5 peerDependency (1c5f099)

1.3.9 (2022-10-17)

Bug Fixes

  • 🐛 update dependencies (91282fa)

1.3.8 (2022-10-17)

Bug Fixes

  • 🐛 update dependencies (6e81619)

1.3.7 (2022-03-19)

Bug Fixes

  • 🐛 update dependencies (0a32541)

1.3.6 (2021-11-11)

Bug Fixes

  • 🐛 update dependencies (a546f1a)

1.3.5 (2021-09-01)

Bug Fixes

  • 🐛 update dev dependencies (284618e)

1.3.4 (2021-06-23)

Bug Fixes

  • 🐛 update dependencies (aef1bc7)
  • 🐛 update dependencies (73ed4bd)

1.3.3 (2021-03-09)

Bug Fixes

1.3.2 (2021-01-24)

Bug Fixes

1.3.1 (2020-12-08)

Bug Fixes

1.3.0 (2020-11-30)

Features

  • expose BreakpointContext (a7619cf)

1.2.6 (2020-11-23)

Bug Fixes

1.2.5 (2020-11-13)

Bug Fixes

1.2.4 (2020-09-28)

Bug Fixes

1.2.3 (2020-09-28)

Bug Fixes

1.2.2 (2020-08-13)

Bug Fixes

1.2.1 (2020-07-31)

Bug Fixes

1.2.0 (2020-06-24)

Bug Fixes

  • CommonJS and ES6 module in same file (2435216)

Features

  • add withBreakpoints HOC for Class components support (ec7325d), closes #33

1.1.2 (2020-04-28)

Bug Fixes

1.1.1 (2020-04-03)

Bug Fixes

1.1.0 (2020-03-30)

Features

  • use wrapRootElement instead of wrapPageElement (1ce28f4)

1.0.2 (2020-03-30)

Bug Fixes

1.0.1 (2020-03-30)

Bug Fixes

1.0.0 (2020-03-30)

Bug Fixes