Skip to content

Commit 7a0f4dc

Browse files
authored
Merge pull request #10 from envato/typescript-4-7-4
Support TS 4.6+ types
2 parents 4fee9ef + 2031e4f commit 7a0f4dc

File tree

8 files changed

+22
-32
lines changed

8 files changed

+22
-32
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This allows you to know the size of each observed element.
2424

2525
# 📚 Docs
2626

27-
This package was developed **and documented** as part of the [`@envato/react-breakpoints`](https://github.com/envato/react-breakpoints) package. It's separated into its own package because I believe it can be used separately if you don't need all the abstractions that React Breakpoints gives you. Please refer to the [React Breakpoints API Docs](https://github.com/envato/react-breakpoints/docs/api.md) for more details about `<Provider>` and `useResizeObserver()`.
27+
This package was developed **and documented** as part of the [`@envato/react-breakpoints`](https://github.com/envato/react-breakpoints) package. It's separated into its own package because I believe it can be used separately if you don't need all the abstractions that React Breakpoints gives you. Please refer to the [React Breakpoints API Docs](https://github.com/envato/react-breakpoints/blob/main/docs/api.md) for more details about `<Provider>` and `useResizeObserver()`.
2828

2929
# ⚡️ Quick start
3030

@@ -40,7 +40,7 @@ import { Provider as ResizeObserverProvider } from '@envato/react-resize-observe
4040
const App = () => <ResizeObserverProvider>...</ResizeObserverProvider>;
4141
```
4242

43-
⚠️ **Caution** — You may need to pass some props to `<Provider>` to increase **browser support**. Please refer to the [React Breakpoints API Docs](https://github.com/envato/react-breakpoints/docs/api.md#provider).
43+
⚠️ **Caution** — You may need to pass some props to `<Provider>` to increase **browser support**. Please refer to the [React Breakpoints API Docs](https://github.com/envato/react-breakpoints/blob/main/docs/api.md#provider).
4444

4545
## Observe an element
4646

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
8+
"compile": "tsc --noEmit",
89
"build": "tsc -b",
910
"prepare": "npm run build"
1011
},
@@ -26,8 +27,8 @@
2627
},
2728
"homepage": "https://github.com/envato/react-resize-observer-hook#readme",
2829
"peerDependencies": {
29-
"react": "16.8 - 17",
30-
"react-dom": "16.8 - 17"
30+
"react": "16.8 - 18",
31+
"react-dom": "16.8 - 18"
3132
},
3233
"devDependencies": {
3334
"@types/react": "^17.0.0",
@@ -46,7 +47,7 @@
4647
"prettier": "^2.2.1",
4748
"react": "^17.0.1",
4849
"react-dom": "^17.0.1",
49-
"typescript": "^4.2.2"
50+
"typescript": "^4.6.0"
5051
},
5152
"eslintConfig": {
5253
"extends": "react-app"

src/ExtendedResizeObserverEntry.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { ObservedElement } from './ObservedElement';
1+
import type { ObservedElement } from './ObservedElement';
22

33
export interface ExtendedResizeObserverEntry extends ResizeObserverEntry {
4-
readonly devicePixelContentBoxSize?: ReadonlyArray<ResizeObserverSize>; // https://github.com/w3c/csswg-drafts/pull/4476
54
target: ObservedElement;
65
}

src/ObservedElement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry';
1+
import type { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry';
22

33
export interface ObservedElement extends Element {
44
onResizeObservation?: (resizeObserverEntry: ExtendedResizeObserverEntry) => void;

src/Provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface ProviderProps {
77
}
88

99
/**
10-
* See API Docs: {@linkcode https://github.com/envato/react-breakpoints/blob/master/docs/api.md#provider|Provider}
10+
* See API Docs: {@linkcode https://github.com/envato/react-breakpoints/blob/main/docs/api.md#provider Provider}
1111
*
1212
* Returns a React context provider with a ResizeObserver instance as its value.
1313
* Uses `window.ResizeObserver` to construct the instance if no `ponyfill` prop is provided.

src/createResizeObserver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry';
1+
import type { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry';
22

33
/**
44
* Creates a new ResizeObserver instance that works with the
5-
* {@linkcode https://github.com/envato/react-breakpoints/blob/master/docs/api.md#useresizeobserver|useResizeObserver}
5+
* {@linkcode https://github.com/envato/react-breakpoints/blob/main/docs/api.md#useresizeobserver useResizeObserver}
66
* hook.
77
* @example
88
* const resizeObserverInstance = createResizeObserver(window.ResizeObserver);

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry';
2-
export { ObservedElement } from './ObservedElement';
1+
export type { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry';
2+
export type { ObservedElement } from './ObservedElement';
33
export { Context } from './Context';
44
export { Provider } from './Provider';
55
export { createResizeObserver } from './createResizeObserver';

src/useResizeObserver.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
import type { ObservedElement } from './ObservedElement';
2+
import type { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry';
13
import { useContext, useCallback, useRef, useState } from 'react';
24
import { Context } from './Context';
3-
import { ObservedElement } from './ObservedElement';
4-
import { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry';
55

66
const boxOptions = {
77
BORDER_BOX: 'border-box', // https://caniuse.com/mdn-api_resizeobserverentry_borderboxsize
88
CONTENT_BOX: 'content-box', // https://caniuse.com/mdn-api_resizeobserverentry_contentboxsize
9-
DEVICE_PIXEL_CONTENT_BOX: 'device-pixel-content-box' // https://github.com/w3c/csswg-drafts/pull/4476
9+
DEVICE_PIXEL_CONTENT_BOX: 'device-pixel-content-box' // https://caniuse.com/mdn-api_resizeobserverentry_devicepixelcontentboxsize
1010
};
1111

1212
/**
13-
* See API Docs: {@linkcode https://github.com/envato/react-breakpoints/blob/master/docs/api.md#useresizeobserver|useResizeObserver}
13+
* See API Docs: {@linkcode https://github.com/envato/react-breakpoints/blob/main/docs/api.md#useresizeobserver useResizeObserver}
1414
*
1515
* Returns a React callback ref to attach to a DOM element. It also returns
1616
* a resize observation entry every time the observed element changes size.
@@ -55,21 +55,11 @@ export const useResizeObserver = (
5555
break;
5656

5757
case boxOptions.DEVICE_PIXEL_CONTENT_BOX:
58-
/* TypeScript 4.2 is missing devicePixelContentBoxSize. */
59-
if (typeof resizeObserverEntry.devicePixelContentBoxSize !== 'undefined') {
60-
isIdentical = resizeObserverEntry.devicePixelContentBoxSize.every((boxSize, index) => {
61-
if (typeof observedEntry.devicePixelContentBoxSize !== 'undefined') {
62-
return (
63-
boxSize.inlineSize === observedEntry.devicePixelContentBoxSize[index].inlineSize &&
64-
boxSize.blockSize === observedEntry.devicePixelContentBoxSize[index].blockSize
65-
);
66-
} else {
67-
throw Error('resizeObserverEntry does not contain devicePixelContentBoxSize.');
68-
}
69-
});
70-
} else {
71-
throw Error('resizeObserverEntry does not contain devicePixelContentBoxSize.');
72-
}
58+
isIdentical = resizeObserverEntry.devicePixelContentBoxSize.every(
59+
(boxSize, index) =>
60+
boxSize.inlineSize === observedEntry.devicePixelContentBoxSize[index].inlineSize &&
61+
boxSize.blockSize === observedEntry.devicePixelContentBoxSize[index].blockSize
62+
);
7363
break;
7464

7565
default:

0 commit comments

Comments
 (0)