1.3.1 • Published 4 years ago

@miraidesigns/utils v1.3.1

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

Utils

A collection of useful Sass and TypeScript functions.


Sass

px2rem

A basic function to convert a given px value into rem.

@use '@miraidesigns/utils';

p {
    font-size: utils.px2rem(16px); // Results in 1rem
}

TypeScript

Functions

NameTypeDescription
isRTL()(): booleanReturns wether or not the page is currently in RTL mode
hasScrollbar(elem, horizontal?)(Element, boolean): booleanReturns wether or not the current element has a visible scrollbar
getScrollbarParent(elem, ignoreElem?)(HTMLElement, string): HTMLElementStarting from the given element, returns the closest parent that has a scrollbar
matchParentHeight(elem)(HTMLElement): voidMatch the given element's height to that of its parent.
throttle(func, delay)((...args: any[]) => void, number): () => voidWill limit how often a function can be executed
debounce(func, timeout)((...args: any[]) => void, number): () => voidWill block a function from being executed again until enough time has passed
empty(string)(string): booleanStrip string of white spaces and check if its empty.
imageLoaded(image)(HTMLImageElement): Promise<boolean>Will resolve promise once an image is fully loaded.

Here is a small example using the throttle function.

// Here we import the throttle function from our utils module.
import { throttle } from '@miraidesigns/utils';

const throttledFunc = () => {
    console.log('I only execute every 250ms');
}

// And we apply the throttle to a scroll event, a very common way to avoid overhead on repeated calls.
window.addEventListener('scroll', throttle(throttledFunc, 250));