Skip to content

Instantly share code, notes, and snippets.

@ca0v
Last active December 23, 2019 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ca0v/4de4d761983cde2a1d751d8cad0843c9 to your computer and use it in GitHub Desktop.
Save ca0v/4de4d761983cde2a1d751d8cad0843c9 to your computer and use it in GitHub Desktop.
svelte gems
/**
* Defines the component lifecycle-ish? (maybe HtmlTag in dom.ts is a parital implementation?)
* Relates to chunks in Block.ts
*/
interface Fragment {
key: string|null;
first: null;
/* create */ c: () => void;
/* claim */ l: (nodes: any) => void;
/* hydrate */ h: () => void;
/* mount */ m: (target: HTMLElement, anchor: any) => void;
/* update */ p: (ctx: any, dirty: any) => void;
/* measure */ r: () => void;
/* fix */ f: () => void;
/* animate */ a: () => void;
/* intro */ i: (local: any) => void;
/* outro */ o: (local: any) => void;
/* destroy */ d: (detaching: 0|1) => void;
}
/**
* Simplest resize detector I've seen...
* puts an "object" on the element absolutely positioned to the 4 edges, watches the window on that object
* for "resize" and fires the callback.
*/
export function add_resize_listener(element, fn) {
if (getComputedStyle(element).position === 'static') {
element.style.position = 'relative';
}
const object = document.createElement('object');
object.setAttribute('style', 'display: block; position: absolute; top: 0; left: 0; height: 100%; width: 100%; overflow: hidden; pointer-events: none; z-index: -1;');
object.setAttribute('aria-hidden', 'true');
object.type = 'text/html';
object.tabIndex = -1;
let win;
object.onload = () => {
win = object.contentDocument.defaultView;
win.addEventListener('resize', fn);
};
if (/Trident/.test(navigator.userAgent)) {
element.appendChild(object);
object.data = 'about:blank';
} else {
object.data = 'about:blank';
element.appendChild(object);
}
return {
cancel: () => {
win && win.removeEventListener && win.removeEventListener('resize', fn);
element.removeChild(object);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment