Skip to content

Instantly share code, notes, and snippets.

View basarat's full-sized avatar
🌹
youtube.com/basaratali

Basarat Ali Syed basarat

🌹
youtube.com/basaratali
View GitHub Profile
@basarat
basarat / window-size.tsx
Created October 13, 2016 06:54
Just and idea on window size measurement
import * as onresize from 'onresize';
export type WindowDetails = {
width: number,
height: number
}
/**
* Doesn't render anything itself. Instead calls a function with the details.
* Warning: will not work for server side rendering
*/
import { StyleSheet, css } from 'aphrodite';
/** Just for autocomplete convinience */
export interface CSSProperties extends React.CSSProperties {
':hover'?: React.CSSProperties;
':active'?: React.CSSProperties;
}
/**
* Takes CSSProperties and return a generated className you can use on your component
@basarat
basarat / form.tsx
Created October 6, 2016 05:20
React forms that don't reload page
import * as React from 'react';
interface FormProps {
/**
* Automatically prevents the default browser behavior so you don't have to
*/
onSubmit: () => any;
children?: JSX.Element;
}
export const Form = ({ onSubmit, children}: FormProps) =>
[] + []; // JavaScript will give you "" (which makes little sense), TypeScript will error
//
// other things that are nonsensical in JavaScript
// - don't give a runtime error (making debugging hard)
// - but TypeScript will give a compile time error (making debugging unnecessary)
//
{} + []; // JS : 0, TS Error
[] + {}; // JS : "[object Object]", TS Error
{} + {}; // JS : NaN, TS Error
/**
* Called if a parameter is missing and
* the default value is evaluated.
*/
function mandatory() {
throw new Error('Missing parameter');
}
function foo(mustBeProvided = mandatory()) {
return mustBeProvided;
}
// You don't normally write code like this:
function add() {
return arguments[0] + arguments[1];
}
// You normally write code like:
function add(num1, num2) {
return num1 + num2;
}
function foo(mayNotBeProvided?) {
return mayNotBeProvided;
}
foo(); // OKAY
foo(123); // OKAY
function foo(mustBeProvided) {
return mustBeProvided;
}
foo(); // ERROR
foo(123); // OKAY
@basarat
basarat / required.js
Last active June 23, 2016 09:53
requied.js
/**
* Called if a parameter is missing and
* the default value is evaluated.
*/
function mandatory() {
throw new Error('Missing parameter');
}
function foo(mustBeProvided = mandatory()) {
return mustBeProvided;
}
/* style.css */
.className {
color: green;
}
// source.ts
import { classNayme } from "./style.css";