Skip to content

Instantly share code, notes, and snippets.

View Rendez's full-sized avatar

Luis Merino Rendez

View GitHub Profile
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 May 23, 2024 22:23
TypeScript types for the Picture-in-Picture feature of the Document and the video tag
interface PictureInPictureResizeEvent extends Event {
readonly target: PictureInPictureWindow;
}
interface PictureInPictureWindow {
readonly width: number;
readonly height: number;
onresize(this: PictureInPictureWindow, ev: PictureInPictureResizeEvent): void;
addEventListener(
type: 'resize',
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 October 13, 2023 11:08
Rendering Downshift in a ShadowRoot
// 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
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

Can.js: how to register mustache templates via sprockets.

Conditions

  • Must use Sprockets for requiring mustache templates.
  • Mustache templates have to be compiled with can.mustache for compatibility with 2.0 and 3.0
  • Must expose compiled templates under a namespace, defaults to the commonly used JST variable exposed in the main object (window)

Example

@Rendez
Rendez / assets.rb
Created November 12, 2014 22:47
padrino and sinatra-assetpack rake task assets:precompile
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.
/**
* 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
$.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.
//
// Copyright (C) 2014 Proudsugar.com
// Author Luis Merino <[email protected]>
//
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(' ');