Skip to content

Commit

Permalink
feat(Sticky): add active prop (#2053)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored and levithomason committed Sep 18, 2017
1 parent aa93166 commit 8f88da7
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 194 deletions.
3 changes: 3 additions & 0 deletions src/modules/Sticky/Sticky.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export interface StickyProps {
/** An element type to render as (string or function). */
as?: any;

/** A Sticky can be active. */
active?: boolean;

/** Offset in pixels from the bottom of the screen when fixing element to viewport. */
bottomOffset?: number;

Expand Down
49 changes: 41 additions & 8 deletions src/modules/Sticky/Sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
customPropTypes,
getElementType,
getUnhandledProps,
META,
isBrowser,
META,
} from '../../lib'

/**
Expand All @@ -18,6 +18,9 @@ export default class Sticky extends Component {
/** An element type to render as (string or function). */
as: customPropTypes.as,

/** A Sticky can be active. */
active: PropTypes.bool,

/** Offset in pixels from the bottom of the screen when fixing element to viewport. */
bottomOffset: PropTypes.number,

Expand Down Expand Up @@ -73,6 +76,7 @@ export default class Sticky extends Component {
}

static defaultProps = {
active: true,
bottomOffset: 0,
offset: 0,
scrollContext: isBrowser ? window : null,
Expand All @@ -89,16 +93,46 @@ export default class Sticky extends Component {

componentDidMount() {
if (!isBrowser) return
const { active } = this.props

const { scrollContext } = this.props
this.handleUpdate()
scrollContext.addEventListener('scroll', this.handleUpdate)
if (active) {
this.handleUpdate()
this.addListener()
}
}

componentWillReceiveProps({ active: next }) {
const { active: current } = this.props

if (current === next) return
if (next) {
this.handleUpdate()
this.addListener()
return
}
this.removeListener()
}

componentWillUnmount() {
if (!isBrowser) return
const { active } = this.props

if (active) this.removeListener()
}

// ----------------------------------------
// Events
// ----------------------------------------

addListener = () => {
const { scrollContext } = this.props

scrollContext.addEventListener('scroll', this.handleUpdate)
}

removeListener = () => {
const { scrollContext } = this.props

scrollContext.removeEventListener('scroll', this.handleUpdate)
}

Expand All @@ -110,7 +144,6 @@ export default class Sticky extends Component {
const { pushing } = this.state

this.ticking = false

this.assignRects()

if (pushing) {
Expand Down Expand Up @@ -163,9 +196,6 @@ export default class Sticky extends Component {
}
}

// Return true if the height of the component is higher than the window
isOversized = () => this.stickyRect.height > window.innerHeight

// Return true when the component reached the bottom of the context
didReachContextBottom = () => {
const { offset } = this.props
Expand All @@ -186,6 +216,9 @@ export default class Sticky extends Component {
return (this.contextRect.bottom + bottomOffset) > window.innerHeight
}

// Return true if the height of the component is higher than the window
isOversized = () => this.stickyRect.height > window.innerHeight

// ----------------------------------------
// Stick helpers
// ----------------------------------------
Expand Down
Loading

0 comments on commit 8f88da7

Please sign in to comment.