All Projects → envato → react-breakpoints

envato / react-breakpoints

Licence: MIT license
Respond to changes in a DOM element's size. With React Breakpoints, element queries are no longer "web design's unicorn" 🦄

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to react-breakpoints

Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+6014.86%)
Mutual labels:  hooks, react-hooks
Awesome React Hooks
Awesome React Hooks
Stars: ✭ 7,616 (+10191.89%)
Mutual labels:  hooks, react-hooks
Beautiful React Hooks
🔥 A collection of beautiful and (hopefully) useful React hooks to speed-up your components and hooks development 🔥
Stars: ✭ 5,242 (+6983.78%)
Mutual labels:  hooks, react-hooks
React-Combine-Provider
combine react providers in ease
Stars: ✭ 29 (-60.81%)
Mutual labels:  hooks, react-hooks
Fre
👻 Tiny Footprint Concurrent UI library for Fiber.
Stars: ✭ 3,195 (+4217.57%)
Mutual labels:  hooks, react-hooks
Constate
React Context + State
Stars: ✭ 3,519 (+4655.41%)
Mutual labels:  hooks, react-hooks
Formik
Build forms in React, without the tears 😭
Stars: ✭ 29,047 (+39152.7%)
Mutual labels:  hooks, react-hooks
react-daterange-picker
A react date range picker to using @material-ui. Live Demo: https://flippingbitss.github.io/react-daterange-picker/
Stars: ✭ 85 (+14.86%)
Mutual labels:  hooks, react-hooks
React Form
⚛️ Hooks for managing form state and validation in React
Stars: ✭ 2,270 (+2967.57%)
Mutual labels:  hooks, react-hooks
Haunted
React's Hooks API implemented for web components 👻
Stars: ✭ 2,197 (+2868.92%)
Mutual labels:  hooks, react-hooks
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (-27.03%)
Mutual labels:  hooks, react-hooks
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (-47.3%)
Mutual labels:  hooks, react-hooks
learn-react-typescript
Learning React contents with TypeScript (Hooks, Redux)
Stars: ✭ 15 (-79.73%)
Mutual labels:  hooks, react-hooks
React Hook Form
📋 React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+33455.41%)
Mutual labels:  hooks, react-hooks
react-europe-2019
Slides and demo app from my keynote
Stars: ✭ 29 (-60.81%)
Mutual labels:  hooks, react-hooks
The Platform
Web. Components. 😂
Stars: ✭ 4,355 (+5785.14%)
Mutual labels:  hooks, react-hooks
max-todos
A basic Todo app developed in React.
Stars: ✭ 19 (-74.32%)
Mutual labels:  hooks, react-hooks
reason-hooks-lib
A set of reusable ReasonReact hooks.
Stars: ✭ 31 (-58.11%)
Mutual labels:  hooks, react-hooks
Graphql Hooks
🎣 Minimal hooks-first GraphQL client
Stars: ✭ 1,610 (+2075.68%)
Mutual labels:  hooks, react-hooks
styled-media-helper
💅 Helps manage media queries with styled components
Stars: ✭ 76 (+2.7%)
Mutual labels:  breakpoints, media-queries

React Breakpoints logo

React Breakpoints

npm version react version license contributor covenant v2.0 adopted


react-breakpoints allows you to respond to changes in a DOM element's size. You can change the evaluated logic and rendered output of components based on observed size changes in DOM elements. For example, you can change a dropdown menu to a horizontal list menu based on its parent container's width without using CSS media queries.

📦 What's in the box?

No polling. No event listening. No sentinel elements. Just a ResizeObserver!

This package provides you with:

  • a <Provider> to instantiate the ResizeObserver;
  • an <Observe> component to observe changes in a DOM element and respond to them.

For power users this package also provides:

  • a useBreakpoints() hook to change a component's behaviour based on the observed size information in the nearest parent <Observe>;
  • a useResizeObserver() hook to connect a DOM element in your component to the instantiated ResizeObserver on <Provider>;
  • a useResizeObserverEntry() hook to retrieve the ResizeObserverEntry put on the nearest <Context>. This is what useBreakpoints() uses under the hood.

🐉 Be careful using this package when…

  • …all you want is the low-level API stuff. See @envato/react-resize-observer-hook.
  • …you want real CSS Element Queries. At the end of the day, this is still a JS solution.
  • …you care deeply about Cumulative Layout Shift on public pages. Keep reading though, this package may still be of value to you!

🏅 This package is really good at…

  • …following the latest draft spec, giving you access to cutting edge features like devicePixelContentBoxSize and per-fragment observation.
  • …performantly observing many elements with a single ResizeObserver instance. None of that "a new ResizeObserver instance per observed element" bloat that some alternative packages implement.
  • …building highly-responsive private dashboards 📊. One key thing this package (and every other ResizeObserver package out there) can contribute negatively to is Cumulative Layout Shifting. At Envato we've had great success using this package on pages that are only visible after signing in, like our Author Dashboard. We've had less success using it in places where search engines can go, on components with responsive styles that changed the layout vertically. One of our company values is "Tell It Like It Is", so we're letting you know to be mindful of when and how you use ResizeObserver for responsive layouts.

⚡️ Quick start

Follow these minimum required steps to get started with react-breakpoints. This is just the tip of the iceberg, though. Check the API Docs for all options.

npm install @envato/react-breakpoints

Wrap your component tree with the provider

import { Provider as ResizeObserverProvider } from '@envato/react-breakpoints';

const App = () => <ResizeObserverProvider>...</ResizeObserverProvider>;

⚠️ Caution — You may need to pass some props to <Provider> to increase browser support. Please refer to the API Docs.

Observe an element and use the results

import { Observe } from '@envato/react-breakpoints';

const exampleBreakpoints = {
    widths: {
      0: 'mobile',
      769: 'tablet',
      1025: 'desktop'
    }
  };

export const ExampleComponent = () => (
  <Observe breakpoints={exampleBreakpoints}>
    {({ observedElementProps, widthMatch = 'ssr' }) => (
      <div {...observedElementProps}>
        <div className={widthMatch}>
      </div>
    )}
  </Observe>
);

See the API Docs for reference guides and usage examples.

Observing vs. Consuming ResizeObserverSize

There is an important distinction between the boxSize you observe and the boxSize you pass to your breakpoints. See Observing vs. Consuming ResizeObserverSize for more information.

Re-rendering

Using useResizeObserver(), useResizeObserverEntry() or useBreakpoints() in your components causes them to re-render every time a resize is observed.

Server-Side Rendering

See Server-Side Rendering for more information.

Maintainers

Contributing

For bug fixes, documentation changes, and small features:

  1. Fork this repository.
  2. Create your feature branch (git checkout -b my-new-feature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin my-new-feature).
  5. Create a new Pull Request.

For larger new features: Do everything as above, but first also make contact with the project maintainers to be sure your change fits with the project direction and you won't be wasting effort going in the wrong direction.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].