css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
@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: 140px
	height: 60px
	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: 1px 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)
            
          
!
            
              
// function component
const AnimatedCard = ({ position, animation, digit }) => {
  return(
    <div className={`flipCard ${position} ${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 
        position={'first'}
        digit={digit1}
        animation={animation1}
        />
      <AnimatedCard 
        position={'second'}
        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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console