-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bug): fixes an issue when using this polyfill along with css-scro…
…ll-snap. Closes #5 feat(node): makes it possible to parse and evaluate the polyfill in a non-browser environment without errors. Closes #7
- Loading branch information
Showing
28 changed files
with
266 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import {SUPPORTS_SCROLL_BEHAVIOR} from "./support/supports-scroll-behavior"; | ||
import {patch} from "./patch/patch"; | ||
import {SUPPORTS_ELEMENT_PROTOTYPE_SCROLL_METHODS} from "./support/supports-element-prototype-scroll-methods"; | ||
import {UNSUPPORTED_ENVIRONMENT} from "./support/unsupported-environment"; | ||
|
||
if (!SUPPORTS_SCROLL_BEHAVIOR || !SUPPORTS_ELEMENT_PROTOTYPE_SCROLL_METHODS) { | ||
if (!UNSUPPORTED_ENVIRONMENT && (!SUPPORTS_SCROLL_BEHAVIOR || !SUPPORTS_ELEMENT_PROTOTYPE_SCROLL_METHODS)) { | ||
patch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const ELEMENT_ORIGINAL_SCROLL_BY = Element.prototype.scrollBy; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const ELEMENT_ORIGINAL_SCROLL_BY = UNSUPPORTED_ENVIRONMENT ? undefined : Element.prototype.scrollBy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const ELEMENT_ORIGINAL_SCROLL_INTO_VIEW = Element.prototype.scrollIntoView; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const ELEMENT_ORIGINAL_SCROLL_INTO_VIEW = UNSUPPORTED_ENVIRONMENT ? undefined : Element.prototype.scrollIntoView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export const ELEMENT_ORIGINAL_SCROLL_LEFT_SET_DESCRIPTOR = Object.getOwnPropertyDescriptor(Element.prototype, "scrollLeft")!.set!; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const ELEMENT_ORIGINAL_SCROLL_LEFT_SET_DESCRIPTOR = UNSUPPORTED_ENVIRONMENT | ||
? undefined | ||
: Object.getOwnPropertyDescriptor(Element.prototype, "scrollLeft")!.set!; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const ELEMENT_ORIGINAL_SCROLL_TO = Element.prototype.scrollTo; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const ELEMENT_ORIGINAL_SCROLL_TO = UNSUPPORTED_ENVIRONMENT ? undefined : Element.prototype.scrollTo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export const ELEMENT_ORIGINAL_SCROLL_TOP_SET_DESCRIPTOR = Object.getOwnPropertyDescriptor(Element.prototype, "scrollTop")!.set!; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const ELEMENT_ORIGINAL_SCROLL_TOP_SET_DESCRIPTOR = UNSUPPORTED_ENVIRONMENT | ||
? undefined | ||
: Object.getOwnPropertyDescriptor(Element.prototype, "scrollTop")!.set!; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const ELEMENT_ORIGINAL_SCROLL = Element.prototype.scroll; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const ELEMENT_ORIGINAL_SCROLL = UNSUPPORTED_ENVIRONMENT ? undefined : Element.prototype.scroll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const WINDOW_ORIGINAL_SCROLL_BY = window.scrollBy; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const WINDOW_ORIGINAL_SCROLL_BY = UNSUPPORTED_ENVIRONMENT ? undefined : window.scrollBy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const WINDOW_ORIGINAL_SCROLL_TO = window.scrollTo; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const WINDOW_ORIGINAL_SCROLL_TO = UNSUPPORTED_ENVIRONMENT ? undefined : window.scrollTo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const WINDOW_ORIGINAL_SCROLL = window.scroll; | ||
import {UNSUPPORTED_ENVIRONMENT} from "../../support/unsupported-environment"; | ||
|
||
export const WINDOW_ORIGINAL_SCROLL = UNSUPPORTED_ENVIRONMENT ? undefined : window.scroll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/smooth-scroll/smooth-scroll-options/i-smooth-scroll-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {ScrollSnappable} from "../../util/scroll-snappable"; | ||
|
||
export interface ISmoothScrollOptions { | ||
startX: number; | ||
startY: number; | ||
endX: number; | ||
endY: number; | ||
startTime: number; | ||
scroller: ScrollSnappable; | ||
method(x: number, y: number): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import {UNSUPPORTED_ENVIRONMENT} from "./unsupported-environment"; | ||
|
||
/** | ||
* Is true if the browser natively supports the Element.prototype.[scroll|scrollTo|scrollBy|scrollIntoView] methods | ||
* @type {boolean} | ||
*/ | ||
export const SUPPORTS_ELEMENT_PROTOTYPE_SCROLL_METHODS = "scroll" in Element.prototype && "scrollTo" in Element.prototype && "scrollBy" in Element.prototype && "scrollIntoView" in Element.prototype; | ||
export const SUPPORTS_ELEMENT_PROTOTYPE_SCROLL_METHODS = UNSUPPORTED_ENVIRONMENT | ||
? false | ||
: "scroll" in Element.prototype && "scrollTo" in Element.prototype && "scrollBy" in Element.prototype && "scrollIntoView" in Element.prototype; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import {UNSUPPORTED_ENVIRONMENT} from "./unsupported-environment"; | ||
|
||
/** | ||
* Is true if the browser natively supports the 'scroll-behavior' CSS-property. | ||
* @type {boolean} | ||
*/ | ||
export const SUPPORTS_SCROLL_BEHAVIOR = "scrollBehavior" in document.documentElement.style; | ||
export const SUPPORTS_SCROLL_BEHAVIOR = UNSUPPORTED_ENVIRONMENT ? false : "scrollBehavior" in document.documentElement.style; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const UNSUPPORTED_ENVIRONMENT = typeof window === "undefined"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const STYLE_ATTRIBUTE_PROPERTY_NAME = "scroll-behavior"; | ||
const STYLE_ATTRIBUTE_PROPERTY_REGEXP = new RegExp(`${STYLE_ATTRIBUTE_PROPERTY_NAME}:\\s*([^;]*)`); | ||
|
||
/** | ||
* Given an Element, this function appends the given ScrollBehavior CSS property value to the elements' 'style' attribute. | ||
* If it doesnt already have one, it will add it. | ||
* @param {Element} element | ||
* @param {ScrollBehavior} behavior | ||
*/ | ||
export function appendScrollBehaviorToStyleAttribute(element: Element, behavior: ScrollBehavior): void { | ||
const addition = `${STYLE_ATTRIBUTE_PROPERTY_NAME}:${behavior}`; | ||
let attributeValue = element.getAttribute("style"); | ||
if (attributeValue == null || attributeValue === "") { | ||
element.setAttribute("style", addition); | ||
return; | ||
} | ||
|
||
// The style attribute may already include a 'scroll-behavior:<something>' in which case that should be replaced | ||
const existingValueForProperty = parseScrollBehaviorFromStyleAttribute(element); | ||
if (existingValueForProperty != null) { | ||
const replacementProperty = `${STYLE_ATTRIBUTE_PROPERTY_NAME}:${existingValueForProperty}`; | ||
// Replace the variant that ends with a semi-colon which it may | ||
attributeValue = attributeValue.replace(`${replacementProperty};`, ""); | ||
// Replace the variant that *doesn't* end with a semi-colon | ||
attributeValue = attributeValue.replace(replacementProperty, ""); | ||
} | ||
|
||
// Now, append the behavior to the string. | ||
element.setAttribute("style", attributeValue.endsWith(";") ? `${attributeValue}${addition}` : `;${attributeValue}${addition}`); | ||
} | ||
|
||
/** | ||
* Given an Element, this function attempts to parse its 'style' attribute (if it has one)' to extract | ||
* a value for the 'scroll-behavior' CSS property (if it is given within that style attribute) | ||
* @param {Element} element | ||
* @returns {ScrollBehavior?} | ||
*/ | ||
export function parseScrollBehaviorFromStyleAttribute(element: Element): ScrollBehavior | undefined { | ||
const styleAttributeValue = element.getAttribute("style"); | ||
if (styleAttributeValue != null && styleAttributeValue.includes(STYLE_ATTRIBUTE_PROPERTY_NAME)) { | ||
const match = styleAttributeValue.match(STYLE_ATTRIBUTE_PROPERTY_REGEXP); | ||
if (match != null) { | ||
const [, behavior] = match; | ||
if (behavior != null && behavior !== "") { | ||
return behavior as ScrollBehavior; | ||
} | ||
} | ||
} | ||
return undefined; | ||
} |
Oops, something went wrong.