Skip to content

Commit

Permalink
add support state classes for parent element yowainwright#187
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahim Rahhal authored and Ibrahim Rahhal committed Feb 26, 2021
1 parent 88e4c30 commit d4ca148
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/stickybits.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,11 @@ class Stickybits {
const pv = p.positionVal
const se = p.scrollEl
const sticky = p.stickyClass
const stickyParent = p.stickyParentClass
const stickyChange = p.stickyChangeClass
const stickyChangeParent = p.stickyChangeParentClass
const stuck = p.stuckClass
const stuckParent = p.stuckParentClass
const vp = p.verticalPosition
const isTop = vp !== 'bottom'
const aS = p.applyStyle
Expand Down Expand Up @@ -350,7 +353,8 @@ class Stickybits {
// Only apply new styles if the state has changed
if (state === it.state && stateChange === it.stateChange) return
rAF(() => {
const stateStyles = {
const stateStyle = { };
stateStyle.element = {
sticky: {
styles: {
position: pv,
Expand Down Expand Up @@ -382,19 +386,37 @@ class Stickybits {
classes: { [stuck]: true },
},
}
stateStyle.parent = {
sticky: {
classes: { [stickyParent]: true }
},
default: {
classes: { }
},
stuck: {
classes: { [stuckParent]: true }
}
}

if (pv === 'fixed') {
stateStyles.default.styles.position = ''
stateStyles.element.default.styles.position = ''
}

const style = stateStyles[it.state]
const style = stateStyles.element[it.state]
const styleParent = stateStyles.parent[it.state]
style.classes = {
[stuck]: !!style.classes[stuck],
[sticky]: !!style.classes[sticky],
[stickyChange]: isStickyChange,
}
styleParent.classes = {
[stuckParent]: !!styleParent.classes[stuckParent],
[stickyParent]: !!styleParent.classes[stickyParent],
[stickyChangeParent]: isStickyChange,
}

aS(style, item)
aS(styleParent, { el: item.el.parentElement })
})
}

Expand Down

0 comments on commit d4ca148

Please sign in to comment.