dom-element-types v1.1.0
DOM element types
Dom element type validations and visibility queries
Install
npm install dom-element-typesUsage
import { isTextInput } from 'dom-element-types';
const passwordFieldElement = document.querySelector('input[type=password]');
isTextInput(passwordFieldElement); //trueDOM element type validations
DOM elements could be classified by its purpose using the following functions.
Functions
isImage
Matches img dom elements
import { isImage } from 'dom-element-types';
const someImage = document.querySelector('img');
isImage(someImage); //trueisLink
Matches a and [role=link] dom elements
import { isLink } from 'dom-element-types';
const someLink = document.querySelector('a');
isLink(someLink); //trueisText
Matches the following text elements: p, h1, h2, h3, h4, h5, h6, ul, ol, dl, dt, li, dd, table, code, pre, blockquote and span
import { isText } from 'dom-element-types';
const someTitle = document.querySelector('h2');
isText(someTitle); //trueisTextInput
Matches the following text inputs elements: datalist, input[type=number], input[type=email], input[type=password], input[type=search], input[type=tel], input[type=text], input[type=url], textarea, [role=listbox], [role=spinbutton], [role=textbox], [role=searchbox], [role=combobox], [contentEditable]
import { isTextInput } from 'dom-element-types';
const someEmailField = document.querySelector('input[type=email]');
isTextInput(someEmailField); //trueisMultilineTextInput
Matches textarea and [contentEditable] elements
import { isMultilineTextInput } from 'dom-element-types';
const someBoxContainer = document.querySelector('div[contentEditable]');
isMultilineTextInput(someBoxContainer); //trueisColorInput
Matches input[type=color] element
import { isColorInput } from 'dom-element-types';
const someColorField = document.querySelector('input[type=color]');
isColorInput(someColorField); //trueisSelect
Matches the following list elements: select, keygen and [role=listbox]
import { isSelect } from 'dom-element-types';
const someList = document.querySelector('select');
isSelect(someList); //trueisMedia
Matches audio and video elements
import { isVideo } from 'dom-element-types';
const someVideo = document.querySelector('video');
isVideo(someVideo); //trueisRange
Matches input[type=range] and [role=slider] elements
import { isRange } from 'dom-element-types';
const someRange = document.querySelector('input[type=range]');
isRange(someRange); //trueisAnyTypeOfDatePicker
Matches the following datepicker elements: input[type=date], input[type=time], input[type=datetime], input[type=datetime-local], input[type=month] and input[type=week]
import { isAnyTypeOfDatePicker } from 'dom-element-types';
const sameDatePicker = document.querySelector('input[type=date]');
isAnyTypeOfDatePicker(sameDatePicker); //trueisDatePicker
Matches input[type=date] element
import { isDatePicker } from 'dom-element-types';
const sameDatePicker = document.querySelector('input[type=date]');
isDatePicker(sameDatePicker); //trueisDatetimePicker
Matches input[type=datetime] and input[type=datetime-local] elements
import { isDatetimePicker } from 'dom-element-types';
const sameDatePicker = document.querySelector('input[type=datetime]');
isDatetimePicker(sameDatePicker); //trueisMonthPicker
Matches input[type=month] element
import { isMonthPicker } from 'dom-element-types';
const sameDatePicker = document.querySelector('input[type=month]');
isMonthPicker(sameDatePicker); //trueisTimePicker
Matches input[type=time] element
import { isTimePicker } from 'dom-element-types';
const sameDatePicker = document.querySelector('input[type=time]');
isTimePicker(sameDatePicker); //trueisWeekPicker
Matches input[type=week] element
import { isWeekPicker } from 'dom-element-types';
const sameDatePicker = document.querySelector('input[type=week]');
isWeekPicker(sameDatePicker); //trueisScrollable
Determines if an element is scrollable by checking if the scrollHeight > clientHeight and if the computed style is configured for scrolling i.e., has overflowY === scroll or overflowY === auto.
import { isScrollable } from 'dom-element-types';
// create a container containing an element that exceeds it's height
let container = document.createElement('div');
document.body.appendChild(container);
container.style = 'overflow-y:scroll;height:400px';
let containerElement = document.createElement('div');
containerElement.style = 'height:800px';
container.appendChild(containerElement);
isScrollable(container); // trueVisibility queries
The following functions are useful to get all the visible DOM elements present in the screen (port view).
Functions
isVisible
The function isVisible expects as a parameter a DOM element.
It will check if the element:
- is rendered inside the screen area
- is not transparent (opacity > 0)
- is visible (visibility !== 'hidden')
import { isVisible } from 'dom-element-types';
const visibleButton = document.querySelector('button');
isVisible(visibleButton); // truegetVisibleElementsInViewPort
It returns an array of elements visible in the screen.
The function getVisibleElementsInViewPort expects as an optional parameter a valid selector otherwise defaults to '*'.
import {
getVisibleElementsInViewPort,
isVisible
} from 'dom-element-types';
const visibleButtons = getVisibleElementsInViewPort('button');
isVisible(visibleButtons[0]); // truegetVisibleElementsInViewPortExpandedData
Same as getVisibleElementsInViewPort function but for each returned element it also returns the boundingClientRect and computedStyle.
Custom validations
In order to create custom validations the user can get all the element type selectors classified by purpose.
getAllElementTypes
Returns an element type selectors map composed of the following types:
- audio
- button
- checkbox
- color
- datePicker
- file
- image
- link
- media
- range
- radio
- select
- text
- textInput
- video
- hasOnClickAttr
import { getAllElementTypes } from 'dom-element-types';
console.log(getAllElementTypes());
/*
Returns:
{
audio: ['audio'],
button: [
'button',
'input[type=button]',
'input[type=reset]',
'input[type=submit]',
'[role=button]',
'[role=menuitem]',
'[role=option]'
],
checkbox: [
'input[type=checkbox]',
'[role=checkbox]',
'[role=menuitemcheckbox]'
],
...
}
*/