cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              @import url('https://fonts.googleapis.com/css?family=Merriweather:700|Montserrat:700|Bungee');

#display {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #273043;
  vertical-align: middle;
  
  h1 {
    display: flex;
    align-items: center;
    justify-content: center;
    
    width: 100%;
    height: 100%;
  }
  #buttons {
    horizontal-align: middle;
    text-align: center;
    width: 100%;
    position: fixed;
    bottom: 30px;
    
    button {
      margin: 5px;
      border-radius: 0;
      box-shadow: none;
      font-size: 1.1em;
      padding: 10px;
      
      .underline {
        text-decoration: underline;
      }
    }
    
    .btn-default {
      background-color: transparent;
      color: white;
    }
    .btn-primary {
      
    }
  }
  
}


#history {
  position: absolute;
  top: 0;
  left: 0;
  color: white;
  padding: 10px;
  opacity: 0.8;
  
  #future {
    color: #9197AE;
  }
  #present {
    color: #EFF6EE;
  }
  #past {
    color: #9197AE;
  }
}

            
          
!
            
              /*********
* REACT
**********/
const Editor = ({ past, present, future, action, undo, redo }) => (
    <div id='display'>
       <div id='history'>
        <div id='future'>
          {future.map((item, id) =>
                      <div>{item.message}</div>
                     )}
        </div>
        <div id='present'>
          <div>{present.message}</div>
        </div>
        <div id='past'>
          {past.map((item, id) =>
                    <div>{item.message}</div>
                   )}
        </div>
      </div>

      <h1 style={{ 
        color: present.color,
        'font-family': present['font-family'],
        'font-size': present['font-size']+'px', 
        'font-style': present['font-style'],
        opacity: present.opacity, 
        transform: 'rotate('+present.rotate+'deg) rotatey('+present.rotatey+'deg)'
      }}>
          Hello
      </h1>
      <div id='buttons'>
        <button className="btn btn-default" onClick={action['color']}><span className='underline'>C</span>olor</button>
        <button className="btn btn-default" onClick={action['font-family']}><span className='underline'>F</span>ont</button>
        <button className="btn btn-default" onClick={action['font-style']}><span className='underline'>I</span>talic</button>
        <button className="btn btn-default" onClick={action['rotatey']}><span className='underline'>M</span>irror</button>
        <button className="btn btn-default" onClick={action['opacity']}><span className='underline'>O</span>pacity</button>
        <button className="btn btn-default" onClick={action['rotate']}><span className='underline'>R</span>otate</button>
        <button className="btn btn-default" onClick={action['font-size']}><span className='underline'>S</span>ize</button>

        <button id="undo" className="btn btn-primary" onClick={undo} disabled={past.length == 0}>Undo</button>
        <button id="redo" className="btn btn-primary" onClick={redo} disabled={future.length == 0}>Redo</button>
      </div>
    </div>
)

/*********
* REACT-REDUX
**********/

const mapStateToProps = (state, ownProps) => {
  return {
    past: state.past.slice(0).reverse(),
    present: state.present,
    future: state.future.slice(0).reverse()
  }
}

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    undo: () => {
      dispatch({
        type: 'UNDO',
      })
    },
    redo: () => {
      dispatch({
        type: 'REDO',
      });
    }
  }
}

/* Import { connect, Provider } from 'react-redux' */
const { connect, Provider } = ReactRedux;

const ConnectedEditor = connect(mapStateToProps, mapDispatchToProps)(Editor);

/* Wrapper function for ReactDOM.render functionality for the app */
const render = () => {
  ReactDOM.render(
    <Provider store={store}>
      <ConnectedEditor action={action} />
    </Provider>, 
    document.getElementById('app')
  );
}

/*********
* REDUX
**********/

// https://redux.js.org/docs/recipes/ImplementingUndoHistory.html
function undoable(reducer) {
  // Call the reducer with empty action to populate the initial state
  const initialState = {
    past: [],
    present: reducer(undefined, {}),
    future: []
  }

  // Return a reducer that handles undo and redo
  return function (state = initialState, action) {
    const { past, present, future } = state

    switch (action.type) {
      case 'UNDO':
        const previous = past[past.length - 1]
        const newPast = past.slice(0, past.length - 1)
        return {
          past: newPast,
          present: previous,
          future: [ present, ...future ]
        }
      case 'REDO':
        const next = future[0]
        const newFuture = future.slice(1)
        return {
          past: [ ...past, present ],
          present: next,
          future: newFuture
        }
      default:
        // Delegate handling the action to the passed reducer
        const newPresent = reducer(present, action)
        if (present === newPresent) {
          return state
        }
        return {
          past: [ ...past, present ],
          present: newPresent,
          future: []
        }
    }
  }
}

// States
const states = {
  color: ['#04E762', '#F5B700', '#DC0073', '#008BF8', '#89FC00'],
  'font-family': ["'Montserrat', sans-serif", "'Merriweather', serif", "'Bungee', cursive"],
  'font-size': [150, 200, 250],
  'font-style': ['normal', 'italic'],
  opacity: [1, 0.8, 0.5, 0.3],
  rotate: [0, 180],
  rotatey: [0, 180]
};

const initialState = {
  message: 'Initial',
  
  color: '#DC0073',
  'font-family': "'Montserrat', sans-serif",
  'font-size': 200,
  'font-style': 'normal',
  opacity: 1,
  rotate: 180,
  rotatey: 180
};

const action = {
  'color': () => {
    store.dispatch({
      type: 'COLOR',
    });
  },
  'font-family': () => {
    store.dispatch({
      type: 'FONT-FAMILY',
    });
  },
  'font-style': () => {
    store.dispatch({
      type: 'FONT-STYLE',
    });
  },
  'rotatey': () => {
    store.dispatch({
      type: 'ROTATEY',
    });
  },
  'opacity': () => {
    store.dispatch({
      type: 'OPACITY',
    });
  },
  'rotate': () => {
    store.dispatch({
      type: 'ROTATE',
    });
  },
  'font-size': () => {
    store.dispatch({
      type: 'FONT-SIZE',
    });
  }
}

// State traversal
Array.prototype.next = function next(item) {
    const index = this.indexOf(item);
    if(index >= 0 && index < this.length - 1) {
      return this[index + 1];
    }else {
      return this[0];
    }
}

const mainReducer = (state = initialState, action) => {
    switch (action.type) {
      case 'COLOR':
      case 'FONT-FAMILY':
      case 'FONT-SIZE':
      case 'FONT-STYLE':
      case 'OPACITY':
      case 'ROTATE':
      case 'ROTATEY':
        let property = action.type.toLowerCase();
        let currentValue = state[property];
        let nextValue = states[property].next(currentValue);
        return Object.assign({}, state, {
          [property]: nextValue,
          message: property+': '+nextValue 
        });
      default:
        return state;
    }
};

/* Import { createStore } from 'redux' */
const { createStore } = Redux;

const undoableReducer = undoable(mainReducer);
const store = createStore(undoableReducer);

/* Initial render */
render();


function handleKeyDown(event) {
    switch(event.key) {
      case 'c':
      case 'C':
        action['color']();
        break;
      case 'f':
      case 'F':
        action['font-family']();
        break;
      case 'i':
      case 'I':
        action['font-style']();
        break;
      case 'm':
      case 'M':
        action['rotatey']();
        break;
      case 'o':
      case 'O':
        action['opacity']();
        break;
      case 'r':
      case 'R':
        action['rotate']();
        break;
      case 's':
      case 'S':
        action['font-size']();
        break;
    }
}

window.addEventListener('keydown', handleKeyDown);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console