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

            
              @import url(https://fonts.googleapis.com/css?family=Lato:100,300);

body {
  background: #DDD;
}

a {
  text-decoration: none;
  color: #333;
  font-family: Lato;
  font-weight: 400;
}

*:focus {
  outline: 0;
}

input[type="wsearch"] {
  background: none;
  border: none;
  border-bottom: 1px solid #AAA;
  padding: 1px;
  font-family: Lato;
  font-weight: 100;
  font-size: 2rem;
  width: 11em;
}

#content {
  height: 100vh;
  display: flex;
  justify-content: center;
}

div.result {
  transition: background-color 0.35s ease-in;
  background-color: none;
}

div.result:hover {
  background-color: #FFFF55;
}

.fade-enter {
  opacity: 0.01;
}

.fade-enter.fade-enter-active {
  opacity: 1;
  transition: opacity 275ms ease-in;
}

.fade-leave {
  opacity: 0.88;
}

.fade-leave.fade-leave-active {
  opacity: 0.01;
  transition: opacity 175ms ease-in;
}


            
          
!
            
              const ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;

class WikiViewer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {results: []};
    this.latestAjax = latest((options) => {
      if (options.url.slice(-1) == '=') return Promise.resolve(null);
      else return $.ajax(options);
    });
  }
  searchWiki = (search) => {
    const options = {
      url: this.props.url + search,
      dataType: 'jsonp'
    };
    this
    .latestAjax(options)
    .then(data => {
      if (data == null) return this.setState({results: []});
      const pages = data.query.pages;
      const results = Object.keys(pages).map(k => pages[k]).map(res => {
        res.url = 'http://en.wikipedia.org/wiki/' + encodeURIComponent(res.title);
        return res;
      });
      this.setState({results});
    })
    .catch(e => console.log(e))
  };
  render() {
    return <div style={{width: '100%'}}>
      <Search onSearch={this.searchWiki} />
      <ResultList results={this.state.results} />
    </div>
  }
}

class Search extends React.Component {
  handleInput = (e) => {
    this.props.onSearch(e.target.value);
  };
  render() {
    const style = {marginBottom: '15px', marginTop: '15px'};
    return <div style={style}>
      <input
        type="wsearch" 
        placeholder="What're we looking for?" 
        value={this.props.search}
        onInput={this.handleInput}
        style={{margin: '0 auto', display: 'block'}}
        //autoFocus
      />
    </div>
  }
}

class ResultList extends React.Component {
  render() {
    const resultNodes = this.props.results.map(result => {
      return <Result data={result} key={result.timestamp} />;
    });
    return <div>
      <ReactCSSTransitionGroup
        transitionName="fade"
        transitionEnterTimeout={250}
        transitionLeaveTimeout={150}
      >
        {resultNodes}
      </ReactCSSTransitionGroup>
    </div>
  }
}

class Result extends React.Component {
  render() {
    const {data} = this.props;
    const style = {padding: '5px'};
    return <div style={style} className='result'>
      <a
        href={data.url}
        target="_blank" 
      >
        <h1 style={{margin: '11px'}}>{data.title}</h1>
        <p style={{margin: '8px'}} dangerouslySetInnerHTML={{__html: data.extract}} />
      </a>
    </div>
  }
}

ReactDOM.render(
  <WikiViewer url='https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exsentences=1&exlimit=max&exintro=&explaintext=&generator=search&gsrnamespace=0&gsrlimit=10&gsrsearch=' />,
  document.getElementById('content'));

console.log('SECRET FEATURE: Double-click anywhere to visit a random wiki page');
document.addEventListener('dblclick', e => {
  const options = {
    url: 'https://en.wikipedia.org/w/api.php?action=query&list=random&format=json&rnnamespace=0&rnlimit=1',
    dataType: 'jsonp'
  }
  $.ajax(options).then(data => {
      const title = data.query.random[0].title;
      window.open('http://wikipedia.org/wiki/' + title);
    });
});

// https://github.com/bjoerge/promise-latest
function latest(fn) {
  let lastAdded
  let pending
  let resolve
  let reject
  return function (...args) {
    // in the future if/when promises gets cancellable, we could abort the previous here like this
    // lastAdded.cancel()
    lastAdded = fn(...args)
    if (!pending) {
      pending = new Promise((_resolve, _reject) => {
        resolve = _resolve
        reject = _reject
      })
    }
    lastAdded.then(fulfill.bind(null, lastAdded), fail.bind(null, lastAdded))
    return pending
  }
  function fulfill(promise, value) {
    if (promise === lastAdded) {
      pending = null
      resolve(value)
    }
  }

  function fail(promise, error) {
    if (promise === lastAdded) {
      pending = null
      reject(error)
    }
  }
}
            
          
!
999px
Loading ..................

Console