Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                #app
              
            
!

CSS

              
                
@import url('https://fonts.googleapis.com/css?family=Droid+Sans+Mono')

//$easeIn: cubic-bezier(0.25, 0.46, 0.45, 0.94)
//$easeOut: cubic-bezier(0.55, 0.085, 0.68, 0.53)
$easeInOut: cubic-bezier(0.455, 0.03, 0.515, 0.955)

$turnDuration: 0.6s

// Animation mixin
=animate($name, $easing, $duration: 300ms, $delay: 0s, $iteration: 1, $direction: normal, $fill-mode: forwards)
	animation: $name $duration $easing $delay $iteration $direction $fill-mode

*
	box-sizing: border-box

body
	margin: 0

#app
	display: flex
	position: relative
	width: 100%
	min-height: 100vh
	justify-content: center
	align-items: center
	background-color: #FBAB7E
	background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%)

header
	display: flex
	position: relative

	h1
		font-family: 'Droid Sans Mono', monospace
		font-weight: lighter
		text-transform: uppercase
		letter-spacing: 0.1em
		color: white

$border: whitesmoke
$card: white
$font-family: 'Droid Sans Mono', monospace
$font-size: 5em
$font-width: lighter
$font-color: lighten(black, 20%)

.flipClock
	display: flex
	justify-content: space-between
	width: 3 * 140px + 80px

.flipUnitContainer
	display: block
	position: relative
	width: 140px
	height: 120px
	perspective-origin: 50% 50%
	perspective: 300px
	background-color: $card
	border-radius: 3px
	box-shadow: 0px 10px 10px -10px grey

.upperCard, .lowerCard
	display: flex
	position: relative
	justify-content: center
	width: 100%
	height: 50%
	overflow: hidden
	border: 1px solid $border
	
	span
		font-size: $font-size
		font-family: $font-family
		font-weight: $font-width
		color: $font-color

.upperCard
	align-items: flex-end
	border-bottom: 0.5px solid $border
	border-top-left-radius: 3px
	border-top-right-radius: 3px
	
	span
		transform: translateY(50%)

.lowerCard
	align-items: flex-start
	border-top: 0.5px solid $border
	border-bottom-left-radius: 3px
	border-bottom-right-radius: 3px
	
	span
		transform: translateY(-50%)

.flipCard
	display: flex
	justify-content: center
	position: absolute
	left: 0
	width: 100%
	height: 50%
	overflow: hidden
	backface-visibility: hidden
	
	span
		font-family: $font-family
		font-size: $font-size
		font-weight: $font-width
		color: $font-color
	
	&.unfold
		top: 50%
		align-items: flex-start
		transform-origin: 50% 0%
		transform: rotateX(180deg) // from 180 to 0
		background-color: $card
		border-bottom-left-radius: 3px
		border-bottom-right-radius: 3px
		border: 0.5px solid $border
		border-top: 0.5px solid $border
		
		span
			transform: translateY(-50%)
		
	&.fold
		top: 0%
		align-items: flex-end
		transform-origin: 50% 100%
		transform: rotateX(0deg) // from 0 to -180
		background-color: $card
		border-top-left-radius: 3px
		border-top-right-radius: 3px
		border: 0.5px solid $border
		border-bottom: 0.5px solid $border
		
		span
			transform: translateY(50%)	

.fold
	@include animate(fold, $easeInOut, 0.6s)
	transform-style: preserve-3d

.unfold
	@include animate(unfold, $easeInOut, 0.6s)
	transform-style: preserve-3d

@keyframes fold
	0%
		transform: rotateX(0deg)
	100%
		transform: rotateX(-180deg)

@keyframes unfold
	0%
		transform: rotateX(180deg)
	100%
		transform: rotateX(0deg)
              
            
!

JS

              
                
// function component
const AnimatedCard = ({ animation, digit }) => {
  return(
    <div className={`flipCard ${animation}`}>
      <span>{digit}</span>
    </div>
  );
};

// function component
const StaticCard = ({ position, digit }) => {
  return(
    <div className={position}>
      <span>{digit}</span>
    </div>
  );
};

// function component
const FlipUnitContainer = ({ digit, shuffle, unit }) => {	
  
  // assign digit values
  let currentDigit = digit;
  let previousDigit = digit - 1;

  // to prevent a negative value
  if ( unit !== 'hours') {
    previousDigit = previousDigit === -1 
      ? 59 
      : previousDigit;
  } else {
    previousDigit = previousDigit === -1 
      ? 23 
      : previousDigit;
  }

  // add zero
  if ( currentDigit < 10 ) {
    currentDigit = `0${currentDigit}`;
  } 
  if ( previousDigit < 10 ) {
    previousDigit = `0${previousDigit}`;
  }

  // shuffle digits
  const digit1 = shuffle 
    ? previousDigit 
    : currentDigit;
  const digit2 = !shuffle 
    ? previousDigit 
    : currentDigit;

  // shuffle animations
  const animation1 = shuffle 
    ? 'fold' 
    : 'unfold';
  const animation2 = !shuffle 
    ? 'fold' 
    : 'unfold';

  return(
    <div className={'flipUnitContainer'}>
      <StaticCard 
        position={'upperCard'} 
        digit={currentDigit} 
        />
      <StaticCard 
        position={'lowerCard'} 
        digit={previousDigit} 
        />
      <AnimatedCard 
        digit={digit1}
        animation={animation1}
        />
      <AnimatedCard 
        digit={digit2}
        animation={animation2}
        />
    </div>
  );
};

// class component
class FlipClock extends React.Component {
	
  constructor(props) {
		super(props);
		this.state = {
			hours: 0,
			hoursShuffle: true,
			minutes: 0,
			minutesShuffle: true,
			seconds: 0,
			secondsShuffle: true
		};
	}
  
	componentDidMount() {
		this.timerID = setInterval(
			() => this.updateTime(),
			50
		);
	}
  
	componentWillUnmount() {
		clearInterval(this.timerID);
	}
  
	updateTime() {
		// get new date
		const time = new Date;
		// set time units
		const hours = time.getHours();
		const minutes = time.getMinutes();
		const seconds = time.getSeconds();
		// on hour chanage, update hours and shuffle state
		if( hours !== this.state.hours) {
			const hoursShuffle = !this.state.hoursShuffle;
			this.setState({
				hours,
				hoursShuffle
			});
		}
		// on minute chanage, update minutes and shuffle state
		if( minutes !== this.state.minutes) {
			const minutesShuffle = !this.state.minutesShuffle;
			this.setState({
				minutes,
				minutesShuffle
			});
		}
		// on second chanage, update seconds and shuffle state
		if( seconds !== this.state.seconds) {
			const secondsShuffle = !this.state.secondsShuffle;
			this.setState({
				seconds,
				secondsShuffle
			});
		}
	}
  
	render() {
    
    // state object destructuring
		const { 
      hours, 
      minutes, 
      seconds, 
      hoursShuffle, 
      minutesShuffle, 
      secondsShuffle 
    } = this.state;
		
    return(
			<div className={'flipClock'}>
				<FlipUnitContainer 
					unit={'hours'}
					digit={hours} 
					shuffle={hoursShuffle} 
					/>
				<FlipUnitContainer 
					unit={'minutes'}
					digit={minutes} 
					shuffle={minutesShuffle} 
					/>
				<FlipUnitContainer 
					unit={'seconds'}
					digit={seconds} 
					shuffle={secondsShuffle} 
					/>
			</div>
		);
	}
}

// function component
const Header = () => {
  return(
    <header>
      <h1>React Flip Clock</h1>
    </header>
  );
};

// function component
const App = () => {
  return (
    <div>
      <Header />
      <FlipClock />
    </div>
  );
};

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

Console