Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Ubuntu');
body {
  background-color: #fafafa;
  background: linear-gradient(to left, #5EB3E2, #afafea);
  font-family: "Ubuntu", Arial, sans-serif;
}

.item-container {
  display: flex;
  flex-direction: column;
  width: 670px;
}

.btn-container {
  display: flex;
  width: 670px;
  padding: 10px;
}

.btn {
  padding: 12px 20px;
  border: 0;
  border-radius: 6px;
  background: #f59b9b;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  color: white;
  font-family: inherit;
  font-size: 14px;
  letter-spacing: .4px;
  text-transform: uppercase;
  cursor: pointer;
  outline: none;
  transform: none;
  transition: 0.3s ease-out;
  
  & + & {
    margin-left: 16px;
  }
}

.item {
  margin: 10px;
  padding: 32px 45px;
  background-color: white;
  border-radius: 6px;
  box-shadow: 1px 2px 8px 0px rgba(0, 0, 0, 0.4);
  color: #585858;
  transition: 0.3s ease-out;
  
  &:hover {
    box-shadow: 1px 4px 12px 0px rgba(68, 66, 132, 0.6);
  }
}
              
            
!

JS

              
                const { Component, Fragment } = React;

class AnimatedItems extends Component {
	componentDidMount() {
		this.initialPositions = this.getElementPositions();
	}

	componentDidUpdate(prevProps) {
		let newChild = null;
		let newPositions;

		// Check if new item has been added
		if (prevProps.children.length !== this.props.children.length) {
			newChild = this.returnNewChild(prevProps);
			newPositions = this.getElementPositions(newChild);
		}
		else {
			newPositions = this.getElementPositions();
		}

		Object.keys(newPositions).forEach(key => {
			const newData = newPositions[key];
			let prevData;
			let deltaX;
			let deltaY;

			if (newData.isNewElement) {
				deltaX = 0;
				deltaY = -newData.clientRect.height / 4;
      }
			else {
				prevData = this.initialPositions[key];
				deltaX = prevData.clientRect.left - newData.clientRect.left;
				deltaY = prevData.clientRect.top - newData.clientRect.top;
			}

			requestAnimationFrame(() => {
				const child = this.container.children[newData.index];
				child.style.transform = `translate3d(${deltaX}px, ${deltaY}px, 0)`;
				child.style.transition = 'all 0s';
				if (newData.isNewElement) {
					child.style.opacity = '0';
				}

				requestAnimationFrame(() => {
					child.style.transition = 'all 0.5s';
					child.style.transform = 'translate3d(0, 0, 0)';
					if (newData.isNewElement) {
						child.style.opacity = '1';
					}
				});

			});
		});
		this.initialPositions = newPositions;
	}

	render() {
		return (
			<div ref={el => this.container = el} className='item-container'>
				{this.props.children}
			</div>
		);
	}

	// find new child that was added to the dom by diff against initial values on mount
	returnNewChild = (prevProps) => {
		return this.props.children.filter(item => {
			const isMatch = prevProps.children.some(prevItem => {
				return item.key === prevItem.key;
			});
			if (!isMatch) {
				return item;
			}
		})
	}

	getElementPositions = (newChild = null) => {
		const { children } = this.props;
		const data = Array.from(this.container.children).reduce((obj, el, index) => {
			obj[children[index].key] = {
				index,
				clientRect: el.getBoundingClientRect(),
				isNewElement: newChild ? newChild[0].key === children[index].key : false,
			};
			return obj;
		}, {});
		return data;
	}
}

class App extends Component {
  state = {
    items: [
      {id: 0, text: 'First Item' },
      {id: 1, text: 'Second Item' },
      {id: 2, text: 'Third Item' },
    ]
  };

  reverseItems = () => {
    this.setState({
      items: this.state.items.reverse(),
    });
  }
  
  shuffle = () => {
    const shuffledItems = this.shuffleArray();
    this.setState({
      items: shuffledItems
    });
  }
  
  insert = () => {
    const currentLength = this.state.items.length;
    const newItem = { id: currentLength, text: `New Item ${currentLength + 1}`};
    this.setState(prevState => ({
      items: [
        newItem,
        ...prevState.items,
      ],
    }));
  }
  
  shuffleArray = () => {
    let array = this.state.items;
    for (let i = array.length - 1; i > 0; i--) {
      let j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  }

  render() {
    const { items } = this.state;
    return (
      <Fragment>
        <div className='btn-container'>
          <button className='btn' onClick={this.reverseItems}>Reverse Items</button>
          <button className='btn' onClick={this.shuffle}>Shuffle items</button>
          <button className='btn' onClick={this.insert}>Insert items</button>
        </div>
        <AnimatedItems>
          {items.map(item => (
            <div key={item.id} className={'item'}>{item.text}</div>
          ))}
        </AnimatedItems>
      </Fragment>
    );
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('app')
);
              
            
!
999px

Console