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(Sticky): add active prop #2053

Merged
merged 1 commit into from
Sep 18, 2017
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
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