3

Twitter has the following UI behaviour that I want to replicate:

  1. a homepage https://twitter.com with an endless feed you can scroll down;
  2. if you click on a tweet it opens up with a dedicated URL (e.g. https://twitter.com/TheTweetOfGod/status/635493904834412545);
  3. this tweet appears to be an embedded page/section 'on top' of the original feed, which you can still see around the edges but shaded darker;
  4. If you click off the embedded tweet element (i.e. on the shaded area) you revert to the original https://twitter.com feed at the same point (i.e. page has not refreshed).

Note that if the tweet URL is opened up in a fresh tab then the author's profile page forms the shaded backdrop instead of the main feed page. So the main feed backdrop is only inherited if the tweet page has been accessed from https://twitter.com.

In web design terms does this design approach have a formal name/definition that might help me identify a suitable solution? I'm assuming it has a server-side dimension.

geotheory
  • 22,624
  • 29
  • 119
  • 196
  • 1
    Have you looked at bootstrap modals? http://getbootstrap.com/javascript/#modals – tobifasc Jul 20 '17 at 08:25
  • Thanks @tobifasc that certainly looks right in terms of style and behaviour. But do you know how to configure unique URL for the modal? I can't see any reference to this. – geotheory Jul 20 '17 at 10:22
  • 1
    Maybe you could change the URL using Javascript like this https://stackoverflow.com/a/3354511 but that seems a bit hacky. I can't find any better solutions either. – tobifasc Jul 20 '17 at 10:29
  • re: 1-4, you can fire a modal and use jquery's `$.load()` to load in the contents of another page into the modal. – Michael Coker Jul 20 '17 at 21:58
  • 1
    Wrote an example for you. Create a file on your server with this code https://codepen.io/mcoker/pen/XgvBKx and a page called `modal-content.html` with this code https://codepen.io/mcoker/pen/zzgLBM and when you click on the link in the first page, it will load `modal-content.html` in a modal. – Michael Coker Jul 20 '17 at 22:33
  • @MichaelCoker Your modal example is super helpful, thank you :) – geotheory Jul 21 '17 at 15:15
  • I note [liveuamap](https://india.liveuamap.com/en/2017/21-july-madhya-pradesh-water-logging-in-bhopal-after-heavy) also uses this technique.. – geotheory Jul 21 '17 at 15:57
  • You're welcome. Not sure what you're looking for exactly in regards to an answer here. Do you want me to submit that as an answer or are you looking for something else? – Michael Coker Jul 21 '17 at 16:04
  • @MichaelCoker it's getting there - your method is about seemless transition from feed page to a sub-item page and back. But the dedicated URL bit is I think equally important because it enables sharing.. – geotheory Jul 21 '17 at 21:17
  • OK this looks promising https://www.w3schools.com/angular/angular_routing.asp – geotheory Jul 22 '17 at 06:45

2 Answers2

4

There are three aspects to your question. Let's first dive into the technology needed to implement everything, and then briefly discuss how Twitter leverages that technology.

TL;DR? Twitter uses the history API combined with AJAX and DOM manipulation to work its magic.


The techniques, and a little bit of background

(a) Changing the URL without refreshing the page (2 and 4 on your list)
There is an API for that, implemented by modern browsers.

window.history.pushState(state, title, URL);
window.history.replaceState(state, title, URL);
window.addEventListener('popstate', (event) => { /* use event.state */ });

The first two functions allow you to simulate the user navigating to URL. The first adds an entry to the navigation history, while the second replaces the current entry. This impacts what will happen when users use the back and forward buttons in their browser.

When users navigate back, or you simulate this using history.back(), the popstate event is fired and the state that you passed into pushState can be accessed via event.sate. The state can be any object, so this is useful to store, say, the title of the page (to update the document.title), the scroll position, or whatever else you want.

(b) Loading content directly
Because the entry is saved in the browsing history, it is possible that users visit this URL directly after having closed the tab or even their browser. They may also share the URL and have others visit it directly. In those cases, there will be no popstate event, but simply a request to your web server for the URL you passed to pushState. The URL must hence be meaningful to the server.

Twitter apparently loads the poster profile as a backdrop in this case. It depends on your use case what you want the page to look like. Anything goes!

(c) Loading content asynchronously (3 on your list)
Back to (a) for a bit. Twitter not only changes the URL, but also loads the tweet, meta data of that tweet and replies to it. This is then displayed in a modal popup.

Again, there is an API1 to load content asynchronously: AJAX. In particular, the XMLHttpRequest object and its functions are of interest. This can be used to make requests to the server and fetch content without needing the page to reload completely.

It is worth mentioning that a new API is being developed: the Fetch API. At the time of writing, there is basic support in all modern major browsers, but it is still somewhat under development.

After having fetched the content, it can be displayed on the page in any which way you like. JavaScript can be used to create, delete and modify elements in the DOM at will.


An example from your question: Twitter

Now that all techniques are on the table, let's summarize what Twitter does.

When a user clicks a tweet in their feed.

  1. Load tweet meta data and replies (as described under (c)).
  2. Create a backdrop and modal and populate them with the loaded content.
    This uses standard techniques: create, delete and modify page elements.
  3. Update the URL (as described under (a)) to enable easy sharing, amongst others.

When a user dismisses the modal.

  1. Delete the modal and backdrop.
  2. Update the URL (as described under (a)).

When a user directly visits the URL to a specific tweet.

  1. Let the server respond with the profile page of the tweet author, with the tweet details loaded in a modal on top of it. Thus, no JavaScript is required at all. Of course, the modal can be dismissed just like in the previously described use case.

Implementing this on your own web site

You correctly identified that there are both client side and server side dimensions to this technique. The beauty of it is that, when implemented correctly, it is completely transparent to users. The only thing they will (not) notice is that there are fewer full page loads.

The references sprinkled throughout this answer should provide good starting points for you!


Final notes

All of this is sometimes also used to create smooth transitions between pages of the same site. In those cases, full pages are loaded asynchronously (as per (c)) and then a smooth transition, usually involving animations, is performed. There are many, many, many, many examples, tutorials and libraries for this. You may want to search for PJAX to learn and find more.

__________
1Not really a single API maybe, but an approach or mindset. See the MDN reference for more details.

Just a student
  • 10,560
  • 2
  • 41
  • 69
1

I think that what is happening in Twitter is that the popup tweet loads the same content as the tweet in its own unique page; not that the modal has an unique URL.

If you use Angular, you can inject the same content into html modal templates or into standalone pages using route provider, and you could link from the modal content to the standalone page using the ID of the specific data to load that content.

EDITED TO ADD:

Here is the source code of a tweet in a stream of tweets, before it pops up as a modal:

 <div class="js-tweet-text-container">
  <p class="TweetTextSize TweetTextSize--normal js-tweet-text tweet-text" lang="en" data-aria-label-part="0">
Does anyone remember a 1990s TV show about a folklore prof investigating urban legends, shown on weird night on channel 4 <a href="/hashtag/folklorethursday?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr"><s>#</s><b>folklorethursday</b></a></p>
</div>

Here is the URL of the tweet when it is shown as a modal in front of the twitter feed: https://twitter.com/vogelbeere/status/887996116549107713

There are so many event listeners on the

tag that it's hard to see which one is the link to the tweet.

Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47
  • Thanks Yvonne. Can this process happen without the page appearing to refresh? It seems (naively) to me that opening up a new tweet page and injecting content from the feed page, and then returning to that feed page at the same point you left would necessitate reloading the pages. Is this what Angular manages? – geotheory Jul 21 '17 at 13:53
  • 1
    Twitter seems to be using Ajax to load its data. Both the Twitter transition and Angular look pretty seamless but whether you would get it to look as if a page reload hadn't happened would depend on your server speed, connection speed, etc, surely? – Yvonne Aburrow Jul 21 '17 at 14:05
  • Is Twitter using [web storage](https://stackoverflow.com/a/23127566/1156245) to remember the feed page as the modal loads up, do you think? – geotheory Jul 21 '17 at 15:56
  • No idea! It could be; or it might be using service workers; or it might just be using database drill-down to display one specific record. – Yvonne Aburrow Jul 26 '17 at 10:24