Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 5be0035

Browse files
authored
Merge pull request #37 from makotot/offset
Offset option
2 parents bd37cfe + 3b664f4 commit 5be0035

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ HTML tag for Scrollspy component if you want to use other than `ul` [optional].
6060

6161
Style attribute to be passed to the generated <ul/> element [optional].
6262

63+
### `offset={ Number }`
64+
65+
Offset value that adjusts to determine the elements are in the viewport.
66+
6367
## Development
6468

6569
```sh

src/js/app.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const App = React.createClass({
1111
minHeight: '600px',
1212
}
1313

14+
const version = require('../../package.json').version
15+
1416
return (
1517
<div className="o-wrapper">
1618

@@ -19,7 +21,13 @@ const App = React.createClass({
1921
<h2 className="c-side-nav__heading c-heading-4 c-heading-4--upper u-font-italic">react<br />scrollspy</h2>
2022
</div>
2123
<nav className="c-side-nav__body">
22-
<Scrollspy items={ ['section-1', 'section-2', 'section-3'] } currentClassName="is-current" className="c-side-nav__list nav-list" style={ {fontWeight: 300} }>
24+
<Scrollspy
25+
items={ ['section-1', 'section-2', 'section-3'] }
26+
currentClassName="is-current"
27+
className="c-side-nav__list nav-list"
28+
style={ {fontWeight: 300} }
29+
offset={ -20 }
30+
>
2331
<li className="c-side-nav__item"><a href="#section-1" className="c-side-nav__link">Getteing Started</a></li>
2432
<li className="c-side-nav__item"><a href="#section-2" className="c-side-nav__link">Example</a></li>
2533
<li className="c-side-nav__item"><a href="#section-3" className="c-side-nav__link">Props</a></li>
@@ -37,7 +45,7 @@ const App = React.createClass({
3745
<span className="o-hero__sub-heading c-lead">Scrollspy Component</span>
3846
</li>
3947
<li className="o-inline-list__item">
40-
<span className="c-tag">v2.3.1</span>
48+
<span className="c-tag">v{ version }</span>
4149
</li>
4250
</ul>
4351
</div>
@@ -114,6 +122,10 @@ const App = React.createClass({
114122
<td className="c-table__data">style</td>
115123
<td className="c-table__data">Style attribute to be passed to the generated <code>{'<ul/>'}</code> element [optional].</td>
116124
</tr>
125+
<tr>
126+
<td className="c-table__data">offset</td>
127+
<td className="c-table__data">Offset value that adjusts to determine the elements are in the viewport [optional].</td>
128+
</tr>
117129
</tbody>
118130
</table>
119131
</div>

src/js/lib/Scrollspy.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ export class Scrollspy extends React.Component {
88
currentClassName: React.PropTypes.string.isRequired,
99
style: React.PropTypes.object,
1010
componentTag: React.PropTypes.string,
11+
offset: React.PropTypes.number,
1112
}
1213
}
1314

1415
static get defaultProps () {
1516
return {
1617
items: [],
1718
currentClassName: '',
19+
style: {},
20+
componentTag: 'ul',
21+
offset: 0,
1822
}
1923
}
2024

@@ -90,7 +94,7 @@ export class Scrollspy extends React.Component {
9094
const doc = document
9195
const scrollTop = doc.documentElement.scrollTop || doc.body.scrollTop
9296
const scrollBottom = scrollTop + winH
93-
const elTop = rect.top + scrollTop
97+
const elTop = rect.top + scrollTop + this.props.offset
9498
const elBottom = elTop + el.offsetHeight
9599

96100
return (elTop < scrollBottom) && (elBottom > scrollTop)
@@ -146,7 +150,7 @@ export class Scrollspy extends React.Component {
146150
}
147151

148152
render () {
149-
const Tag = this.props.componentTag || 'ul'
153+
const Tag = this.props.componentTag
150154
let counter = 0
151155
const items = React.Children.map(this.props.children, (child, idx) => {
152156
if (!child) {

0 commit comments

Comments
 (0)