Skip to content

Add option to set swup main element via data attribute #20

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

Merged
merged 1 commit into from May 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/Swup/Resources/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ import SwupSlideTheme from '@swup/slide-theme';

class default_1 extends Controller {
connect() {
const dataContainers = this.containersValue;
const mainElement = this.mainElementValue || dataContainers[0] || '#swup';
const allElements = [mainElement].concat(dataContainers);
const containersList = allElements.filter((item, index) => {
return allElements.indexOf(item) === index;
});
const options = {
containers: ['#swup'],
plugins: ['slide' === this.themeValue ? new SwupSlideTheme() : new SwupFadeTheme(), new SwupFormsPlugin()],
containers: containersList,
plugins: [
'slide' === this.themeValue
? new SwupSlideTheme({ mainElement: mainElement })
: new SwupFadeTheme({ mainElement: mainElement }),
new SwupFormsPlugin(),
],
};
if (this.hasMainElementValue) {
options.mainElement = this.mainElementValue;
}
if (this.hasAnimateHistoryBrowsingValue) {
options.animateHistoryBrowsing = this.animateHistoryBrowsingValue;
}
Expand All @@ -20,9 +34,6 @@ class default_1 extends Controller {
if (this.hasCacheValue) {
options.cache = this.cacheValue;
}
if (this.hasContainersValue) {
options.containers = this.containersValue;
}
if (this.hasLinkSelectorValue) {
options.linkSelector = this.linkSelectorValue;
}
Expand All @@ -45,6 +56,7 @@ default_1.values = {
linkSelector: String,
theme: String,
debug: Boolean,
mainElement: String,
};

export { default_1 as default };
27 changes: 21 additions & 6 deletions src/Swup/Resources/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default class extends Controller {
cacheValue: boolean;
hasCacheValue: boolean;
containersValue: string[];
hasContainersValue: boolean;
mainElementValue: string;
hasMainElementValue: boolean;
linkSelectorValue: string;
hasLinkSelectorValue: boolean;
themeValue: string;
Expand All @@ -40,14 +41,31 @@ export default class extends Controller {
// custom values
theme: String,
debug: Boolean,
mainElement: String,
};

connect() {
const dataContainers = this.containersValue;
const mainElement = this.mainElementValue || dataContainers[0] || '#swup';
const allElements = [mainElement].concat(dataContainers);
const containersList = allElements.filter((item, index) => {
return allElements.indexOf(item) === index;
});

const options: any = {
containers: ['#swup'],
plugins: ['slide' === this.themeValue ? new SwupSlideTheme() : new SwupFadeTheme(), new SwupFormsPlugin()],
containers: containersList,
plugins: [
'slide' === this.themeValue
? new SwupSlideTheme({ mainElement: mainElement })
: new SwupFadeTheme({ mainElement: mainElement }),
new SwupFormsPlugin(),
],
};

if (this.hasMainElementValue) {
options.mainElement = this.mainElementValue;
}

if (this.hasAnimateHistoryBrowsingValue) {
options.animateHistoryBrowsing = this.animateHistoryBrowsingValue;
}
Expand All @@ -57,9 +75,6 @@ export default class extends Controller {
if (this.hasCacheValue) {
options.cache = this.cacheValue;
}
if (this.hasContainersValue) {
options.containers = this.containersValue;
}
if (this.hasLinkSelectorValue) {
options.linkSelector = this.linkSelectorValue;
}
Expand Down
151 changes: 140 additions & 11 deletions src/Swup/Resources/assets/test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ const startStimulus = () => {
};

describe('SwupController', () => {
let container;
afterEach(() => {
clearDOM();
actualSwupOptions = null;
});

beforeEach(() => {
container = mountDOM(`
it('connect', async () => {
const container = mountDOM(`
<html>
<head>
<title>Symfony UX</title>
Expand All @@ -62,14 +65,6 @@ describe('SwupController', () => {
</body>
</html>
`);
});

afterEach(() => {
clearDOM();
actualSwupOptions = null;
});

it('connect', async () => {
const bodyElement = getByTestId(container, 'body');
expect(bodyElement).not.toHaveClass('connected');

Expand All @@ -81,4 +76,138 @@ describe('SwupController', () => {
expect(actualSwupOptions.cache).toBe(true);
expect(actualSwupOptions.animateHistoryBrowsing).toBe(true);
});

it('neither main element nor containers provided', async () => {
const container = mountDOM(`
<html>
<head>
<title>Symfony UX</title>
</head>
<body>
<div
data-testid="body"
data-controller="check swup"
data-swup-link-selector-value="a"
data-swup-animation-selector-value="[transition-*]"
data-swup-debug-value="data-debug"
data-swup-cache-value="data-cache"
data-swup-animate-history-browsing-value="data-animate-history-browsing">
<div id="nav"></div>
<div id="swup">
<a href="/">Link</a>
</div>
</div>
</body>
</html>
`);
const bodyElement = getByTestId(container, 'body');
expect(bodyElement).not.toHaveClass('connected');

startStimulus();
await waitFor(() => expect(bodyElement).toHaveClass('connected'));
expect(actualSwupOptions.mainElement).toEqual(undefined);
expect(actualSwupOptions.containers).toEqual(['#swup']);
});

it('only data-main-element is provided,', async () => {
const container = mountDOM(`
<html>
<head>
<title>Symfony UX</title>
</head>
<body>
<div
data-testid="body"
data-controller="check swup"
data-swup-main-element-value="#main"
data-swup-link-selector-value="a"
data-swup-animation-selector-value="[transition-*]"
data-swup-debug-value="data-debug"
data-swup-cache-value="data-cache"
data-swup-animate-history-browsing-value="data-animate-history-browsing">
<div id="nav"></div>
<div id="main"></div>
<div id="swup">
<a href="/">Link</a>
</div>
</div>
</body>
</html>
`);
const bodyElement = getByTestId(container, 'body');
expect(bodyElement).not.toHaveClass('connected');

startStimulus();
await waitFor(() => expect(bodyElement).toHaveClass('connected'));
expect(actualSwupOptions.mainElement).toEqual('#main');
expect(actualSwupOptions.containers).toEqual(['#main']);
});

it('only data-containers provided', async () => {
const container = mountDOM(`
<html>
<head>
<title>Symfony UX</title>
</head>
<body>
<div
data-testid="body"
data-controller="check swup"
data-swup-containers-value="[&quot;#swup&quot;, &quot;#nav&quot;]"
data-swup-link-selector-value="a"
data-swup-animation-selector-value="[transition-*]"
data-swup-debug-value="data-debug"
data-swup-cache-value="data-cache"
data-swup-animate-history-browsing-value="data-animate-history-browsing">
<div id="nav"></div>
<div id="swup">
<a href="/">Link</a>
</div>
</div>
</body>
</html>
`);
const bodyElement = getByTestId(container, 'body');
expect(bodyElement).not.toHaveClass('connected');

startStimulus();
await waitFor(() => expect(bodyElement).toHaveClass('connected'));
expect(actualSwupOptions.mainElement).toEqual(undefined);
expect(actualSwupOptions.containers).toEqual(['#swup', '#nav']);
});

it('data-main-element and data-containers are provided,', async () => {
const container = mountDOM(`
<html>
<head>
<title>Symfony UX</title>
</head>
<body>
<div
data-testid="body"
data-controller="check swup"
data-swup-main-element-value="#main"
data-swup-containers-value="[&quot;#swup&quot;, &quot;#nav&quot;]"
data-swup-link-selector-value="a"
data-swup-animation-selector-value="[transition-*]"
data-swup-debug-value="data-debug"
data-swup-cache-value="data-cache"
data-swup-animate-history-browsing-value="data-animate-history-browsing">
<div id="nav"></div>
<div id="main"></div>
<div id="swup">
<a href="/">Link</a>
</div>
</div>
</body>
</html>
`);
const bodyElement = getByTestId(container, 'body');
expect(bodyElement).not.toHaveClass('connected');

startStimulus();
await waitFor(() => expect(bodyElement).toHaveClass('connected'));
expect(actualSwupOptions.mainElement).toEqual('#main');
expect(actualSwupOptions.containers).toEqual(['#main', '#swup', '#nav']);
});
});