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.

            
              * {
	box-sizing: border-box;
}

html, body {
	padding: 0;
	margin: 0;
	height: 100%;
	width: 100%;
	font-family: 'Helvetica Nueue', Arial, sans-serif;
	font-weight: 400;
	color: #444;
	background: #FAFAFA;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
}

html {
	overflow: hidden;
}

app {
	height: 100%;
	display: flex;
	flex-direction: column;
	flex-wrap: nowrap;
	justify-content: center;
	align-items: center;
	align-content: stretch;
	overflow: hidden;
}

a {
	color: blue;
}

header {
	width: 100%;
	height: 56px;
	color: #FFF;
	background: #222;
	font-size: 20px;
	box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
	padding: 15px;
	will-change: transform;
	z-index: 50;
	text-align: center;
	
	h1 {
		float: left;
		margin: 0;
		padding: 0;
		font-size: 20px;
		line-height: 26px;
		
		sub {
			font-size: 60%;
			&, a { color: #BBB; }
		}
	}
	
	progress-spinner {
		font-size: 24px;
	}
	
	input {
		float: right;
		padding: 5px;
		display: block;
		font-size: 16px;
		border: 1px solid #555;
		border-radius: 5px;
		color: #000;
		outline: none;
		
		&:focus {
			border-color: #69F;
		}
	}
}

main {
	flex: 1;
	background: #FAFAFA;
	padding: 10px;
	width: 100%;
	text-align: center;
	overflow-x: hidden;
	overflow-y: auto;
	overflow-scrolling: touch;
}


.stock {
	position: relative;
	display: inline-block;
	max-width: 400px;
	min-width: 300px;
	padding: 20px 10px;
	margin: 10px;
	background-color: #FFF;
	border-radius: 3px;
	box-shadow: 0 1px 5px rgba(0,0,0,0.3);
	transition: all 1s ease;
	will-change: opacity, background-color;
	&[change-type=up] {
		background-color: #DFD;
	}
	&[change-type=down] {
		background-color: #FDD;
	}
	&[loading] {
		opacity: 0.4;
	}
	.delete {
		position: absolute;
		right: 3px;
		top: 3px;
		display: block;
		width: 14px;
		height: 14px;
		line-height: 14px;
		font-size: 14px;
		border-radius: 50%;
		background: rgba(0,0,0,0.3);
		color: #FFF;
		cursor: pointer;
		opacity: 0.25;
		&:hover { opacity:1; }
	}
	h1 {
		margin: 0;
		font-size: 300%;
		font-weight: normal;
	}
	h2 {
		margin: 0;
		font-weight: normal;
	}
	h3 {
		margin: 0;
		font-weight: normal;
	}
}
            
          
!
            
              let { h, render, Component } = preact;  // import { ... } from 'preact';
/** @jsx h */

// load from localStorage
let symbols = [];
try { symbols = JSON.parse(localStorage.getItem('symbols')) || []; }catch(e){}

class App extends Component {
	state = { symbols };

	updateText = e => {
		this.setState({ text: e.target.value });
	};

	fetch = () => {
		let { symbols } = this.state;
		localStorage.setItem('symbols', JSON.stringify(symbols));
		this.setState({ loading:true });
		fetchStocks(symbols).then( stockData => {
			this.setState({ loading:false, stockData });
		});
	};

	handleKey = e => {
		if (e.keyCode===13) return this.addSymbol(), false;
	};

	addSymbol = () => {
		let { text, symbols } = this.state;
		symbols = symbols.concat(text.toUpperCase());
		this.setState({ text:'', symbols });
	};

	deleteSymbol = name => {
		let { symbols, stockData } = this.state;
		this.setState({
			symbols: symbols.filter( s => s!==name ),
			stockData: stockData.filter( s => s.symbol!==name )
		});
	};

	getSymbol(name) {
		return (this.state.stockData || []).find(s=>s.symbol===name)
	}

	componentDidMount() {
		this.fetch();
		this.timer = setInterval(this.fetch, 30*1000);
	}

	componentWillUnmount() {
		clearInterval(this.timer);
	}

	componentDidUpdate({ }, { symbols }) {
		if (symbols!==this.state.symbols) this.fetch();
	}

	render({}, { loading=true, symbols=[], text='' }) {
		return (
			<app>
				<header>
					<h1>
						Stock Ticker
						<sub>powered by <a href="https://git.io/preact" target="_blank">preact</a></sub>
					</h1>
					<input
						type="text"
						placeholder="Add symbol..."
						value={text.toUpperCase().replace(/[^A-Z]/,'')}
						pattern="^[A-Za-z]+$"
						onInput={this.updateText}
						onKeyDown={this.handleKey}
					/>
					{ loading && <progress-spinner dark /> }
				</header>
				<main>
					{ symbols.map( symbol => (
						<Symbol
					 		symbol={symbol}
							{...this.getSymbol(symbol)}
							onDelete={this.deleteSymbol}
						/>
					)) }
				</main>
			</app>
		);
	}
}


class Symbol extends Component {
	delete = () => {
		let { symbol, onDelete } = this.props;
		onDelete(symbol);
	};

	render({ symbol, name, value, change, percentChange, changeType }) {
		return (
			<div class="stock" loading={!name || null} change-type={changeType}>
				<span class="delete" onClick={this.delete}>×</span>
				<h2>{name} ({symbol})</h2>
				<h1>{ value ? ('$'+value.toLocaleString()) : '...' }</h1>
				<h3>{changeType} {percentChange}%</h3>
			</div>
		);
	}
}


// Start 'er up:
setTimeout( () => render(<App />, document.body), 1);



/** A sort of NASDAQ API Client */

const CORS = '//cors-everywhere.herokuapp.com/';
//const CORS = '//crossorigin.me/';
const NASDAQ = 'http://www.nasdaq.com/quotedll/quote.dll?page=dynamic&mode=data';

let enc = encodeURIComponent;

let emptyList = new Promise( r => r([]) );

function fetchStocks(list) {
	if (!list || !list.length) return emptyList;
	let url = CORS + NASDAQ;
	url += '&selected=' + enc(list[0]);
	url += list.reduce( (t, name) => t+`&symbol=${enc(name)}`, '');
	url += '&t=' + Math.floor(new Date().getTime()/500);
	return fetch(url).then( r => r.text() ).then(normalizeData);
}

let normalizeData = data => (
	(data || '')
		.replace(/(^\s*\*?|\*?\s*$)/g,'')
		.replace(/&nbsp;/g, ' ')
		.split('*')
		.map( row => (row || '').replace(/(^\||\|$)/g,'').split('|') )
		.map( v => ({
			symbol: v[0].toUpperCase(),
			name: v[16],
			value: parseFloat(v[1].replace(/,/g,'')) || 0,
			change: parseFloat(v[2].replace(/,/g,'')) || 0,
			percentChange: parseFloat((v[3].replace(/,/g,'')+'').replace(/[%$]/gm,'')) || 0,
			changeType: v[5].toLowerCase()
		}))
);
            
          
!
999px
Loading ..................

Console