Skip to content

Commit

Permalink
User experience improvements to hacker news demo
Browse files Browse the repository at this point in the history
  • Loading branch information
thesephist committed Dec 28, 2018
1 parent 6549473 commit a4bf4df
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 513 deletions.
1,032 changes: 528 additions & 504 deletions docs/hacker-news-reader.html

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions samples/hn-reader/200.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<title>Torus Hacker News</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="apple-touch-icon-144x144.png" />
<link rel="apple-touch-icon-precomposed" sizes="60x60" href="apple-touch-icon-60x60.png" />
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="apple-touch-icon-152x152.png" />
<style>
html,
body {
Expand Down
Binary file added samples/hn-reader/apple-touch-icon-114x114.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/hn-reader/apple-touch-icon-120x120.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/hn-reader/apple-touch-icon-144x144.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/hn-reader/apple-touch-icon-152x152.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/hn-reader/apple-touch-icon-57x57.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/hn-reader/apple-touch-icon-60x60.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/hn-reader/apple-touch-icon-72x72.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/hn-reader/apple-touch-icon-76x76.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/hn-reader/favicon.ico
Binary file not shown.
8 changes: 8 additions & 0 deletions samples/hn-reader/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<title>Torus Hacker News</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="apple-touch-icon-144x144.png" />
<link rel="apple-touch-icon-precomposed" sizes="60x60" href="apple-touch-icon-60x60.png" />
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="apple-touch-icon-152x152.png" />
<style>
html,
body {
Expand Down
2 changes: 1 addition & 1 deletion samples/hn-reader/index.min.js

Large diffs are not rendered by default.

41 changes: 33 additions & 8 deletions samples/hn-reader/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ const decodeHTML = html => {
//> Shortcut function to go from a username to the link to the user's profile
// on news.ycombinator.com. I didn't make a user view in this app
// because I personally rarely visit profiles on HN.
const stopProp = evt => evt.stopPropagation();
const userLink = username => {
const href = `https://news.ycombinator.com/user?id=${username}`;
return jdom`<a href="${href}" target="_blank" noreferrer>${username}</a>`;
return jdom`<a href="${href}" target="_blank" onclick="${stopProp}" noreferrer>${username}</a>`;
}

//> ## Records and Stores
Expand Down Expand Up @@ -152,13 +153,15 @@ class StoryStore extends StoreOf(Story) {
// It could be more efficient, but it works, and flipping pages is
// not super frequent in the average HN reader use case, so it's not a catastrophe.
nextPage() {
this.start ++;
this.reset();
this.fetch();
this.gotoPage(this.start + 1);
}

previousPage() {
this.start = Math.max(0, this.start - 1);
this.gotoPage(Math.max(0, this.start - 1));
}

gotoPage(pageNumber) {
this.start = pageNumber;
this.reset();
this.fetch();
}
Expand Down Expand Up @@ -309,7 +312,7 @@ class StoryListing extends StyledComponent {
<div class="title">${attrs.order ? attrs.order + '.' : ''} ${title}</div>
<div class="url ${(url || !this.expanded) ? 'mono' : 'content'}">
${url ? (
jdom`<a href="${url}" target="_blank" noreferrer>${url}</a>`
jdom`<a href="${url}" target="_blank" onclick="${stopProp}" noreferrer>${url}</a>`
) : text}
</div>
<div class="meta">
Expand Down Expand Up @@ -408,7 +411,7 @@ class CommentListing extends StyledComponent {
|
${userLink(author)}
|
${kids.length} replies</div>
${kids.length === 1 ? '1 reply' : kids.length + ' replies'}</div>
<div class="text">${decodeHTML(text)}</div>
${!this.folded ? (jdom`<div class="children">
${this.kidsList.node}
Expand Down Expand Up @@ -450,9 +453,22 @@ class StoryList extends Styled(ListOf(StoryListing)) {
styles() {
return {
'padding-left': 0,
'.loadingMessage': {
'margin': '52px 0',
'font-style': 'italic',
},
}
}

compose() {
const nodes = this.nodes;
//> On slow connections, the list of stories may take a second or two to load. Rather
// than awkwardly showing an empty list wit no stories, let's show a message.
return jdom`<ul>
${nodes.length ? nodes : jdom`<div class="loadingMessage">loading your stories...</div>`}
</ul>`;
}

}

//> A `StoryPage` is the page showing an individual story and any comments under it.
Expand Down Expand Up @@ -508,6 +524,7 @@ class App extends StyledComponent {

this.nextPage = this.nextPage.bind(this);
this.previousPage = this.previousPage.bind(this);
this.homeClick = this.homeClick.bind(this);

//> Define our routing actions.
this.bind(router, ([name, params]) => {
Expand Down Expand Up @@ -548,6 +565,9 @@ class App extends StyledComponent {
//> These styles still cascade, so this styles all links on the page
'a': {
'color': BRAND_COLOR,
'&:visited': {
'color': '#a4abbb',
},
},
//> This styles all buttons on the page
'button': {
Expand Down Expand Up @@ -596,6 +616,11 @@ class App extends StyledComponent {
this.resetScroll();
}

homeClick() {
router.go('/');
this.stories.gotoPage(0);
}

//> When views switch, it's nice to automatically scroll up to the top of the page
// to read the new stuff. This does that.
resetScroll() {
Expand All @@ -604,7 +629,7 @@ class App extends StyledComponent {

compose() {
return jdom`<main>
<h1 onclick="${() => router.go('/')}">
<h1 onclick="${this.homeClick}">
${this.activePage ? '👈' : '🏠'} Hacker News
</h1>
${this.activeStory ? (
Expand Down

0 comments on commit a4bf4df

Please sign in to comment.