npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ekwoka/x-ajax

v0.3.2

Published

A simple Ajax Loader for AlpineJS

Downloads

8

Readme

A simple Ajax Loader for AlpineJS. Just feed it a URL, either explicitly or via a reactive Alpine expression, and watch as the content is loaded and replaced.

At it's core, this is a replacement for using x-html and handling your own fetch.

Install

npm i @ekwoka/x-ajax

Import to build:

import Alpine from 'alpinejs';
import Ajax from '@ekwoka/x-ajax';

Alpine.plugin(Ajax);

window.Alpine = Alpine;
Alpine.start();

Usage

Just feed a URL into x-ajax and the children of that element will be replaced by that URL. So long as the expression resolved to a string that can be used in a fetch, you'll be golden. This can use a responsive Alpine variable to continually replace the section with new AJAX content.

<div x-ajax="otherThing">...loading</div>

Use the modifier query followed by a querySelector string to find a specific element within the returned document.

<div x-ajax.query.main="otherThing">...loading</div>
// will find the
<main>
  inside the fetched resource and only use that

  <div x-ajax.query.class.thing-wrapper="otherThing">...loading</div>
  // class modifier after query will append . to the query for class name support within the query modifier
</main>

When using a selector to grab multiple elements, use the modifier all

<div x-ajax.query.class.product.all="otherThing">...loading</div>
// will find all [class="product"] elements and append them as children

When using a single query selector, use the modifier replace to destroy the x-ajax element and replace it with the selected element

<main x-ajax.query.main.replace="otherThing">...loading</main>
// will find the
<main>inside the fetched resource and replace this element with that element</main>

Halting Fetch

If you want to prevent the fetch from happening until a future time (while not wrapping the element in an x-if), you can have the expression resolve to any falsy value (false, null, undefined, 0, "", NaN). This will not trigger the fetch and replace.

<div x-ajax="ready ? myAwesomeURL : false">Waiting</div>

This will trigger an event on the element accessible at @halted for debugging purposes.

Error Handling and Events

x-ajax mimics the native object replacement API and emits two events on the element: load and error.

Naturally, error fires if there was any error in the process, most likely to be a network error, but will also fire if x-ajax cannot find the elements in the returned document. load fires after the network call and parsing, but before actually swapping the dom elements, which generally is incapable of producing an error. As such, these events can be accessed with alpine as so:

<div x-ajax.replace="myCoolURL" @load="success = true" @error="error = $event.detail"></div>

These can be used to change the url to a fallback, or to display an error message.

Roadmap

  • Add 'Children' when using a query to not use the direct selected tag, but only maintain its children

Author

👤 Eric Kwoka

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator