doc: toggle all flavor selectors sticky on click#49526
Conversation
|
Review requested:
|
doc/api_assets/api.js
Outdated
| const flavorSelectors = document.querySelectorAll('.js-flavor-selector'); | ||
|
|
||
| flavorSelectors.forEach((selector) => { | ||
| selector.checked = flavorSetting !== vCommonJS; | ||
| selector.addEventListener('click', (e) => { |
There was a problem hiding this comment.
I find it a bit confusing to call them "selector", because selector usually refers to a CSS selector.
| const flavorSelectors = document.querySelectorAll('.js-flavor-selector'); | |
| flavorSelectors.forEach((selector) => { | |
| selector.checked = flavorSetting !== vCommonJS; | |
| selector.addEventListener('click', (e) => { | |
| const flavorToggles = document.querySelectorAll('.js-flavor-selector'); | |
| flavorToggles.forEach((toggleElement) => { | |
| toggleElement.checked = flavorSetting !== vCommonJS; | |
| toggleElement.addEventListener('click', (e) => { |
There was a problem hiding this comment.
Elsewhere in the file it's referred to as "selector", see "flavorSelector" in the "setupCopyButton" function in the same file. Would maybe be even more confusing if it's not the same as in the other function. The class name also ends with "selector".
I tried to mimic the style of the other functions to increase coherency.
There was a problem hiding this comment.
But commit your suggestion if you feel like it's better, it's fine by me.
There was a problem hiding this comment.
I opened #49536, let's see what the others think
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
doc/api_assets/api.js
Outdated
|
|
||
| function setupFlavorSelectors() { | ||
| const kFlavorPreference = 'customFlavor'; | ||
| const flavorSetting = sessionStorage.getItem(kFlavorPreference); |
There was a problem hiding this comment.
I do not see any value in using a sessionStorage here. As the lifecycle would be contained to the current session. A localStorage makes more sense here.
There was a problem hiding this comment.
Pushed a new version with localStorage. This leaves some "garbage" values there though since it doesn't get removed, which may be more of an issue with localStorage than with sessionStorage. Adding a "removeItem" when the value is not "true" could fix that and keep it clean, but maybe not a big issue.
There was a problem hiding this comment.
I assume that localStorage should sync with the user preference. Either true or false :)
There was a problem hiding this comment.
Yes, but it's not really necessary to store the string "false" it can just be undefined aka be removed (removeItem) to avoid wasting localStorage space. But if it's fine for you, then it's fine for me, may just be nitpicking.
There was a problem hiding this comment.
Yeah, sorry, what I meant is that we should track both states. And for me the absence of a value is more than enough.
But we should remove then the value when set to false.
There was a problem hiding this comment.
Ok, pushed a fix to remove value when false.
doc/api_assets/api.js
Outdated
| const flavorSelectors = document.querySelectorAll('.js-flavor-selector'); | ||
|
|
||
| flavorSelectors.forEach((selector) => { | ||
| selector.checked = flavorSetting === 'true' |
There was a problem hiding this comment.
This is unoptimized. You're running this same check for every checkbox, knowing that this check is only needed once.
Also you probably don't need to set the value of the checkbox if flavorSetting is different from true
There was a problem hiding this comment.
Pushed optimized code. You need to set the checkbox value also when it's not true, leaving that unchanged.
doc/api_assets/api.js
Outdated
| updateHashes(); | ||
| } | ||
|
|
||
| function setupFlavorSelectors() { |
There was a problem hiding this comment.
| function setupFlavorSelectors() { | |
| function setupFlavorToggles() { |
There was a problem hiding this comment.
This comment applies to all current suggestions:
If you want to change every mention of "select" to "toggle", then we also need to change all the CSS files and also all the existing functions in api.js and possibly more. There are also test cases that might be affected.
I think this renaming thing should go in a separate issue so we can land this feature. No need to spend time risking introducing other issues on something that is not really a problem imho.
There was a problem hiding this comment.
I think I've done all the preparation work in #49536, do you think I've missed something? We can't land this PR as is because it's now inconsistent with main, but I think all you need to do is to merge my suggestions and this can land as soon as we have a green CI.
There was a problem hiding this comment.
Ok, so you've committed the renaming directly onto main already?
The "setupCopyButton" function in doc/api_assets/api.js also uses the name "selector" everywhere, is that included? Not sure what else.
There was a problem hiding this comment.
Pushed merge and rename.
doc/api_assets/api.js
Outdated
| function setupFlavorSelectors() { | ||
| const kFlavorPreference = 'customFlavor'; | ||
| const flavorSetting = localStorage.getItem(kFlavorPreference) === 'true'; | ||
| const flavorSelectors = document.querySelectorAll('.js-flavor-selector'); |
There was a problem hiding this comment.
| const flavorSelectors = document.querySelectorAll('.js-flavor-selector'); | |
| const flavorToggles = document.querySelectorAll('.js-flavor-toggle'); |
doc/api_assets/api.js
Outdated
| const flavorSetting = localStorage.getItem(kFlavorPreference) === 'true'; | ||
| const flavorSelectors = document.querySelectorAll('.js-flavor-selector'); | ||
|
|
||
| flavorSelectors.forEach((selector) => { |
There was a problem hiding this comment.
| flavorSelectors.forEach((selector) => { | |
| flavorToggles.forEach((toggle) => { |
doc/api_assets/api.js
Outdated
| selector.checked = flavorSetting; | ||
| selector.addEventListener('change', (e) => { |
There was a problem hiding this comment.
| selector.checked = flavorSetting; | |
| selector.addEventListener('change', (e) => { | |
| toggle.checked = flavorSetting; | |
| toggle.addEventListener('change', (e) => { |
doc/api_assets/api.js
Outdated
| localStorage.removeItem(kFlavorPreference); | ||
| } | ||
|
|
||
| flavorSelectors.forEach((el) => { |
There was a problem hiding this comment.
| flavorSelectors.forEach((el) => { | |
| flavorToggles.forEach((el) => { |
doc/api_assets/api.js
Outdated
| // Make link to other versions of the doc open to the same hash target (if it exists). | ||
| setupAltDocsLink(); | ||
|
|
||
| setupFlavorSelectors(); |
There was a problem hiding this comment.
| setupFlavorSelectors(); | |
| setupFlavorToggles(); |
|
Landed in b500037, thanks for the contribution! |
|
Cool, thanks for the feedback! |
PR-URL: nodejs#49526 Fixes: nodejs#49508 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Fixes: #49508