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

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

Merged
merged 1 commit into from
May 23, 2022
Merged

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

merged 1 commit into from
May 23, 2022

Conversation

optior
Copy link
Contributor

@optior optior commented Dec 6, 2020

Q A
Bug fix? no
New feature? yes
Tickets
License MIT

This pull request adds the possibility to change the mainElement of the swup theme. For this purpose the new data attribute data-main-element is introduced.

Copy link
Contributor

@tgalopin tgalopin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 40 (options.containers = this.element.getAttribute('data-containers').split(' ');), the containers are overriden, which may create a weird behavior if you use both mainElement and containers.

WDYT we should do if both are present in the HTML?

src/Swup/Resources/assets/src/controller.js Outdated Show resolved Hide resolved
@optior
Copy link
Contributor Author

optior commented Dec 6, 2020

That's a good question. If the attribute data-containers is meant to specify addtional css selectors for page reloading those values should be merged (data-containers and data-main-element). WDYT?

@tgalopin
Copy link
Contributor

tgalopin commented Dec 8, 2020

Yes, I think that would be the best course of action:

1/ if neither a main element nor containers are provided, use #swup by default
2/ if only a data-main-element is provided, use it both as a container and in themes
3/ if only a data-containers is provided, use it in containers and consider the first container as the main element
4/ if both are provided, always add the main element in the containers list if it's not already, use the main element in themes and use the containers in Swup

I think that makes the most sense

Also, can you rebase on main as the controller changed a bit?

@optior
Copy link
Contributor Author

optior commented Dec 8, 2020

I rebased my branch on the current main and implemented the required code.

Do I have to build src/Swup/Resources/assets/dist/controller.js or is this file generated automatically?

@tgalopin
Copy link
Contributor

tgalopin commented Dec 8, 2020

You should build it :)

@stof
Copy link
Member

stof commented Dec 8, 2020

You should build it :)

@tgalopin it might be worth improving the CI to ensure this does not get forgotten, if possible.

@tgalopin
Copy link
Contributor

tgalopin commented Dec 9, 2020

Hey @optior !

I had a local look at your PR, it's great, thanks!

The final thing that I think we can do it add tests for the various situations. In test/controller.test.js, there is a first initial test that doesn't check much things, but as we can access the options from inside a CheckController it should be possible to ensure the right containers option is passed to the Swup instance. Do you think you can create such tests for the different cases we talked about earlier?

@optior
Copy link
Contributor Author

optior commented Dec 11, 2020

I'll give it a try on the weekend, never done any js testing before.

@tgalopin
Copy link
Contributor

Awesome, thanks! Feel free to ping me on Symfony Devs Slack if you have questions!

@nicolas-grekas
Copy link
Member

Friendly ping @optior

@optior
Copy link
Contributor Author

optior commented Jan 7, 2021

I have no clue how to write those tests. Unfortunately I have currently no time to dig into it. It would be great if anybody else could write these tests.

@optior
Copy link
Contributor Author

optior commented Jan 29, 2021

Finally I got the tests working..but now I can't fix the linter errors.

@tgalopin Are the tests ok this way? Any idea how I can fix the linter errors?

@weaverryan
Copy link
Member

Hey @optior!

I'm sorry hasn't gotten more attention yet :/. Would you be willing to update it to the latest version? The big change is that we refactored away from data- attributes to use Stimulus values. I think, since you understand this PR, that should be a fairly simple change. But let me know: I'd like to get this merged one way or another.

Thanks!

@optior
Copy link
Contributor Author

optior commented May 10, 2022

Hey @weaverryan,

I'll update this pr in the next couple days.

@optior
Copy link
Contributor Author

optior commented May 20, 2022

Hey @weaverryan,

I just pushed the updated files.

@weaverryan
Copy link
Member

Thank you @optior! And nice additions to the tests too :)

@weaverryan weaverryan merged commit 6b629c5 into symfony:2.x May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants