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
Fix case of absolutely positioned elements in a container
- added getRef so consumers can specify which element is used for calculations
- necessary for absolutely positioned elements in a container
- added deprecation notice for passing elements as children
- added some new test cases
- updated README and examples
console.log('Element is now %s', isVisible ?'visible':'hidden');
42
+
functiononChange(isVisible) {
43
+
console.log("Element is now %s", isVisible ?"visible":"hidden");
47
44
}
48
45
49
-
functionMyComponent(props) {
46
+
functionMyComponent(props) {
50
47
return (
51
48
<VisibilitySensor onChange={onChange}>
52
49
<div>...content goes here...</div>
@@ -55,25 +52,43 @@ function MyComponent (props) {
55
52
}
56
53
```
57
54
55
+
### Child Function syntax
56
+
58
57
You can also pass a child function, which can be convenient if you don't need to store the visibility anywhere:
59
58
60
59
```js
61
-
functionMyComponent(props) {
60
+
functionMyComponent(props) {
62
61
return (
63
62
<VisibilitySensor>
64
-
{({isVisible}) =>
65
-
<div>I am {isVisible ?'visible':'invisible'}</div>
66
-
}
63
+
{({isVisible, visibilityRect, getRef }) => (
64
+
<div ref={getRef()}>I am {isVisible ?"visible":"invisible"}</div>
65
+
)}
67
66
</VisibilitySensor>
68
67
);
69
68
}
70
69
```
71
70
72
-
Props
73
-
----
71
+
The child function must return an element. The 3 arguments that you can access from the child function are:
72
+
73
+
-`isVisible`: Boolean
74
+
-`getRef`: a Function allowing you to specify which element should be used for calculating visibility. This is useful when you have something absolutely-positioned. Using `getRef` is optional, and if it is not called a wrapper `<div>` will be created with the ref.
75
+
-`visibilityRect`: an Object indicating which sides of the element are visible, in the shape of:
76
+
77
+
```
78
+
{
79
+
top: Boolean,
80
+
bottom: Boolean,
81
+
left: Boolean,
82
+
right: Boolean
83
+
}
84
+
```
85
+
86
+
---
87
+
88
+
## Props
74
89
75
90
-`onChange`: callback for whenever the element changes from being within the window viewport or not. Function is called with 1 argument `(isVisible: boolean)`
76
-
-`active`: (default `true`) boolean flag for enabling / disabling the sensor. When `active !== true` the sensor will not fire the `onChange` callback.
91
+
-`active`: (default `true`) boolean flag for enabling / disabling the sensor. When `active !== true` the sensor will not fire the `onChange` callback.
77
92
-`partialVisibility`: (default `false`) consider element visible if only part of it is visible. Also possible values are - 'top', 'right', 'bottom', 'left' - in case it's needed to detect when one of these become visible explicitly.
78
93
-`offset`: (default `{}`) with offset you can define amount of px from one side when the visibility should already change. So in example setting `offset={{top:10}}` means that the visibility changes hidden when there is less than 10px to top of the viewport. Offset works along with `partialVisibility`
79
94
-`minTopValue`: (default `0`) consider element visible if only part of it is visible and a minimum amount of pixels could be set, so if at least 100px are in viewport, we mark element as visible.
@@ -87,16 +102,14 @@ Props
87
102
-`resizeThrottle`: (default: `-1`) by specifying a value > -1, you are enabling throttle instead of the delay to trigger checks on resize event. Throttle supercedes delay.
88
103
-`containment`: (optional) element to use as a viewport when checking visibility. Default behaviour is to use the browser window as viewport.
89
104
-`delayedCall`: (default `false`) if is set to true, wont execute on page load ( prevents react apps triggering elements as visible before styles are loaded )
90
-
-`children`: can be a React element or a function. If you provide a function, it will be called with 1 argument `{isVisible: ?boolean, visibilityRect: Object}`
105
+
-`children`: can be a React element or a function. If you provide a function, it will be called with 1 argument `{isVisible: ?boolean, visibilityRect: Object}`
91
106
92
107
It's possible to use both `intervalCheck` and `scrollCheck` together. This means you can detect most visibility changes quickly with `scrollCheck`, and an `intervalCheck` with a higher `intervalDelay` will act as a fallback for other visibility events, such as resize of a container.
93
108
94
-
Thanks
95
-
----
109
+
## Thanks
96
110
97
111
Special thanks to [contributors](https://github.com/joshwnj/react-visibility-sensor/graphs/contributors)
0 commit comments