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

fix(theme-classic): avoid rendering empty search box container #7990

Closed
wants to merge 8 commits into from

Conversation

drizzlesconsin
Copy link

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

image

https://stackblitz.com/github/facebook/docusaurus/tree/starter

The search box should not be displayed when the search function is not enabled.

Test Plan

Test links

Deploy preview: https://deploy-preview-_____--docusaurus-2.netlify.app/

Related issues/PRs

@facebook-github-bot
Copy link
Contributor

Hi @drizzlesconsin!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@netlify
Copy link

netlify bot commented Aug 23, 2022

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit efb479e
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/63066e6c4d391b0007d3c340
😎 Deploy Preview https://deploy-preview-7990--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Aug 23, 2022

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🔴 41 🟢 98 🟢 100 🟢 100 🟠 80 Report
/docs/installation 🟠 78 🟢 100 🟢 100 🟢 100 🟢 90 Report

@Josh-Cena Josh-Cena changed the title fix(theme-classic): fix empty searchBox fix(theme-classic): avoid rendering empty search box container Aug 23, 2022
@Josh-Cena Josh-Cena added pr: polish This PR adds a very minor behavior improvement that users will enjoy. to backport This PR is planned to be backported to a stable version of Docusaurus labels Aug 23, 2022
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.

Thank you!

Just pending the CLA

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Aug 23, 2022
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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.

Actually, this doesn't work. If you check locally, the empty container is still rendered. This is because the <SearchBar /> element is always there; it just sometimes returns null (which is not the same as a literal null children). We need a better solution.

@drizzlesconsin
Copy link
Author

}: Props): JSX.Element {
}: Props): JSX.Element | null {
const {siteConfig} = useDocusaurusContext();
if (!siteConfig.themeConfig?.algolia) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this will not work for non-official search plugins

Copy link
Author

Choose a reason for hiding this comment

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

em...Let's see if there's another way.

@slorber
Copy link
Collaborator

slorber commented Aug 24, 2022

The problem is that we are trying to add an implicit search bar by default (ie not configured through themeConfig.navbar.items). When user does not want search, it does not make sense.

What I suggest is that in 3.0 we can do a breaking change:

  • stop automatically adding an implicit search-bar, force user to be explicit and always provide it as a navbar item
  • warn if user has no search navbar item but there's a an algolia config provided, eventually try to restore a retrocompatible behavior?
  • make sure it works nicely on mobile too!

Another possible solution: using CSS .myContainer:has(> *) but it's not supported enough unfortunately

In the future, we should allow search plugin authors to register their own Navbar item components. They would be able to create a type: "my-local-search" instead of overwriting @theme/SearchBar


Do you still want to work on this? (considering you opened a patch-1 branch, and we are looking for a good solution requiring you to invest time, not a quickfix)

Fixing it just for yourself should be possible with swizzling or custom CSS

@slorber slorber marked this pull request as draft August 24, 2022 09:31
@drizzlesconsin
Copy link
Author

Is it possible to use this method here?

children.type() === null

I read:
React component children detect if empty / null before render
Is there any way for Parent to know if Child rendered null?

@drizzlesconsin drizzlesconsin marked this pull request as ready for review August 25, 2022 09:29
@slorber
Copy link
Collaborator

slorber commented Aug 25, 2022

There's no good solution to this problem in React. I don't want to introduce hacky workarounds in our theme when there's a clear elegant solution: be explicit and avoid rendering this parent search component in the first place

@drizzlesconsin
Copy link
Author

Looks like it can only be hack with CSS.

@slorber slorber marked this pull request as draft August 26, 2022 15:45
@lex111
Copy link
Contributor

lex111 commented Oct 27, 2022

What about using the named Noop component and then before rendering SearchBar, checking the value of SearchBar.name !== 'Noop'? Maybe is a lesser of an evil?

@slorber
Copy link
Collaborator

slorber commented Oct 8, 2023

will be fixed in #9385

@slorber slorber closed this Oct 8, 2023
@slorber slorber removed the to backport This PR is planned to be backported to a stable version of Docusaurus label Nov 10, 2023
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: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Navbar renders a useless SearchBox element when site does not have any search plugin
5 participants