Skip to content

Instantly share code, notes, and snippets.

@gaearon
gaearon / Stateful.js
Created April 23, 2015 16:57
Stateful.js
import React, { Component } from 'react';
// Usage:
//
// @Stateful({
// initialize(props) {
// return {
// count: 0
// };
// },
@gaearon
gaearon / react-dnd-1.0-alpha-example.js
Last active August 29, 2015 14:19
React DnD 1.0 alpha example
import React from 'react';
import { configureDragDrop, configureDragDropContext } from 'react-dnd';
import HTML5Backend from 'react-dnd/modules/backends/HTML5';
// Note: @configureDragDropContext is called a “decorator”.
// This desugars roughly as class App { ... } App = configureDragDropContext(HTML5Backend)(App);
// You can enable decorators by putting { "stage": 1 } in your .babelrc.
@configureDragDropContext(HTML5Backend)
export default class App extends React.Component {
export default {
getInitialState() {
const data = {};
this.subscribe(this.props, this.context, (key, value) => {
data[key] = value;
});
this.unsubscribe();
return { data };
let BlogPostPage = React.createClass({
render() {
<div>
<SiteNavigation />
<MainContentArea>
{connectToStores({
posts: store => ({
post: store.getPost(this.props.postId),
nextPost: store.getPostAfter(this.props.postId)
})
@gaearon
gaearon / PaginatedList.js
Created March 9, 2015 13:43
PaginatedList with invalidation
'use strict';
var { union, find, without, clone } = require('underscore'),
invariant = require('react/lib/invariant');
class PaginatedList {
constructor(ids) {
this._ids = ids || [];
this._pageCount = 0;
this._nextPageUrl = null;
@gaearon
gaearon / Scrolljack.jsx
Last active April 1, 2020 17:11
Scrolljack.jsx
'use strict';
var React = require('react'),
PureRenderMixin = require('../mixins/PureRenderMixin'),
getSupportedTransformProperty = require('../utils/getSupportedTransformProperty'),
{ PropTypes, Children } = React;
const transformProperty = getSupportedTransformProperty();
const styles = {
root: {
@gaearon
gaearon / getSupportedTransformProperty.js
Created January 27, 2015 19:45
getSupportedTransformProperty.js
'use strict';
var TRANSFORM_VARIANTS = {
'WebkitTransform': '-webkit-transform',
'Transform': 'transform'
};
function test() {
var testEl = document.createElement('div'),
style = testEl.style;
'use strict';
var ScreenSizeStore = require('../stores/ScreenSizeStore');
var ScreenSizeMixin = {
getInitialState() {
return this.getScreenSizeState();
},
getScreenSize() {
'use strict';
var AppDispatcher = require('../dispatcher/AppDispatcher'),
ActionTypes = require('../constants/ActionTypes'),
ScreenSizes = require('../constants/ScreenSizes'),
{ createStore } = require('../utils/StoreUtils');
function getScreenSize() {
if (window.matchMedia('(min-width: 100em)').matches) {
return ScreenSizes.EXTRA_LARGE;
'use strict';
/**
* Adapted from https://github.com/Khan/react-components/blob/master/js/timeout-transition-group.jsx
* with the following additions:
*
* - Use BEM-ish modifiers (--enter, --enter--active, --leave, --leave--active)
* - Work better with rAF batching strategy (see https://github.com/facebook/react/issues/2292)
*
* The CSSTransitionGroup component uses the 'transitionend' event, which