Skip to content

Helpers

The components are built from small functions: finding elements, classes, events, styles, and reading options. They are exported too, so your own code can use the same ones.

They are in the module build only. The <script> build defines the components, as ML.Modal and the others, and nothing else.

import { $q, addClass, evt } from '@mikewgd/myjs';
// Or only the helpers, from the folder that has nothing else in it:
import { ready } from '@mikewgd/myjs/core';
ready(() => {
evt($q('#open'), 'click', () => addClass($q('#panel'), 'open'));
});

Importing the helpers does nothing by itself: they never touch window or document until you call them. Each one is documented, with examples, in the API reference under Functions.

Group Functions
Finding $q, $qAll, findParent
Classes hasClass, addClass, removeClass, toggleClass
Events evt, removeEvt, isBound, clicked, customEventTrigger, triggerEvent, ready, Events
Elements create, clean, assertElement
Attributes data, getAttr
Size windowDimen, position, dimens, collides
Styles getStyle, setStyles, cssTransition, cssTransform
Group Functions
Types isUndef, isString, isBool, isNum
Converting bool, nodeArr
Options toInt, toBool, clamp, extend, parObj (data-* attributes like width:700:align:top)
URLs urlParams
  • Everything here works in IE10. Where a browser function is missing there, the helper does the work: create and data instead of dataset, nodeArr instead of Array.from, triggerEvent instead of new Event().
  • evt and removeEvt keep a list of the listeners they add, which is what isBound and Events read. A listener added with addEventListener is not in it.
  • isUndef, isNum and isBool are lenient on purpose: options that come from a data-* attribute are strings. They are not type guards for that reason.

A checklist. An unticked item is not built, and not promised: it says what is missing today. A ticked item has been built.

  • All the data-* attributes. data(element) with no name could give them all, as an object. Today a name is required.
  • debounce and throttle. Breakpoints has its own debounce of the resize event.
  • uid(prefix). Tooltip counts its own ids, and the accessibility work will need more of them.
  • closest and matches. IE10 has neither, and findParent only takes one tag name and one class.
  • Delegated events. delegate(root, selector, type, callback).
  • Focus helpers. Keeping the focus in an element, and giving it back. Modal and Dropdown need both.
  • lockScroll. Modal stops the page from scrolling with a class on <html>.
  • prefersReducedMotion(). For Animate and Carousel.
  • Both spellings. getStyle takes margin-right and setStyles takes marginLeft. Accepting both in both would remove a trap.
  • isNum that is strict. isNum(''), isNum(null) and isNum([]) are true.