fix(theme-classic): avoid rendering empty search box container
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
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
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!
[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...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site settings.
⚡️ 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 |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
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.
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
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?
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
Looks like it can only be hack with CSS.
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?
will be fixed in https://github.com/facebook/docusaurus/pull/9385