Skip to content

Ensure mobile <abbr> tooltips don't appear off-screen #10918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions _static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,8 @@ footer {

.wy-nav-content-wrap {
background-color: var(--content-wrap-background-color);
position: relative;
overflow-x: auto;
}

.wy-nav-content {
Expand Down Expand Up @@ -1814,30 +1816,22 @@ p + .classref-constant {
padding: 0.5em 0.75em;
}

/* Allow :abbr: tags' content to be displayed on mobile platforms by tapping the word */
@media (hover: none), (hover: on-demand), (-moz-touch-enabled: 1), (pointer:coarse) {
/* Do not enable on desktop platforms to avoid doubling the tooltip */
abbr[title] {
position: relative;
}

abbr[title]:hover::after,
abbr[title]:focus::after {
content: attr(title);

position: absolute;
left: 0;
bottom: -32px;
width: auto;
white-space: nowrap;

background-color: #1e1e1e;
color: #fff;
border-radius: 3px;
box-shadow: 1px 1px 5px 0 rgba(0, 0, 0, 0.4);
font-size: 14px;
padding: 3px 5px;
}
.abbr-tooltip {
position: absolute;
transform: translate(var(--offset-x), var(--offset-y));
width: max-content;
max-width: 100vw;
padding: 0 1rem;
z-index: 1;
}
.abbr-tooltip > div {
background-color: #1e1e1e;
color: #fff;
border-radius: 3px;
box-shadow: 1px 1px 5px 0 rgba(0, 0, 0, 0.4);
font-size: 14px;
padding: 3px 5px;
white-space: normal;
}

/* Giscus */
Expand Down
33 changes: 33 additions & 0 deletions _static/js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,39 @@ $(document).ready(() => {
}
});

/** @type HTMLElement */
let currentlyShownTooltip = null;
const touchDeviceQuery = window.matchMedia('(hover: none), (hover: on-demand), (-moz-touch-enabled: 1), (pointer:coarse)');

/* Allow :abbr: tags' content to be displayed on mobile platforms by tapping the word */
document.addEventListener('click', (event) => {
if (currentlyShownTooltip) {
currentlyShownTooltip.remove();
currentlyShownTooltip = null;
}

/* Do not enable on desktop platforms to avoid doubling the tooltip */
if (!touchDeviceQuery.matches) {
return;
}

/** @type HTMLElement */
const abbr = event.target;

if (abbr.matches('abbr[title]')) {
currentlyShownTooltip = document.createElement('div');
currentlyShownTooltip.classList.add('abbr-tooltip')
currentlyShownTooltip.appendChild(document.createElement('div')).innerText = abbr.getAttribute('title');
abbr.appendChild(currentlyShownTooltip);

const tooltipRect = currentlyShownTooltip.getBoundingClientRect();
const distanceOffScreenX = Math.max(0, tooltipRect.right - window.innerWidth);
const distanceOffScreenY = Math.max(0, tooltipRect.bottom - window.innerHeight);
currentlyShownTooltip.style.setProperty('--offset-x', -distanceOffScreenX + 'px');
currentlyShownTooltip.style.setProperty('--offset-y', -distanceOffScreenY + 'px');
}
});

// Unfold the first (general information) section on the home page.
if (!hasCurrent && menuHeaders.length > 0) {
menuHeaders[0].classList.add('active');
Expand Down