Skip to content

ui-sref on a parent element breaks all ui-srefs on children #2962

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

Closed
rluba opened this issue Sep 2, 2016 · 10 comments
Closed

ui-sref on a parent element breaks all ui-srefs on children #2962

rluba opened this issue Sep 2, 2016 · 10 comments

Comments

@rluba
Copy link

rluba commented Sep 2, 2016

Example use case:

  • There’s a table where each row links to a sub-state.
  • For the user’s convenience the whole <tr> is made clickable by adding ui-sref="A" to it.
  • Some of the table columns contain <a> links to other states (implemented by using ui-sref="B" on those link tags).

When you click on any of those links – because you want to visit state B – the link to A on the <tr> elements takes precedence because ui-sref’s click handler does not call e.stopPropagation() when it handles the click event. So the transition to B gets superseded by a transition to A when the parent element processes the event. Therefore, you always end up in state A – no matter where you click.

Adding e.stopPropagation() somewhere in the click handler solves the problem.

Tested with [email protected] and [email protected]

@rluba
Copy link
Author

rluba commented Sep 14, 2016

@christopherthielen, I just discovered a new problem that arises when e.stopPropagation() is added – which is probably why it wasn’t added originally:

When the link is part of a dropdown (eg. angular-bootstrap’s one), calling e.stopPropagation() will prevent the dropdown from closing after the click. This seems to be a bigger issue than the nested ui-sref case, so we should probably revert the patch.

A possible fix might be to let ui-sref check if a transition is in progress and don’t transition in that case, but I’m sure that this will have other negative side effects. 😕 (Eg. clicking on links while a long-running transition is in progress will no longer work…)

Any better ideas?

@rluba
Copy link
Author

rluba commented Sep 14, 2016

Another way might be to share the transition timeout variable between all ui-sref clickhooks and set and clear it within the handler. Then any ui-sref could check if another ui-sref already initiated a transition during this cycle, so it won’t interfere with other transitions.

But that still feels a bit messy.

@shql
Copy link

shql commented Sep 15, 2016

@rluba & @christopherthielen

We also run into breaking issues due to this change. We have a ui-sref element that changes the state but its container has also a click event listener that does stuff. With this change the container event is never fired. I strongly recommend to revert this as you cannot rely on event propagation anymore.

What do you guys think?

@christopherthielen christopherthielen modified the milestones: 1.0.0-final, 1.0.0-beta.2 Sep 15, 2016
@christopherthielen
Copy link
Contributor

christopherthielen commented Sep 15, 2016

what if you just wrote a stop-propagation directive for those ui-sref that are doubly nested?

.directive('stopPropagation', () => ({
  restrict: 'A',
  link: (scope, elem) => elem.on('click', event => event.stopPropagation())
}));
<a ui-sref="foo">
  <a ui-sref="foo.bar" stop-propagation></a>
</a>

@christopherthielen
Copy link
Contributor

@rluba I've reverted the commit for beta.3. Can you try the stopPropagation directive technique?

@rluba
Copy link
Author

rluba commented Sep 23, 2016

I'm currently using my "transition timeout as shared variable" fix in a local fork and it works great. I'll submit a PR as soon as I'm confident that there are no side effects.

@shql
Copy link

shql commented Sep 23, 2016

Thanks guys

@christopherthielen
Copy link
Contributor

Here's a minimal reproduction: http://plnkr.co/edit/vp0aMZgYIuRK2fXvx96b?p=preview

Clicking 'home.foo' ui-sref in the <td> activates the ui-sref on the <tr>

@christopherthielen
Copy link
Contributor

Here's a viable workaround (<stop-propagation> directive)
http://plnkr.co/edit/mxtjxiq3eVssYhfnKsSf?p=preview

@christopherthielen
Copy link
Contributor

christopherthielen commented Sep 23, 2016

I'll submit a PR as soon as I'm confident that there are no side effects.

I'd like to see your solution. However, know that I'm wary of adding more code (and managing more state) for a case which I don't think is very common and which there is a simple workaround.

rluba added a commit to rluba/ui-router that referenced this issue Oct 5, 2016
rluba added a commit to rluba/ui-router that referenced this issue Oct 5, 2016
…the transitions started by ui-srefs on child elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants