Skip to content

Commit

Permalink
feat(core): ✨ allow custom data when adding to history
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierfoucrier committed May 11, 2023
1 parent e1e4b5f commit 2eaab1c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions packages/core/__tests__/utils/history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ it('manage history with programmatic replace', async () => {
expect(h.rs).toHaveBeenCalledTimes(1);
});

it('manage history with programmatic push and custom data', async () => {
const custom = {
custom: 'data',
};

history.add(first.url, 'barba', 'push', custom);
expect(history.current.data).toEqual(custom);
});

it('manage history with programmatic replace and custom data', async () => {
const custom = {
custom: 'data',
};

history.add(first.url, 'barba', 'replace', custom);
expect(history.current.data).toEqual(custom);
});

it('store custom user data', async () => {
const custom = {
custom: 'data',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class History {
/**
* Add a new state.
*/
public add(url: string, trigger: Trigger, action?: HistoryAction): void {
public add(url: string, trigger: Trigger, action?: HistoryAction, data?: object): void {
// If no state, it will be updated later.
const ns = 'tmp';
const method = action ?? this._getAction(trigger);
Expand All @@ -124,7 +124,7 @@ export class History {
y: window.scrollY,
},
url,
data: {},
data: data ?? {},
};

switch (method) {
Expand Down

0 comments on commit 2eaab1c

Please sign in to comment.