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

refactor(theme-common): allow optional desktopBreakpoint param in useWindowSize #9335

Merged
merged 16 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/docusaurus-theme-common/src/hooks/useWindowSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ const windowSizes = {

type WindowSize = keyof typeof windowSizes;

const DesktopThresholdWidth = 996;

function getWindowSize() {
function getWindowSize({
desktopThresholdWidth = 996,
}: {
desktopThresholdWidth?: number;
}): WindowSize {
if (!ExecutionEnvironment.canUseDOM) {
return windowSizes.ssr;
}
return window.innerWidth > DesktopThresholdWidth

return window.innerWidth > desktopThresholdWidth
? windowSizes.desktop
: windowSizes.mobile;
}
Expand All @@ -43,17 +46,17 @@ const DevSimulateSSR = process.env.NODE_ENV === 'development' && true;
* In development mode, this hook will still return `"ssr"` for one second, to
* catch potential layout shifts, similar to strict mode calling effects twice.
*/
export function useWindowSize(): WindowSize {
export function useWindowSize(desktopThresholdWidth = 996): WindowSize {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not fan of using a single arg there, please use a flexible object instead because we don't want to do a breaking change if we later introduce a new breakpoint some day or if you or someone else come with the need for 3 different sizes. If we add flexibility for your use case, let's make it flexible enough for other use cases as well.

This lib has a decent API to copy imho: https://github.com/iiroj/use-breakpoint

const BREAKPOINTS = { mobile: 0, tablet: 768, desktop: 1280 }

const CurrentBreakpoint = () => {
  const { breakpoint, maxWidth, minWidth } = useBreakpoint(
    BREAKPOINTS,
    'desktop'
  )
  return <p>The current breakpoint is {breakpoint}!</p>
}

(by the way you could as well use this lib directly in your code, similar behavior to what we implemented here).

const [windowSize, setWindowSize] = useState<WindowSize>(() => {
if (DevSimulateSSR) {
return 'ssr';
}
return getWindowSize();
return getWindowSize({desktopThresholdWidth});
});

useEffect(() => {
function updateWindowSize() {
setWindowSize(getWindowSize());
setWindowSize(getWindowSize({desktopThresholdWidth}));
}

const timeout = DevSimulateSSR
Expand All @@ -66,7 +69,7 @@ export function useWindowSize(): WindowSize {
window.removeEventListener('resize', updateWindowSize);
clearTimeout(timeout);
};
}, []);
}, [desktopThresholdWidth]);

return windowSize;
}
2 changes: 2 additions & 0 deletions website/docs/styling-layout.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ Docusaurus uses `996px` as the cutoff between mobile screen width and desktop. I
}
```

:::tip Header and Sidebar Some components, such as the header and sidebar, use a `useWindowSize` hook to set their breakpoint value. If you change the breakpoint value in your custom CSS, you probably also want to update the invocations of the `useWindowSize` hook by swizzling the components it's used in and passing it an argument that updates the breakpoint value (for example, to change the breakpoint to 796px, pass the object `{ desktopThresholdWidth: 796 }` as an argument to the `useWindowSize` hook). :::
jgarrow marked this conversation as resolved.
Show resolved Hide resolved

## CSS modules {#css-modules}

To style your components using [CSS Modules](https://github.com/css-modules/css-modules), name your stylesheet files with the `.module.css` suffix (e.g. `welcome.module.css`). Webpack will load such CSS files as CSS modules and you have to reference the class names as properties of the imported CSS module (as opposed to using plain strings). This is similar to the convention used in [Create React App](https://facebook.github.io/create-react-app/docs/adding-a-css-modules-stylesheet).
Expand Down
Loading