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

#14 Fix placeholder #15

Merged
merged 3 commits into from
Jun 26, 2018
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
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ <h3 id="Options-marginTop" class="option">
<h3 id="Options-wrapper" class="option">
wrapper
<span class="option__type">HTMLElement</span>
<span class="option__default">document.body</span>
<span class="option__default">parentElement|document.body</span>
</h3>
<p>Ipsum laboriosam odio minus sunt officiis. Veniam sed saepe inventore labore ratione adipisci Sed unde sapiente porro accusamus quo Dolorem deserunt veniam ducimus neque velit. Esse saepe deserunt ipsam similique</p>
<p>Ipsum laboriosam odio minus sunt officiis. Veniam sed saepe inventore labore ratione adipisci Sed unde sapiente porro accusamus quo Dolorem deserunt veniam ducimus neque velit. Esse saepe deserunt ipsam similique</p>
Expand Down
83 changes: 46 additions & 37 deletions docs/index.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion docs/initially-hidden.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
display: block;
}
footer {
width: 100%;
height: 100px;
margin: 0;
padding: 0;
width: 100%;
background-color: #aaf;
}
</style>
</head>
Expand Down
63 changes: 63 additions & 0 deletions docs/placeholding.html
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>
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

Binary file modified lib/index.js.gz
Binary file not shown.
20 changes: 5 additions & 15 deletions src/placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Placeholder {
this.original = element;
this.storeInitialComputedStyles();
this.element = Placeholder.createPlaceholder();
this.applySpacingInitialStyles();
this.applyInitialStyles();
this.cachedRect = this.element && this.updateRect();
this.shouldPlacehold = placehold;

Expand All @@ -59,13 +59,15 @@ export default class Placeholder {
}
}

applySpacingInitialStyles(): void {
applyInitialStyles(): void {
if (!this.initialComputedStyles) {
return;
}
this.element.style.margin = this.initialComputedStyles.margin;
this.element.style.minWidth = this.initialComputedStyles.minWidth;
this.element.style.minHeight = this.initialComputedStyles.minHeight;
this.element.style.width = this.initialComputedStyles.width;
this.element.style.height = this.initialComputedStyles.height;
}

destroy(): void {
Expand Down Expand Up @@ -114,20 +116,8 @@ export default class Placeholder {
this.updateRect();
}

removeStyles(): void {
if (!this.original || !this.element) {
return;
}
this.element.style.width = '';
this.element.style.height = '';
}

update(forceUpdate: boolean = false): void {
if (this.shouldPlacehold) {
this.applyStyles(forceUpdate);
} else {
this.removeStyles();
}
this.applyStyles(forceUpdate);
this.onUpdate();
}

Expand Down
34 changes: 23 additions & 11 deletions src/sticky.js
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;
Copy link
Collaborator

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?

Copy link
Owner Author

@RyoNkmr RyoNkmr Jun 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.parentElement: ?Element;

https://github.com/facebook/flow/blob/v0.75.0/lib/dom.js#L439

that's why includes void

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood 👍

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 {
Expand Down Expand Up @@ -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');
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 = () => {},
) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Array.prototype.[filter|find] type refinement problem workaround
facebook/flow#1414 (comment)

😭

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/stuck.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Stuck {

constructor(
settings: StickySetting[] | StickySetting = [],
defaultOptions: StickyOptions = {},
defaultOptions: StickyOptions = { placehold: true, observe: true },
sharedStacking: boolean = true,
) {
this.defaultOptions = defaultOptions;
Expand Down