Gathering detailed insights and metrics for react-sticky
Gathering detailed insights and metrics for react-sticky
Gathering detailed insights and metrics for react-sticky
Gathering detailed insights and metrics for react-sticky
npm install react-sticky
87.8
Supply Chain
94
Quality
75.3
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,641 Stars
368 Commits
385 Forks
20 Watching
26 Branches
30 Contributors
Updated on 29 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-34.4%
8,641
Compared to previous day
Last week
-13.3%
61,403
Compared to previous week
Last month
-7.3%
287,599
Compared to previous month
Last year
-13.8%
3,929,306
Compared to previous year
2
Make your React components sticky!
The 6.0.3 release is the last release maintained. This means we will not be considering any PR's and/or responding to any issues until a new maintainer is identified. It is highly recommended that you begin transitioning to another sticky library to ensure better support and sustainability. This is obviously less than ideal - sorry for any inconvenience!
There's a CSS alternative to react-sticky
: the position: sticky
feature. However it currently does not have full browser support, specifically a lack of IE11 support and some bugs with table elements. Before using react-sticky
, check to see if the browser support and restrictions prevent you from using position: sticky
, as CSS will always be faster and more durable than a JS implementation.
1position: -webkit-sticky; 2position: sticky; 3top: 0;
1npm install react-sticky
The goal of react-sticky
is make it easier for developers to build UIs that have sticky elements. Some examples include a sticky navbar, or a two-column layout where the left side sticks while the right side scrolls.
react-sticky
works by calculating the position of a <Sticky>
component relative to a <StickyContainer>
component. If it would be outside the viewport, the styles required to affix it to the top of the screen are passed as an argument to a render callback, a function passed as a child.
1<StickyContainer> 2 <Sticky>{({ style }) => <h1 style={style}>Sticky element</h1>}</Sticky> 3</StickyContainer>
The majority of use cases will only need the style to pass to the DOM, but some other properties are passed for advanced use cases:
style
(object) - modifiable style attributes to optionally be passed to the element returned by this function. For many uses, this will be the only attribute needed.isSticky
(boolean) - is the element sticky as a result of the current event?wasSticky
(boolean) - was the element sticky prior to the current event?distanceFromTop
(number) - number of pixels from the top of the Sticky
to the nearest StickyContainer
's topdistanceFromBottom
(number) - number of pixels from the bottom of the Sticky
to the nearest StickyContainer
's bottomcalculatedHeight
(number) - height of the element returned by this functionThe Sticky
's child function will be called when events occur in the parent StickyContainer
,
and will serve as the callback to apply your own logic and customizations, with sane style
attributes
to get you up and running quickly.
Here's an example of all of those pieces together:
app.js
1import React from 'react'; 2import { StickyContainer, Sticky } from 'react-sticky'; 3// ... 4 5class App extends React.Component { 6 render() { 7 return ( 8 <StickyContainer> 9 {/* Other elements can be in between `StickyContainer` and `Sticky`, 10 but certain styles can break the positioning logic used. */} 11 <Sticky> 12 {({ 13 style, 14 15 // the following are also available but unused in this example 16 isSticky, 17 wasSticky, 18 distanceFromTop, 19 distanceFromBottom, 20 calculatedHeight 21 }) => ( 22 <header style={style}> 23 {/* ... */} 24 </header> 25 )} 26 </Sticky> 27 {/* ... */} 28 </StickyContainer> 29 ); 30 }, 31};
When the "stickiness" becomes activated, the arguments to the sticky function are modified. Similarly, when deactivated, the arguments will update accordingly.
<StickyContainer />
Props<StickyContainer />
supports all valid <div />
props.
<Sticky />
PropsSet relative
to true
if the <Sticky />
element will be rendered within
an overflowing <StickyContainer />
(e.g. style={{ overflowY: 'auto' }}
) and you want
the <Sticky />
behavior to react to events only within that container.
When in relative
mode, window
events will not trigger sticky state changes. Only scrolling
within the nearest StickyContainer
can trigger sticky state changes.
Sticky state will be triggered when the top of the element is topOffset
pixels from the top of the closest <StickyContainer />
. Positive numbers give the impression of a lazy sticky state, whereas negative numbers are more eager in their attachment.
app.js
1<StickyContainer> 2 ... 3 <Sticky topOffset={80}> 4 { props => (...) } 5 </Sticky> 6 ... 7</StickyContainer>
The above would result in an element that becomes sticky once its top is greater than or equal to 80px away from the top of the <StickyContainer />
.
Sticky state will be triggered when the bottom of the element is bottomOffset
pixels from the bottom of the closest <StickyContainer />
.
app.js
1<StickyContainer> 2 ... 3 <Sticky bottomOffset={80}> 4 { props => (...) } 5 </Sticky> 6 ... 7</StickyContainer>
The above would result in an element that ceases to be sticky once its bottom is 80px away from the bottom of the <StickyContainer />
.
Set disableCompensation
to true
if you do not want your <Sticky />
to apply padding to
a hidden placeholder <div />
to correct "jumpiness" as attachment changes from position:fixed
and back.
app.js
1<StickyContainer> 2 ... 3 <Sticky disableCompensation> 4 { props => (...) } 5 </Sticky> 6 ... 7</StickyContainer>
When disableHardwareAcceleration
is set to true
, the <Sticky />
element will not use hardware acceleration (e.g. transform: translateZ(0)
). This setting is not recommended as it negatively impacts
the mobile experience, and can usually be avoided by improving the structure of your DOM.
app.js
1<StickyContainer> 2 ... 3 <Sticky disableHardwareAcceleration> 4 { props => (...) } 5 </Sticky> 6 ... 7</StickyContainer>
React.Fragments does not correspond to an actual DOM node, so react-sticky
can not calculate its position. Because of this, React.Fragments is not supported.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/12 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
109 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More