Skip to content

Commit 5f67b5d

Browse files
committed
use cookies to track image visibility preference
1 parent 098dda5 commit 5f67b5d

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

javascripts/toggle-images.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// import { sendEvent } from './events'
2+
import Cookies from 'js-cookie'
23

4+
// Determines whether images are hidden or displayed on first visit.
5+
const hideImagesByDefault = false
6+
7+
// Set the image placeholder icon here.
38
const placeholderImagePath = '/assets/images/octicons/image.svg'
49

5-
// This module does two things:
10+
// This module does a few things:
611
// 1. Wraps every image in a div so they can be toggled individually.
712
// 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.
814
export default function () {
915
const toggleImagesBtn = document.getElementById('js-toggle-images')
1016
if (!toggleImagesBtn) return
@@ -17,33 +23,44 @@ export default function () {
1723
return
1824
}
1925

26+
// Look for a cookie with image visibility preference; otherwise, use the default.
27+
const hideImagesPreferred = (Cookies.get('hideImagesPreferred') === 'true') || hideImagesByDefault
28+
2029
// Get the aria-labels from the span elements containing the hide/show tooltips for single images.
2130
// (We do it this way instead of hardcoding text here for localization-friendliness.)
2231
const tooltipHideSingle = document.getElementById('js-hide-single-image').getAttribute('aria-label')
2332
const tooltipShowSingle = document.getElementById('js-show-single-image').getAttribute('aria-label')
2433

34+
// For every image...
2535
for (const img of images) {
2636
// First, wrap each image in a button and add some attributes.
2737
const parentDiv = img.parentNode
2838
const parentButton = document.createElement('button')
2939
parentDiv.replaceChild(parentButton, img)
3040
parentButton.appendChild(img)
3141
parentButton.classList.add('tooltipped', 'tooltipped-nw', 'btn-toggle-image')
32-
parentButton.setAttribute('aria-label', tooltipHideSingle)
42+
43+
// Set the relevant tooltip text, and hide the image if that is the preference.
44+
if (hideImagesPreferred) {
45+
parentButton.setAttribute('aria-label', tooltipShowSingle)
46+
toggleImage(img, 'hide', tooltipShowSingle)
47+
} else {
48+
parentButton.setAttribute('aria-label', tooltipHideSingle)
49+
}
3350

3451
// On any individual image button click...
3552
parentButton.addEventListener('click', (e) => {
3653
// Determine current state.
3754
const hideOnNextClick = parentButton.getAttribute('aria-label') === tooltipShowSingle
3855

39-
// Hide or show!
56+
// Hide or show the image!
4057
if (hideOnNextClick) {
4158
toggleImage(img, 'show', tooltipHideSingle)
4259
} else {
4360
toggleImage(img, 'hide', tooltipShowSingle)
4461
}
4562

46-
// Don't leave the button in focus after click or the tooltip will stay displayed.
63+
// Remove focus from the button after click so the tooltip does not stay displayed.
4764
parentButton.blur()
4865
})
4966
}
@@ -60,7 +77,9 @@ export default function () {
6077
showIcon.style.display = 'none'
6178
toggleImagesBtn.setAttribute('aria-label', tooltipHideAll)
6279

63-
let showOnNextClick = true
80+
// If images are hidden by default, showOnNextClick should be false.
81+
// If images are not hidden by default, showOnNextClick should be true.
82+
let showOnNextClick = !hideImagesPreferred
6483

6584
toggleImagesBtn.addEventListener('click', (e) => {
6685
if (showOnNextClick) {
@@ -77,10 +96,13 @@ export default function () {
7796
toggleImages(images, 'show', tooltipHideSingle)
7897
}
7998

99+
// Save this preference as a cookie.
100+
Cookies.set('hideImagesPreferred', showOnNextClick)
101+
80102
// Toggle the action on every click.
81103
showOnNextClick = !showOnNextClick
82104

83-
// Track image toggle events
105+
// TODO Track image toggle events
84106
// sendEvent({ type: 'imageToggle' })
85107
})
86108
}

0 commit comments

Comments
 (0)