swup / fragment-plugin

A swup plugin for dynamically replacing containers based on rules 🧩
https://swup-fragment-plugin.netlify.app
MIT License
15 stars 1 forks source link

Simplify `visit.fragmentVisit` #35

Closed hirasso closed 1 year ago

hirasso commented 1 year ago

Description

Right now visit.fragmentVisit contains quite some unnecessary information:

export type FragmentVisit = {
    rule: {
        from: Path,
        to: Path,
        containers: string[],
        matchesFrom: (path: Path) => boolean,
        matchesTo: (path: Path) => boolean,
        name?: string
    },
    containers: string[]
}

I'd propose to simplify it to this:

/**
 * The state of the current visit
 */
export type FragmentVisit = {
    name?: string,
    containers: string[];
};

Usage Example

Don't reset the scroll position for a fragment visit with name "my-fragment-visit":

swup.hooks.on('visit:start', async (visit) => {
  if (visit.fragmentVisit?.name === 'my-fragment-visit') {
    visit.scroll.reset = true;
  }
})

visit.fragmentVisit.containers would be equal to visit.containers, but I would keep then around for debugging purposes.

Any other ideas?

Since visit.fragmentVisit is not documented yet, I would consider this a non-breaking change.

Checks

daun commented 1 year ago

Looks good! Can't really say which keys might be useful here, but the two suggested should come in handy.