cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-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.

            
              *{
  box-sizing: border-box;
}
body{
  background: rgb(0,127,180);
}

h3{
  color: #fff;
  text-align:center;
  text-shadow: 0px 1px 5px rgba(0,0,0,0.5);
}

li{
  cursor: pointer;
  list-style: none;
  display: block;
  border: 1px solid black;
  background: #fff;
  margin: 1% 5%;
  padding: 1%;
  box-shadow: 0px 1px 5px rgba(0,0,0,0.5);
}
  li:hover{
    background:beige;
  }
button{
  -webkit-appearance: none;
  background: white;
  border: 1px solid black;
  border-radius: 5px;
  padding: 0.5em;
  box-shadow: 0px 1px 5px rgba(0,0,0,0.5);
  display:inline-block;
  margin-right:5%;
  margin-bottom:25px;
  cursor:pointer;
}
button:hover{
  color: #fff;
  background: rgb(255,85,0);
}
button:active{
  box-shadow: inset 0px 1px 5px rgba(0,0,0,0.5);
}
button:focus{outline:0;}
.selection{
  text-align:right;
}
.fa{
  margin-right: 5px;
}
.selection ul{
  border: 1px solid black;
  background: #fff;
  margin: 1% 5%;
  padding: 2% 4%;
  box-shadow: 0px 1px 5px rgba(0,0,0,0.5);
}
.selection li{
  text-align:left;
  cursor: auto;
  display: block;
  border: none;
  background: #fff;
  margin: 0;
  padding: 0;
  box-shadow: none;
}
.bold,.bold:hover{
  font-weight:bolder;
  color: #fff;
  background: rgb(255,85,0);
}
            
          
!
            
              console.clear();

//Importing all needed modules by destructuring
const { observable, action, computed } = mobx;
const { observer } = mobxReact;
const { Component } = React;

//Enforcing Mobx Strict Mode
mobx.useStrict(true);

/*Mobx Store*/
class UserStore {
	//Observable values can be watched by Observers
	@observable users = [];
	@observable selectedUser = {};
	@computed get selectedId() { 
		return this.selectedUser.id;
	}

	constructor() {
		//Managing Async tasks like ajax calls with Mobx actions
		$.get('https://jsonplaceholder.typicode.com/users').then( users => { 
			this.setUsers(users); 
		});
	}
	//In strict mode, only actions can modify mobx state
	@action setUsers(users) {
		this.users = [...users];
	}
	@action selectUser(user) {
		this.selectedUser = user;
	}
}

//Child Component / Profile (Functional Component)
const Profile = ({ onClick, label, selected }) => {
	const classes = selected ? 'bold' : '';
	return (
		<li onClick={ onClick } className={classes}>
			<i className="fa fa-user"></i> { label } 
		</li>
	);
};

//Child Component /  (Functional Component)
const Selection = ({ user }) => {
	return (
		<ul>
			<li><i className="fa fa-book fa-fw"></i> Name: {user.name}</li>
			<li><i className="fa fa-user fa-fw"></i> Username: { user.username }</li>
			<li><i className="fa fa-phone fa-fw"></i> Phone: {user.phone}</li>
			<li>
				<i className="fa fa-envelope fa-fw"></i> Email: 
				<a href={"mailto:"+user.email}>{user.email}</a>
			</li>
		</ul>
	);
};

//Main Component / Parent Component / App (Class Based Component)
//Observers can react (ba dum tss) to changes in observables
@observer
class App extends React.Component {  
	renderSelection(){
		if (_.isEmpty(this.props.store.selectedUser)) return <noscript />;
		return (
			<div className='selection'>
				<Selection user={this.props.store.selectedUser}/>
				<button onClick={() => {this.props.store.selectUser({})}}>Close Profile</button>
			</div>
		)
	}

	renderProfiles(){
		return this.props.store.users.map((user) => {
			return (
				<Profile
					selected = {user.id === this.props.store.selectedId}
					key = {user.id} 
					label = {user.name} 
					onClick = { () => {this.props.store.selectUser(user)} }
					/>
			);
		});
	}

	render(){
		return (
			<div>
				<h3>Employee Directory</h3>
				{this.renderSelection()}
				{this.renderProfiles()}
			</div>
		);
	}
}

/*Implementation Details*/
//Creating a new Mobx Store Object for our App
const Store = new UserStore();

//Rendering the app to the container and passing in the Mobx Store
ReactDOM.render( <App store={Store} />, document.querySelector("#container"));
            
          
!
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