You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-13Lines changed: 42 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ yarn add react-scroll-parallax
29
29
30
30
## Usage
31
31
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:
**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).
68
68
69
-
## Parallax Component Props
69
+
## \<Parallax> Props
70
70
71
71
The following are all props that can be passed to the React `<Parallax />` component:
72
72
@@ -81,38 +81,46 @@ The following are all props that can be passed to the React `<Parallax />` compo
81
81
|**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.
82
82
|**tag** |`String` |`div` |Optionally pass an element tag name to be applied to the outer most parallax element.
83
83
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
+
constAppContainer= () => (
90
+
<ParallaxProvider>
91
+
<Router>
92
+
<App />
93
+
</Router>
94
+
</ParallaxProvider>
95
+
);
96
+
```
97
+
98
+
### Parallax Controller Context
85
99
86
100
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:
87
101
88
102
```jsx
89
103
classFooextendsComponent {
90
-
91
104
static contextTypes = {
92
105
parallaxController:PropTypes.object.isRequired,
93
106
};
94
107
95
108
doSomething() {
96
109
// do stuff with this.context.parallaxController
97
-
}
98
-
99
-
...
110
+
}
111
+
}
100
112
```
101
113
102
114
or for stateless functional components like:
103
115
104
116
```jsx
105
117
constBar= (props, context) => (
106
-
107
118
// do stuff with context.parallaxController
108
-
109
-
...
110
119
);
111
120
112
121
Bar.contextTypes= {
113
122
parallaxController:PropTypes.object.isRequired,
114
123
};
115
-
116
124
```
117
125
118
126
### Available Methods
@@ -127,6 +135,27 @@ Updates all cached attributes for parallax elements then updates their positions
127
135
128
136
Removes window scroll and resize listeners then resets all styles applied to parallax elements.
129
137
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
+
classImageextendsComponent {
144
+
static contextTypes = {
145
+
parallaxController:PropTypes.object.isRequired,
146
+
};
147
+
148
+
handleLoad= () => {
149
+
// updates cached values after image dimensions have loaded
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