@basekits/kit-dom
DOM utility functions kit for basekits.
Install
npm i @basekits/kit-type @basekits/kit-object @basekits/kit-error @basekits/kit-validator @basekits/kit-function
npm i @basekits/kit-dom
Usage
const kit = require('@basekits/core')
kit.addKit(require('@basekits/kit-type'))
kit.addKit(require('@basekits/kit-object'))
kit.addKit(require('@basekits/kit-error'))
kit.addKit(require('@basekits/kit-validator'))
kit.addKit(require('@basekits/kit-function'))
kit.addKit(require('@basekits/kit-dom'))
Available Items
The following methods will be available after adding this kit:
.onOutsideClick(elements = [], fn, opts = {once: true})
Executes a function when click event happens outside of one of the elements
. elements
can be:
- Array of selector strings.
- Array of DOM elements.
- A selector string.
- A DOM element.
function closeDropdown() {
}
kit.onOutsideClick( document.getElementById('sample'), closeDropdown, {once: true} )
opts
has once
property which causes function to be invoked only once.
.getCaretPos(element)
Returns the start and the end index of the current text cursor position in the text and textarea inputs.
const viewport = kit.getCaretPos( document.querySelector('input[type="text"]') )
User selected some text in the input if start and end are different.
.setCaretPos(element, start, end)
Sets the caret position in the text and textarea inputs.
const el = document.querySelector('input[type="text"]')
kit.setCaretPos( el, 4, 4 )
kit.setCaretPos( el, 4 )
Selects some text in the input if start and end are different.
.getViewportDimensions()
Returns the width and height of the current visible area in the browser.
const viewport = kit.getViewportDimensions()
.getAbsoluteDistanceFromTop(element)
Returns element's absolute distance from top of the page in pixels.
const fromTop = kit.getAbsoluteDistanceFromTop(element)
.getViewportRelativeDistanceFromTop(element)
Returns element's relative distance from top of the viewport in pixels.
const fromTop = kit.getViewportRelativeDistanceFromTop(element)
.getViewportRelativeDistanceFromBottom(element)
Returns element's relative distance from bottom of the viewport in pixels.
const fromTop = kit.getViewportRelativeDistanceFromBottom(element)
.setDOMGlobal(name, value)
Override window
or document
globals.
kit.setDOMGlobal('window', window)
kit.setDOMGlobal('document', document)