Skip to content

Instantly share code, notes, and snippets.

View dmail's full-sized avatar
🍒

Damien Maillard dmail

🍒
  • Datadog
  • Antibes
View GitHub Profile
export const listenActiveElement = (element, listener) => {
let { activeElement } = document
const check = (event) => {
const previousActiveElement = activeElement
activeElement = document.activeElement
if (previousActiveElement !== activeElement) {
listener({ previousActiveElement, activeElement, event })
}
}
@dmail
dmail / getRelativeBoundingClientRect.js
Created April 5, 2018 12:38
get element position relative to an other
// keep in mind element may be inside an iframe
// var childWindow = elem.document.frames.window;
// while (childWindow != parentWindow) {
// offset.left = offset.left + childWindow.frameElement.getBoundingClientRect().left;
// offset.top = offset.top + childWindow.frameElement.getBoundingClientRect().top;
// childWindow = childWindow.parent;
// }
const getRelativeBoundingClientRect = (element, relativeElement) => {
const { left, top, right, bottom } = element.getBoundingClientRect()
@dmail
dmail / delayFunction.js
Last active March 30, 2018 14:58
a more friendly setTimeout
export const delayFunction = ({ fn, ms = 0 } = {}) => {
let delayed = false
let delayedMs
let paused = false
let pausedMs
let id
const callback = () => {
delayed = false
fn()
@dmail
dmail / batch-ui-mutations.js
Last active March 28, 2018 12:29
Utility to ensure UI mutations are executed together
export const batchUIMutation = (fnMutatingUI) => {
if (typeof window !== 'undefined' && window.requestAnimationFrame) {
const id = requestAnimationFrame(() => {
cancelAnimationFrame(id)
fnMutatingUI()
})
return () => cancelAnimationFrame(id)
}
const id = setTimeout(() => {
@dmail
dmail / url.js
Last active January 5, 2024 11:09
URL parts naming. Inspired from web browsers API (new URL(), window.location) and rfc3986.
/*
href
┌────────────────────────────────────────┴──────────────────────────────────────────────┐
origin │
┌────────────┴──────────────┐ │
│ authority │
│ ┌───────────────┴───────────────────────────┐ │
│ │ host resource
│ │ ┌──────────┴─────────────────┐ ┌────────────┴───────────┬───────┐
│ │ hostname │ pathname │ │
@dmail
dmail / trackFocusReason.js
Last active April 18, 2018 12:35
track focus reason
const isAncestorOf = (node, possibleDescendantNode) => {
let ancestor = possibleDescendantNode.parentNode
while (ancestor) {
if (ancestor === node) {
return true
}
ancestor = ancestor.parentNode
}
return false
}
@dmail
dmail / bubble-iframe.js
Created June 13, 2017 14:03
Bubbling iframe events
const bubbleIframeEvents = (iframe, document) => {
const iframeWindow = iframe.contentWindow
iframeWindow.addEventListener(
'mousemove',
(event) => {
const boundingClientRect = iframe.getBoundingClientRect()
const fakeEvent = new CustomEvent(
'mousemove',
{
@dmail
dmail / get-array-index-name-status.js
Created November 14, 2016 08:57
Function returning how a name match the naming requirements for an array index in JavaScript
const STRING = 0; // name is a string it cannot be an array index
const INFINITE = 1; // name is casted to Infinity, NaN or -Infinity, it cannot be an array index
const FLOATING = 2; // name is casted to a floating number, it cannot be an array index
const NEGATIVE = 3; // name is casted to a negative integer, it cannot be an array index
const TOO_BIG = 4; // name is casted to a integer above Math.pow(2, 32) - 1, it cannot be an array index
const VALID = 5; // name is a valid array index
const maxArrayIndexValue = Math.pow(2, 32) - 1;
function getArrayIndexStatusForString(name) {
if (isNaN(name)) {
@dmail
dmail / file-using-lazy-error.js
Created October 3, 2016 19:56
Example of a function lazyly throwing an error thus potentially loosing error cause
import lazy from './lazy-error.js';
const fn = lazy();
function test() {
fn();
}
test();
@dmail
dmail / function-prototype-bind-soft.js
Last active September 15, 2016 09:57
Experimental Function.prototype.bind that does not force this when it's explicitely passed by call() or apply()
if (!Function.prototype.bindSoft) { // set once
const originalFunctionSymbol = Symbol();
Function.prototype.bindSoft = function(thisValue) {
const originalFunction = this;
const boundFunction = originalFunction.bind(thisValue);
// store pointer to original function
boundFunction[originalFunctionSymbol] = originalFunction;
return boundFunction;
};