Skip to content

Commit

Permalink
fix: update TypeScript to support optional settings
Browse files Browse the repository at this point in the history
  • Loading branch information
yitengjun committed May 23, 2023
1 parent 24b570c commit 3b97573
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { UkiyoOptions, TElement } from '../types/index';
export default class Ukiyo {
private elements;
private options;
private options?;
private instances;
private externalRAF;
private requestId?;
private timer?;
private onResizeEvent;
private isInit;
constructor(elements: TElement | null, options: UkiyoOptions);
constructor(elements: TElement | null, options?: UkiyoOptions);
/**
* Initializes
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/types/parallax.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export declare class Parallax {
private wrapperHeight?;
private damp;
private elementTagName;
constructor(element: HTMLElement, options: Partial<UkiyoOptions>);
constructor(element: HTMLElement, options?: UkiyoOptions);
/**
* Create Parallax
*/
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import type { UkiyoOptions, TElement } from '../types/index';

export default class Ukiyo {
private elements: HTMLElement[] = [];
private options!: UkiyoOptions;
private options?: UkiyoOptions;
private instances: Parallax[];
private externalRAF: boolean;
private requestId?: number;
private timer?: number;
private onResizeEvent: EventListenerOrEventListenerObject;
private isInit: boolean;

constructor(elements: TElement | null, options: UkiyoOptions) {
constructor(elements: TElement | null, options?: UkiyoOptions) {
if (!elements) {
throw new Error(`Argument 'elements' is null.`);
}
Expand Down
16 changes: 8 additions & 8 deletions src/parallax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Parallax {
private damp: number;
private elementTagName: string;

constructor(element: HTMLElement, options: Partial<UkiyoOptions>) {
constructor(element: HTMLElement, options?: UkiyoOptions) {
this.element = element;
this.wrapper = document.createElement('div');

Expand Down Expand Up @@ -83,7 +83,7 @@ export class Parallax {

// Difference between the height of the element and the wrapper
this.overflow =
Math.floor((elementHeight - elementHeight * this.options.scale) * 10) /
Math.floor((elementHeight - elementHeight * this.options.scale!) * 10) /
10;

// When using both margin: auto and position: absolute
Expand Down Expand Up @@ -189,7 +189,7 @@ export class Parallax {
wrapperStyle.setProperty('height', elementHeight + 'px', 'important');
elementStyle.setProperty(
'height',
elementHeight * this.options.scale + 'px',
elementHeight * this.options.scale! + 'px',
'important',
);

Expand All @@ -203,7 +203,7 @@ export class Parallax {
const elementOptionWrapperClass: string | null | undefined =
this.element.getAttribute('data-u-wrapper-class');
const customClass: string | null =
elementOptionWrapperClass || this.options.wrapperClass;
elementOptionWrapperClass || this.options.wrapperClass!;

if (customClass) {
this.wrapper.classList.add(customClass);
Expand Down Expand Up @@ -303,9 +303,9 @@ export class Parallax {
const percentage = Math.min(100, Math.max(0, percentageDistance)) / 100;

const speedDiff =
(this.overflow! * this.options.speed - this.overflow!) / 2;
(this.overflow! * this.options.speed! - this.overflow!) / 2;
const translateValue =
this.overflow! * (1 - percentage) * this.options.speed * this.damp -
this.overflow! * (1 - percentage) * this.options.speed! * this.damp -
speedDiff;

return Number(translateValue.toFixed(4));
Expand All @@ -317,8 +317,8 @@ export class Parallax {
private calcDamp(screenSize: number): number {
const max = 1;
const min = 0.5;
let scale = this.options.scale;
let speed = this.options.speed;
let scale = this.options.scale!;
let speed = this.options.speed!;

if ((speed >= 1.4 || scale >= 1.4) && screenSize <= 1000) {
if (scale < 1) scale = 1;
Expand Down
10 changes: 5 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ export interface UkiyoOptions {
* Parallax scale
* @default 1.5
*/
scale: number;
scale?: number;

/**
* Parallax speed
* @default 1.5
*/
speed: number;
speed?: number;

/**
* Wrapper element class
* @default null
*/
wrapperClass: string | null;
wrapperClass?: string | null;

/**
* Enabling the CSS will-change property during parallax animation
* @default false
*/
willChange: boolean;
willChange?: boolean;

/**
* Animate parallax using external requestAnimationFrame()
* @default false
*/
externalRAF: boolean;
externalRAF?: boolean;
}

0 comments on commit 3b97573

Please sign in to comment.