Skip to content

Instantly share code, notes, and snippets.

View kimili's full-sized avatar

Michael Bester kimili

View GitHub Profile
@kimili
kimili / compact.js
Created September 19, 2018 02:04
JavaScript Compact
const compact = arr => arr.filter(Boolean);
compact([1, false, 2, '', 3, null, 'abc']); // [1, 2, 3, 'abc']
@kimili
kimili / _snapshot.twig
Created September 24, 2019 18:36
Instagram-style lazy page loading, triggered when a user scrolls towards the bottom of the list. Imports Axios for XHR calls, and ImagesLoaded for handling updates when the images have finished loading. The templating is done in Twig, in the context of a CraftCMS site.
{% set image = snapshot.asset.one() %}
<article class="snapshot snapshot--card">
<header class="snapshot__header">
<h3 class="snapshot__title visually-hidden">
<a href="{{ snapshot.url }}" data-target="overlay" data-overlay-full-width="true">{{ snapshot.title }}</a>
</h3>
<p class="snapshot__author">{{ snapshot.author }}</p>
<p class="snapshot__location">{{ snapshot.geography.one() }}</p>
</header>
const Countdown = {
init() {
this.container = document.querySelector('.countdown');
if ( !this.container || !this.container.dataset.targetDate ) {
return;
}
this.parts = {
days: document.querySelector('.countdown__part--days'),
hours: document.querySelector('.countdown__part--hours'),
minutes: document.querySelector('.countdown__part--minutes'),
@kimili
kimili / card-helper.js
Last active January 12, 2021 16:46
Card helper utility. Takes a DOM element with a link in it and makes the entire thing clickable, based on the first link within.
const CardHelper = {
init(selector = '.card') {
this.card = null;
this.link = null;
this.mousedowntime = 0;
this.selector = selector;
const body = document.querySelector('body');
body.addEventListener('mousedown', this.handleCardMouseDown.bind(this));
@kimili
kimili / toggle-section.js
Last active April 14, 2021 17:14
Web Component for a toggle section: an expandable collapsible block with a heading.
class ToggleSection extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this.render();
this.btn.addEventListener('click', () => {
this.setAttribute('open', this.getAttribute('open') === 'true' ? 'false' : 'true');
@kimili
kimili / toggle-section.css
Created January 8, 2022 15:36
Toggle Section
toggle-section {
display: block;
--heading-font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
--heading-font-size: 2rem;
--primary-color: #222;
}
toggle-section[role="region"] {
border-top: 2px solid #ddd;