Skip to content

Commit

Permalink
feat(history): plugin fixes and new option adding (#5665)
Browse files Browse the repository at this point in the history
* fixed issue with the history back navigation

[ADD] Added option to keep the query string parameters when changing user when navigating
[FIXED] History back via browser button not working

* Added typing for History plugin options

Added typing for History plugin options
- enabled
- keepQuery

* keepQuery default value: false

Changed the default value of the option keepQuery to false

* Update history.js

fixed param getting object name
  • Loading branch information
VahanMargaryan authored Apr 26, 2022
1 parent 333a3ae commit 798c7c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/modules/history/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function History({ swiper, extendParams, on }) {
root: '',
replaceState: false,
key: 'slides',
keepQuery: false,
},
});

Expand Down Expand Up @@ -58,6 +59,9 @@ export default function History({ swiper, extendParams, on }) {
} else if (!location.pathname.includes(key)) {
value = `${key}/${value}`;
}
if (swiper.params.history.keepQuery) {
value += location.search;
}
const currentState = window.history.state;
if (currentState && currentState.value === value) {
return;
Expand Down Expand Up @@ -86,7 +90,7 @@ export default function History({ swiper, extendParams, on }) {

const setHistoryPopState = () => {
paths = getPathValues(swiper.params.url);
scrollToSlide(swiper.params.speed, swiper.paths.value, false);
scrollToSlide(swiper.params.speed, paths.value, false);
};

const init = () => {
Expand Down
14 changes: 14 additions & 0 deletions src/types/modules/history.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ export interface HistoryMethods {}
export interface HistoryEvents {}

export interface HistoryOptions {
/**
* Enables History Plugin.
*
* @default false
*/
enabled?: boolean;

/**
* Swiper page root, useful to specify when you use Swiper history mode not on root website page.
* For example can be `https://my-website.com/` or `https://my-website.com/subpage/` or `/subpage/`
Expand All @@ -26,4 +33,11 @@ export interface HistoryOptions {
* @default 'slides'
*/
key?: string;

/**
* Keep query parameters when changing browser url.
*
* @default false
*/
keepQuery?: boolean;
}

0 comments on commit 798c7c0

Please sign in to comment.