0.7.577 • Published 10 months ago

xref-js v0.7.577

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

xref({})

xref is a lightweight JavaScript library that transforms multi-page applications (MPAs) into smooth, single-page-like experiences. It leverages the power of AJAX and CSS transitions to create seamless page transitions without the complexity of a full single-page application (SPA) framework.

This package is a Experimental. API is subject to change. The Library works as intended (with bugs) as a npm package or as module inside a app. Inserting the xref.umd.js in eg. a WordPress-App won't work very good right now

In general xref assumes you don not want to update the <head>tag when a page-transition is happening. It only switched the ol body with the new body tag.

Features

  • Easy integration with existing multi-page websites
  • Smooth page transitions with customizable animations
  • Maintains browser history and supports back/forward navigation
  • Lightweight and dependency-free
  • TypeScript support

Install

npm install xref-js
yarn add xref-js
<script src="https://unpkg.com/xref-js@latest/dist/xref.umd.js" defer></script>

Info: Buggy as umd module inside already coded websites

Basic Usage

1. Import

import xref from 'xref-js';

2. Initialize xref with your desired options:

xref({
  debug: true,
  head: {
    update: true, // default: false
    retrigger: {
      css: true, // default: false
      js: true // default: false
    }
  },
  /**
   * `prefetch` can fetch HTML Pages
   * before clicking on them by 
   * setting `prefetch.active` to
   * `true`, `event:'mouseover'` &
   * `delay:0` are default
   */
  prefetch: {
    active: true,
    delay: 0,
    event: 'mouseover',
    media: true,
    js: true,
    css: true
  },
  /**
   * `transition` is the global 
   * Object for transition out and
   * in of pages. 
  */
  transition: {
    /**
     * the DOM Element to swap to the new
     * HTML content to (everything outside
     * stays as on initial page load!)
     */
    swapHtml: 'main',
    /**
     * duration of the main
     * animation in and out!
     * info: duration times of
     * in and out are added.
     * depeding on partial 
     * transtion duration
     * animation is:
     * ________________
     *   [t] of partial out
     * + [t] of swapHtml out
     * + [t] of swapHtml in
     * + [t] of partial in
     * ________________
     * = total time of transition
    */
    duration: 100,
    /**
     * Easing function of the 
     * main page transition
    */
    easing: 'ease',
    /**
     * the transition when the element
     * is inserted in the page
     */
    in: {
      /**
       * translates to CSS Keyframes
       * @keyframe xref-animation-in-[i] {
       *    0%: {
       *      opacity: 0
       *    },
       *    100% {
       *      // if  'to' is set here would be something
       *    }
       * }
      */
      from: {
        opacity: 0,
        filter: 'blur(50px)'
      },
    },
    /**
     * the transition when the element
     * is removed from the page
     */
    out: {
      /**
       * translates to CSS Keyframes
       * @keyframe xref-animation-in-[i] {
       *    0%: {
       *       // if  'from' is set here would be something
       *    },
       *    100% {
       *      opacity: 0
       *    }
       * }
      */
      to: {
        opacity: 0,
        filter: 'blur(50px)'
      }
    },
    callback: {
      onEnter: () => {},
      onStart: () => {},
      onPlay: () => {},
      onPause: () => {},
      onFinish: () => {}
    }
    /** `transition.partials` is an Array of HTMLElements that can be animated 
     * out BEFORE the `transition.swapHtml` Element is animated out 
     * and AFTER the the `transition.swapHtml` Element is animated in.
     */
    partials: [
      {
        element: 'header, footer, aside',
        duration: 100,
        easing: 'ease-in-out',
        in: {
          from: {
            opacity: 0,
          },
        },
        out: {
          to: {
            opacity: 0,
          }
        }
      },
    ]
  }
});

You can have seperate animations for 'in' and 'out' or set either one them, the missing one is assumed the provided played backwards!

You can set 'from' and 'to' optionaly, it would make sense to only do 'from' in the 'in' objecte and do 'to' in the 'out' object.

3. Ensure your HTML links are relative or to the same domain:

<a href="/about">About</a>
<a href="/contact">Contact</a>

xref will automatically intercept clicks on these links and handle the page transitions.

How It Works

xref intercepts clicks on links within your site. Instead of allowing the browser to load a new page, it:

  1. Fetches the new page content via AJAX
  2. Smoothly transitions out the current page content
  3. Updates the browser's history
  4. Swaps in the new page content with a transition effect
  5. Manages the <head> tag to ensure styles and scripts are correctly updated

This creates a fluid, app-like experience while maintaining the structure and SEO benefits of a traditional multi-page site.

Advanced Usage

xref offers several advanced features and customization options:

  • Custom transition effects
  • Control over which HTML element is swapped
  • Handling of external scripts and styles
  • Integration with CSS frameworks like Tailwind CSS

Refer to the full documentation for detailed information on these features.

API & Types

interface XrefOptions: {
  transition?: {
    swapHtml?: string,
    duration?: number,
    easing?: string,
    out?: {
      from?: Record<string?, string | number | boolean>,
      to?: Record<string?, string | number | boolean>,
    },
    in?: {
      from?: Record<string?, string | number | boolean>,
      to?: Record<string?, string | number | boolean>,
    },
    callback?: {
      onEnter?: Function,
      onStart?: Function,
      onPlay?: Function,
      onPause?: Function,
      onFinish?: Function
    },
    state?: {
      started?: boolean,
      playing?: boolean,
      paused?: boolean,
      finished?: boolean,
    },
    partials?: [
        {
          element?: string,
          duration?: number,
          easing?: string,
          out?: {
            from?: Record<string?, string | number | boolean>,
            to?: Record<string?, string | number | boolean>,
          },
          in?: {
            from?: Record<string?, string | number | boolean>,
            to?: Record<string?, string | number | boolean>,
          }
        }
      ]
    }
  },
  prefetch?: {
    active?: boolean,
    event?: string,
    delay?: number,
    media?: boolean,
    css?: boolean,
    js?: boolean 
  },
  head?: {
    update?: boolean,
    retrigger?: {
      css?: boolean,
      js?: boolean,
      include?: RegexPattern | string,
      exclude?: RegexPattern | string,
    },
  }
};

Browser Support

xref works in all modern browsers that support the History API and CSS transitions. For older browsers, it will gracefully fall back to normal page loads.

Issues & future Features

  • bug: page swap should update the page same a hard reload
  • bug: script dont get updated -> a new page inserted should trigger DOM content loaded on a event listeners DOMContentLoaded and load
  • info: works best with a static header, change script in <head> and end of the body doest load scripts correctly
0.7.577

10 months ago

0.7.574

10 months ago

0.7.573

10 months ago

0.7.576

10 months ago

0.7.575

10 months ago

0.7.570

10 months ago

0.7.572

10 months ago

0.7.571

10 months ago

0.7.569

10 months ago

0.7.568

10 months ago

0.7.567

10 months ago

0.7.566

10 months ago

0.7.565

10 months ago

0.7.564

10 months ago

0.7.563

10 months ago

0.7.562

10 months ago

0.7.561

10 months ago

0.7.560

10 months ago

0.7.559

10 months ago

0.7.558

10 months ago

0.7.557

10 months ago

0.7.556

10 months ago

0.7.555

10 months ago

0.7.554

10 months ago

0.7.553

10 months ago

0.7.552

10 months ago

0.7.551

10 months ago

0.7.550

10 months ago

0.7.549

10 months ago

0.7.548

10 months ago

0.7.547

10 months ago

0.7.546

10 months ago

0.7.545

10 months ago

0.7.544

10 months ago

0.7.543

10 months ago

0.7.542

10 months ago

0.7.541

10 months ago

0.7.540

10 months ago

0.7.539

10 months ago

0.7.538

10 months ago

0.7.537

10 months ago

0.7.536

10 months ago

0.7.535

10 months ago

0.7.534

10 months ago

0.7.533

10 months ago

0.7.532

10 months ago

0.7.531

10 months ago

0.7.530

10 months ago

0.7.529

10 months ago

0.7.528

10 months ago

0.7.527

10 months ago

0.7.526

10 months ago

0.7.525

10 months ago

0.7.524

10 months ago

0.7.523

10 months ago

0.7.522

10 months ago

0.7.521

10 months ago

0.7.520

10 months ago

0.7.519

10 months ago

0.7.518

10 months ago

0.7.517

10 months ago

0.7.516

10 months ago

0.7.515

10 months ago

0.7.514

10 months ago

0.7.513

10 months ago

0.7.512

10 months ago

0.7.510

10 months ago

0.7.51

10 months ago

0.7.4

10 months ago

0.7.2

10 months ago

0.7.1

10 months ago

0.7.0

10 months ago

0.6.9

10 months ago

0.6.8

10 months ago

0.6.7

10 months ago

0.6.6

10 months ago

0.6.5

10 months ago

0.6.4

10 months ago

0.6.3

10 months ago

0.6.2

10 months ago

0.6.1

10 months ago

0.6.0

10 months ago

0.5.9

10 months ago

0.5.8

10 months ago

0.5.7

10 months ago

0.5.6

10 months ago

0.5.5

10 months ago

0.5.4

10 months ago

0.5.3

10 months ago

0.5.2

10 months ago

0.5.1

10 months ago

0.5.0

10 months ago

0.4.9

10 months ago

0.4.8

10 months ago

0.4.7

10 months ago

0.4.6

10 months ago

0.4.5

10 months ago

0.4.3

10 months ago

0.4.2

10 months ago

0.4.1

10 months ago

0.4.0

10 months ago

0.3.9

10 months ago

0.3.8

10 months ago

0.3.7

10 months ago

0.3.6

10 months ago

0.3.5

10 months ago

0.3.4

10 months ago

0.3.3

10 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.9

10 months ago

0.2.8

10 months ago

0.2.7

10 months ago

0.2.6

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.9

10 months ago

0.1.8

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago

0.0.0

10 months ago