Skip to content

Instantly share code, notes, and snippets.

Avatar
🚌
🌊

Christopher Robert Van Wiemeersch cvan

🚌
🌊
View GitHub Profile
@cvan
cvan / commands.js
Created Jan 26, 2021
cypress disable basic css transitions + animations
View commands.js
Cypress.Commands.add('cssDisableMotion', (selector = 'head') => {
cy.get('html').then(() => {
document.querySelector(selector).insertAdjacentHTML('beforeend', `
<style>
/* Disable CSS transitions. */
* { -webkit-transition: none !important; -moz-transition: none !important; -o-transition: none !important; transition: none !important; }
/* Disable CSS animations. */
* { -webkit-animation: none !important; -moz-animation: none !important; -o-animation: none !important; animation: none !important; }
@cvan
cvan / with-dynamic-route-hydrated.js
Created Jan 1, 2021
next.js dynamic routes wait for hydration to complete
View with-dynamic-route-hydrated.js
import React, { useEffect, useState } from 'react';
import { withRouter } from 'next/router';
// @example `"/films/[slug]"`
const IS_DYNAMIC_ROUTE = urlPathname => String(urlPathname).includes('[');
// Usage in your dynamic-route components:
// @example
// export default withDynamicRoute(Page);
@cvan
cvan / is-dynamic-route.js
Created Dec 5, 2020
is dynamic next.js route
View is-dynamic-route.js
/**
* Identify `/[param]/` in route string.
* @see https://github.com/vercel/next.js/blob/master/packages/next/next-server/lib/router/utils/is-dynamic.ts#L7
*
* @param {route} The value from next/router's `route.asPath` (e.g., `/browse/[listingId]`).
* @returns {boolean} True if the route looks like a Next.js dynamic route.
*/
const TEST_ROUTE = /\/\[[^/]+?\](?=\/|$)/;
export function isDynamicRoute(route) {
return TEST_ROUTE.test(route);
View cypress-debug.js
const enableCypressDebug = win => {
if (process.env.DEBUG === '1') {
win.localStorage.setItem('debug', 'cypress:*');
}
};
beforeEach(enableCypressDebug);
View nodemon-safe-kill.js
if (process.env.NODE_ENV === 'development') {
// This helps for nodemon to not be prematurely killed.
// See nodemon's docs: https://github.com/remy/nodemon/blob/master/README.md#controlling-shutdown-of-your-script
// Adapted from source: https://www.benjiegillam.com/2011/08/node-js-clean-restart-and-faster-development-with-nodemon/
const gracefulShutdown = cb => {
setTimeout(() => {
process.exit(0);
cb();
}, 5000);
};
@cvan
cvan / strip-trailing-slashes-in-url.js
Created Jul 28, 2020
strip trailing slashes in a URL (JavaScript)
View strip-trailing-slashes-in-url.js
const stripTrailingSlashesInUrl = str => str.replace(/\/+$/g, '');
@cvan
cvan / strip-html-comments.js
Created Jul 21, 2020
strip html comments javascript
View strip-html-comments.js
function stripHtmlComments(html) {
// Remove HTML comments (useful for removing developer-facing comments from production HTML markup).
return html.replace(/<!--[\s\S]*?(?:-->)/g, '');
}
@cvan
cvan / parse-a-single-qs-value.js
Created Jul 7, 2020
JS regex one-liner for query-string value parsing
View parse-a-single-qs-value.js
// Just replace `placeholder_name` with the actual key for which you want its value.
var placeholderName = decodeURIComponent((window.location.search.match(/[?&]placeholder_name=([^&]+)/)||['', ''])[1]);
@cvan
cvan / useMedia-hook.js
Created Jul 1, 2020
useMedia - React hook for `window.matchMedia` for matching media-query
View useMedia-hook.js
function useMedia(mediaQuery) {
const match = () => {
if (!window.matchMedia) {
return false;
}
return window.matchMedia(mediaQuery).matches;
};
const [value, set] = useState(match);
View theinhardt-fontfix.scss
@mixin text-crop($line-height: 1.3, $top-adjustment: 0px, $bottom-adjustment: 0px) {
// Configured in Step 1
$top-crop: 4;
$bottom-crop: 14;
$crop-font-size: 36;
$crop-line-height: 1.2;
// Apply values to calculate em-based margins that work with any font size
$dynamic-top-crop: max(($top-crop + ($line-height - $crop-line-height) * ($crop-font-size / 2)), 0) / $crop-font-size;
$dynamic-bottom-crop: max(($bottom-crop + ($line-height - $crop-line-height) * ($crop-font-size / 2)), 0) / $crop-font-size;