-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from envato/typescript
Add TypeScript definitions
- Loading branch information
Showing
15 changed files
with
166 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createContext } from 'react'; | ||
|
||
export const Context = createContext<ResizeObserver | null>(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ObservedElement } from './ObservedElement'; | ||
|
||
export interface ExtendedResizeObserverEntry extends ResizeObserverEntry { | ||
target: ObservedElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry'; | ||
|
||
export interface ObservedElement extends Element { | ||
onResizeObservation?: (resizeObserverEntry: ExtendedResizeObserverEntry) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createResizeObserver } from './createResizeObserver'; | ||
import { Context } from './Context'; | ||
|
||
interface ProviderProps { | ||
ponyfill?: typeof ResizeObserver; | ||
children: React.ReactNode; | ||
} | ||
|
||
/** | ||
* See API Docs: {@linkcode https://github.com/envato/react-breakpoints/blob/master/docs/api.md#provider|Provider} | ||
* | ||
* Returns a React context provider with a ResizeObserver instance as its value. | ||
* Uses `window.ResizeObserver` to construct the instance if no `ponyfill` prop is provided. | ||
* @example | ||
* return ( | ||
* <Provider ponyfill={ResizeObserver}> | ||
* ... | ||
* </Provider> | ||
* ); | ||
*/ | ||
export const Provider = ({ ponyfill = undefined, children }: ProviderProps) => { | ||
const instance = createResizeObserver(ponyfill || window.ResizeObserver); | ||
|
||
return <Context.Provider value={instance}>{children}</Context.Provider>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry'; | ||
|
||
/** | ||
* Creates a new ResizeObserver instance that works with the | ||
* {@linkcode https://github.com/envato/react-breakpoints/blob/master/docs/api.md#useresizeobserver|useResizeObserver} | ||
* hook. | ||
* @example | ||
* const resizeObserverInstance = createResizeObserver(window.ResizeObserver); | ||
* return ( | ||
* <Context.Provider value={resizeObserverInstance}> | ||
* ... | ||
* </Context.Provider> | ||
* ); | ||
*/ | ||
export const createResizeObserver = (Constructor: typeof ResizeObserver) => { | ||
const handleResizeObserverEntry = (resizeObserverEntry: ExtendedResizeObserverEntry) => { | ||
const { onResizeObservation } = resizeObserverEntry.target; | ||
onResizeObservation && onResizeObservation(resizeObserverEntry); | ||
}; | ||
|
||
return new Constructor((entries: ExtendedResizeObserverEntry[]) => entries.forEach(handleResizeObserverEntry)); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { Context } from './Context'; | ||
export { Provider } from './Provider'; | ||
export { createResizeObserver } from './createResizeObserver'; | ||
export { useResizeObserver } from './useResizeObserver'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useContext, useCallback, useRef, useState } from 'react'; | ||
import { Context } from './Context'; | ||
import { ObservedElement } from './ObservedElement'; | ||
import { ExtendedResizeObserverEntry } from './ExtendedResizeObserverEntry'; | ||
|
||
/** | ||
* See API Docs: {@linkcode https://github.com/envato/react-breakpoints/blob/master/docs/api.md#useresizeobserver|useResizeObserver} | ||
* | ||
* Returns a React callback ref to attach to a DOM element. It also returns | ||
* a resize observation entry every time the observed element changes size. | ||
* This causes the component that uses `useResizeObserver` to rerender. | ||
* @example | ||
* const [ref, observedEntry] = useResizeObserver(options); | ||
* const observedWidth = observedEntry.borderBoxSize[0].inlineSize; | ||
* return ( | ||
* <div ref={ref}> | ||
* This container is {observedWidth}px wide. | ||
* </div> | ||
* ); | ||
*/ | ||
export const useResizeObserver = ( | ||
options: ResizeObserverOptions = {} | ||
): [React.RefCallback<ObservedElement | null>, ExtendedResizeObserverEntry | null] => { | ||
const resizeObserver = useContext<ResizeObserver | null>(Context); | ||
|
||
const [observedEntry, setObservedEntry] = useState<ExtendedResizeObserverEntry | null>(null); | ||
|
||
const handleResizeObservation = (resizeObserverEntry: ExtendedResizeObserverEntry) => | ||
setObservedEntry(resizeObserverEntry); | ||
|
||
const ref = useRef<ObservedElement | null>(null); | ||
|
||
const setRef = useCallback( | ||
node => { | ||
if (ref.current) { | ||
resizeObserver?.unobserve(ref.current); | ||
delete ref.current.onResizeObservation; | ||
} | ||
|
||
if (node) { | ||
node.onResizeObservation = handleResizeObservation; | ||
resizeObserver?.observe(node, options); | ||
} | ||
|
||
ref.current = node; | ||
}, | ||
[resizeObserver, options] | ||
); | ||
|
||
return [setRef, observedEntry]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"jsx": "react-jsx", | ||
"declaration": true, | ||
"declarationMap": true, | ||
"sourceMap": true, | ||
"outDir": "./dist", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"include": ["./src/**/*"] | ||
} |