@very-simple/ajax 1.0.0-beta.6 → 1.0.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -87,6 +87,19 @@ start({
87
87
  })
88
88
  ```
89
89
 
90
+ `morphHeads`, `executeScripts` and `viewTransitions` also accept a callback
91
+ receiving `{ from, to, isBackForward }` and returning a boolean. Combined with
92
+ `data-ajax-state` this allows deciding per link, e.g. to skip morphing the
93
+ `<head>` when loading a partial:
94
+
95
+ ```html
96
+ <a href="/partial" data-ajax-state='{"isPartial": true}'>Partial</a>
97
+ ```
98
+
99
+ ```ts
100
+ start({ morphHeads: ({ to }) => !to.state?.isPartial })
101
+ ```
102
+
90
103
  ### `visit(url, options?)`
91
104
 
92
105
  A lower-level API for triggering a visit programmatically, e.g. in
package/dist/index.d.ts CHANGED
@@ -20,12 +20,12 @@ type MergeStrategy = 'replace' | 'morph' | 'before' | 'after' | 'prepend' | 'app
20
20
  interface Config {
21
21
  /** Strategy used to merge the swapped content into the current DOM. */
22
22
  merge?: MergeStrategy;
23
- /** Morph the `<head>` instead of leaving it untouched. */
24
- morphHeads?: boolean;
25
- /** Re-execute `<script>` tags found in the swapped content. */
26
- executeScripts?: boolean;
27
- /** Wrap navigations in the View Transitions API, if supported. */
28
- viewTransitions?: boolean;
23
+ /** Morph the `<head>` instead of leaving it untouched. A callback decides per visit. */
24
+ morphHeads?: Resolvable<boolean>;
25
+ /** Re-execute `<script>` tags found in the swapped content. A callback decides per visit. */
26
+ executeScripts?: Resolvable<boolean>;
27
+ /** Wrap navigations in the View Transitions API, if supported. A callback decides per visit. */
28
+ viewTransitions?: Resolvable<boolean>;
29
29
  /** Called instead of the default merge to render the new document. */
30
30
  render?: (newDocument: Document) => Promise<void> | void;
31
31
  /** Custom scroll handling, e.g. restoring position on back/forward visits. */
@@ -43,6 +43,12 @@ interface Config {
43
43
  /** Called before an element is removed from the DOM by a merge. */
44
44
  unmount?: (el: Element) => void;
45
45
  }
46
+ /** A value, or a callback resolving it right before the swap. */
47
+ type Resolvable<T> = T | ((info: {
48
+ from: PageState;
49
+ to: PageState;
50
+ isBackForward: boolean;
51
+ }) => T);
46
52
  type ScrollPosition = {
47
53
  top: number;
48
54
  };
@@ -95,6 +101,6 @@ declare const scrollPositions: Map<string, ScrollPosition>;
95
101
  * Load a new page, merge the regions/bodies and add a new history entry
96
102
  * according to the history mode.
97
103
  */
98
- declare const visit: (url: string, { history: historyMode, isBackForward, autoFocus, request, state, regions, morphHeads, merge: mergeStrategy, executeScripts: shouldExecuteScripts, viewTransitions, loadingDelay, progressHideDelay, }?: VisitOptions) => Promise<void>;
104
+ declare const visit: (url: string, { history: historyMode, isBackForward, autoFocus, request, state, regions, morphHeads, merge: mergeStrategy, executeScripts: executeScriptsOption, viewTransitions, loadingDelay, progressHideDelay, }?: VisitOptions) => Promise<void>;
99
105
 
100
- export { type AjaxState, type Config, type EventMap, type LoadingOptions, type MergeStrategy, type PageState, type ScrollBehavior, type ScrollPosition, type StartOptions, type VisitOptions, scrollPositions, start, visit };
106
+ export { type AjaxState, type Config, type EventMap, type LoadingOptions, type MergeStrategy, type PageState, type Resolvable, type ScrollBehavior, type ScrollPosition, type StartOptions, type VisitOptions, scrollPositions, start, visit };
package/dist/index.js CHANGED
@@ -231,7 +231,7 @@ var visit = async (url, {
231
231
  regions = config.regions,
232
232
  morphHeads = config.morphHeads,
233
233
  merge: mergeStrategy = config.merge,
234
- executeScripts: shouldExecuteScripts = config.executeScripts,
234
+ executeScripts: executeScriptsOption = config.executeScripts,
235
235
  viewTransitions = config.viewTransitions,
236
236
  loadingDelay = config.loadingDelay,
237
237
  progressHideDelay = config.progressHideDelay
@@ -274,6 +274,10 @@ var visit = async (url, {
274
274
  const to = { url, state, document: newDocument };
275
275
  await emit2("before-swap", { from, to, isBackForward, signal });
276
276
  if (signal.aborted) return;
277
+ const resolve = (option) => typeof option === "function" ? option({ from, to, isBackForward }) : option;
278
+ const shouldMorphHeads = resolve(morphHeads);
279
+ const shouldExecuteScripts = resolve(executeScriptsOption);
280
+ const useViewTransitions = resolve(viewTransitions);
277
281
  const getMergeStrategy = (oldEl, newEl) => newEl.getAttribute(`${config.prefix}merge`) || oldEl.getAttribute(`${config.prefix}merge`) || mergeStrategy;
278
282
  let autoFocusEl;
279
283
  const hasMatchingRegions = regions?.some(
@@ -320,17 +324,17 @@ var visit = async (url, {
320
324
  if (position) window.scrollTo(position);
321
325
  };
322
326
  if (config.render) {
323
- if (morphHeads) Idiomorph2.morph(document.head, newDocument.head);
327
+ if (shouldMorphHeads) Idiomorph2.morph(document.head, newDocument.head);
324
328
  await config.render(newDocument);
325
- } else if (viewTransitions && document.startViewTransition) {
329
+ } else if (useViewTransitions && document.startViewTransition) {
326
330
  await document.startViewTransition(async () => {
327
- if (morphHeads) Idiomorph2.morph(document.head, newDocument.head);
331
+ if (shouldMorphHeads) Idiomorph2.morph(document.head, newDocument.head);
328
332
  await mergeRegions();
329
333
  await emit2("after-swap", { from, to, isBackForward, signal });
330
334
  applyScrollBehavior();
331
335
  }).updateCallbackDone;
332
336
  } else {
333
- if (morphHeads) Idiomorph2.morph(document.head, newDocument.head);
337
+ if (shouldMorphHeads) Idiomorph2.morph(document.head, newDocument.head);
334
338
  await mergeRegions();
335
339
  await emit2("after-swap", { from, to, isBackForward, signal });
336
340
  applyScrollBehavior();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@very-simple/ajax",
3
- "version": "1.0.0-beta.6",
3
+ "version": "1.0.0-beta.7",
4
4
  "description": "A very simple turbolinks inspired ajax library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",