Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pin property #205

Merged
merged 1 commit into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class Headroom extends Component {
children: PropTypes.any.isRequired,
disableInlineStyles: PropTypes.bool,
disable: PropTypes.bool,
pin: PropTypes.bool,
upTolerance: PropTypes.number,
downTolerance: PropTypes.number,
onPin: PropTypes.func,
Expand All @@ -30,6 +31,7 @@ export default class Headroom extends Component {
parent: () => window,
disableInlineStyles: false,
disable: false,
pin: false,
upTolerance: 5,
downTolerance: 0,
onPin: noop,
Expand Down Expand Up @@ -149,6 +151,10 @@ export default class Headroom extends Component {
)
}
}

if (prevProps.pin !== this.props.pin) {
this.handleScroll()
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -343,6 +349,7 @@ export default class Headroom extends Component {
delete divProps.onUnfix
delete divProps.disableInlineStyles
delete divProps.disable
delete divProps.pin
delete divProps.parent
delete divProps.children
delete divProps.upTolerance
Expand Down
7 changes: 7 additions & 0 deletions src/shouldUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export default function (
scrollDirection,
distanceScrolled,
}
// We're pinned
} else if (props.pin) {
return {
action: state.state !== 'pinned' ? 'pin' : 'none',
scrollDirection,
distanceScrolled,
}
// We're at the top and not fixed yet.
} else if (currentScrollY <= props.pinStart && state.state !== 'unfixed') {
return {
Expand Down
21 changes: 21 additions & 0 deletions test/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('shouldUpdate', () => {
propDefaults = {
disableInlineStyles: false,
disable: false,
pin: false,
upTolerance: 0,
downTolerance: 0,
offset: 0,
Expand Down Expand Up @@ -217,4 +218,24 @@ describe('shouldUpdate', () => {
const result = shouldUpdate(100, 110, propDefaults, state)
expect(result.action).to.equal('unpin-snap')
})

it("should return an action of 'pin' if props.pin is set and not pinned, yet", () => {
const state = {
height: 100,
state: 'something',
}

const result = shouldUpdate(100, 110, { ...propDefaults, pin: true }, state)
expect(result.action).to.equal('pin')
})

it("should return an action of 'none' if props.pin is set and already pinned", () => {
const state = {
height: 100,
state: 'pinned',
}

const result = shouldUpdate(100, 110, { ...propDefaults, pin: true }, state)
expect(result.action).to.equal('none')
})
})
1 change: 1 addition & 0 deletions www/pages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Another option is to use CSS. The component has a `headroom` class as well as a
* `onUnfix` — callback called when header position is no longer fixed
* `upTolerance` — scroll tolerance in px when scrolling up before component is pinned
* `downTolerance` — scroll tolerance in px when scrolling down before component is pinned
* `pin` — pins the header so that it is always visible
* `disable` — disable pinning and unpinning
* `wrapperStyle` — pass styles for the wrapper div (this maintains the components vertical space at the top of the page).
* `parent` — provide a custom 'parent' element for scroll events. `parent` should be a function which resolves to the desired element.
Expand Down