11// import { sendEvent } from './events'
22import Cookies from 'js-cookie'
33
4- // Determines whether images are hidden or displayed on first visit.
5- const hideImagesByDefault = false
64
75// Set the image placeholder icon here.
86const placeholderImagePath = '/assets/images/octicons/image.svg'
@@ -11,7 +9,7 @@ const placeholderImagePath = '/assets/images/octicons/image.svg'
119 * This module adds a new icon button in the margin to toggle all images on the page.
1210 * It uses cookies to keep track of a user's selected image preference.
1311 */
14- export default function ( ) {
12+ export default function ( hideImagesByDefault = false , focusButtonByDefault = false ) {
1513 const toggleImagesBtn = document . getElementById ( 'js-toggle-images' )
1614 if ( ! toggleImagesBtn ) return
1715
@@ -25,7 +23,7 @@ export default function () {
2523 toggleImagesBtn . removeAttribute ( 'hidden' )
2624
2725 // Look for a cookie with image visibility preference; otherwise, use the default.
28- const hideImagesPreferred = ( Cookies . get ( 'hideImagesPreferred' ) === 'true' ) || hideImagesByDefault
26+ const hideImagesPreferred = hideImagesByDefault || ( Cookies . get ( 'hideImagesPreferred' ) === 'true' )
2927
3028 // Hide the images if that is the preference.
3129 if ( hideImagesPreferred ) {
@@ -42,10 +40,17 @@ export default function () {
4240
4341 // Set the starting state depending on user preferences.
4442 if ( hideImagesPreferred ) {
43+ onIcon . setAttribute ( 'hidden' , true )
4544 offIcon . removeAttribute ( 'hidden' )
4645 toggleImagesBtn . setAttribute ( 'aria-label' , tooltipImagesOff )
46+ // Show the tooltip if images are hidden by default to help users see the toggle button.
47+ // Downside: the button will begin with focus whenever the user goes to a new page.
48+ if ( focusButtonByDefault ) {
49+ toggleImagesBtn . focus ( { preventScroll : true } )
50+ }
4751 } else {
4852 onIcon . removeAttribute ( 'hidden' )
53+ offIcon . setAttribute ( 'hidden' , true )
4954 toggleImagesBtn . setAttribute ( 'aria-label' , tooltipImagesOn )
5055 }
5156
0 commit comments