Skip to content

Commit 7aa2bac

Browse files
committed
Merge branch 'release/2.2.0'
# Conflicts: # package.json
2 parents bca9efe + 23b1dfa commit 7aa2bac

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ The `layers` prop takes an array of objects that will represent each image (or c
150150

151151
## \<ParallaxProvider>
152152

153-
The `<ParallaxProvider />` component is meant to wrap a top level component in your application and is necessary to provide access though React's context API to the parallax controller. This component should only be used once in you app, for instance in an `<AppContainer />` component that won't be mounted/unmounted during route changes. Like so:
153+
The `<ParallaxProvider />` component is meant to wrap a top level component in your application and is necessary to provide access though React context API to the parallax controller. This component should only be used once in you app, for instance in an `<AppContainer />` component that won't be mounted/unmounted during route changes. Like so:
154154

155155
```jsx
156156
const AppContainer = () => (
@@ -195,6 +195,19 @@ export withController(MyComponent);
195195

196196
```
197197

198+
Also `parallaxController` is accessible using `useController()` [React hook](https://reactjs.org/docs/hooks-intro.html) in components without writing a class or wrapping them in HOC.
199+
200+
```jsx
201+
import { useController } from 'react-scroll-parallax';
202+
203+
const MyComponent = () => {
204+
const { parallaxController } = useController();
205+
// do stuff with `parallaxController`
206+
207+
return <div />;
208+
};
209+
```
210+
198211
### Available Methods
199212

200213
Access the following methods on `parallaxController` via context:
@@ -228,9 +241,27 @@ class Image extends Component {
228241
export withController(Image);
229242
```
230243

244+
If your parallax components are stuck and acting weird, this is most likely due to the fact that your page initial scroll was not at the top on load. Here's a possible solution to this problem using `useController()` hook. It can be used in your application top level component or specifically in the part of your application where you are experiencing problems.
245+
246+
```jsx
247+
const ParallaxCache = () => {
248+
const { parallaxController } = useController();
249+
250+
useLayoutEffect(() => {
251+
const handler = () => parallaxController.update();
252+
window.addEventListener('load', handler);
253+
return () => window.removeEventListener('load', handler);
254+
}, [parallaxController]);
255+
256+
return null;
257+
};
258+
259+
// <ParallaxCache /> now can be used anywhere you have problems with cached attributes
260+
```
261+
231262
## Troubleshooting
232263

233-
If you're encountering issues like the parallax element jumping around or becoming stuck, there's a few likely culprits. Since this lib caches important positioning states it's posible for these to be outdated and incorrect. The most likely cause for this type of problem is the result of images loading and increasing the height of an element and/or the page. This can be fixed easily by [updating the cache](#example-usage-of-context). Another likely issue is the CSS positioning applied to the parent or parallax element itself is `fixed`. Fixed positioning parallax elements is currently not supported and may appear to work in some cases but break in others. Avoid using `position: fixed` and instead use `static`, `relative` or `absolute`, which this lib was designed to support. If none of these are relevant and you still have trouble please post an issue with your code or a demo that reproduces the problem.
264+
If you're encountering issues like the parallax element jumping around or becoming stuck, there's a few likely culprits. Since this lib caches important positioning states it's possible for these to be outdated and incorrect. The most likely cause for this type of problem is the result of images loading and increasing the height of an element and/or the page. This can be fixed easily by [updating the cache](#example-usage-of-context). Another likely issue is the CSS positioning applied to the parent or parallax element itself is `fixed`. Fixed positioning parallax elements is currently not supported and may appear to work in some cases but break in others. Avoid using `position: fixed` and instead use `static`, `relative` or `absolute`, which this lib was designed to support. If none of these are relevant and you still have trouble please post an issue with your code or a demo that reproduces the problem.
234265

235266
## Browser Support
236267

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-scroll-parallax",
3-
"version": "2.1.3",
3+
"version": "2.2.0",
44
"description": "React components to create parallax scroll effects for banners, images or any other DOM elements.",
55
"repository": {
66
"type": "git",

src/components/useController.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useContext } from 'react';
2+
import ParallaxContext from '../helpers/ParallaxContext';
3+
4+
export default () => {
5+
const parallaxController = useContext(ParallaxContext);
6+
7+
if (!parallaxController) {
8+
throw new Error(
9+
'Could not find `react-scroll-parallax` context value. Please ensure the component is wrapped in a <ParallaxProvider>'
10+
);
11+
}
12+
13+
return { parallaxController };
14+
};

src/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,11 @@ type RemoveProps<T, U extends keyof T> = Pick<T, Exclude<keyof T, U>>;
116116
export function withController<P extends WithControllerInjectedProps>(
117117
Component: React.ComponentType<P>
118118
): React.ComponentType<RemoveProps<P, 'parallaxController'>>;
119+
120+
export interface ParallaxContextValue {
121+
parallaxController: ParallaxController;
122+
}
123+
124+
export const ParallaxContext: React.Context<ParallaxContextValue>;
125+
126+
export function useController(): WithControllerInjectedProps;

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// all module exports
2+
export useController from './components/useController';
23
export withController from './components/withController';
34
export Parallax from './components/Parallax';
45
export ParallaxProvider from './components/ParallaxProvider';
56
export ParallaxBanner from './components/ParallaxBanner';
7+
export ParallaxContext from './helpers/ParallaxContext';

0 commit comments

Comments
 (0)