Skip to content

Commit 66665c9

Browse files
Add preventScrollRestoration option for scroll resortation (#202)
* Add preventScrollRestoration * Prettied * Prettied * Added documentation for preventScrollRestoration
1 parent f17c93f commit 66665c9

File tree

8 files changed

+1473
-910
lines changed

8 files changed

+1473
-910
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Additional props:
6060

6161
`modal`: When `false`it allows to interact with elements outside of the drawer without closing it. Defaults to`true`.
6262

63+
`preventScrollRestoration`: When `true` it prevents scroll restoration when the drawer is closed after a navigation happens inside of it. Defaults to `true`.
64+
6365
### Trigger
6466

6567
The button that opens the dialog. [Props](https://www.radix-ui.com/docs/primitives/components/dialog#trigger).

pnpm-lock.yaml

Lines changed: 1455 additions & 898 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type DialogProps = {
4949
modal?: boolean;
5050
nested?: boolean;
5151
onClose?: () => void;
52+
preventScrollRestoration?: boolean;
5253
} & (WithFadeFromProps | WithoutFadeFromProps);
5354

5455
function Root({
@@ -69,6 +70,7 @@ function Root({
6970
fixed,
7071
modal = true,
7172
onClose,
73+
preventScrollRestoration = true,
7274
}: DialogProps) {
7375
const [isOpen = false, setIsOpen] = React.useState<boolean>(false);
7476
const [hasBeenOpened, setHasBeenOpened] = React.useState<boolean>(false);
@@ -124,6 +126,7 @@ function Root({
124126
modal,
125127
nested,
126128
hasBeenOpened,
129+
preventScrollRestoration,
127130
});
128131

129132
function getScale() {

src/use-position-fixed.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ export function usePositionFixed({
77
modal,
88
nested,
99
hasBeenOpened,
10+
preventScrollRestoration,
1011
}: {
1112
isOpen: boolean;
1213
modal: boolean;
1314
nested: boolean;
1415
hasBeenOpened: boolean;
16+
preventScrollRestoration: boolean;
1517
}) {
1618
const [activeUrl, setActiveUrl] = React.useState(typeof window !== 'undefined' ? window.location.href : '');
1719
const scrollPos = React.useRef(0);
@@ -64,7 +66,7 @@ export function usePositionFixed({
6466
document.body.style.right = 'unset';
6567

6668
requestAnimationFrame(() => {
67-
if (activeUrl !== window.location.href) {
69+
if (preventScrollRestoration && activeUrl !== window.location.href) {
6870
setActiveUrl(window.location.href);
6971
return;
7072
}

src/use-snap-points.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ export function useSnapPoints({
3333

3434
const shouldFade =
3535
(snapPoints &&
36-
snapPoints.length > 0 &&
37-
(fadeFromIndex || fadeFromIndex === 0) &&
38-
!Number.isNaN(fadeFromIndex) &&
39-
snapPoints[fadeFromIndex] === activeSnapPoint) ||
36+
snapPoints.length > 0 &&
37+
(fadeFromIndex || fadeFromIndex === 0) &&
38+
!Number.isNaN(fadeFromIndex) &&
39+
snapPoints[fadeFromIndex] === activeSnapPoint) ||
4040
!snapPoints;
4141

4242
const activeSnapPointIndex = React.useMemo(

test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
"typescript": "5.2.2",
2525
"vaul": "workspace:^"
2626
}
27-
}
27+
}

test/postcss.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ module.exports = {
33
tailwindcss: {},
44
autoprefixer: {},
55
},
6-
}
6+
};

test/tailwind.config.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Config } from 'tailwindcss'
1+
import type { Config } from 'tailwindcss';
22

33
const config: Config = {
44
content: [
@@ -10,11 +10,10 @@ const config: Config = {
1010
extend: {
1111
backgroundImage: {
1212
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
13-
'gradient-conic':
14-
'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
13+
'gradient-conic': 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
1514
},
1615
},
1716
},
1817
plugins: [],
19-
}
20-
export default config
18+
};
19+
export default config;

0 commit comments

Comments
 (0)