Skip to content

Commit de0a826

Browse files
committed
Merge pull request #23 from Lullabot/22-active-class
Adds activeClass property
2 parents 3dd6c73 + a7fd926 commit de0a826

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ var Sticky = require('react-stickynode');
5151
- `enabled {Boolean}` - The switch to enable or disable Sticky (true by default).
5252
- `top {Number/String}` - The offset from the top of window where the top of the element will be when sticky state is triggered (0 by default). If it is a selector to a target (via `querySelector()`), the offset will be the height of the target.
5353
- `bottomBoundary {Number/String}` - The offset from the top of document which release state will be triggered when the bottom of the element reaches at. If it is a selector to a target (via `querySelector()`), the offset will be the bottom of the target.
54-
- `enableTransforms {Boolean}` - Enable the use of CSS3 transforms (true by default)
54+
- `enableTransforms {Boolean}` - Enable the use of CSS3 transforms (true by default).
55+
- `activeClass {String}` - Class name to be applied to the element when the sticky state is active (`active` by default).
5556

5657
## Install & Development
5758

src/Sticky.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class Sticky extends Component {
315315
}
316316

317317
return (
318-
<div ref='outer' className={classNames('sticky-outer-wrapper', self.props.className)} style={outerStyle}>
318+
<div ref='outer' className={classNames('sticky-outer-wrapper', self.props.className, {[self.props.activeClass]: self.state.status === STATUS_FIXED})} style={outerStyle}>
319319
<div ref='inner' className='sticky-inner-wrapper' style={innerStyle}>
320320
{self.props.children}
321321
</div>
@@ -328,7 +328,8 @@ Sticky.defaultProps = {
328328
enabled: true,
329329
top: 0,
330330
bottomBoundary: 0,
331-
enableTransforms: true
331+
enableTransforms: true,
332+
activeClass: 'active'
332333
};
333334

334335
/**
@@ -349,7 +350,8 @@ Sticky.propTypes = {
349350
PropTypes.string,
350351
PropTypes.number
351352
]),
352-
enableTransforms: PropTypes.bool
353+
enableTransforms: PropTypes.bool,
354+
activeClass: PropTypes.string
353355
};
354356

355357
module.exports = Sticky;

tests/unit/Sticky-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ describe('Sticky', function () {
137137
// Scroll down to 10px, and Sticky should fix
138138
window.scrollTo(0, 10);
139139
shouldBeFixedAt(inner, 0);
140+
expect(outer.className).to.contain('active');
140141

141142
// Scroll up to 0px, and Sticky should reset
142143
window.scrollTo(0, 0);
143144
shouldBeReset(inner);
145+
expect(outer.className).to.not.contain('active');
144146
});
145147

146148
it('should work as expected (long Sticky)', function () {
@@ -158,26 +160,32 @@ describe('Sticky', function () {
158160
// Scroll down to 10px, and Sticky should stay as it was
159161
window.scrollTo(0, 10);
160162
shouldBeReleasedAt(inner, 0);
163+
expect(outer.className).to.not.contain('active');
161164

162165
// Scroll down to 1500px, and Sticky should fix to the bottom
163166
window.scrollTo(0, 1500);
164167
shouldBeFixedAt(inner, -432);
168+
expect(outer.className).to.contain('active');
165169

166170
// Scroll up to 1300px, and Sticky should release
167171
window.scrollTo(0, 1300);
168172
shouldBeReleasedAt(inner, 1068);
173+
expect(outer.className).to.not.contain('active');
169174

170175
// Scroll down to 1350px, and Sticky should release as it was
171176
window.scrollTo(0, 1350);
172177
shouldBeReleasedAt(inner, 1068);
178+
expect(outer.className).to.not.contain('active');
173179

174180
// Scroll up to 10px, and Sticky should fix
175181
window.scrollTo(0, 10);
176182
shouldBeFixedAt(inner, 0);
183+
expect(outer.className).to.contain('active');
177184

178185
// Scroll down to 20px, and Sticky should release
179186
window.scrollTo(0, 20);
180187
shouldBeReleasedAt(inner, 10);
188+
expect(outer.className).to.not.contain('active');
181189
});
182190

183191
it('should work as expected with original postion 20px from top (short Sticky)', function () {
@@ -195,10 +203,12 @@ describe('Sticky', function () {
195203
// Scroll down to 10px, and Sticky should stay
196204
window.scrollTo(0, 10);
197205
shouldBeReset(inner);
206+
expect(outer.className).to.not.contain('active');
198207

199208
// Scroll down to 50px, and Sticky should fix
200209
window.scrollTo(0, 50);
201210
shouldBeFixedAt(inner, 0);
211+
expect(outer.className).to.contain('active');
202212
});
203213

204214
it('should work as expected with original top 20px and 400px bottom boundary (short Sticky)', function () {
@@ -218,14 +228,17 @@ describe('Sticky', function () {
218228
// Scroll down to 10px, and Sticky should stay
219229
window.scrollTo(0, 10);
220230
shouldBeReset(inner);
231+
expect(outer.className).to.not.contain('active');
221232

222233
// Scroll down to 50px, and Sticky should fix
223234
window.scrollTo(0, 50);
224235
shouldBeFixedAt(inner, 0);
236+
expect(outer.className).to.contain('active');
225237

226238
// Scroll down to 150px, and Sticky should release
227239
window.scrollTo(0, 150);
228240
shouldBeReleasedAt(inner, 80);
241+
expect(outer.className).to.not.contain('active');
229242
});
230243

231244
it('should not be sticky if bottom boundary is shorter then its height (short Sticky)', function () {
@@ -244,11 +257,13 @@ describe('Sticky', function () {
244257
// Scroll down to 10px, and Sticky should stay
245258
window.scrollTo(0, 10);
246259
shouldBeReset(inner);
260+
expect(outer.className).to.not.contain('active');
247261

248262
// Micic status was not 0 (STATUS_ORIGINAL), scroll down to 20px, and Sticky should stay
249263
sticky.state.status = 2; // STATUS_FIXED;
250264
window.scrollTo(0, 20);
251265
shouldBeReset(inner);
266+
expect(outer.className).to.not.contain('active');
252267
});
253268

254269
it('should work as expected with selector bottom boundary (short Sticky)', function () {
@@ -268,14 +283,17 @@ describe('Sticky', function () {
268283
// Scroll down to 10px, and Sticky should fix
269284
window.scrollTo(0, 10);
270285
shouldBeFixedAt(inner, 20);
286+
expect(outer.className).to.contain('active');
271287

272288
// Scroll down to 50px, and Sticky should fix
273289
window.scrollTo(0, 50);
274290
shouldBeFixedAt(inner, 20);
291+
expect(outer.className).to.contain('active');
275292

276293
// Scroll down to 150px, and Sticky should release
277294
window.scrollTo(0, 150);
278295
shouldBeReleasedAt(inner, 100);
296+
expect(outer.className).to.not.contain('active');
279297
});
280298

281299
it('should stick to the top when window resizes larger then Sticky (long Sticky)', function () {
@@ -293,8 +311,10 @@ describe('Sticky', function () {
293311
// Scroll down to 10px, and Sticky should fix
294312
window.scrollTo(0, 10);
295313
shouldBeReleasedAt(inner, 0);
314+
expect(outer.className).to.not.contain('active');
296315

297316
window.resizeTo(0, 900);
298317
shouldBeFixedAt(inner, 0);
318+
expect(outer.className).to.contain('active');
299319
});
300320
});

0 commit comments

Comments
 (0)