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

11 months ago

0.7.572

11 months ago

0.7.571

11 months ago

0.7.569

11 months ago

0.7.568

11 months ago

0.7.567

11 months ago

0.7.566

11 months ago

0.7.565

11 months ago

0.7.564

11 months ago

0.7.563

11 months ago

0.7.562

11 months ago

0.7.561

11 months ago

0.7.560

11 months ago

0.7.559

11 months ago

0.7.558

11 months ago

0.7.557

11 months ago

0.7.556

11 months ago

0.7.555

11 months ago

0.7.554

11 months ago

0.7.553

11 months ago

0.7.552

11 months ago

0.7.551

11 months ago

0.7.550

11 months ago

0.7.549

11 months ago

0.7.548

11 months ago

0.7.547

11 months ago

0.7.546

11 months ago

0.7.545

11 months ago

0.7.544

11 months ago

0.7.543

11 months ago

0.7.542

11 months ago

0.7.541

11 months ago

0.7.540

11 months ago

0.7.539

11 months ago

0.7.538

11 months ago

0.7.537

11 months ago

0.7.536

11 months ago

0.7.535

11 months ago

0.7.534

11 months ago

0.7.533

11 months ago

0.7.532

11 months ago

0.7.531

11 months ago

0.7.530

11 months ago

0.7.529

11 months ago

0.7.528

11 months ago

0.7.527

11 months ago

0.7.526

11 months ago

0.7.525

11 months ago

0.7.524

11 months ago

0.7.523

11 months ago

0.7.522

11 months ago

0.7.521

11 months ago

0.7.520

11 months ago

0.7.519

11 months ago

0.7.518

11 months ago

0.7.517

11 months ago

0.7.516

11 months ago

0.7.515

11 months ago

0.7.514

11 months ago

0.7.513

11 months ago

0.7.512

11 months ago

0.7.510

11 months ago

0.7.51

11 months ago

0.7.4

11 months ago

0.7.2

11 months ago

0.7.1

11 months ago

0.7.0

11 months ago

0.6.9

11 months ago

0.6.8

11 months ago

0.6.7

11 months ago

0.6.6

11 months ago

0.6.5

11 months ago

0.6.4

11 months ago

0.6.3

11 months ago

0.6.2

11 months ago

0.6.1

11 months ago

0.6.0

11 months ago

0.5.9

11 months ago

0.5.8

11 months ago

0.5.7

11 months ago

0.5.6

11 months ago

0.5.5

11 months ago

0.5.4

11 months ago

0.5.3

11 months ago

0.5.2

11 months ago

0.5.1

11 months ago

0.5.0

11 months ago

0.4.9

11 months ago

0.4.8

11 months ago

0.4.7

11 months ago

0.4.6

11 months ago

0.4.5

11 months ago

0.4.3

11 months ago

0.4.2

11 months ago

0.4.1

11 months ago

0.4.0

11 months ago

0.3.9

11 months ago

0.3.8

11 months ago

0.3.7

11 months ago

0.3.6

11 months ago

0.3.5

11 months ago

0.3.4

11 months ago

0.3.3

11 months ago

0.3.2

11 months ago

0.3.1

11 months ago

0.3.0

11 months ago

0.2.9

11 months ago

0.2.8

11 months ago

0.2.7

11 months ago

0.2.6

11 months ago

0.2.5

11 months ago

0.2.4

11 months ago

0.2.3

11 months ago

0.2.2

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.9

11 months ago

0.1.8

11 months ago

0.1.7

11 months ago

0.1.6

11 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago

0.0.0

11 months ago