Skip to content

Instantly share code, notes, and snippets.

View ibrennan's full-sized avatar
🎯
Focusing

Ian Brennan ibrennan

🎯
Focusing
View GitHub Profile
@ibrennan
ibrennan / useVisibilityState.js
Last active February 20, 2024 21:23
useVisibilityState - A simple React Hook for tracking document.visibilityState, useful for things like suspending API polling on inactive tabs
import { useState, useEffect, useCallback } from 'react';
export const useVisbilityState = () => {
const [visibilityState, setVisibilityState] = useState(null);
const handleVisbilityChange = useCallback(() => {
setVisibilityState(document.visibilityState);
}, [setVisibilityState]);
useEffect(() => {
git config --global core.excludesfile ~/.gitignore
echo ".DS_Store" >> ~/.gitignore
echo "node_modules/" >> ~/.gitignore
echo "npm-debug.log" >> ~/.gitignore
echo ".sass-cache/" >> ~/.gitignore
echo "*.sublime-workspace" >> ~/.gitignore
@ibrennan
ibrennan / scroll-tracking.js
Created May 18, 2015 14:24
Google Analytics Scroll Position Tracking
// Implementation for tracking the vertical scroll of a page and firing events at 25%, 50%, 75%, 100% positions
// Requires jQuery (feel free to fork a version that doesn't!)
var scrollPercentages = {
scroll25: false,
scroll50: false,
scroll75: false,
scroll100: false
}
@ibrennan
ibrennan / gist:8d2d0edf0f92591d1a6a
Created January 24, 2015 20:55
Swift - Initiate new view controller from AppDelegate
// Name of storyboard, without the .storyboard extension
var storyboard = UIStoryboard(name: "Main", bundle: nil)
// The type is the view controller class name, in this case 'ViewControllerLaunch'
// You need to make sure the view controller in storyboard has an identifier set
// The identifier in this example is 'Launch'
var viewController: ViewControllerLaunch = storyboard.instantiateViewControllerWithIdentifier("Launch") as ViewControllerLaunch
// Show this view controller
window?.rootViewController = viewController
@ibrennan
ibrennan / Swift - Hex to UIColor
Last active August 29, 2015 14:10 — forked from shadcn/gist:de147c42d7b3063ef7bc
Handy Swift function for converting a hex value (#FF0000) to Apple UIColor
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString
if (cString.hasPrefix("#")) {
cString = cString.substringFromIndex(advance(cString.startIndex, 1))
}
if (countElements(cString) != 6) {
return UIColor.grayColor()
}