Skip to content

Commit 08e8eca

Browse files
committed
Improve the example
1 parent 55b2437 commit 08e8eca

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

examples/cra/src/styles.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,32 @@ injectGlobal`
1111
}
1212
`
1313

14+
export const button = css`
15+
height: 30px;
16+
width: 100%;
17+
`
18+
19+
export const app = css`
20+
display: flex;
21+
flex-direction: column;
22+
align-items: center;
23+
width: 100%;
24+
& > button {
25+
position: fixed;
26+
margin-top: 51px;
27+
}
28+
`
29+
1430
export const nav = css`
1531
display: flex;
1632
height: 50px;
1733
padding: 10px 0;
1834
justify-content: center;
1935
width: 100%;
2036
box-sizing: border-box;
37+
position: fixed;
2138
`
39+
2240
export const container = css`
2341
font-family: sans-serif;
2442
text-align: center;
@@ -30,6 +48,7 @@ export const container = css`
3048
justify-content: center;
3149
overflow: auto;
3250
width: 100%;
51+
margin-top: 94px;
3352
`
3453

3554
export const growContainer = css`
@@ -78,8 +97,3 @@ export const altVisible = css`
7897
background: orange;
7998
color: black;
8099
`
81-
82-
export const button = css`
83-
height: 30px;
84-
width: 100%;
85-
`

examples/cra/src/viewportAnotherElement.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { cx } from 'emotion'
33
import useIsInViewport from './use-is-in-viewport'
44
import {
55
altVisible,
6+
app,
67
box,
78
button,
89
container,
@@ -16,7 +17,7 @@ export function SimpleElement() {
1617
const [hidden, toggleHide] = React.useState(false)
1718

1819
return (
19-
<>
20+
<div className={app}>
2021
<button
2122
className={button}
2223
onClick={() => toggleHide(h => !h)}
@@ -37,7 +38,7 @@ export function SimpleElement() {
3738
<p>{isInViewport ? 'In viewport' : 'Out of viewport'}</p>
3839
</div>
3940
</div>
40-
</>
41+
</div>
4142
)
4243
}
4344

@@ -58,7 +59,7 @@ export const RefForwardingElement = React.forwardRef(function RefForwardingEleme
5859
const [hidden, toggleHide] = React.useState(false)
5960

6061
return (
61-
<>
62+
<div className={app}>
6263
<button
6364
className={button}
6465
onClick={() => toggleHide(h => !h)}
@@ -76,7 +77,9 @@ export const RefForwardingElement = React.forwardRef(function RefForwardingEleme
7677
[altVisible]: isFirstInViewport
7778
})}
7879
>
79-
<p>{isFirstInViewport ? 'First child in viewport' : 'First child out of viewport'}</p>
80+
<p>
81+
{isFirstInViewport ? 'First child >= 75% in viewport' : 'First child out of viewport'}
82+
</p>
8083
</div>
8184
<div
8285
ref={secondChildRef}
@@ -87,9 +90,13 @@ export const RefForwardingElement = React.forwardRef(function RefForwardingEleme
8790
[altVisible]: isSecondInViewport
8891
})}
8992
>
90-
<p>{isSecondInViewport ? 'Second child in viewport' : 'Second child out of viewport'}</p>
93+
<p>
94+
{isSecondInViewport
95+
? 'Second child >= 75% in viewport'
96+
: 'Second child out of viewport'}
97+
</p>
9198
</div>
9299
</div>
93-
</>
100+
</div>
94101
)
95102
})

examples/cra/src/viewportParentDocument.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react'
22
import { cx } from 'emotion'
33
import useIsInViewport from './use-is-in-viewport'
4-
import { box, button, inWindowViewport, outsideWindowViewport, visible } from './styles'
4+
import { app, box, button, inWindowViewport, outsideWindowViewport, visible } from './styles'
55

66
export function SimpleElement() {
77
const [isInViewport, childRef] = useIsInViewport()
88
const [hidden, toggleHide] = React.useState(false)
99

1010
return (
11-
<>
11+
<div className={app}>
1212
<button
1313
className={button}
1414
onClick={() => toggleHide(h => !h)}
@@ -27,7 +27,7 @@ export function SimpleElement() {
2727
>
2828
<p>{isInViewport ? 'In viewport' : 'Out of viewport'}</p>
2929
</div>
30-
</>
30+
</div>
3131
)
3232
}
3333

@@ -51,7 +51,7 @@ export const RefForwardingElement = React.forwardRef(function RefForwardingEleme
5151
const [hidden, toggleHide] = React.useState(false)
5252

5353
return (
54-
<>
54+
<div className={app}>
5555
<button
5656
className={button}
5757
onClick={() => toggleHide(h => !h)}
@@ -70,6 +70,6 @@ export const RefForwardingElement = React.forwardRef(function RefForwardingEleme
7070
>
7171
<p>{isInViewport ? 'In viewport' : 'Out of viewport'}</p>
7272
</div>
73-
</>
73+
</div>
7474
)
7575
})

0 commit comments

Comments
 (0)