Skip to content

Commit 5caf712

Browse files
committed
respect user defaults on button icon text
1 parent ca40a1b commit 5caf712

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

javascripts/toggle-images.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,45 @@ const hideImagesByDefault = false
88
const placeholderImagePath = '/assets/images/octicons/image.svg'
99

1010
// This module does a few things:
11-
// 1. Wraps every image in a div so they can be toggled individually.
12-
// 2. Adds a button to toggle all images on the page.
13-
// 3. Sets a cookie to keep track of a user's selected preference.
11+
//
12+
// 1. Wraps every image in a button so they can be toggled individually.
13+
// 2. Adds a new icon button in the margin to toggle all images on the page.
14+
//
15+
// It uses cookies to keep track of a user's selected image preference.
1416
export default function () {
1517
const toggleImagesBtn = document.getElementById('js-toggle-images')
1618
if (!toggleImagesBtn) return
1719

20+
// If there are no images on the page, return!
1821
const images = document.querySelectorAll('img')
22+
if (!images.length) return
1923

20-
// If there are no images on the page, hide the button entirely and return.
21-
if (!images.length) {
22-
toggleImagesBtn.style.display = 'none'
23-
return
24-
}
24+
// The button is hidden by default so it doesn't appear on browsers with JS disabled.
25+
// If there are images on a docs page and JS is enabled, display the toggle button.
26+
toggleImagesBtn.style.display = 'block'
27+
// Remove focus from the button after click so the tooltip does not stay displayed.
28+
toggleImagesBtn.blur()
2529

2630
// Look for a cookie with image visibility preference; otherwise, use the default.
2731
const hideImagesPreferred = (Cookies.get('hideImagesPreferred') === 'true') || hideImagesByDefault
2832

33+
/* 1. INDIVIDUAL IMAGE HANDLING */
34+
2935
// Get the aria-labels from the span elements containing the hide/show tooltips for single images.
30-
// (We do it this way instead of hardcoding text here for localization-friendliness.)
36+
// (We do it this way instead of hardcoding text in JS for localization friendliness.)
3137
const tooltipHideSingle = document.getElementById('js-hide-single-image').getAttribute('aria-label')
3238
const tooltipShowSingle = document.getElementById('js-show-single-image').getAttribute('aria-label')
3339

3440
// For every image...
3541
for (const img of images) {
36-
// First, wrap each image in a button and add some attributes.
37-
const parentDiv = img.parentNode
42+
const parentSpan = img.parentNode
43+
// Create a button and add some attributes.
3844
const parentButton = document.createElement('button')
39-
parentDiv.replaceChild(parentButton, img)
40-
parentButton.appendChild(img)
4145
parentButton.classList.add('tooltipped', 'tooltipped-nw', 'btn-toggle-image')
46+
// Wrap the image in the button.
47+
parentButton.appendChild(img)
48+
// Replace the image's parent span with the new button.
49+
parentSpan.parentNode.replaceChild(parentButton, parentSpan)
4250

4351
// Set the relevant tooltip text, and hide the image if that is the preference.
4452
if (hideImagesPreferred) {
@@ -65,6 +73,8 @@ export default function () {
6573
})
6674
}
6775

76+
/* 2. PAGE-WIDE TOGGLE BUTTON HANDLING */
77+
6878
// Get the span elements containing the hide and show icons.
6979
const hideIcon = document.getElementById('js-hide-icon')
7080
const showIcon = document.getElementById('js-show-icon')
@@ -73,9 +83,14 @@ export default function () {
7383
const tooltipHideAll = hideIcon.getAttribute('aria-label')
7484
const tooltipShowAll = showIcon.getAttribute('aria-label')
7585

76-
// The icon should be "Hide" to start, so we suppress the "Show" icon here.
77-
showIcon.style.display = 'none'
78-
toggleImagesBtn.setAttribute('aria-label', tooltipHideAll)
86+
// Set the starting state depending on user preferences.
87+
if (hideImagesPreferred) {
88+
showIcon.style.display = 'block'
89+
toggleImagesBtn.setAttribute('aria-label', tooltipShowAll)
90+
} else {
91+
hideIcon.style.display = 'block'
92+
toggleImagesBtn.setAttribute('aria-label', tooltipHideAll)
93+
}
7994

8095
// If images are hidden by default, showOnNextClick should be false.
8196
// If images are not hidden by default, showOnNextClick should be true.

0 commit comments

Comments
 (0)