Skip to content

Commit 59f502b

Browse files
authored
Merge pull request #59 from swup/feat/update-readme
2 parents 12cbc60 + 30e941e commit 59f502b

File tree

1 file changed

+86
-13
lines changed

1 file changed

+86
-13
lines changed

README.md

+86-13
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,28 @@ html.is-animating .transition-main {
147147

148148
## Options
149149

150-
```typescript
150+
### Type Signature: `Options`
151+
152+
```js
151153
export type Options = {
152-
rules: Array<{
153-
from: string | string[];
154-
to: string | string[];
155-
containers: string[];
156-
name?: string;
157-
scroll?: boolean | string;
158-
focus?: boolean | string;
159-
}>;
154+
rules: Rule[];
160155
debug?: boolean;
161156
};
162157
```
163158

159+
### Type Signature: `Rule`
160+
161+
```js
162+
export type Rule = {
163+
from: string | string[];
164+
to: string | string[];
165+
containers: string[];
166+
name?: string;
167+
scroll?: boolean | string;
168+
focus?: boolean | string;
169+
};
170+
```
171+
164172
### rules
165173

166174
The rules that define whether a visit will be considered a fragment visit. Each rule consists of
@@ -175,7 +183,7 @@ The rule's `from`/`to` paths are converted to a regular expression by [path-to-r
175183
{
176184
from: ['/users', '/users/:filter?'],
177185
to: ['/users', '/users/:filter?'],
178-
containers: ['#user-list'],
186+
containers: ['#user-list']
179187
}
180188
];
181189
}
@@ -196,7 +204,7 @@ Required, Type: `string[]` – Selectors of containers to be replaced if the vis
196204
**Notes**
197205

198206
- **only IDs and no nested selectors are allowed**. `#my-element` is valid, while
199-
`.my-element` or `#wrap #child` both will throw an error.
207+
`.my-element` or `#wrap #child` both will throw an error.
200208
- if **any** of the selectors in `containers` doesn't return a match in the current document, the rule will be skipped.
201209
- Fragment elements **must either match a swup container or be a descendant of one of them**
202210

@@ -384,7 +392,9 @@ This will work fine, until you apply a `transform` to one of the modal's parent
384392

385393
```css
386394
html.is-changing .transition-main {
387-
transition: opacity 250ms, transform 250ms;
395+
transition:
396+
opacity 250ms,
397+
transform 250ms;
388398
}
389399
html.is-animating .transition-main {
390400
opacity: 0;
@@ -398,7 +408,7 @@ The reason for this is that a CSS `transform` establishes a [containing block fo
398408
You have two options to fix this:
399409

400410
1. Don't apply CSS `transform`s to any of the parents of a modal
401-
2. Use `<detail open>` for the modal:
411+
2. Use `<dialog open>` for the modal:
402412

403413
```diff
404414
- <main id="overlay" class="modal">
@@ -429,3 +439,66 @@ The first `<main>` element in a document will be considered the main content by
429439
**Cons**:
430440

431441
- Wrapping your `<main>` content inside a `<dialog>` will produce [semantically incorrect markup](https://stackoverflow.com/a/75007908/586823). We still think it's the cleanest approach for now, until the [Popover API](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API) reaches [wider browser support](https://caniuse.com/?search=popover).
442+
443+
## API methods
444+
445+
Fragment plugin provides a few API functions for advanced use cases. To be able to access those, you'll need to keep a reference to the plugin during instanciation:
446+
447+
```js
448+
const fragmentPlugin = new SwupFragmentPlugin({ rules });
449+
const swup = new Swup({ plugins: [fragmentPlugin] });
450+
/** You can now call the plugin's public API, for example: */
451+
fragmentPlugin.getFragmentVisit(route);
452+
```
453+
454+
### `getFragmentVisit(route)`
455+
456+
Get information about the fragment visit for a given route. Returns either `FragmentVisit` or `undefined`.
457+
458+
```js
459+
/**
460+
* Get information about which containers will
461+
* be replaced when hovering over links:
462+
*/
463+
document.querySelectorAll('a[href]').forEach((el) => {
464+
el.addEventListener('mouseenter', () => {
465+
const fragmentVisit = fragmentPlugin.getFragmentVisit({
466+
from: window.location.href, // the current URL
467+
to: el.href // the URL of the link
468+
});
469+
console.log(`will replace ${fragmentVisit?.containers || swup.options.containers}`);
470+
});
471+
});
472+
```
473+
474+
### `prependRule(rule)`
475+
476+
Prepends a [rule](#type-signature-rule) to the array of rules.
477+
478+
```js
479+
fragmentPlugin.prependRule({ from: '/foo/', to: '/bar/', containers: ['#foobar'] });
480+
```
481+
482+
### `appendRule(rule)`
483+
484+
Appends a [rule](#type-signature-rule) to the array of rules.
485+
486+
```js
487+
fragmentPlugin.prependRule({ from: '/baz/', to: '/bat/', containers: ['#bazbat'] });
488+
```
489+
490+
### `getRules()`
491+
492+
Get a **clone** of all registered fragment rules
493+
494+
```js
495+
console.log(fragmentPlugin.getRules());
496+
```
497+
498+
### `setRules(rules)`
499+
500+
Overwrite all fragment rules with the provided rules. This methods provides the lowest-level access to the rules. For example, you could use it to remove all rules with the name `foobar`:
501+
502+
```js
503+
fragmentPlugin.setRules(fragmentPlugin.getRules().filter((rule) => rule.name !== 'foobar'));
504+
```

0 commit comments

Comments
 (0)