Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow defining slots in split-layout (#6938) (CP: 24.1) #6957

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 32 additions & 11 deletions packages/split-layout/src/vaadin-split-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,38 @@ class SplitLayout extends ElementMixin(ThemableMixin(PolymerElement)) {

/** @private */
_processChildren() {
[...this.children].forEach((child, i) => {
if (i === 0) {
this._primaryChild = child;
child.setAttribute('slot', 'primary');
} else if (i === 1) {
this._secondaryChild = child;
child.setAttribute('slot', 'secondary');
} else {
child.removeAttribute('slot');
}
});
const children = [...this.children];

children.filter((child) => child.hasAttribute('slot')).forEach((child) => this._processChildWithSlot(child));

children
.filter((child) => !child.hasAttribute('slot'))
.forEach((child, i) => this._processChildWithoutSlot(child, i));
}

/** @private */
_processChildWithSlot(child) {
const slot = child.getAttribute('slot');
if (child.__autoSlotted) {
this[`_${slot}Child`] = null;
child.removeAttribute('slot');
} else {
this[`_${slot}Child`] = child;
}
}

/** @private */
_processChildWithoutSlot(child, idx) {
let slotName;
if (this._primaryChild || this._secondaryChild) {
slotName = this._primaryChild ? 'secondary' : 'primary';
} else {
slotName = idx === 0 ? 'primary' : 'secondary';
}

this[`_${slotName}Child`] = child;
child.setAttribute('slot', slotName);
child.__autoSlotted = true;
}

/** @private */
Expand Down
92 changes: 91 additions & 1 deletion packages/split-layout/test/split-layout.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { aTimeout, fixtureSync, nextFrame, track } from '@vaadin/testing-helpers';
import { aTimeout, fixtureSync, nextFrame, nextRender, track } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import '../vaadin-split-layout.js';

Expand Down Expand Up @@ -74,6 +74,96 @@ describe('split layout', () => {
expect(getComputedStyle(first).pointerEvents).to.equal('visible');
expect(getComputedStyle(second).pointerEvents).to.equal('visible');
});

describe('elements with slot pre-defined', () => {
it('should respect pre-defined slot values in both elements', async () => {
const layout = fixtureSync(`
<vaadin-split-layout>
<div id="second" slot="secondary">secondary</div>
<div id="first" slot="primary">primary</div>
</vaadin-split-layout>
`);
await nextRender();
expect(layout.querySelector('#first').slot).to.be.equal('primary');
expect(layout.querySelector('#second').slot).to.be.equal('secondary');
});

it('should assign a slot if only one element has "secondary" slot pre-defined', async () => {
const layout = fixtureSync(`
<vaadin-split-layout>
<div id="second" slot="secondary">secondary</div>
<div id="first">primary</div>
</vaadin-split-layout>
`);
await nextRender();
expect(layout.querySelector('#first').slot).to.be.equal('primary');
expect(layout.querySelector('#second').slot).to.be.equal('secondary');
});

it('should assign a slot if only element has "primary" slot pre-defined', async () => {
const layout = fixtureSync(`
<vaadin-split-layout>
<div id="second">secondary</div>
<div id="first" slot="primary">primary</div>
</vaadin-split-layout>
`);
await nextRender();
expect(layout.querySelector('#first').slot).to.be.equal('primary');
expect(layout.querySelector('#second').slot).to.be.equal('secondary');
});

it('should respect assigned slot if only one element has slot pre-defined after order is inverted', async () => {
const layout = fixtureSync(`
<vaadin-split-layout>
<div id="second">secondary</div>
<div id="first" slot="primary">primary</div>
</vaadin-split-layout>
`);
await nextRender();

const first = layout.querySelector('#first');
layout.prepend(first);
await nextRender();

expect(layout.querySelector('#first').slot).to.be.equal('primary');
expect(layout.querySelector('#second').slot).to.be.equal('secondary');
});

it('should swap slots if children without pre-defined slots invert order', async () => {
const layout = fixtureSync(`
<vaadin-split-layout>
<div id="second">secondary</div>
<div id="first">primary</div>
</vaadin-split-layout>
`);
await nextRender();

const second = layout.querySelector('#second');
layout.prepend(second);
await nextRender();

expect(layout.querySelector('#first').slot).to.be.equal('secondary');
expect(layout.querySelector('#second').slot).to.be.equal('primary');
});

it('should assign slots only for direct children', async () => {
const layout = fixtureSync(`
<vaadin-split-layout>
<div id="first">primary</div>
<vaadin-split-layout id="second">
<div id="nested-first" slot="primary"></div>
<div id="nested-second" slot="secondary"></div>
</vaadin-split-layout>
</vaadin-split-layout>
`);
await nextRender();

const first = layout.querySelector('#first');
expect(first.slot).to.be.equal('primary');
const second = layout.querySelector('#second');
expect(second.slot).to.be.equal('secondary');
});
});
});
});

Expand Down