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

[v3] improve styles for reduced motion preferences #3196

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/purple-eggs-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nextra-theme-docs': patch
---

Improve styles for reduced motion preferences
7 changes: 7 additions & 0 deletions packages/nextra-theme-docs/css/hamburger.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@
}
}
}

& g,
& path,
&.open g,
&.open path {
@apply motion-reduce:_transition-none;
}
dimaMachina marked this conversation as resolved.
Show resolved Hide resolved
}
9 changes: 0 additions & 9 deletions packages/nextra-theme-docs/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ summary {
}
}

@media (prefers-reduced-motion: reduce) and (max-width: 767px) {
article:before,
.nextra-sidebar-container,
.nextra-sidebar-container.open,
body.resizing .nextra-sidebar-container {
@apply _transition-none;
}
}
dimaMachina marked this conversation as resolved.
Show resolved Hide resolved

/* Content Typography */
summary::-webkit-details-marker {
@apply _hidden;
Expand Down
4 changes: 1 addition & 3 deletions packages/nextra-theme-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,5 @@
"tailwindcss": "^3.4.1",
"vitest": "^2.0.3"
},
"sideEffects": [
"./src/polyfill.ts"
]
"sideEffects": false
}
10 changes: 6 additions & 4 deletions packages/nextra-theme-docs/src/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from 'react'
import scrollIntoView from 'scroll-into-view-if-needed'
import { useActiveAnchor, useMenu, useThemeConfig } from '../contexts'
import { renderComponent } from '../utils'
import { renderComponent, useIsWindowResizing } from '../utils'
import { Anchor } from './anchor'
import { Collapse } from './collapse'
import { LocaleSwitch } from './locale-switch'
Expand Down Expand Up @@ -381,6 +381,7 @@ export function Sidebar({
const hasI18n = themeConfig.i18n.length > 0
const hasMenu =
themeConfig.darkMode || hasI18n || themeConfig.sidebar.toggleButton
const isWindowResizing = useIsWindowResizing()

return (
<>
Expand All @@ -391,22 +392,23 @@ export function Sidebar({
className={cn(
'motion-reduce:_transition-none [transition:background-color_1.5s_ease]',
menu
? '_fixed _inset-0 _z-10 _bg-black/80 dark:_bg-black/60'
? 'max-md:_fixed max-md:_inset-0 max-md:_z-10 max-md:_bg-black/80 max-md:dark:_bg-black/60'
87xie marked this conversation as resolved.
Show resolved Hide resolved
: '_bg-transparent'
dimaMachina marked this conversation as resolved.
Show resolved Hide resolved
)}
onClick={() => setMenu(false)}
/>
<aside
className={cn(
'nextra-sidebar-container _flex _flex-col',
'md:_top-16 md:_shrink-0 motion-reduce:_transform-none',
'md:_top-16 md:_shrink-0 motion-reduce:_transform-none motion-reduce:_transition-none',
'_transform-gpu _transition-all _ease-in-out',
'print:_hidden',
showSidebar ? 'md:_w-64' : 'md:_w-20',
asPopover ? 'md:_hidden' : 'md:_sticky md:_self-start',
menu
? 'max-md:[transform:translate3d(0,0,0)]'
: 'max-md:[transform:translate3d(0,-100%,0)]'
: 'max-md:[transform:translate3d(0,-100%,0)]',
isWindowResizing && 'max-md:_transition-none _transition-none'
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can keep the previous polyfill and add

Suggested change
isWindowResizing && 'max-md:_transition-none _transition-none'
'[.resizing_&]:_transition-none'

I will push my changes to your branch

)}
Copy link
Contributor Author

@87xie 87xie Sep 3, 2024

Choose a reason for hiding this comment

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

enable reduced motion

before:

enabled-reduce-motion-before.mov

after:

enabled-after.mov

disable reduced motion

before:

disabled-before.mov

after:

disabled-after.mov

ref={containerRef}
>
Expand Down
1 change: 0 additions & 1 deletion packages/nextra-theme-docs/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './polyfill'
import { ThemeProvider } from 'next-themes'
import type { NextraThemeLayoutProps } from 'nextra'
import { useRouter } from 'nextra/hooks'
Expand Down
15 changes: 0 additions & 15 deletions packages/nextra-theme-docs/src/polyfill.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/nextra-theme-docs/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getGitIssueUrl } from './get-git-issue-url'
export { renderComponent, renderString } from './render'
export { useGitEditUrl } from './use-git-edit-url'
export { useIsWindowResizing } from './use-is-window-resizing'
30 changes: 30 additions & 0 deletions packages/nextra-theme-docs/src/utils/use-is-window-resizing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect, useRef, useState } from 'react'

export function useIsWindowResizing() {
const [isWindowResizing, setIsWindowResizing] = useState(false)
const innerState = useRef(false)

useEffect(() => {
let resizeTimer: ReturnType<typeof setTimeout>
const handleResize = () => {
if (innerState.current === false) {
innerState.current = true
setIsWindowResizing(true)
}

clearTimeout(resizeTimer)
resizeTimer = setTimeout(() => {
innerState.current = false
setIsWindowResizing(false)
}, 200)
}
window.addEventListener('resize', handleResize)

return () => {
clearTimeout(resizeTimer)
window.removeEventListener('resize', handleResize)
}
}, [])

return isWindowResizing
}
Copy link
Collaborator

@dimaMachina dimaMachina Sep 5, 2024

Choose a reason for hiding this comment

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

this hook is very heavy for performance since it updates DOM children on every pixel change

Screen.Recording.2024-09-05.at.14.33.30.mov

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used the preview page from other PRs, and it seems like there's a similar issue when resizing. I'm not sure if it's related to this hook.

Loading