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

useLogo hook does not update on theme change #2646

Closed
kimburgess opened this issue Apr 23, 2020 · 6 comments
Closed

useLogo hook does not update on theme change #2646

kimburgess opened this issue Apr 23, 2020 · 6 comments
Assignees
Labels
bug An error in the Docusaurus core causing instability or issues with its execution

Comments

@kimburgess
Copy link

🐛 Bug Report

When using the useLogo hook provided by theme-classic in a custom page, the associated attributes do not update following a chance between dark/light themes.

Have you read the Contributing Guidelines on issues?

👍

To Reproduce

  1. Create a custom page with the following minimal content:
import React from 'react';
import Layout from '@theme/Layout';
import useLogo from '@theme/hooks/useLogo';

function LogoTest() {
  const {logoImageUrl} = useLogo();

  return (
    <Layout title="LogoTest">
      <img src={logoImageUrl} />
    </Layout>
    );
}

export default LogoTest;
  1. Ensure both navbar.logo.src and navbar.logo.srcDark are set in the config.

  2. Build + load in browser.

  3. Toggle theme light/dark state.

Expected behavior

src attribute should change between src and srcDark values.

Actual Behavior

src remains on original value.

Your Environment

  • Docusaurus version used: v2.0.0-alpha.50
  • Environment name and version (e.g. Chrome 78.0.3904.108, Node.js 10.17.0): FF 72.0.2
  • Operating system and version (desktop or mobile): NixOS (linux)

Reproducible Demo

See above.

@kimburgess kimburgess added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Apr 23, 2020
@lex111
Copy link
Contributor

lex111 commented Apr 23, 2020

We have not documented this hook, so it is not intended to be used by end-users.
But if you want this hook to work, you need to use state of isClient, as shown below.

@kimburgess
Copy link
Author

Awesome. Thanks for looking into this.

After putting that into the test case unfortunately the behavior is the same.

import React from 'react';
import Layout from '@theme/Layout';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useLogo from '@theme/hooks/useLogo';

function LogoTest() {
  const {isClient} = useDocusaurusContext();
  const {logoImageUrl} = useLogo();

  return (
    <Layout title="LogoTest">
      <img key={isClient} src={logoImageUrl} />
    </Layout>
    );
}

export default LogoTest;

@lex111
Copy link
Contributor

lex111 commented Apr 23, 2020

Indeed it is, even useThemeContext does not work in pages.
@yangshun any ideas on this?

@kimburgess
Copy link
Author

kimburgess commented Apr 23, 2020

I'm still building up my mental model of this project, but in case it's of relevance pulling isDarkTheme directly from useTheme does work, but only on initial page load.

@yangshun yangshun removed the status: needs triage This issue has not been triaged by maintainers label May 14, 2020
@yangshun yangshun self-assigned this May 14, 2020
@yangshun yangshun added this to the v2.0.0-alpha.55 milestone May 14, 2020
@lex111 lex111 removed this from the v2.0.0-alpha.55 milestone May 19, 2020
@SamChou19815
Copy link
Contributor

After some investigation, I found the reason.

First, you should not use useTheme(). It's designed to be used internally by useThemeContext(). You should use useThemeContext() instead. useLogo is fine since it calls useThemeContext() under the hood.

useThemeContext(), as the name implies, must work under some context. The context is <ThemeProvider>, which is part of Layout. See

.

In a page, you might have something like

import React from 'react';
import Layout from '@theme/Layout';

function Page() {
  return (
    <Layout title="Foo" description="bar">
      Baz
    </Layout>
  );
}

export default Page;

If you try to useLogo, it might become:

import React from 'react';
import Layout from '@theme/Layout';

function Page() {
  // oops, use the ThemeProvider without being its child.
  const logo = useLogo();

  return (
    <Layout title="Foo" description="bar">
      Baz
    </Layout>
  );
}

export default Page;

A workaround is to move the content of the page to another component:

import React from 'react';
import Layout from '@theme/Layout';

function Content() {
  const logo = useLogo();
  return logo;
}

function Page() {
  return (
    <Layout title="Foo" description="bar">
      <Content />
    </Layout>
  );
}

export default Page;

@lex111
Copy link
Contributor

lex111 commented Nov 17, 2020

Since we removed the useLogo hook (see #3730), this issue is no longer relevant, so I'm closing it.

@lex111 lex111 closed this as completed Nov 17, 2020
cainmagi added a commit to cainmagi/sync-stream that referenced this issue Jun 14, 2021
Try to fix the bug caused by theme context. See facebook/docusaurus#2646
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Projects
None yet
Development

No branches or pull requests

4 participants