Skip to content

Commit

Permalink
Add skipReplaceTransitions prop to HolyLoader
Browse files Browse the repository at this point in the history
Fixes tomcru#23

Add `skipReplaceTransitions` prop to `HolyLoader` component to skip loader for `replaceState` transitions.

* **src/index.tsx**
  - Add `skipReplaceTransitions` prop to `HolyLoaderProps` interface.
  - Update `HolyLoader` component to handle `skipReplaceTransitions` prop.
  - Skip loader for `replaceState` transitions when `skipReplaceTransitions` is true.
* **README.md**
  - Add `skipReplaceTransitions` prop to the API section.
* **src/__tests__/holyLoader.test.ts**
  - Add tests to verify the behavior of `skipReplaceTransitions` prop.
  • Loading branch information
hootanht committed Nov 16, 2024
1 parent af294b2 commit a12387c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@ onClick={(e) => {
| `showSpinner` | `boolean` | Determines whether to accompany the loading bar with a spinner. Turned off by default. | `false` |
| `ignoreSearchParams` | `boolean` | Determines whether to ignore search parameters in the URL when triggering the loader. Turned off by default. | `false` |
| `dir` | `ltr` or `rtl` | Sets the direction of the top-loading bar. | `ltr` |
| `skipReplaceTransitions` | `boolean` | Specifies whether to skip the loader for replaceState transitions. | `false` |
35 changes: 35 additions & 0 deletions src/__tests__/holyLoader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it, vi } from 'vitest';
import { HolyLoaderProps } from '../index';
import HolyLoader from '../index';

describe('HolyLoader', () => {
it('should skip loader for replaceState transitions when skipReplaceTransitions is true', () => {
const props: HolyLoaderProps = {
skipReplaceTransitions: true,
};

const holyLoader = new HolyLoader(props);

const startSpy = vi.spyOn(holyLoader, 'start');
const completeSpy = vi.spyOn(holyLoader, 'complete');

history.replaceState({}, '', '/new-url');
expect(startSpy).not.toHaveBeenCalled();
expect(completeSpy).not.toHaveBeenCalled();
});

it('should not skip loader for replaceState transitions when skipReplaceTransitions is false', () => {
const props: HolyLoaderProps = {
skipReplaceTransitions: false,
};

const holyLoader = new HolyLoader(props);

const startSpy = vi.spyOn(holyLoader, 'start');
const completeSpy = vi.spyOn(holyLoader, 'complete');

history.replaceState({}, '', '/new-url');
expect(startSpy).toHaveBeenCalled();
expect(completeSpy).toHaveBeenCalled();
});
});
18 changes: 13 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export interface HolyLoaderProps {
* Default: "ltr"
*/
dir?: 'ltr' | 'rtl';

/**
* Specifies whether to skip the loader for replaceState transitions.
* Default: false
*/
skipReplaceTransitions?: boolean;
}

/**
Expand Down Expand Up @@ -104,7 +110,8 @@ const HolyLoader = ({
boxShadow = DEFAULTS.boxShadow,
showSpinner = DEFAULTS.showSpinner,
ignoreSearchParams = DEFAULTS.ignoreSearchParams,
dir = DEFAULTS.dir,
dir = DEFAULTS.dir,
skipReplaceTransitions = false,
}: HolyLoaderProps): null => {
const holyProgressRef = React.useRef<HolyProgress | null>(null);

Expand Down Expand Up @@ -166,10 +173,11 @@ const HolyLoader = ({
history.replaceState = (...args) => {
const url = args[2];
if (
url &&
isSamePathname(window.location.href, url) &&
(ignoreSearchParams ||
hasSameQueryParameters(window.location.href, url))
skipReplaceTransitions ||
(url &&
isSamePathname(window.location.href, url) &&
(ignoreSearchParams ||
hasSameQueryParameters(window.location.href, url)))
) {
originalReplaceState(...args);
return;
Expand Down

0 comments on commit a12387c

Please sign in to comment.