Skip to content

Commit

Permalink
Add preventScrollRestoration option for scroll resortation (#202)
Browse files Browse the repository at this point in the history
* Add preventScrollRestoration

* Prettied

* Prettied

* Added documentation for preventScrollRestoration
  • Loading branch information
shkumbinhasani committed Jan 13, 2024
1 parent f17c93f commit 66665c9
Show file tree
Hide file tree
Showing 8 changed files with 1,473 additions and 910 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Additional props:

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

`preventScrollRestoration`: When `true` it prevents scroll restoration when the drawer is closed after a navigation happens inside of it. Defaults to `true`.

### Trigger

The button that opens the dialog. [Props](https://www.radix-ui.com/docs/primitives/components/dialog#trigger).
Expand Down
2,353 changes: 1,455 additions & 898 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type DialogProps = {
modal?: boolean;
nested?: boolean;
onClose?: () => void;
preventScrollRestoration?: boolean;
} & (WithFadeFromProps | WithoutFadeFromProps);

function Root({
Expand All @@ -69,6 +70,7 @@ function Root({
fixed,
modal = true,
onClose,
preventScrollRestoration = true,
}: DialogProps) {
const [isOpen = false, setIsOpen] = React.useState<boolean>(false);
const [hasBeenOpened, setHasBeenOpened] = React.useState<boolean>(false);
Expand Down Expand Up @@ -124,6 +126,7 @@ function Root({
modal,
nested,
hasBeenOpened,
preventScrollRestoration,
});

function getScale() {
Expand Down
4 changes: 3 additions & 1 deletion src/use-position-fixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ export function usePositionFixed({
modal,
nested,
hasBeenOpened,
preventScrollRestoration,
}: {
isOpen: boolean;
modal: boolean;
nested: boolean;
hasBeenOpened: boolean;
preventScrollRestoration: boolean;
}) {
const [activeUrl, setActiveUrl] = React.useState(typeof window !== 'undefined' ? window.location.href : '');
const scrollPos = React.useRef(0);
Expand Down Expand Up @@ -64,7 +66,7 @@ export function usePositionFixed({
document.body.style.right = 'unset';

requestAnimationFrame(() => {
if (activeUrl !== window.location.href) {
if (preventScrollRestoration && activeUrl !== window.location.href) {
setActiveUrl(window.location.href);
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/use-snap-points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export function useSnapPoints({

const shouldFade =
(snapPoints &&
snapPoints.length > 0 &&
(fadeFromIndex || fadeFromIndex === 0) &&
!Number.isNaN(fadeFromIndex) &&
snapPoints[fadeFromIndex] === activeSnapPoint) ||
snapPoints.length > 0 &&
(fadeFromIndex || fadeFromIndex === 0) &&
!Number.isNaN(fadeFromIndex) &&
snapPoints[fadeFromIndex] === activeSnapPoint) ||
!snapPoints;

const activeSnapPointIndex = React.useMemo(
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"typescript": "5.2.2",
"vaul": "workspace:^"
}
}
}
2 changes: 1 addition & 1 deletion test/postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module.exports = {
tailwindcss: {},
autoprefixer: {},
},
}
};
9 changes: 4 additions & 5 deletions test/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Config } from 'tailwindcss'
import type { Config } from 'tailwindcss';

const config: Config = {
content: [
Expand All @@ -10,11 +10,10 @@ const config: Config = {
extend: {
backgroundImage: {
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
'gradient-conic':
'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
'gradient-conic': 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
},
},
},
plugins: [],
}
export default config
};
export default config;

0 comments on commit 66665c9

Please sign in to comment.