1. Web Design
  2. UI Design

How to Code an Inline Sharing Menu


In this tutorial, we will learn how to build a so-called “inline sharing menu”. This interface works by popping-up a menu which allows readers to share the page, quoting the highlighted text. You’ll find a similar interface on a few popular sites like Medium.

Before we begin building our sharing menu, we’ll examine how this similar interface in Medium works more closely, such as when it is shown and how it is positioned against the highlighted area. This is a helpful step that will provide us proper technical insight, eventually also determining how we will write our codes our own code.

Let’s go!

Examining the Medium Interface

In the following image, we can see that the sharing menu in Medium appears at the centre of the highlighted text, regardless of the length; whether we select only a single word, a sentence, or the whole paragraph.

Highlighted word with the sharing button in Mediumcom
Inline Sharing UI in Medium 

If we look under the hood via Chrome DevTools, we can find the sharing menu position is given through top and left property in the the inline style. We can see the sharing button is also given with an additional modifier class, highlightMenu--active, which makes it visible.

Note: If you are not yet familiar with terms like Modifier, Block, and Element, you should have a look at this earlier tutorial: An Introduction to the BEM Methodology.

Styles that position the inline sharing menu in Medium

In the Styles tab, we can see its initial position is set through CSS with the absolute position, the z-index property to raise above the other elements on the page, the top, and with the visibility property to put the sharing buttons out of sight.

To summarise, we will need to:

  1. Retrieve the length of the selected area so that we can determine the centre point of the selection.
  2. Create a modifier to show the element.
  3. Set the sharing menu postion with the top and left property added through inline styles.

Build the Sharing Menu 

In this example we’ll include Facebook and Twitter buttons within the sharing menu. We deliver the Facebook and Twitter icon with SVG, wrapped within a button and a couple of div elements. Additionally, as you can see from the following snippet, we also add a span element to form the triangle at the bottom of the menus.

There’s no definitive rule in term of the colours and the shape of the menu; feel free to style the menu to match your site design. Worth paying attention to are the button size; the height and the width. Our sharing menu is, as you can see below, 84px wide and 40px tall. We will use these two values to position the sharing menu at the centre of the highlighted area later on.

Inline sharing menu size inspected through Chrome DevTools

The styles thats set the initial position and visibility.

And lastly, the class that we will append to make the sharing button visible.

Make the Sharing Menu Functional

At this point, our inline sharing menu should not be visible on the page. Also, when we click the Facebook and the Twitter button, the sharing window is nowhere to be seen. So, in this section, we’ll write the JavaScript to make our buttons functional. And we begin with the following, getHighlight() function.

This function will:

  • Retrieve the the highlighted area using the native JavaScript function, getSelection().
  • Return an Object containing the selected text, the element wrapping the text, and the Rectangle Object of the selected area which gives us the size as well as its position —topbottomleft, and right — within the page.

Our next function is called showMenu(). As the name implies, this function will reveal the sharing menu.

Specifically, the code in this function do the following:

  1. Get the Object from getHighlighted() function.
  2. Hide and set the sharing menu to its initial position when the highlighted area is empty; it does not contains text.
  3. Prevent the buttons from appearing if the highlighted text is not wrapped within a paragraph.
  4. Lastly, set the top and the left position, and append the sharing--shown class to make the sharing buttons visible. I’ve also added a few lines of inline comments describing where some of the defined numbers are derived from.

We’re going to assume that most users use mouse to highlight the content on the web, thus we bind this function on the mouseup event. Mobile devices will usually have their own contextual menus on text selection, so we’re focusing on the web for this tutorial.

safari ios
Text selection in Safari iOS

We delay the execution though by 100ms using the setTimeout() function, to ensure the content is properly selected.

Our last function, openShareWindow(), is to launch the sharing window when the buttons in the menu are clicked. In this tutorial, we will primarily use it to deliver the Twitter sharing window since Facebook has its own JavaScript SDK.


Next, we bind the sharing buttons with the click event and attach a function that will launch the sharing window.

For the Facebook sharing button, we use the Facebook JavaScript SDK. The SDK allows us to pass text to appear in the sharing window through the quote parameter.

Twitter does not provide a JavaScript SDK in this way. So here we use our function, openShareWindow(), and pass a formatted URL that complies with their guidelines along with the size of the window.

When we click the Twitter sharing button, it should launch a window that appears, as follows.

Wrapping Up

We are all set with our inline sharing menu! Head over to the demo to see it in action, or the source code to look the functions in full. Next, you can improve the inline sharing menu with transitions or animation to deliver a more engaging experience.

Further Resources

There are several JavaScripts APIs that we used to get the sharing menu up and running. Some of them have been mentioned in previous Envato Tuts+ tutorials, while others might be completely new to you. So, here I’ve included references to complement this tutorial.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.