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

Add a direct theme prop? #7

Closed
slorber opened this issue Jun 1, 2020 · 7 comments
Closed

Add a direct theme prop? #7

slorber opened this issue Jun 1, 2020 · 7 comments

Comments

@slorber
Copy link

slorber commented Jun 1, 2020

Hi,

I added this button to a React SPA with a dark mode toggle (Docusaurus v2). The button theme should change when the theme is toggled, and we should "decorelate" to the initial user preferenced stored in the browser.

This leads to unnatural integration where I have to provide a "mapping", while I know exactly which theme for the button I want.

import GitHubButton from "react-github-btn";
import useThemeContext from "@theme/hooks/useThemeContext";

const ThemedGithubStarButton = () => {
  // Button theme is choosen according to current Docusaurus theme, not browser preference
  const { isDarkTheme } = useThemeContext();
  const starButtonTheme = isDarkTheme ? "light" : "dark";
  const dataColorSchemeMapping = `no-preference: ${starButtonTheme}; light: ${starButtonTheme}; dark: ${starButtonTheme};`;
  return (
    <GitHubButton
      href="https://github.com/typescript-cheatsheets/react-typescript-cheatsheet"
      data-color-scheme={dataColorSchemeMapping}
      data-size="large"
      data-show-count="true"
      aria-label="Star typescript-cheatsheets/react-typescript-cheatsheet on GitHub"
    >
      Star
    </GitHubButton>
  );
}; 

(more context here: typescript-cheatsheets/react#233)

@slorber
Copy link
Author

slorber commented Jun 1, 2020

Also it does not seem to work so great with SSR / hydration. My mapping clearly ask for "white" theme, yet in mount it's actually "dark"

image

@ntkme
Copy link
Contributor

ntkme commented Jun 1, 2020

If you update the underneath dependency github-buttons to the just released 2.11.0, you can just specify data-color-scheme="light" or data-color-scheme="dark".

@ntkme
Copy link
Contributor

ntkme commented Jun 1, 2020

initial user preference stored in the browser

SSR does not know about user preference on the browser side.

@ntkme ntkme closed this as completed Jun 1, 2020
@slorber
Copy link
Author

slorber commented Jun 1, 2020 via email

@ntkme
Copy link
Contributor

ntkme commented Jun 1, 2020

Unfortunately, that is a problem in react:

facebook/react#10591

@ntkme
Copy link
Contributor

ntkme commented Jun 1, 2020

Quoted from facebook/react#10591:

React does not introduce any differences on its own, whether in text content, anchor tags, or anywhere else.

But if your components return different things on client and server, React won't attempt to fix the difference.

You are expected to return the same thing on the client and the server. Otherwise it's a bug in your app. React warns you about mismatches in development, and you should fix if you see any.

@Nihat217716
Copy link

1e68366a6a8b0092d769f2a056973e08f44520a5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants