Skip to content

Commit 6a708b9

Browse files
committed
wrap images in a button so they can be individually toggled
1 parent 6752ab3 commit 6a708b9

2 files changed

Lines changed: 64 additions & 20 deletions

File tree

javascripts/toggle-images.js

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,86 @@ export default function () {
1515
return
1616
}
1717

18+
// Get the aria-labels from the span elements containing the hide/show tooltips for single images.
19+
// (We do it this way instead of hardcoding text here for localization-friendliness.)
20+
const tooltipHideSingle = document.getElementById('js-hide-single-image').getAttribute('aria-label')
21+
const tooltipShowSingle = document.getElementById('js-show-single-image').getAttribute('aria-label')
22+
23+
for (const img of images) {
24+
// First, wrap each image in a button and add some attributes.
25+
const parentDiv = img.parentNode
26+
const parentButton = document.createElement('button')
27+
parentDiv.replaceChild(parentButton, img)
28+
parentButton.appendChild(img)
29+
parentButton.classList.add('tooltipped', 'tooltipped-nw', 'btn-toggle-image')
30+
parentButton.setAttribute('aria-label', tooltipHideSingle)
31+
32+
// On any individual image button click...
33+
parentButton.addEventListener('click', (e) => {
34+
// Determine current state.
35+
const hideOnNextClick = parentButton.getAttribute('aria-label') === tooltipShowSingle
36+
37+
// Hide or show!
38+
if (hideOnNextClick) {
39+
toggleImage(img, 'show', tooltipHideSingle)
40+
} else {
41+
toggleImage(img, 'hide', tooltipShowSingle)
42+
}
43+
})
44+
}
45+
1846
// Get the span elements containing the hide and show icons.
1947
const hideIcon = document.getElementById('js-hide-icon')
2048
const showIcon = document.getElementById('js-show-icon')
2149

2250
// Get the aria-labels from the span elements for the tooltips.
23-
const tooltipHide = hideIcon.getAttribute('aria-label')
24-
const tooltipShow = showIcon.getAttribute('aria-label')
51+
const tooltipHideAll = hideIcon.getAttribute('aria-label')
52+
const tooltipShowAll = showIcon.getAttribute('aria-label')
2553

2654
// The icon should be "Hide" to start, so we suppress the "Show" icon here.
2755
showIcon.style.display = 'none'
28-
toggleImagesBtn.setAttribute('aria-label', tooltipHide)
56+
toggleImagesBtn.setAttribute('aria-label', tooltipHideAll)
2957

30-
let hideImages = true
58+
let showOnNextClick = true
3159

3260
toggleImagesBtn.addEventListener('click', (e) => {
33-
if (hideImages) {
61+
if (showOnNextClick) {
3462
// Button should say "Show" on first click
3563
showIcon.style.display = 'inline'
3664
hideIcon.style.display = 'none'
37-
toggleImagesBtn.setAttribute('aria-label', tooltipShow)
38-
toggleImages(images, 'hide')
65+
toggleImagesBtn.setAttribute('aria-label', tooltipShowAll)
66+
toggleImages(images, 'hide', tooltipShowSingle)
3967
} else {
4068
// Button should say "Hide" on another click
4169
showIcon.style.display = 'none'
4270
hideIcon.style.display = 'inline'
43-
toggleImagesBtn.setAttribute('aria-label', tooltipHide)
44-
toggleImages(images, 'show')
71+
toggleImagesBtn.setAttribute('aria-label', tooltipHideAll)
72+
toggleImages(images, 'show', tooltipHideSingle)
4573
}
4674

4775
// Toggle the action on every click.
48-
hideImages = !hideImages
76+
showOnNextClick = !showOnNextClick
4977

5078
// Track image toggle events
5179
// sendEvent({ type: 'imageToggle' })
5280
})
5381
}
5482

55-
function toggleImages (images, action) {
83+
function toggleImages (images, action, tooltipText) {
5684
for (const img of images) {
57-
if (action === 'show') {
58-
img.src = img.getAttribute('originalSrc')
59-
} else {
60-
if (!img.getAttribute('originalSrc')) img.setAttribute('originalSrc', img.src)
61-
img.src = '/assets/images/octicons/image.svg'
62-
}
85+
toggleImage(img, action, tooltipText)
86+
}
87+
}
88+
89+
function toggleImage (img, action, tooltipText) {
90+
const parentButton = img.parentNode
91+
92+
if (action === 'show') {
93+
img.src = img.getAttribute('originalSrc')
94+
parentButton.setAttribute('aria-label', tooltipText)
95+
} else {
96+
if (!img.getAttribute('originalSrc')) img.setAttribute('originalSrc', img.src)
97+
img.src = '/assets/images/octicons/image.svg'
98+
parentButton.setAttribute('aria-label', tooltipText)
6399
}
64100
}

stylesheets/overrides.scss

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,18 @@ pre .bluebox {
155155
}
156156
}
157157

158-
.markdown-body li img {
158+
.markdown-body img {
159+
max-height: 600px;
160+
padding: 0;
161+
}
162+
163+
.markdown-body button.btn-toggle-image {
164+
border: none;
165+
margin-top: 2px;
159166
max-width: calc(100% - 32px);
167+
background-color: transparent;
160168
}
161169

162-
.markdown-body img {
163-
max-height: 600px;
170+
.markdown-body button.btn-toggle-image img {
171+
padding: 0;
164172
}

0 commit comments

Comments
 (0)