Selection menus place actions directly above highlighted text, letting readers share or act on a passage without copying it first. The browser's Selection API exposes both the content and the geometry of the selected area, which is enough to anchor a control at the top-center of the highlight and to grab the selected string.

Markup and styling

The page needs sample text plus a control that stays out of the render tree until a selection exists. A <template> element holds a <span> whose contents are the menu control; nothing inside a template renders until JavaScript inserts it.

<article>
  <h1>Select over the text below</h1> 
  <p>Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML. CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript. CSS is designed to enable the separation of presentation and content, including layout, colors, and fonts. This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics. </p>
</article>
<template><span id="control"></span></template>

The template's <span> becomes the control, and in this example it prompts the reader to tweet the selection. Styling it comes next:

#control {
    background-image: url("data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 width='40px' height='40px'><foreignObject width='40px' height='40px'><div xmlns='http://www.w3.org/1999/xhtml' style='width:40px;height:40px;line-height:40px;text-align:center;color:transparent;text-shadow: 0 0 yellow, 2px 4px black, -1px -1px black;font-size:35px;'>💬</div></foreignObject></svg>");
  cursor: pointer;
  position: absolute;
  width: 40px;
  height: 40px;
}
#control::before{
  background-color: black;
  color: white;
  content: " tweet this! ";
  display: block;
  font-weight: bold;
  margin-left: 37px;
  margin-top: 6px;
  padding: 2px;
  width: max-content;
  height: 20px;
}

Positioning the control with the Selection API

The core logic hangs off the pointerup event on the element that carries the sample text. The flow:

  • Clone the control out of the template into a control variable.
  • Read the selection via document.getSelection() and check the selected string is not empty.
  • Measure the selected area with getBoundingClientRect(), stored as rect.
  • From rect, compute the control's top and left so it sits slightly above the highlight and horizontally centered.
  • Save the selected string on a user-defined property of control for later sharing.
  • Insert control into the page with appendChild().
var control = document.importNode(document.querySelector('template').content, true).childNodes[0];
document.querySelector('p').onpointerup = () => {
  let selection = document.getSelection(), text = selection.toString();
  if (text !== "") {
    let rect = selection.getRangeAt(0).getBoundingClientRect();
    control.style.top = `calc(${rect.top}px - 48px)`;
    control.style.left = `calc(${rect.left}px + calc(${rect.width}px / 2) - 40px)`;
    control['text']= text; 
    document.body.appendChild(control);
  }
}

Handling the click and cleanup

Clicking the control opens a tab on Twitter's "New Tweet" page with the selected text pre-filled.

control.addEventListener('pointerdown', oncontroldown, true);

function oncontroldown(event) {
  window.open(`https://twitter.com/intent/tweet?text=${this.text}`)
  this.remove();
  document.getSelection().removeAllRanges();
  event.stopPropagation();
}

After the tweet prompt fires, the control is no longer needed, so it is removed together with any selection on the page; propagation of the pointerdown event down the DOM tree is halted at the same point.

Dismissal also has to work from anywhere else. An onpointerdown handler on the page removes the control and the selection whenever the click lands outside the control itself.

document.onpointerdown = ()=> {    
  let control = document.querySelector('#control');
  if (control !== null) {control.remove();document.getSelection().removeAllRanges();}
}

Is the template required?

No. The control can instead be hidden and shown with the hidden HTML attribute or CSS display, or built entirely in JavaScript. The right choice depends on execution efficiency, needed fallbacks, and how it fits the rest of the application.

UX guardrails

  • Don't inject your own text into the selection — appending a link back to your site in the auto-generated tweet, for instance. It is intrusive; if a citation is genuinely needed, show the user a preview of the final text before posting, or they may be confused or surprised.
  • Keep the control out of the way so it doesn't cover surrounding content; heavy overlap amounts to CSS "data loss."
  • Start from why users select text on the site at all, then place controls so they don't obstruct that task.