Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: toggle all flavor selectors sticky on click #49526

Merged
merged 8 commits into from
Sep 9, 2023

Conversation

eldoy
Copy link
Contributor

@eldoy eldoy commented Sep 7, 2023

Fixes: #49508

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/website

@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label Sep 7, 2023
doc/api_assets/api.js Outdated Show resolved Hide resolved
Comment on lines 143 to 147
const flavorSelectors = document.querySelectorAll('.js-flavor-selector');

flavorSelectors.forEach((selector) => {
selector.checked = flavorSetting !== vCommonJS;
selector.addEventListener('click', (e) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it a bit confusing to call them "selector", because selector usually refers to a CSS selector.

Suggested change
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) => {

Copy link
Contributor Author

@eldoy eldoy Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But commit your suggestion if you feel like it's better, it's fine by me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #49536, let's see what the others think

eldoy and others added 2 commits September 7, 2023 15:37
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
@@ -136,6 +136,25 @@
updateHashes();
}

function setupFlavorSelectors() {
const kFlavorPreference = 'customFlavor';
const flavorSetting = sessionStorage.getItem(kFlavorPreference);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that localStorage should sync with the user preference. Either true or false :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, pushed a fix to remove value when false.

const flavorSelectors = document.querySelectorAll('.js-flavor-selector');

flavorSelectors.forEach((selector) => {
selector.checked = flavorSetting === 'true'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed optimized code. You need to set the checkbox value also when it's not true, leaving that unchanged.

@@ -136,6 +136,29 @@
updateHashes();
}

function setupFlavorSelectors() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function setupFlavorSelectors() {
function setupFlavorToggles() {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed merge and rename.

function setupFlavorSelectors() {
const kFlavorPreference = 'customFlavor';
const flavorSetting = localStorage.getItem(kFlavorPreference) === 'true';
const flavorSelectors = document.querySelectorAll('.js-flavor-selector');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const flavorSelectors = document.querySelectorAll('.js-flavor-selector');
const flavorToggles = document.querySelectorAll('.js-flavor-toggle');

const flavorSetting = localStorage.getItem(kFlavorPreference) === 'true';
const flavorSelectors = document.querySelectorAll('.js-flavor-selector');

flavorSelectors.forEach((selector) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flavorSelectors.forEach((selector) => {
flavorToggles.forEach((toggle) => {

Comment on lines 145 to 146
selector.checked = flavorSetting;
selector.addEventListener('change', (e) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
selector.checked = flavorSetting;
selector.addEventListener('change', (e) => {
toggle.checked = flavorSetting;
toggle.addEventListener('change', (e) => {

localStorage.removeItem(kFlavorPreference);
}

flavorSelectors.forEach((el) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flavorSelectors.forEach((el) => {
flavorToggles.forEach((el) => {

@@ -182,6 +205,8 @@
// Make link to other versions of the doc open to the same hash target (if it exists).
setupAltDocsLink();

setupFlavorSelectors();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setupFlavorSelectors();
setupFlavorToggles();

@aduh95 aduh95 merged commit b500037 into nodejs:main Sep 9, 2023
@aduh95
Copy link
Contributor

aduh95 commented Sep 9, 2023

Landed in b500037, thanks for the contribution!

@eldoy
Copy link
Contributor Author

eldoy commented Sep 9, 2023

Cool, thanks for the feedback!

ruyadorno pushed a commit that referenced this pull request Sep 28, 2023
PR-URL: #49526
Fixes: #49508
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This was referenced Sep 28, 2023
alexfernandez pushed a commit to alexfernandez/node that referenced this pull request Nov 1, 2023
PR-URL: nodejs#49526
Fixes: nodejs#49508
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change all documentation code examples to CJS when clicking on CJS for one
5 participants