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

feat: sync color mode between browser tabs #6723

Merged
merged 2 commits into from
Feb 23, 2022
Merged

Conversation

lex111
Copy link
Contributor

@lex111 lex111 commented Feb 19, 2022

Motivation

This is quite handy, especially when you have several tabs open from the same docs site.

Also fixes #6733.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

  1. Open two tabs in the browser
  2. On one of them switch the color theme
  3. Make sure that on the second one the color theme was also automatically changed

Related PRs

@lex111 lex111 added the pr: new feature This PR adds a new API or behavior. label Feb 19, 2022
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Feb 19, 2022
@netlify
Copy link

netlify bot commented Feb 19, 2022

✔️ [V2]

🔨 Explore the source changes: 0de81ae

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/6216545d2a3b480008694ebf

😎 Browse the preview: https://deploy-preview-6723--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Feb 19, 2022

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 59
🟢 Accessibility 100
🟢 Best practices 92
🟢 SEO 100
🟢 PWA 90

Lighthouse ran on https://deploy-preview-6723--docusaurus-2.netlify.app/

@github-actions
Copy link

github-actions bot commented Feb 19, 2022

Size Change: +629 B (0%)

Total Size: 782 kB

Filename Size Change
website/build/assets/js/main.********.js 591 kB +629 B (0%)
ℹ️ View Unchanged
Filename Size
website/.docusaurus/globalData.json 48 kB
website/build/assets/css/styles.********.css 106 kB
website/build/index.html 37.6 kB

compressed-size-action

Copy link
Collaborator

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

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

That's interesting, certainly works!

@gerardomata
Copy link

this is so cool

@Josh-Cena Josh-Cena added the status: awaiting review This PR is ready for review, will be merged after maintainers' approval label Feb 22, 2022
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

LGTM thanks 👍

console.error(err);
}
};
window.addEventListener('storage', onChange);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think it's going to be a problem but it's worth double-checking if this storage event listener could throw in some cases? (like iframe + blocking cookies, see #4501)

@@ -31,6 +31,10 @@ const ToggleComponent = memo(
const [focused, setFocused] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm I have mitigated feeling because we now have the toggle state in 2 places: localstorage + react state

but it's probably fine because localstorage can't be considered reliable in all cases (and we want the toggle to keep working on such cases)

that should be fine for now 👍 but I have some refactoring ideas for later

setTheme(coerceToTheme(storedTheme));
}
} catch (err) {
console.error(err);
Copy link
Collaborator

Choose a reason for hiding this comment

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

a good candidate for reportError later, see #6747

@slorber slorber merged commit 0c807b3 into main Feb 23, 2022
@slorber slorber deleted the lex111/sync-theme-mode branch February 23, 2022 15:55
@Josh-Cena Josh-Cena removed the status: awaiting review This PR is ready for review, will be merged after maintainers' approval label Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Toggle not always in sync with color mode with multiple toggles
5 participants