Skip to content

Instantly share code, notes, and snippets.

View mattmccray's full-sized avatar

Matt McCray mattmccray

View GitHub Profile
@mattmccray
mattmccray / component.coffee
Last active August 29, 2015 13:57
Build React Components using natural CoffeeScript syntax.
# Public: Define components as CoffeeScript classes
#
# Example:
#
# class UserChip extends Component
#
# @staticMethod: -> # becomes a static method on the React Component
# "hello"
#
# render: ->

So... I would like to be able to use coffeescript without having to add the ".coffee" extension in my requires (in the lib code, not the test code).

I would guess that in my test a require('coffee-script/register') would work -- but I'd be wrong. :)

For example, I'd like to get this working:

util.coffee:

module.exports= 
  log: (msg)->
@mattmccray
mattmccray / ng-core.coffee
Last active August 29, 2015 14:04
A CoffeeScript-rific way of using AngularJS.
# Requires SugarJS...
@Ng=
annotateDependencies: (Class, props={})->
Class.$inject or= []
Class.$injected or= []
for name,deps of props
deps= [ deps ].flatten()
Class.$inject.push deps.shift()
Class.$injected.push { name, params:deps }
@mattmccray
mattmccray / range.js
Created October 13, 2014 13:58
Creates a 'range' Array, extracted from Liquid.js
// Creates a 'range' Array, extracted from Liquid.js
function makeRange(from, to) {
var arr= [],
left= parseInt(from),
right= parseInt(to);
// Check if left and right are NaN, if so try as characters
if( isNaN(left + right) ) {
// TODO Add in error checking to make sure ranges are single
// character, A-Z or a-z, etc.
left = from.charCodeAt(0);
@mattmccray
mattmccray / 0_reuse_code.js
Last active August 29, 2015 14:08
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@mattmccray
mattmccray / 1_example.md
Last active August 29, 2015 14:09
StyleBuilder for React components

StyleBuilder

Simple JS-based StyleSheets with no cascading.

That's right. There is intentionally no support for 'externalizing' the CSS, or any cascading support. This is a feature. Be explicit. It's best for everyone.

Also a feature: CSS variables are expanded as soon as the objects are merged. So no, you can't change a variable and have it automatically update any currently rendered nodes.

@mattmccray
mattmccray / component-with-mixins.js
Created February 17, 2015 19:42
ES6 Classes w/Mixins -- Idea -- React 0.13
export function ComponentWithMixins( ...mixins) {
class MixedComponent extends React.Component { }
for( let mixin of mixins) {
// TODO: Would need to handle mixin collisions...
for( let name of Object.keys( mixin)) {
MixedComponent.prototype[ name]= mixin[ name]
}
}
@mattmccray
mattmccray / componentStyles.js
Last active August 29, 2015 14:16
React Inline Styles, ES6 Template Strings Processed via LessCSS
/* global less, _ */
export function componentStyles( context={}, filename='?') {
if( _.isArray( context)) {
return componentStyles()( context)
}
let ctx= _.extend( {}, componentStyles.context, context)
return function ( rules, ...subs){
@mattmccray
mattmccray / StoreStateMixin.js
Last active August 29, 2015 14:16
Alt StoreStateMixin
import alt from 'data/alt'
export function StoreStateMixin( ...stores) {
stores= stores.map(( store)=>{
if( typeof( store) === 'string') {
return alt.getStore( store)
}
else {
return store
@mattmccray
mattmccray / AnimationMixin.js
Created March 10, 2015 07:22
React AnimationMixin - for Animate.css
// Extracted from a live application. Known to work with latest Chrome/Firefox
// and Animate.css. (IE untested)
export let AnimationMixin= {
playAnimation( animation='pulse', speed=1000) {
if(! this.isMounted()) {
log.warn( "Trying to call .playAnimation() on an unmounted component!")
return
}