docusaurus icon indicating copy to clipboard operation
docusaurus copied to clipboard

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

Open drizzlesconsin opened this issue 3 years ago • 10 comments

Pre-flight checklist

  • [x] I have read the Contributing Guidelines on pull requests.
  • [x] 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

drizzlesconsin avatar Aug 23 '22 12:08 drizzlesconsin

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!

facebook-github-bot avatar Aug 23 '22 12:08 facebook-github-bot

[V2]

Built without sensitive environment variables

Name Link
Latest commit efb479e57df6a006995be9ea924d1aa752cda4ff
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.

netlify[bot] avatar Aug 23 '22 12:08 netlify[bot]

⚡️ 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

github-actions[bot] avatar Aug 23 '22 12:08 github-actions[bot]

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

facebook-github-bot avatar Aug 23 '22 14:08 facebook-github-bot

https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-theme-search-algolia/src/theme/SearchBar/index.tsx#L268-L270

That can only be determined by the algolia configuration.

drizzlesconsin avatar Aug 24 '22 05:08 drizzlesconsin

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 avatar Aug 24 '22 09:08 slorber

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 avatar Aug 24 '22 18:08 drizzlesconsin

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

slorber avatar Aug 25 '22 12:08 slorber

Looks like it can only be hack with CSS.

drizzlesconsin avatar Aug 25 '22 12:08 drizzlesconsin

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?

lex111 avatar Oct 27 '22 20:10 lex111

will be fixed in https://github.com/facebook/docusaurus/pull/9385

slorber avatar Oct 08 '23 17:10 slorber