Skip to content

Instantly share code, notes, and snippets.

// moustache replacement
const soupStrainer = (template, values) =>
[...template.matchAll(/\{\{([^}]*)\}\}/gi)].reduce(
(acc, [full, path]) =>
acc.replace(
full,
path.split(`.`).reduce((acc, curr) => acc[curr], values)
),
template
)
@joeldbirch
joeldbirch / SvgImg.js
Last active May 25, 2019 03:29
React component providing a replacement img tag which makes fetchSvgInline work with server-side rendering (tested on Gatsby). "fetchSvgInline" (by @stowball) fetches a remote SVG as an <img> and converts it to an inline SVG.
import React, {Component, createRef } from 'react'
// Include Matt Stow's fetchSvgInline from https://gist.github.com/stowball/e51dde035346eba783f3d8b54dbbf2a4
import { fetchSvgInline } from '../utilities/helpers'
export default class extends Component {
constructor(props) {
super(props)
this.imgRef = createRef()
}
@joeldbirch
joeldbirch / quick-admin-bugfix.js
Created February 21, 2017 06:30
Quick Admin bugfix for checkboxes in table cells
var handleCheckboxes = function (html, rowIndex, colIndex, cellNode) {
var $cellNode = $(cellNode);
var $check = $cellNode.find(':checked');
return ($check.length) ? $check.val() : $cellNode.text();
}
// Then add to exportOptions.format.body option for each file type, eg:
[...]
@joeldbirch
joeldbirch / gist:8431697
Last active January 3, 2016 07:49
A simplified rewrite of code I'm using to add and remove a class on a website header to indicate when the page is scrolled to the top. Untested since rewrite, but should work if I haven't made typos.
(function(win, $){
$(document).ready(function(){
var scroll_class = 'scrolled-top';
var scroll_timer;
var $win = $(win);
var $head = $('#head');
$win.on('scroll', function(){
@joeldbirch
joeldbirch / Gruntfile.coffee
Created May 5, 2013 04:18
Getting grunt to livereload the page when Compass Sass files change.
'use strict'
module.exports = (grunt) ->
# Project configuration.
grunt.initConfig
# Task configuration.
compass:
options:
@joeldbirch
joeldbirch / initialise-foobox-later
Created April 24, 2013 06:26
WordPress snippet to make FooBox's initialisation code appear last in the source order. Ensures FooBox assets are present prior to initialisation code running, preventing errors.
function fix_foobox_initialisation() {
remove_action('wp_print_footer_scripts', array($GLOBALS['foobox'], 'inline_dynamic_js') );
add_action('wp_footer', array($GLOBALS['foobox'], 'inline_dynamic_js'), 99999 );
}
add_action('wp', 'fix_foobox_initialisation');
@joeldbirch
joeldbirch / superfish-vertical-supersubs.css
Last active December 16, 2015 03:39
This is just superfish-vertical.css with a few lines added so the "supersubs-like" effect also applies to the top level menu. Comments in uppercase explain the additions.
/* adding sf-vertical class in addition to sf-menu creates a vertical menu */
/* eg. <ul class="sf-menu sf-vertical"> ... */
.sf-vertical {
min-width: 10em; /* <--- MAKE THIS MIN-WIDTH */
*width: 10em; /* <--- ADD FALLBACK WIDTH FOR IE7 */
}
.sf-vertical ul {
left: 100%;
top: 0;
}
@joeldbirch
joeldbirch / supposition.js
Created April 12, 2013 12:22
Updated version of Supposition. This version is compatible with Superfish 1.6+. See this ancient page for description, example and caveats: http://users.tpg.com.au/j_birch/plugins/superfish/supposition-test/
/*
* Supposition v0.3a - an optional enhancer for Superfish jQuery menu widget
*
* Copyright (c) 2013 Joel Birch - based on work by Jesse Klaasse - credit goes largely to him.
* Special thanks to Karl Swedberg for valuable input.
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
@joeldbirch
joeldbirch / iOS bfcache buster
Created March 29, 2013 03:23
Seems to be the only way to reset Apple's hacky iOS "hover" state after back button click. Uses jQuery event attachment, but can be easily swapped for addEventListener.
if (/iPhone|iPad|iPod/i.test(navigator.userAgent)) {
$(window).on('pageshow', function(e) {
if (e.originalEvent.persisted) {
window.location.reload();
}
});
}
@joeldbirch
joeldbirch / jQuery Superfish pathClass support
Created July 13, 2012 06:33
Add current class to ancestor li elements prior to initialising Superfish
$(document).ready(function(){
//this will be used more than once so save it to a variable
var menu = $('ul.sf-menu')
//given that you already seem to have the current page marked with 'current' on the relevant li,
//find that li, then find any ancestors of it and add 'current' to them also
menu.find('.current').parents('li').addClass('current');
//once you have done that, then initialise Superfish