Skip to content

Commit 7bd055e

Browse files
committed
more clarifications and add image example to readme
1 parent 51438c9 commit 7bd055e

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ yarn add react-scroll-parallax
2929

3030
## Usage
3131

32-
The `<ParallaxProvider />` should wrap the component tree that contains all `<Parallax />` components. This should be a top level component like `<AppContainer />`. The `<ParallaxProvider />` will then provide necessary context to the [`parallaxController`](#parallax-controller-context) for all `<Parallax />` elements. For example:
32+
The [`<ParallaxProvider />`](#parallaxprovider) should wrap the component tree that contains all `<Parallax />` components. This should be a top level component like `<AppContainer />`. For example:
3333

3434
```jsx
3535
import { ParallaxProvider } from 'react-scroll-parallax';
@@ -59,14 +59,14 @@ const Image = () => (
5959
slowerScrollRate
6060
tag="figure"
6161
>
62-
<img src="/image.jpg" />
62+
<Image src="/image.jpg" />
6363
</Parallax>
6464
);
6565
```
6666

67-
**NOTE:** Scroll state and positions of elements on the page are cached for performance reasons. This means that if the page height changes (perhaps from images loading) after `<Parallax />` components are mounted the controller won't properly determine when the elements are in view. To correct this you can call the `parallaxController.update()` method from any child component of the `<ParallaxProvider />` via `context`. More details on how here: [Parallax Controller Context](#parallax-controller-context).
67+
**NOTE:** Scroll state and positions of elements on the page are cached for performance reasons. This means that if the page height changes (most likely from [images loading](#example-usage-of-context)) after `<Parallax />` components are mounted the controller won't properly determine when the elements are in view. To correct this you can call the `parallaxController.update()` method from any child component of the `<ParallaxProvider />` via `context`. More details on how here: [Parallax Controller Context](#parallax-controller-context).
6868

69-
## Parallax Component Props
69+
## \<Parallax> Props
7070

7171
The following are all props that can be passed to the React `<Parallax />` component:
7272

@@ -81,38 +81,46 @@ The following are all props that can be passed to the React `<Parallax />` compo
8181
|**slowerScrollRate** |`Boolean` |`false` |Internally swaps the min/max offset y values of the parallax component to give the appearance of moving faster or slower than the default rate of scroll.
8282
|**tag** |`String` |`div` |Optionally pass an element tag name to be applied to the outer most parallax element.
8383

84-
## Parallax Controller Context
84+
## \<ParallaxProvider>
85+
86+
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:
87+
88+
```jsx
89+
const AppContainer = () => (
90+
<ParallaxProvider>
91+
<Router>
92+
<App />
93+
</Router>
94+
</ParallaxProvider>
95+
);
96+
```
97+
98+
### Parallax Controller Context
8599

86100
Access the Parallax Controller via [React context](https://facebook.github.io/react/docs/context.html) in any components rendered within a `<ParallaxProvider />` by defining the `contextTypes` like so:
87101

88102
```jsx
89103
class Foo extends Component {
90-
91104
static contextTypes = {
92105
parallaxController: PropTypes.object.isRequired,
93106
};
94107

95108
doSomething() {
96109
// do stuff with this.context.parallaxController
97-
}
98-
99-
...
110+
}
111+
}
100112
```
101113

102114
or for stateless functional components like:
103115

104116
```jsx
105117
const Bar = (props, context) => (
106-
107118
// do stuff with context.parallaxController
108-
109-
...
110119
);
111120

112121
Bar.contextTypes = {
113122
parallaxController: PropTypes.object.isRequired,
114123
};
115-
116124
```
117125

118126
### Available Methods
@@ -127,6 +135,27 @@ Updates all cached attributes for parallax elements then updates their positions
127135

128136
Removes window scroll and resize listeners then resets all styles applied to parallax elements.
129137

138+
### Example usage of context
139+
140+
The most common use case that would require access to the controller is dealing with images. Since the controller caches attributes for performance they will need to be updated with the correct values once the image loads. Here's an example of how you could do that with an `<Image />` component:
141+
142+
```jsx
143+
class Image extends Component {
144+
static contextTypes = {
145+
parallaxController: PropTypes.object.isRequired,
146+
};
147+
148+
handleLoad = () => {
149+
// updates cached values after image dimensions have loaded
150+
this.context.parallaxController.update();
151+
};
152+
153+
render() {
154+
return <img src={this.props.src} onLoad={this.handleLoad} />;
155+
}
156+
}
157+
```
158+
130159
## Browser Support
131160

132161
React scroll parallax should support the last two versions of all major browsers and has been tested on desktop Chrome, Firefox, Safari and Edge, as well as the following: iOS 9, iOS 10, Android 4 and IE11. If you encounter any errors for browsers that should be supported please post an issue.

0 commit comments

Comments
 (0)