Skip to content

feat(theme-common): Get desktopThresholdWidth from docusaurus config #9261

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

Closed
wants to merge 6 commits into from

Conversation

jgarrow
Copy link
Contributor

@jgarrow jgarrow commented Aug 25, 2023

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

The desktopThresholdWidth in the useWindowSize hook is currently hardcoded at 996px. This makes it impossible to customize the breakpoint for the navbar, which creates inconsistencies when other CSS media query breakpoints are customized for the rest of the site.

This change allows for customization of that value by looking for a breakpoints.desktop value in the themeConfig in the docusaurus.config.js file. If none is present, it will default to the original 996 value.

Test Plan

An additional theme config has been added to website/_dogfooding/dogfooding.config.js:

/** @type {import('@docusaurus/types').ThemeConfig} */
const dogFoodingThemeConfig = {
  breakpoints: {
    desktop: 744,
  }
}
exports.dogFoodingThemeConfig = dogFoodingThemeConfig;

The website/docusaurus.config.js config has been updated to add this new dogfoodingThemeConfig to its existing themeConfig. Spin up that site and change the viewport width to observe that the navbar hamburger menu icon is visible below and hidden above 744px.

Test links

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

Related issues/PRs

@facebook-github-bot
Copy link
Contributor

Hi @jgarrow!

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 cla@meta.com. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Aug 25, 2023
@netlify
Copy link

netlify bot commented Aug 25, 2023

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit 480684c
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/64e8c33f5b7535000807f5c9
😎 Deploy Preview https://deploy-preview-9261--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 configuration.

@github-actions
Copy link

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟠 80 🟢 97 🟢 92 🟢 100 🟠 89 Report
/docs/installation 🟠 75 🟢 98 🟢 92 🟢 100 🟠 89 Report

@slorber
Copy link
Collaborator

slorber commented Aug 31, 2023

Hi @jgarrow

I'm sorry but unfortunately we can't do this. The value 996 is not arbitrary, and is hardcoded in many more places notably Infima (https://infima.dev/), the design system that Docusaurus theme classic uses. Changing the value on the JS side, and not taking into consideration the CSS side, is going to lead to weird unexpected behaviors.

For example you'll notice that on your preview, for a 755px width, if you click on the mobile drawer (which is still displayed despite your breakpoint), the drawer overlay will be applied but the drawer will not render:

CleanShot 2023-08-31 at 13 32 28@2x

This is just one edge case, there are probably others.


If you want to have configurable breakpoints, it needs to be implemented "full-stack", including CSS and Infima.

Afaik there's no CSS variable support for mediaqueries: https://bholmes.dev/blog/alternative-to-css-variable-media-queries/

Infima uses PostCSS:

@custom-media --ifm-narrow-window (max-width: 996px);

@media (--ifm-narrow-window) {
  padding: 0;
}

Which currently compiles to:

@media (max-width: 996px) {
  padding: 0;
}

The value is hardcoded also in the Infima distributed CSS file. It would require a few changes to our infrastructure (and probably apply https://github.com/csstools/postcss-custom-media at the Docusaurus side) to support configurable breakpoints.

Overall I'm not even sure all this is a good idea. There might be some design system components that wouldn't render well under a different viewport size. If you want to customize things, you'd rather do that on a case-by-case basis instead of changing the global breakpoint.

If your goal is to only affect the navbar, then you should swizzle the navbar and reconfigure the CSS/JS according to your new breakpoint. And maybe all the rest should keep behaving exactly as before?

@slorber slorber closed this Aug 31, 2023
@slorber slorber added the closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests) label Aug 31, 2023
@jgarrow
Copy link
Contributor Author

jgarrow commented Sep 5, 2023

If your goal is to only affect the navbar, then you should swizzle the navbar and reconfigure the CSS/JS according to your new breakpoint. And maybe all the rest should keep behaving exactly as before?

@slorber The issue is that there's no way to change that hardcoded breakpoint while still maintaining all of the same underlying logic. All I would like to do is change the navbar breakpoint (I need it larger), but unless there's a way to swizzle hooks and other non-component .js files, the current set up requires swizzling that component AND going down a copy/paste rabbit hole starting with the useWindowSize hook just to change the breakpoint. Is there an easier way to go about this?

@slorber
Copy link
Collaborator

slorber commented Sep 7, 2023

@jgarrow we enable you to fork the navbar entrypoint component, which gives you the opportunity to not use our own @docusaurus/theme-common package if needed and replace anything in the dependency graph it's using.

Copy/pasting is fine, as well as using any alternative implementation that suits your custom goal.

Similarly if you find a React behavior that annoys you, React is a deps of our code, and you can't patch React by simply swizzling Docusaurus code. You'd need to fork it, use patch-package, vendor it or any other solution. You can consider "theme-common" that way too: it's an utility lib not necessarily meant to be overriden. If you agree with the code in it, use it, otherwise, don't use it for it on your own.


The best I could concede here is to allow you passing breakpoints to the hook as parameters so that it becomes a bit more flexible and you can avoid copy/pasting, but I don't want this to be part of the public config API of Docusaurus.

useWindowSize({breakpoints = defaultBreakpoints})`

But keep in mind that by adding this we are slighly increasing bundle size for all users, despite maybe only you will ever use it. The goal of theme-common is not to support every possible use case you might have by swizzling.

@jgarrow
Copy link
Contributor Author

jgarrow commented Sep 8, 2023

@slorber I understand that the primary purpose of the @docusaurus/theme-common package is to serve as a utility library, and it's not necessarily intended to be heavily customized or overridden. However, since other aspects of the theme’s CSS are customizable, including media queries, it would be expected that the breakpoint in useWindowSize would be as well.

A small change to allow for an optional desktopThresholdWidth in the useWindowSize hook would enhance the theme’s versatility for other users like myself who are wanting to take advantage of this theme’s capabilities while still being able to change breakpoints. It would be a small enough change to be mindful of bundle size, but impactful enough that fewer users will resort to extensive forking.

If you’re open to it, I’d like to open a PR for this addition.

@slorber
Copy link
Collaborator

slorber commented Sep 8, 2023

I wouldn't call copy pasting a relatively small function an extensive fork 😅

But OK 👍accepting a small pr

@t8or
Copy link

t8or commented Sep 21, 2023

Just finding thread, I would love to have this built in plz <3

@jgarrow
Copy link
Contributor Author

jgarrow commented Sep 21, 2023

@slorber I simplified the implementation to just be function params vs a sizing config in the docusaurus.config.js file. New PR here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants