Skip to content

Commit 7dc39df

Browse files
authored
Merge pull request #13 from envato/typescript
Rewrite code in TypeScript
2 parents 3056aa2 + 20640ea commit 7dc39df

22 files changed

+635
-484
lines changed

.babelrc

Lines changed: 0 additions & 6 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ yarn.lock
1818
.env.development.local
1919
.env.test.local
2020
.env.production.local
21+
.eslintcache
2122

2223
npm-debug.log*
2324
yarn-debug.log*

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019-2020 Envato
3+
Copyright (c) 2019-2021 Envato
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<p align="center">
88
<img alt="npm version" src="https://img.shields.io/npm/v/@envato/react-breakpoints?style=for-the-badge" />
9-
<img alt="react version" src="https://img.shields.io/npm/dependency-version/@envato/react-breakpoints/dev/react?style=for-the-badge">
9+
<img alt="react version" src="https://img.shields.io/npm/dependency-version/@envato/react-breakpoints/peer/react?style=for-the-badge">
1010
<img alt="license" src="https://img.shields.io/npm/l/@envato/react-breakpoints?style=for-the-badge" />
1111
<a href="CODE-OF-CONDUCT.md"><img alt="contributor covenant v2.0 adopted" src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg?style=for-the-badge" /></a>
1212
</p>
@@ -15,23 +15,32 @@
1515

1616
`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.
1717

18-
## 📦 What's in the box?
18+
# 📦 What's in the box?
19+
1920
> No polling. No event listening. No sentinel elements. **Just a [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)!**
2021
2122
This package provides you with:
2223

23-
* a [`<Provider>`](/docs/api.md#provider) to instantiate the `ResizeObserver`;
24-
* an [`<Observe>`](/docs/api.md#observe) component to observe changes in a DOM element and respond to them.
24+
- a [`<Provider>`](/docs/api.md#provider) to instantiate the `ResizeObserver`;
25+
- an [`<Observe>`](/docs/api.md#observe) component to observe changes in a DOM element and respond to them.
2526

2627
For power users this package also provides:
2728

28-
* a [`useBreakpoints()`](/docs/api.md#usebreakpoints) hook to change a component's behaviour based on the observed size information in the nearest parent `<Observe>`;
29-
* a [`useResizeObserver()`](/docs/api.md#useresizeobserver) hook to connect a DOM element in your component to the instantiated `ResizeObserver` on `<Provider>`;
30-
* a [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) hook to retrieve the `ResizeObserverEntry` put on the nearest `<Context>`. This is what `useBreakpoints()` uses under the hood.
29+
- a [`useBreakpoints()`](/docs/api.md#usebreakpoints) hook to change a component's behaviour based on the observed size information in the nearest parent `<Observe>`;
30+
- a [`useResizeObserver()`](/docs/api.md#useresizeobserver) hook to connect a DOM element in your component to the instantiated `ResizeObserver` on `<Provider>`;
31+
- a [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) hook to retrieve the `ResizeObserverEntry` put on the nearest `<Context>`. This is what `useBreakpoints()` uses under the hood.
32+
33+
# 🐉 Be careful using this package when&hellip;
3134

32-
# 🚧 Developer status
35+
- &hellip;all you want is the low-level API stuff. See [@envato/react-resize-observer-hook](https://github.com/envato/react-resize-observer-hook).
36+
- &hellip;you want _real_ CSS Element Queries. At the end of the day, this is still a JS solution.
37+
- &hellip;you care deeply about [Cumulative Layout Shift](https://web.dev/cls/) on public pages. **Keep reading though, this package may still be of value to you!**
3338

34-
Several projects within Envato are currently using this package, giving me confidence that the API is clear and the code adds value. The package is still in an early stage, but exposure to "the wild" will help reveal more edge-cases and hopefully make the package more robust overall.
39+
# 🏅 This package is _really good_ at&hellip;
40+
41+
- &hellip;following the latest [draft spec](https://drafts.csswg.org/resize-observer/), giving you access to cutting edge features like `devicePixelContentBoxSize` and [per-fragment](https://drafts.csswg.org/css-break-3/) observation.
42+
- &hellip;performantly observing many elements with a single `ResizeObserver` instance. None of that "a new `ResizeObserver` instance per observed element" bloat that [some](https://github.com/ZeeCoder/use-resize-observer/blob/314b29c33cfcd2c51b8854b775b0a2a5c325d94a/src/index.ts#L151-L157) alternative packages implement.
43+
- &hellip;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](https://web.dev/cls/). 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.**
3544

3645
# ⚡️ Quick start
3746

@@ -41,54 +50,55 @@ Follow these **minimum required steps** to get started with `react-breakpoints`.
4150
npm install @envato/react-breakpoints
4251
```
4352

44-
## Set up the provider
53+
## Wrap your component tree with the provider
4554

46-
```javascript
55+
```jsx
4756
import { Provider as ResizeObserverProvider } from '@envato/react-breakpoints';
4857

49-
const App = () => (
50-
<ResizeObserverProvider>
51-
...
52-
</ResizeObserverProvider>
53-
)
58+
const App = () => <ResizeObserverProvider>...</ResizeObserverProvider>;
5459
```
5560

5661
⚠️ **Caution** — You may need to pass some props to `<Provider>` to increase browser support. Please refer to the [API Docs](/docs/api.md#provider).
5762

58-
## Observe an element
59-
60-
Everything you render through `<Observe>` has access to the size of the element that is given `{...observedElementProps}`. This is called the "Observe Scope".
63+
## Observe an element and use the results
6164

62-
```javascript
65+
```jsx
6366
import { Observe } from '@envato/react-breakpoints';
6467

65-
const MyObservingComponent = () => (
66-
<Observe
67-
breakpoints={{
68-
widths: {
69-
0: 'mobile',
70-
769: 'tablet',
71-
1025: 'desktop'
72-
}
73-
}}
74-
render={({ observedElementProps, widthMatch = 'ssr' }) => (
68+
const exampleBreakpoints = {
69+
widths: {
70+
0: 'mobile',
71+
769: 'tablet',
72+
1025: 'desktop'
73+
}
74+
};
75+
76+
export const ExampleComponent = () => (
77+
<Observe breakpoints={exampleBreakpoints}>
78+
{({ observedElementProps, widthMatch = 'ssr' }) => (
7579
<div {...observedElementProps}>
7680
<div className={widthMatch}>
7781
</div>
7882
)}
79-
/>
83+
</Observe>
8084
);
8185
```
8286

8387
See the [API Docs](/docs/api.md) for reference guides and usage examples.
8488

85-
# Observing vs. Consuming boxes
89+
# Observing vs. Consuming `ResizeObserverSize`
90+
91+
There is an important distinction between the `boxSize` you observe and the `boxSize` you pass to your breakpoints. See [Observing vs. Consuming `ResizeObserverSize`](/docs/boxSizes.md) for more information.
92+
93+
# Re-rendering
94+
95+
Using [`useResizeObserver()`](/docs/api.md#useresizeobserver), [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) or [`useBreakpoints()`](/docs/api.md#usebreakpoints) in your components causes them to re-render **every time a resize is observed**.
8696

87-
There is an important distinction between the `box` you observe and the `box` you consume for triggering breakpoints. See [Observing vs. Consuming Boxes](/docs/boxes.md) for more information.
97+
In some cases, you may want to optimise this. If you only want to re-render your components when the returned breakpoint values actually change, use `React.useMemo` or `React.memo`.
8898

8999
# Re-rendering
90100

91-
Using [`useResizeObserver()`](/docs/api.md#useresizeobserver), [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) or [`useBreakpoints()`](/docs/api.md#usebreakpoints) in your components causes them to re-render **every time a resize is observed**.
101+
Using [`useResizeObserver()`](/docs/api.md#useresizeobserver), [`useResizeObserverEntry()`](/docs/api.md#useresizeobserverentry) or [`useBreakpoints()`](/docs/api.md#usebreakpoints) in your components causes them to re-render **every time a resize is observed**.
92102

93103
In some cases, you may want to optimise this. If you only want to re-render your components when breakpoint values actually change, use `React.useMemo` or `React.memo`.
94104

@@ -98,7 +108,7 @@ See [Server-Side Rendering](/docs/server-side-rendering.md) for more information
98108

99109
# Maintainers
100110

101-
* [Marc Dingena](https://github.com/mdingena) (owner)
111+
- [Marc Dingena](https://github.com/mdingena) (owner)
102112

103113
# Contributing
104114

0 commit comments

Comments
 (0)