Skip to content

Commit

Permalink
fix(core): 🐛 keep container position
Browse files Browse the repository at this point in the history
when container is not the only child, keep its position rather than append it at the and of the
wrapper
  • Loading branch information
thierrymichel committed Aug 22, 2019
1 parent e92240a commit 5482154
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
34 changes: 31 additions & 3 deletions packages/core/__tests__/utils/dom.test.ts
Expand Up @@ -18,9 +18,7 @@ wrapper.appendChild(container);

// Expected
const checkDoc = new RegExp(
`^<html>[\\s\\S]+body[\\s\\S]+${dom['_attr'].wrapper}[\\s\\S]+${
dom['_attr'].container
}[\\s\\S]+${namespace}[\\s\\S]+</html>$`
`^<html>[\\s\\S]+body[\\s\\S]+${dom['_attr'].wrapper}[\\s\\S]+${dom['_attr'].container}[\\s\\S]+${namespace}[\\s\\S]+</html>$`
);

afterEach(() => {
Expand Down Expand Up @@ -77,3 +75,33 @@ it('get namespace', () => {
expect(dom.getNamespace()).toBe(namespace);
expect(dom.getNamespace(wrapper)).toBe(namespace);
});

it('remove container', () => {
document.body.appendChild(wrapper);

dom.removeContainer(container);

expect(wrapper.children.length).toBe(0);
});

it('add container', () => {
document.body.appendChild(wrapper);

dom.removeContainer(container);
dom.addContainer(container, wrapper);

expect(wrapper.children.length).toBe(1);
});

it('add container with sibling', () => {
const sibling = document.createElement('div');

document.body.appendChild(wrapper);
wrapper.appendChild(sibling);

dom.removeContainer(container);
dom.addContainer(container, wrapper);

expect(wrapper.children.length).toBe(2);
expect(container.nextElementSibling).toBe(sibling);
});
12 changes: 4 additions & 8 deletions packages/core/src/modules/Transitions.ts
Expand Up @@ -26,7 +26,7 @@ import {
// Hooks
import { hooks } from '../hooks';
// Utils
import { helpers } from '../utils';
import { dom, helpers } from '../utils';
// Modules
import { Logger } from './Logger';
import { Store } from './Store';
Expand Down Expand Up @@ -281,20 +281,16 @@ export class Transitions {
* Add next container.
*/
public async add(data: ITransitionData, wrapper: Wrapper): Promise<void> {
wrapper.appendChild(data.next.container);
dom.addContainer(data.next.container, wrapper);
hooks.do('nextAdded', data);
}

/**
* Remove current container.
*/
public async remove(data: ITransitionData): Promise<void> {
const { container } = data.current;

if (document.body.contains(container)) {
container.parentNode.removeChild(container);
hooks.do('currentRemoved', data);
}
dom.removeContainer(data.current.container);
hooks.do('currentRemoved', data);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/utils/dom.ts
Expand Up @@ -20,6 +20,7 @@ import { schemaAttribute } from '../schemas/attribute';
export class Dom {
private _attr: ISchemaAttribute = schemaAttribute;
private _parser: DOMParser = new DOMParser();
private _sibling: HTMLElement = null;

/**
* Convert HTMLDocument to string.
Expand Down Expand Up @@ -82,6 +83,27 @@ export class Dom {
);
}

/**
* Remove container and store next sibling (if applicable).
*/
public removeContainer(container: HTMLElement) {
if (document.body.contains(container)) {
this._sibling = container.nextElementSibling as HTMLElement;
container.parentNode.removeChild(container);
}
}

/**
* Add container before next sibling or at the end of the wrapper.
*/
public addContainer(container: HTMLElement, wrapper: HTMLElement) {
if (this._sibling) {
wrapper.insertBefore(container, this._sibling);
} else {
wrapper.appendChild(container);
}
}

/**
* Get `[data-barba-namespace]`.
*/
Expand Down

0 comments on commit 5482154

Please sign in to comment.