Skip to content

Instantly share code, notes, and snippets.

@rhysburnie
rhysburnie / useHasSlot.js
Last active January 25, 2023 13:43
vue3 determine if slot is being used (has content)
import { useSlots } from 'vue';
export default function useHasSlot() {
const slots = useSlots();
return function hasSlot(name) {
return slots[name] && !isEmptySlot(slots[name]());
};
}
function isEmptySlot(items) {
@rhysburnie
rhysburnie / ActiveElementObserver.js
Last active April 26, 2022 07:16
Simple document.activeElement observation
export default function ActiveElementObserver (handler = function () {}) {
let prev;
let raf;
this.observe = _ => {
cancelAnimationFrame(raf);
raf = undefined;
if (document.activeElement !== prev) {
handler({ activeElement: document.activeElement, prev });
prev = document.activeElement;
@rhysburnie
rhysburnie / Looper.js
Last active December 1, 2017 10:23
Small frame looper for quick setup
function Looper(update, targetFPS = 30, autoplay = true) {
let updating = false;
this.autoplay = autoplay;
this.targetFPS = targetFPS;
const advanceFrame = () => {
updating = true;
const next = () => {
const delay = this.autoplay ? 1000 / (this.targetFPS || 30) : 0;
setTimeout(() => updating = false, delay);
@rhysburnie
rhysburnie / dat.GUI.convertToDisplay.js
Created October 29, 2017 03:41
dat.GUI convert a field to a display instead of input
dat.GUI.prototype.convertToDisplay = function(owner, property) {
var field = this.__controllers.find(function(item){return item.property === property});
var parent = field.domElement.parentElement;
var value = owner[property];
delete owner[property];
Object.defineProperty(owner, property, {
get: function() {
return value;
},
set: function(newValue) {
@rhysburnie
rhysburnie / node-or-browser.js
Last active October 22, 2024 23:28
Detect node or browser
// determine if in-browser or using node.js
// thruthy
var _nodejs = (
typeof process !== 'undefined' && process.versions && process.versions.node);
if (_nodejs) {
_nodejs = {
version: process.versions.node
};
}
function transformsSupport() {
if (!window.getComputedStyle) {
return false;
}
var el = document.createElement('p'),
has3d,
transformPropName,
transforms = {
webkitTransform: '-webkit-transform',
@rhysburnie
rhysburnie / index.js
Last active September 25, 2017 05:34
iso8601-duration conversions - requirebin sketch
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
var iso8601Duration = require('iso8601-duration');
// https://github.com/wking/milliseconds-to-iso-8601-duration/blob/master/milliseconds-to-iso-8601-duration.js
var millisecondsToISO8601Duration = function(milliseconds) {
if (milliseconds == 0) {
return 'P0D';
@rhysburnie
rhysburnie / console-utilities.js
Last active September 19, 2017 01:06
Suppress certain console.errors
/* eslint-disable no-console */
const originalConsoleError = console.error;
export function restoreConsoleError() {
console.error = originalConsoleError;
}
export function createSuppressedConsoleError(ignoreThese = []) {
if (console.error === originalConsoleError) {
console.error = (...args) => {
@rhysburnie
rhysburnie / domElementPropType.js
Last active August 17, 2017 01:01
Proptype for dom element
const createDomElementPropType = (isRequired = false)
=> (props, propName, componentName)
=> {
if (isRequired && !props[propName]) {
return new Error(`${componentName}: prop '${propName}' is required, must be a dom Element`);
}
if (!props[propName] instanceof Element) {
return new Error(`${componentName}: invalid prop '${propName}', must be a dom Element`);
}
};
@rhysburnie
rhysburnie / clone-all.sh
Created April 22, 2017 01:00
backup (and optional delete) repositories
#!/bin/bash
USER=;
PAGE=1;
curl "https://api.github.com/users/$USER/repos?page=$PAGE&per_page=100" |
grep -e 'git_url*' |
cut -d \" -f 4 |
xargs -L1 git clone