Skip to content

Commit

Permalink
fix types, add a file to test out TS intellisense
Browse files Browse the repository at this point in the history
  • Loading branch information
nickforddev committed Jul 3, 2020
1 parent bd6b812 commit ecf1526
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/freezeframe/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const classes = {
};

export const defaultOptions = {
selector: classes.SELECTOR,
responsive: true,
trigger: 'hover',
overlay: false,
Expand Down
35 changes: 24 additions & 11 deletions packages/freezeframe/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import imagesLoaded from 'imagesloaded';
import {
SelectorOrNotes,
SelectorOrNodes,
Freeze,
FreezeFrameEvent,
FreezeFrameOptions,
Expand All @@ -13,7 +13,8 @@ import {
dedupeImages,
isTouch,
wrapNode,
htmlToNode
htmlToNode,
error
} from './utils/index';
import * as templates from './templates';
import {
Expand All @@ -29,6 +30,8 @@ class Freezeframe {

$images: HTMLImageElement[] = [];

#target: SelectorOrNodes;

#isTouch: boolean;

private _eventListeners = {
Expand All @@ -44,24 +47,34 @@ class Freezeframe {
}

constructor(
selectorOrNodes: any = classes.SELECTOR,
options: FreezeFrameOptions
target: SelectorOrNodes | FreezeFrameOptions = classes.SELECTOR,
options?: FreezeFrameOptions
) {
this.options = selectorOrNodes instanceof Object && !options
? { ...defaultOptions, ...selectorOrNodes }
: { ...defaultOptions, ...options };
const target = this.options.selector || selectorOrNodes;
this._init(target);
if (
typeof target === 'string'
|| target instanceof Element
|| target instanceof HTMLCollection
|| target instanceof NodeList
) {
this.options = { ...defaultOptions, ...options };
this.#target = target;
} else if (typeof target === 'object' && !options) {
this.options = { ...defaultOptions, ...target };
this.#target = this.options.selector;
} else {
error('Invalid FreezeFrame.js configuration');
}
this._init(this.#target);
}

private _init(selectorOrNodes: SelectorOrNotes): void {
private _init(selectorOrNodes: SelectorOrNodes): void {
this._injectStylesheet();
this.#isTouch = isTouch();
this._capture(selectorOrNodes);
this._load(this.$images);
}

private _capture(selectorOrNodes: SelectorOrNotes): void {
private _capture(selectorOrNodes: SelectorOrNodes): void {
this.$images = pipe(
normalizeElements,
validateElements,
Expand Down
8 changes: 4 additions & 4 deletions packages/freezeframe/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SelectorOrNotes, FreezeFrameOptions } from '../../types/index';
import { SelectorOrNodes, FreezeFrameOptions } from '../../types/index';

export const pipe = (...fns: Function[]) => (
(...initArgs: any[]) => {
Expand Down Expand Up @@ -33,18 +33,18 @@ export const validateFilename = (filePath: string): boolean => {
return ext === 'gif';
};

export const normalizeElements = (selectorOrNodes: SelectorOrNotes) => {
export const normalizeElements = (selectorOrNodes: SelectorOrNodes) => {
return typeof selectorOrNodes === 'string'
? document.querySelectorAll(selectorOrNodes)
: selectorOrNodes;
};

export const validateElements = (
elements: HTMLElement | HTMLElement[],
elements: Element | Element[],
_: any,
options: FreezeFrameOptions
) => {
const list = elements instanceof HTMLElement ? [elements] : elements;
const list = elements instanceof Element ? [elements] : elements;
return Array.from(list).reduce((acc, image) => {
if (!(image instanceof HTMLImageElement)) {
const $children = image.querySelectorAll('img');
Expand Down
34 changes: 34 additions & 0 deletions packages/freezeframe/tests/ts-intellisense/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable no-new */
import FreezeFrame from '../../src';

// happy paths
new FreezeFrame();
new FreezeFrame('.foo');
new FreezeFrame('#foo');
new FreezeFrame(document.body);
new FreezeFrame(document.getElementById('foo'));
new FreezeFrame(document.getElementsByClassName('foo'));
new FreezeFrame(document.querySelector('.foo'));
new FreezeFrame(document.querySelectorAll('.foo'));
new FreezeFrame({
selector: '.foo'
});
new FreezeFrame({
selector: document.body,
responsive: false,
trigger: 'click',
overlay: false,
warnings: false
});

// sad paths
new FreezeFrame(true);
new FreezeFrame(3);
new FreezeFrame({});
new FreezeFrame({
foo: 3
});
new FreezeFrame({
selector: 3
});
new FreezeFrame('.foo', {});
17 changes: 10 additions & 7 deletions packages/freezeframe/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type SelectorOrNotes = string | HTMLAllCollection
export type SelectorOrNodes = string
| Element
| HTMLCollectionOf<Element>
| NodeListOf<Element>

export type TriggerType = 'hover' | 'click' | false
export type TriggerType = string | false

export enum FreezeFrameEventTypes {
START = 'start',
Expand All @@ -9,11 +12,11 @@ export enum FreezeFrameEventTypes {
}

export interface FreezeFrameOptions {
selector?: string,
responsive: boolean,
trigger: TriggerType,
overlay: boolean,
warnings: boolean
selector: SelectorOrNodes,
responsive?: boolean,
trigger?: TriggerType,
overlay?: boolean,
warnings?: boolean
}

export interface Freeze {
Expand Down

0 comments on commit ecf1526

Please sign in to comment.