Skip to content

Instantly share code, notes, and snippets.

View Rendez's full-sized avatar

Luis Merino Rendez

View GitHub Profile
View use-fonts-loaded.ts
import {useLayoutEffect, useState} from 'react';
const fontFaces = [
['tangerine', new FontFace('"Tangerine", serif', 'url(https://fonts.googleapis.com/css?family=Tangerine)')]
] as const;
export function useFontsLoaded(types = ['tangerine']) {
const [fontsLoaded, setFontsLoaded] = useState(false);
const depTypes = types.join(' ');
@Rendez
Rendez / pip.d.ts
Last active September 24, 2023 03:24
TypeScript types for the Picture-in-Picture feature of the Document and the video tag
View pip.d.ts
interface PictureInPictureResizeEvent extends Event {
readonly target: PictureInPictureWindow;
}
interface PictureInPictureWindow {
readonly width: number;
readonly height: number;
onresize(this: PictureInPictureWindow, ev: PictureInPictureResizeEvent): void;
addEventListener(
type: 'resize',
View pre-commit-hooks-test-case.sh
checkGitFile() {
git ls-files --error-unmatch $1 > /dev/null 2>&1
}
# checking for test existence of files using react hooks
filesUsingHooks=$(git diff --cached --name-only --diff-filter=ACR | grep -l -E '^import.+\<use(State|Effect|Context|Reducer|Callback|Memo|Ref|ImperativeHandle|LayoutEffect|DebugValue)' --include \*.js --exclude \*.\*.js)
if [ -n "$filesUsingHooks" ]; then
headline "Checking if your react hooks have tests"
for fileUsingHooks in "$filesUsingHooks"
@Rendez
Rendez / downshift-shadow-root.js
Last active January 9, 2023 09:23
Rendering Downshift in a ShadowRoot
View downshift-shadow-root.js
// this check is borrowed from react
const isProxySupported =
typeof Proxy === 'function' &&
// https://github.com/facebook/react/issues/12011
!Object.isSealed(new Proxy({}, {}));
function createProxyEnvironment(shadowRoot) {
const doc = shadowRoot.ownerDocument;
const properties = {
document: doc,
@Rendez
Rendez / SSRComponent.js
Last active July 11, 2018 09:56
SSR-safe React Component
View SSRComponent.js
const unexpectedLifecycleMethods = ['componentDidMount', 'componentDidUpdate', 'componentWillUpdate', 'UNSAFE_componentWillUpdate', 'componentShouldReceiveProps', 'UNSAFE_componentShouldReceiveProps'];
class SSRComponent extends React.Component {
constructor(...args) {
super(...args);
unexpectedLifecycleMethods.forEach((methodName) => {
if (this.hasOwnProperty(methodName)) {
throw new Error(`Cannot declare '${methodName}' in a server-side-rendered component`);
}
});
@Rendez
Rendez / README.md
Last active August 29, 2015 14:11
Mustache extension(s) in Sprockets for Can.JS via JST
View README.md
@Rendez
Rendez / assets.rb
Created November 12, 2014 22:47
padrino and sinatra-assetpack rake task assets:precompile
View assets.rb
namespace :assets do
def load_apps
require File.expand_path('config/boot.rb', Rake.application.original_dir)
Padrino.mounted_apps
end
desc "Precompile all assets"
task :precompile => 'precompile:build'
namespace :precompile do
@Rendez
Rendez / returning-visitor.js
Created February 24, 2014 10:40
Create a 'returning visitor' or 'new visitor' property for Mixpanel.
View returning-visitor.js
/**
* If the mixpanel cookie exists this user was in the site before,
* we have to consider him returning, only every 24 hours, just like GA.
*/
var RETURNING_VISITOR = document.cookie.indexOf('_mixpanel') != -1;
// if the mixpanel cookie does not exists the user is considered new
if (!RETURNING_VISITOR) {
$.cookie('mp_visitor', true, { expires: 1, path: '/' });
// else the user might not be returning.
} else if ($.cookie('mp_visitor') !== null) {
@Rendez
Rendez / times.jquery.js
Created January 21, 2014 12:01
Ruby port of 'times' function in jquery
View times.jquery.js
$.fn.times = function(callback) {
var i = 0;
do { callback(i); } while (this[0] > ++i);
};
@Rendez
Rendez / snooper.js
Created January 5, 2014 16:03
Snooper Class: creates fake methods for the Mixpanel JS API. It uses sinon.js and sandboxing. Capybara Poltergeist: use injectJS to attach this file before the page loads, and use it to test whether your app is calling the right mixpanel method in your integration tests.
View snooper.js
//
// Copyright (C) 2014 Proudsugar.com
// Author Luis Merino <luis@proudsugar.com>
//
function Snooper() {
// used to keep tab on the sync (push-array) mixpanel fakes'
window.__mixpanel = {};
// fake methods lists
this.stubs = 'track alias name_tag people.set people.set_once people.increment people.append people.track_charge people.clear_charges people.delete_user'.split(' ');