-
Notifications
You must be signed in to change notification settings - Fork 1
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
#14 Fix placeholder #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset=""> | ||
<title></title> | ||
<style> | ||
html, body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
#container { | ||
display: flex; | ||
} | ||
.box { | ||
width: 300px; | ||
height: 250px; | ||
background-color: #33a; | ||
} | ||
.box:nth-child(n+2) { | ||
margin-top: 30px; | ||
} | ||
.box.box--large { | ||
height: 400px; | ||
background-color: #a3a; | ||
} | ||
main { | ||
flex: 1; | ||
background-color: #bbf; | ||
} | ||
footer { | ||
width: 100%; | ||
height: 100px; | ||
margin: 0; | ||
padding: 0; | ||
background-color: #aaf; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<section id="container"> | ||
<main> | ||
<p>main contents</p> | ||
</main> | ||
<aside> | ||
<div id="js-box00" class="box js-box" style="z-index: 3">BOX0</div> | ||
<div id="js-box01" class="box js-box" style="z-index: 2">BOX1</div> | ||
<div id="js-box02" class="box box--large" style="z-index: 1">BOX02</div> | ||
</aside> | ||
</section> | ||
<footer> | ||
footer | ||
</footer> | ||
<script src="./index.js"></script> | ||
<script> | ||
const { Stuck } = StuckJs; | ||
const stuck = new Stuck([ | ||
{ selector: '#js-box00' }, | ||
{ selector: '#js-box01', placehold: false }, | ||
{ selector: '#js-box02' }, | ||
], { wrapper: '#container'}); | ||
</script> | ||
</body> | ||
</html> |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
/* @flow */ | ||
import Placeholder from './placeholder'; | ||
|
||
type MaybeHTMLElement = HTMLElement|Element|null|void; | ||
type SelectorOrElement = string|HTMLElement; | ||
|
||
export type StickyOptions = { | ||
marginTop?: number, | ||
wrapper?: HTMLElement|string, | ||
placehold?: boolean, | ||
observe?: boolean, | ||
wrapper?: SelectorOrElement, | ||
placehold: boolean, | ||
observe: boolean, | ||
}; | ||
|
||
export default class Sticky { | ||
|
@@ -56,18 +59,19 @@ export default class Sticky { | |
return this.$$wrapper; | ||
} | ||
|
||
set wrapper(value: HTMLElement|string): void { | ||
if (document.body === null) { | ||
throw new Error('[Stuck.js] document.body is not HTMLElement in this environment'); | ||
set wrapper(value: SelectorOrElement): void { | ||
if (!(document.body instanceof HTMLElement)) { | ||
throw new TypeError('[Stuck.js] document.body is not HTMLElement in this environment'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 nice |
||
} | ||
this.$$wrapper = Sticky.normalizeElement(value, document.body); | ||
const parent = ((this.placeholder && this.placeholder.element) || this.element).parentElement; | ||
this.$$wrapper = Sticky.normalizeElement(value, parent, document.body); | ||
this.floor = Sticky.computeAbsoluteFloor(this.$$wrapper); | ||
this.options.wrapper = this.$$wrapper; | ||
} | ||
|
||
constructor( | ||
element: HTMLElement, | ||
options: StickyOptions = {}, | ||
options: StickyOptions = { placehold: true, observe: true }, | ||
activate: boolean = true, | ||
onUpdate: () => mixed = () => {}, | ||
) { | ||
|
@@ -97,7 +101,7 @@ export default class Sticky { | |
Sticky.activate(); | ||
} | ||
|
||
this.placeholder.shouldPlacehold = this.isSticky; | ||
this.placeholder.shouldPlacehold = this.options.placehold && this.isSticky; | ||
} | ||
|
||
static computeAbsoluteFloor(target: HTMLElement): number { | ||
|
@@ -107,11 +111,19 @@ export default class Sticky { | |
return absoluteBottom - paddingBottomPixels; | ||
} | ||
|
||
static normalizeElement(value: string|HTMLElement, fallback: HTMLElement): HTMLElement { | ||
static normalizeElement(value: SelectorOrElement, ...fallbacks: MaybeHTMLElement[]): HTMLElement { | ||
if (value instanceof HTMLElement) { | ||
return value; | ||
} | ||
return document.querySelector(value) || fallback; | ||
|
||
const element: ?HTMLElement = ([document.querySelector(value), ...fallbacks] | ||
.find(item => !!item && item instanceof HTMLElement): any); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 😭 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. get the power of Typescript 🙄 |
||
|
||
if (element instanceof HTMLElement) { | ||
return element; | ||
} | ||
|
||
throw new TypeError('[Stuck-js] Could not find HTMLElement'); | ||
} | ||
|
||
static register(instance: Sticky): void { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does this include
void
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/facebook/flow/blob/v0.75.0/lib/dom.js#L439
that's why includes void
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
understood 👍