Opening the product web page "on top" of the feed page, such as Twitter

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

  • home page https://twitter.com with an endless feed that you can scroll down to;
  • If you click on a tweet, it will open with a highlighted URL (for example, https://twitter.com/TheTweetOfGod/status/635493904834412545 )
  • This tweet is an inline page / section "on top" of the original feed, which you can still see around the edges, but darker and darker;
  • If you click on the inline tweet element (i.e. the shaded area), you will revert to the original https://twitter.com with the same ones (i.e. the page has not refreshed).

Note that if the Tweet URL opens in a new tab, the author's profile page renders a shaded background instead of the channel home page. Thus, the main background for the feed is only inherited if the message https://twitter.com was referred to the tweet page .

In terms of web design, does this design approach have a formal name / definition that might help me figure out a suitable solution? I am assuming it is server-side sized.

+3


source to share


2 answers


There are three aspects to your question. Let me first dive into the technology required to implement everything, and then briefly discuss how Twitter uses this technology.

TL; DR? Twitter uses the history API in combination with AJAX and DOM manipulation to do its magic.


Methods and some background

(a) Changing the url without refreshing the page (2 and 4 on your list)
There is an API for this 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 a user switching to URL

. The first adds an entry to the navigation history, and the second replaces the current entry. This affects what happens when users use the back and forward buttons in their browser.

When users navigate backwards, or mimic this with history.back()

, an event occurs popstate

, and state

- you went through pushState

, you can access through event.sate

. state

can be any object, so this is useful for storing, say, the page title (to update document.title

), the scroll position, or whatever you want.

(b) Downloading Content Directly
Since the entry is stored in the browsing history, users can visit this URL immediately after closing a tab or even their browser. They can also share URLs while others visit it directly. In such cases, there will be no event popstate

, but just a request to your web server for the URL

one you passed to pushState

. Therefore, the URL must be meaningful to the server.

Twitter is obviously loading the poster profile as a background in this case. It depends on your use case what you want the page to look like. Everything goes!

(c) Loading content asynchronously (3 on your list)
Go back to (a) for bits. Twitter not only changes the URL but also downloads the tweets, the metadata of that tweet, and responds to it. This is then displayed as a modal popup.

Again, API 1 : AJAX exists to load content asynchronously . In particular, the object XMLHttpRequest

and its functions are of interest . This can be used to send requests to the server and get content without having to completely reload the page.

It should be noted that a new API is being developed: the Fetch API . At the time of this writing, there is basic support in all modern major browsers, but it is still in development.

Once you have selected content, it can be displayed on the page in any way. JavaScript can be used to create, delete, and modify elements in the DOM as you see fit.


Example from your question: Twitter



Now that all the methods are on the table, let's summarize what Twitter is doing.

When a user clicks on a tweet in their feed.

  • Download the tweet metadata and responses (as described in section (c) ).
  • Create background and modal and fill them with uploaded content.
    This uses the standard methods: create , delete and modify page elements.
  • Update the URL (as described in section (a) ) to make sharing easier.

When the user rejects the modal.

  • Remove modal and background.
  • Update the URL (as described in section (a) ).

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

  • Let the server respond with the tweet author's profile page, and the tweet details are loaded into a modal on top of it. So JavaScript is not required at all. Of course, the modal can be rejected in the same way as in the previously described use case.

Implementing this in your own website

You have correctly determined that there are side sizes for this technique on both the client and server side. The beauty is that, when implemented correctly, it is completely transparent to users. The only thing they (not) will notice is that the page load is less.

The links sprinkled in this answer should serve as a good starting point for you!


Final Notes

All of this is sometimes also used to create smooth transitions between pages on the same site. In these cases, full pages are loaded asynchronously (according to (c) ) and then a smooth transition occurs, usually involving animation. There are many , many , many , many examples, tutorials and libraries for this. You can search PJAX to learn and find more.

__________
1 Actually, not one API, but an approach or thinking. See MDN link for details .

+4


source


I think what happens on Twitter is that the pop-up tweet downloads the same content as the tweet on its unique page; not that the modal has a unique url.

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

CHANGE ADD:

Here is the source code of the tweet in the tweet stream before it appears 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 tweet URL when shown as modal in front of the Twitter feed: https://twitter.com/vogelbeere/status/887996116549107713

In the tag

there are so many event listeners that it is difficult to see which one is the link to the tweet.

+1


source







All Articles