File tree Expand file tree Collapse file tree 2 files changed +71
-11
lines changed
Expand file tree Collapse file tree 2 files changed +71
-11
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import { storiesOf } from '@storybook/react'
44import { action } from '@storybook/addon-actions'
55import Observer from '../src/index'
66import ScrollWrapper from './ScrollWrapper'
7+ import RootComponent from './Root'
78
89const Header = props =>
910 < div
@@ -60,17 +61,41 @@ storiesOf('Intersection Observer', module)
6061 </ Observer >
6162 </ ScrollWrapper > ,
6263 )
63- . add ( 'With rootMargin' , ( ) =>
64- < ScrollWrapper >
65- < Observer
66- threshold = { 0 }
67- rootMargin = "100px"
68- onChange = { action ( 'Child Observer inview' ) }
69- >
70- { inView =>
71- < Header > { `Header is fully inside the viewport: ${ inView } ` } </ Header > }
72- </ Observer >
73- </ ScrollWrapper > ,
64+ . add ( 'With root' , ( ) =>
65+ < RootComponent >
66+ { node =>
67+ < ScrollWrapper >
68+ < Observer
69+ threshold = { 0 }
70+ root = { node }
71+ rootMargin = "64px"
72+ rootId = "window1"
73+ onChange = { action ( 'Child Observer inview' ) }
74+ >
75+ { inView =>
76+ < Header
77+ > { `Header is inside the root viewport: ${ inView } ` } </ Header > }
78+ </ Observer >
79+ </ ScrollWrapper > }
80+ </ RootComponent > ,
81+ )
82+ . add ( 'With root and rootMargin' , ( ) =>
83+ < RootComponent style = { { padding : 64 } } >
84+ { node =>
85+ < ScrollWrapper >
86+ < Observer
87+ threshold = { 0 }
88+ root = { node }
89+ rootMargin = "64px"
90+ rootId = "window2"
91+ onChange = { action ( 'Child Observer inview' ) }
92+ >
93+ { inView =>
94+ < Header
95+ > { `Header is inside the root viewport: ${ inView } ` } </ Header > }
96+ </ Observer >
97+ </ ScrollWrapper > }
98+ </ RootComponent > ,
7499 )
75100 . add ( 'Trigger once' , ( ) =>
76101 < ScrollWrapper >
Original file line number Diff line number Diff line change 1+ import React , { PureComponent } from 'react'
2+
3+ const style = {
4+ margin : '64px' ,
5+ backgroundColor : 'slategrey' ,
6+ overflowY : 'scroll' ,
7+ position : 'absolute' ,
8+ top : '0' ,
9+ left : '0' ,
10+ right : '0' ,
11+ bottom : '0' ,
12+ }
13+
14+ class RootComponent extends PureComponent {
15+ state = {
16+ node : null ,
17+ }
18+
19+ handleNode = node => {
20+ this . rootNode = node
21+ this . setState ( {
22+ node,
23+ } )
24+ }
25+
26+ render ( ) {
27+ return (
28+ < div ref = { this . handleNode } style = { { ...style , ...this . props . style } } >
29+ { this . state . node ? this . props . children ( this . state . node ) : null }
30+ </ div >
31+ )
32+ }
33+ }
34+
35+ export default RootComponent
You can’t perform that action at this time.
0 commit comments