Skip to content

Instantly share code, notes, and snippets.

View breadadams's full-sized avatar

Brad Adams breadadams

View GitHub Profile
@gaearon
gaearon / uselayouteffect-ssr.md
Last active July 7, 2024 18:47
useLayoutEffect and server rendering

If you use server rendering, keep in mind that neither useLayoutEffect nor useEffect can run until the JavaScript is downloaded.

You might see a warning if you try to useLayoutEffect on the server. Here's two common ways to fix it.

Option 1: Convert to useEffect

If this effect isn't important for first render (i.e. if the UI still looks valid before it runs), then useEffect instead.

function MyComponent() {
@morajabi
morajabi / useRect.js
Created February 18, 2019 14:35
useRect — getBoundingClientRect() React Hook with resize handler
import { useLayoutEffect, useCallback, useState } from 'react'
export const useRect = (ref) => {
const [rect, setRect] = useState(getRect(ref ? ref.current : null))
const handleResize = useCallback(() => {
if (!ref.current) {
return
}
@505aaron
505aaron / App.test.js
Last active July 21, 2020 08:15
createPortal Mock for react-test-renderer
jest.mock('react-dom');
import React from 'react';
import { createPortal } from 'react-dom';
import renderer from 'react-test-renderer';
import ShallowRenderer from 'react-test-renderer/shallow';
import ReactDOM from 'react-dom';
class Drop extends React.Component {
constructor(props) {

Aligning images

This is a guide for aligning images.

See the full Advanced Markdown doc for more tips and tricks

left alignment

@teameh
teameh / PanResponder_Overview.js
Last active October 21, 2023 05:28
React native PanResponder interface overview
this._panResponder = PanResponder.create({
// ----------- NEGOTIATION:
// A view can become the touch responder by implementing the correct negotiation methods.
// Should child views be prevented from becoming responder on first touch?
onStartShouldSetPanResponderCapture: (evt, gestureState) => () => {
console.info('onStartShouldSetPanResponderCapture');
return true;
},
@hsleonis
hsleonis / better-font-smoothing.css
Last active June 13, 2024 18:26
Better font smoothing in cross browser
html {
/* Adjust font size */
font-size: 100%;
-webkit-text-size-adjust: 100%;
/* Font varient */
font-variant-ligatures: none;
-webkit-font-variant-ligatures: none;
/* Smoothing */
text-rendering: optimizeLegibility;
-moz-osx-font-smoothing: grayscale;
@NiklasHogefjord
NiklasHogefjord / klarna-checkout-extra-fields.php
Last active September 16, 2019 13:19
Add custom input fields to Klarna Checkout page. Added via shortcode [woocommerce_klarna_checkout_extra_fields]. Saved as post_meta to the order directly on .blur via ajax.
<?php
/**
* Plugin Name: Klarna Checkout Extra Fields
* Plugin URI: http://krokedil.com
* Description: Add custom input fields to Klarna Checkout page. Added via shortcode [woocommerce_klarna_checkout_extra_fields]. Saved as post_meta to the order directly on .blur via ajax.
* Version: 1.1
* Author: Krokedil
* Author URI: http://krokedil.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
@bomberstudios
bomberstudios / Change Font.sketchplugin
Last active May 20, 2024 03:42
Change font family for all text layers in Sketch
// Change font (ctrl a)
var doc = context.document,
selection = context.selection,
font_name = [doc askForUserInput:"Font name:" initialValue:"Arial"];
function check_layer(layer){
log(layer)
var className = layer.className()
log("Checking layer " + layer + " of klass: " + className)
if (className == "MSTextLayer") {
@timohausmann
timohausmann / gist:5003280
Created February 21, 2013 08:43
Javascript Math: get the distance between two points.
/**
* return the distance between two points.
*
* @param {number} x1 x position of first point
* @param {number} y1 y position of first point
* @param {number} x2 x position of second point
* @param {number} y2 y position of second point
* @return {number} distance between given points
*/
Math.getDistance = function( x1, y1, x2, y2 ) {
@terkel
terkel / _decimal.scss
Last active June 17, 2024 14:07
Rounding decimals in Sass
// _decimal.scss | MIT License | gist.github.com/terkel/4373420
// Round a number to specified digits.
//
// @param {Number} $number A number to round
// @param {Number} [$digits:0] Digits to output
// @param {String} [$mode:round] (round|ceil|floor) How to round a number
// @return {Number} A rounded number
// @example
// decimal-round(0.333) => 0