Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import shallowequal from 'shallowequal'
import raf from 'raf'
import shouldUpdate from './shouldUpdate'

export { default as useHeadroom } from "./useHeadroom";
const noop = () => {}

export default class Headroom extends Component {
Expand Down Expand Up @@ -345,4 +346,4 @@ export default class Headroom extends Component {
</div>
)
}
}
}
2 changes: 1 addition & 1 deletion src/shouldUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ export default function (
distanceScrolled,
}
}
}
}
28 changes: 28 additions & 0 deletions src/useHeadroom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";

export default ({ onPin, onUnpin }) => {
const [scroll, setScroll] = React.useState(0);

// Tracking scroll value
React.useEffect(() => {
const handleScroll = () => setScroll(window.scrollY);
window.addEventListener("scroll", handleScroll);
// Cleanup function
return () => window.removeEventListener("scroll", handleScroll);
}, []);

const scrollRef = React.useRef({ scroll: scroll });

// Handling onPin and onUnpin callbacks
React.useEffect(() => {
if (onPin && scrollRef.current.scroll <= scroll) onPin();
if (onUnpin && scrollRef.current.scroll > scroll) onUnpin();
}, [scrollRef.current.scroll <= scroll]);

// Handling the backward scroll behavior
React.useEffect(() => {
scrollRef.current.scroll = scroll;
}, [scroll]);

return scrollRef.current.scroll > scroll || scroll === 0;
};
2 changes: 1 addition & 1 deletion test/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,4 @@ describe('shouldUpdate', () => {
const result = shouldUpdate(100, 110, propDefaults, state)
expect(result.action).to.equal('unpin-snap')
})
})
})