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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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="root">React components will be rendered here...</div>

              
            
!

CSS

              
                .profile
  border: 2px solid silver
  border-radius: 6px
  padding: 10px

.avater
  margin-bottom: 8px
  img
    display: inline-block
    vertical-align: middle

  span
    margin-left: 10px

.details
  width: 100%

.toggle
  background: #3498db
  width: 100px
  padding: 4px 0
  color: #FFF
  border-radius: 3px

  &:before
    content: ''
    display: inline-block
    position: relative
    left: 3px
    border: 6px solid transparent
    border-left-color: #FFF

  &.active:before
    left: 0
    top: 3px
    border-color: #FFF transparent transparent

  &:hover
    cursor: pointer



              
            
!

JS

              
                const { FunctionComponent, useState, useEffect } = React;
const { createStore, combineReducers, Action } = Redux;
const { Provider, connect } = ReactRedux;

console.clear()

////////////////////////////////////////
//
// Common
//

interface UserData {
  name: string;
  image: string;
  desc: string;
  url: string;
}

const userData: UserData = {
  name: 'Nulab Taro',
  image: 'https://picsum.photos/64',
  desc: 'Results-driven collaboration software that amplifies productivity',
  url: 'https://nulab.com/',
};

////////////////////////////////////////
//
// Toggle
//

const Toggle: FunctionComponent<{ enabled: boolean }> =
  ({ initial = false, children }) => {
    const [enabled, setEnabled] = useState(initial);
    return (
      <div className={'details'}>
        <button className={classNames('toggle', {'active' : enabled})} onClick={() => setEnabled(!enabled)}>
          {enabled ? 'Hide' : 'Show'} details
        </button>
        {enabled && children}
      </div>
    );
  };

////////////////////////////////////////
//
// UserProfile
//

const UserAvater = ({ user }) => (
  <div className="avater">
    <img src={user.image} />
    <span>{user.name}</span>
  </div>
);

const UserDetails = ({ user }) => (
  <ul>
    <li>{user.desc}</li>
    <li><a href={user.url}>URL</a></li>
  </ul>
);

const useFetch = defaultData => {
  const [data, updateData] = useState(defaultData);

  useEffect(() => updateData(userData));

  return data
}

const UserProfile = () => {
  const result = useFetch({})

  return (
    <div className="profile">
      <UserAvater user={result} />
      <Toggle>
        <UserDetails user={result} />
      </Toggle>
    </div>
  )
}

////////////////////////////////////////
//
// Redux version
//

enum ActionNames {
  LOADED = 'profile/LOADED',
  TOGGLE = 'profile/TOGGLE',
}

interface CounterAction extends Action {
  type: ActionNames;
  payload: UserData?;
}

interface ProfileState {
  isOpen: boolean;
  user?: UserData;
}

const reducer = (state: ProfileState = { isOpen: false }, action: CounterAction) => {
  switch(action.type) {
    case ActionNames.LOADED:
      return { ...state, user: action.payload };
    case ActionNames.TOGGLE:
      return { ...state, isOpen: !state.isOpen };
    default:
      return state;
  }
}

const store = createStore(reducer);

const UserProfileRBase = ({ state, dispatch }: any) => {
  useEffect(() => { dispatch({ type: ActionNames.LOADED, payload: userData }) });

  return (
    <div className="profile">
      {state.user && <UserAvater user={state.user} />}
      <div className={'details'}>
        <button className={'toggle'} onClick={() => dispatch({ type: ActionNames.TOGGLE })}>
          {state.isOpen ? 'Hide' : 'Show'} details
        </button>
        {state.user && state.isOpen && <UserDetails user={state.user} />}
      </div>
    </div>
  );
};

const UserProfileR = connect(
  (state: ProfileState) => ({ state }),
)(UserProfileRBase)

////////////////////////////////////////
//
// render both components
//

ReactDOM.render(
  (<div>
    <h3>UserProfile Hooks version</h3>
    <UserProfile />
    <UserProfile />

    <h3>UserProfile Redux version</h3>
    <Provider store={store}>
      <UserProfileR/>
    </Provider>
    <Provider store={store}>
      <UserProfileR/>
    </Provider>
   </div>),
  document.querySelector('#root'));

              
            
!
999px

Console