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

Link click handling breaks the <Router> isolation/context principle #298

Open
adjourn opened this issue Sep 6, 2023 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@adjourn
Copy link

adjourn commented Sep 6, 2023

Describe the bug

I'm making the assumption here that this package supports multiple routers running at the same time as it does use context for isolation instead of some kind of global store. Described bug is not an issue if my assumption is wrong.

Router component provides context which isolates and enforces the scope of router functionality.
For example useLocation under <Router> returns the location for that particular router.

Link click handling (A component) breaks that principle and functionality because link click event handler is added to document and if you have multiple routers (with default integration or custom), only one integration setter will be always called.

I propose to handle it in some sort of scoped way, for example:
<Router scopeEl={htmlElementRef} source={integration}> and

const target = args.scopeEl || document;
target .addEventListener("click", handleAnchorClick);

Or a bit more advanced (and potentially buggy): automatically find the nearest parent HTML element for <Router>.

Your Example Website or App

https://codesandbox.io/s/solid-app-router-example-forked-25ysls?file=/Index.js

Steps to Reproduce the Bug or Issue

  1. Go to Home (/) if not already there and open console
  2. Click on any of the "Post X (modal)" links
  3. Clear console log
  4. Click on any of the "Post X (modal)" links in modal (links under router 2)

You can see in logs [router 1] set global loc: that even though this link is rendered under second <Router>, it will still invoke the setter for first <Router> integration.

Expected behavior

Link behaviour respects the context.
Link rendered under <Router1 source={integration1}> should invoke the integration1 setter.
Link rendered under <Router2 source={integration2}> should invoke the integration2 setter.

Screenshots or Videos

No response

Platform

  • OS: any
  • Browser: any
  • Version: 0.8.3

Additional context

No response

@adjourn
Copy link
Author

adjourn commented Sep 6, 2023

Tried out my first suggestion locally, works wonderfully.

let scope1= null;
let scope2= null;

return (
  <>
    <div ref={scope1}>
      <Router source={integration1} scopeEl={scope1}>
        <Routes1 />
      </Router>
    </div>
  
    <div ref={scope2}>
      <Router source={integration2} scopeEl={scope2}>
        <Routes2 />
      </Router>
    </div>
  </>
);

Changes in router (node_modules dist/index.js):

// Router
const routerState = createRouterContext(integration, base, data, out, scopeEl);
// ...
function createRouterContext(integration, base = "", data, out, scopeEl) {
// ...
const target = scopeEl || document;
target.addEventListener("click", handleAnchorClick);
onCleanup(() => target.removeEventListener("click", handleAnchorClick));

What do you think?

@ryansolid
Copy link
Member

We should look at this.. Either that or have the router create an element itself.

@ryansolid ryansolid added the enhancement New feature or request label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants