Skip to content

Commit

Permalink
fix(*): use unknown instead of any type for layout snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannick Garthen committed Aug 18, 2019
1 parent 4cff725 commit 5a87c8f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,5 @@ setInterval(() => {
</main>
</ViewportProvider>,
document.getElementById('root'),
)
}, 1000)
);
}, 1000);
6 changes: 3 additions & 3 deletions lib/ObserveViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ interface IState extends IChildProps {}

interface IProps {
children?: (props: IChildProps) => React.ReactNode;
onUpdate?: (props: IChildProps, layoutSnapshot: any) => void;
recalculateLayoutBeforeUpdate?: (props: IChildProps) => any;
onUpdate?: (props: IChildProps, layoutSnapshot: unknown) => void;
recalculateLayoutBeforeUpdate?: (props: IChildProps) => unknown;
disableScrollUpdates: boolean;
disableDimensionsUpdates: boolean;
deferUpdateUntilIdle: boolean;
Expand Down Expand Up @@ -98,7 +98,7 @@ export default class ObserveViewport extends React.Component<IProps, IState> {
cancelAnimationFrame(this.tickId);
}

handleViewportUpdate = (viewport: IViewport, layoutSnapshot: any) => {
handleViewportUpdate = (viewport: IViewport, layoutSnapshot: unknown) => {
const scroll = this.props.disableScrollUpdates ? null : viewport.scroll;
const dimensions = this.props.disableDimensionsUpdates
? null
Expand Down
14 changes: 7 additions & 7 deletions lib/ViewportProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ const getCurrentDefaultViewport = (() => {
defaultValue = {
scroll: getClientScroll(),
dimensions: getClientDimensions(),
}
};
}
return defaultValue
}
return defaultValue;
};
})();

export const ViewportContext = React.createContext({
removeViewportChangeListener: (handler: TViewportChangeHandler) => { },
removeViewportChangeListener: (handler: TViewportChangeHandler) => {},
addViewportChangeListener: (
handler: TViewportChangeHandler,
options: IViewportChangeOptions,
) => { },
) => {},
getCurrentViewport: getCurrentDefaultViewport,
hasRootProviderAsParent: false,
version: '__VERSION__',
Expand Down Expand Up @@ -84,7 +84,7 @@ const shouldSkipIteration = (
export default class ViewportProvider extends React.PureComponent<
IProps,
{ hasListeners: boolean }
> {
> {
static defaultProps: {
experimentalSchedulerEnabled: false;
};
Expand Down Expand Up @@ -145,7 +145,7 @@ export default class ViewportProvider extends React.PureComponent<
if (recalculateLayoutBeforeUpdate) {
const getDuration = createPerformanceMarker();
const layoutState = recalculateLayoutBeforeUpdate(state);
return [layoutState, getDuration()];
return [layoutState, getDuration()] as const;
}
return null;
},
Expand Down
12 changes: 5 additions & 7 deletions lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ interface IEffectOptions<T> extends IOptions {
recalculateLayoutBeforeUpdate?: (viewport: IViewport) => T;
}

type HandleViewportChangeType = (viewport: IViewport, snapshot: any) => void;

export const useViewportEffect = <T = any>(
handleViewportChange: HandleViewportChangeType,
export const useViewportEffect = <T>(
handleViewportChange: (viewport: IViewport, snapshot: T) => void,
options: IViewPortEffectOptions<T> = {},
) => {
const {
Expand Down Expand Up @@ -58,7 +56,7 @@ export const useViewport = (options: IFullOptions = {}): IViewport => {
return state;
};

export const useScrollEffect = <T = any>(
export const useScrollEffect = <T = unknown>(
effect: (scroll: IScroll, snapshot: T) => void,
options: IEffectOptions<T> = {},
) => {
Expand All @@ -80,7 +78,7 @@ export const useScroll = (options: IOptions = {}): IScroll => {
return scroll;
};

export const useDimensionsEffect = <T = any>(
export const useDimensionsEffect = <T = unknown>(
effect: (scroll: IDimensions, snapshot: T) => void,
options: IEffectOptions<T> = {},
) => {
Expand Down Expand Up @@ -124,7 +122,7 @@ export const useRect = (
);
};

export const useLayoutSnapshot = <T = any>(
export const useLayoutSnapshot = <T = unknown>(
recalculateLayoutBeforeUpdate: (viewport: IViewport) => T,
options: IFullOptions = {},
): null | T => {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ interface DOMRectReadOnly {
toJSON: () => any;
}

declare module "@testing-library/react"
declare module '@testing-library/react';
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface IViewportChangeOptions {
notifyDimensions: () => boolean;
notifyOnlyWhenIdle: () => boolean;
priority: () => PriorityType;
recalculateLayoutBeforeUpdate?: (viewport: IViewport) => any;
recalculateLayoutBeforeUpdate?: (viewport: IViewport) => unknown;
}

export interface IViewportCollectorUpdateOptions {
Expand Down
5 changes: 2 additions & 3 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const warnNoContextAvailable = (location: string) => {
if (process.env.NODE_ENV === 'production') {
return;
}
const fromHook = location.startsWith('use')
const fromHook = location.startsWith('use');
if (fromHook) {
console.warn(
`react-viewport-utils: ${location} hook is not able to connect to a <ViewportProvider>. Therefore it cannot detect updates from the viewport and will not work as expected. To resolve this issue please add a <ViewportProvider> as a parent of the component using the hook, e.g. directly in the ReactDOM.render call:
Expand All @@ -130,8 +130,7 @@ ReactDOM.render(
document.getElementById('root')
);`,
);
return

return;
}
console.warn(
`react-viewport-utils: ${location} component is not able to connect to a <ViewportProvider>. Therefore it cannot detect updates from the viewport and will not work as expected. To resolve this issue please add a <ViewportProvider> as a parent of the <ObserveViewport> component, e.g. directly in the ReactDOM.render call:
Expand Down

0 comments on commit 5a87c8f

Please sign in to comment.