Code Monkey home page Code Monkey logo

alpinejs-ray's Introduction

alpinejs-ray

npm version license test status
npm downloads jsdelivr downloads

alpinejs-ray

Debug Alpine.js code with Ray to fix problems faster

Install this package into any project using Alpine.js to send messages to the Ray app.

Note: use version ^1.4 of this package for Alpine v2 and ^2.0 for Alpine v3.

Installation

Installation via CDN (recommended)

The preferred way to use this package is to load it via CDN, which must be done before loading Alpine.

The axios library must be loaded prior to loading alpinejs-ray and Alpine:

<script src="https://cdn.jsdelivr.net/npm/axios@latest/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/alpinejs-ray@2/dist/standalone.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer>

Installation via Module Import

First, install alpinejs-ray with npm (or your preferred package manager):

npm install alpinejs-ray

Although not the recommended way, the package can be imported as an ESM module along with alpinejs and axios:

import Alpine from 'alpinejs';
import AlpineRayPlugin from 'alpinejs-ray';

window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

window.Alpine = Alpine;

Alpine.plugin(AlpineRayPlugin);
Alpine.start();

Configuration

To configure alpinejs-ray, you must create an alpineRayConfig property on the window object before loading alpinejs-ray:

<script>
    window.alpineRayConfig = {
        interceptErrors: true,
        logComponentsInit: true,
        logCustomEvents: true,
        logEvents: ['abc'],
    };
</script>

<!-- load axios and alpinejs-ray -->
Name Type(s) Default Description
logComponentsInit boolean false Send info on component initializations to Ray
logErrors boolean false Send javascript errors to Ray instead of the console
logEvents boolean, array false Send specified custom events to Ray, or false to disable

Usage

After installing the plugin, access the $ray() magic method within your components:

<button @click="$ray().text('hello world')">Say Hello</button>

See the node-ray reference for a complete list of available methods.

Directives

Use the x-ray directive within your HTML markup to easily send data to Ray. The value of the directive must be a valid javascript expression.

<div x-data>
    <!-- sends 'hello world' and the value of the 'mystore.somevalue' Alpine store to Ray -->
    <div x-ray="'hello world'"></div>
    <div x-ray="$store.mystore.somevalue"></div>
</div>

The x-ray directive values are reactive; if the value changes, the new data will be sent to and displayed in Ray in-place. The changed value will be momentarily highlighted in Ray to indicate that it was updated.

Example Components

<button @click="$ray('hello from alpine')">Send to Ray</button>
<div x-data="onClickData()">
    <div x-show="show">Hi There Ray!</div>

    <button x-on:click="toggle()">Show/Hide (Ray)</button>
</div>

<script>
    function onClickData() {
        return {
            init() {
                this.$ray().html('<strong>init on-click-ray data</strong>');
            },
            toggle() {
                this.show = !this.show;
                this.$ray('toggled show value to ' + (this.show ? 'true' : 'false'));
            },
            show: false,
        };
    }
</script>

Displaying errors

Errors can be displayed in Ray automatically, with the portion of the code that caused the error highlighted.

screenshot

Tracking Data Stores

Alpine stores can be automatically sent to Ray whenever the store data is mutated. Consider the following:

window.Alpine.store('mydata', {
    showing: false,
});

setInterval(() => {
    window.Alpine.store('mydata').showing = !window.Alpine.store('mydata').showing;
}, 3000);

To watch the store and display changes in Ray, use the $ray().watchStore('name') method:

<div x-data="componentData()">
    <div x-show="$store.mydata.showing">Hi There Ray!</div>
    <button x-on:click="toggle()">Show/Hide (Ray)</button>
</div>

<script>
    window.Alpine.store('mydata', {
        showing: false,
    });

    function componentData() {
        return {
            init() {
                this.$ray().watchStore('mydata');
            },
            toggle() {
                this.$store.mydata.showing = !this.$store.mydata.showing;
            },
        };
    }
</script>

Development Setup

For the development of alpinejs-ray, clone the repository and install dependencies via npm:

npm install

Finally, build all library files; they will be output to the dist directory.

npm run build:all

Testing

alpinejs-ray uses Jest for unit tests. To run the test suite:

npm run test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

alpinejs-ray's People

Contributors

dependabot[bot] avatar depfu[bot] avatar github-actions[bot] avatar gldrenthe89 avatar patinthehat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

alpinejs-ray's Issues

Error when adding alpinejs-ray to Alpine Version 3 project

Describe the bug

When I add alpinejs-ray to an Alpine V3.2.3 project I get the following error in the console

alpine.js?id=a3692cc7446e217f9238:2 Uncaught TypeError: e.Alpine.addMagicProperty is not a function at Object.start (alpine.js?id=a3692cc7446e217f9238:2) at alpine.js?id=a3692cc7446e217f9238:2 at alpine.js?id=a3692cc7446e217f9238:2 at alpine.js?id=a3692cc7446e217f9238:2

Uncaught TypeError: Cannot read property 'parentElement' of undefined at Yn (alpine.js?id=a3692cc7446e217f9238:2) at alpine.js?id=a3692cc7446e217f9238:2

To Reproduce

Steps to reproduce the behavior:

  1. create a new project with latest alpine js (v3.2.3 at this time) and alpinejs-ray
    "dependencies": { "alpinejs": "^3.2.3", "alpinejs-ray": "^1.4.0", }

  2. Add alpinejs-ray to your alpine config
    import { Ray, ray } from 'node-ray/web';
    import Alpine from 'alpinejs';
    import AlpineRayPlugin from 'alpinejs-ray';
    window.alpineRayConfig = {
    logComponentsInit: true,
    logCustomEvents: false,
    interceptErrors: true,
    interceptSpruce: true,
    };
    window.ray = ray;
    window.Ray = Ray;
    window.axios = require('axios');
    window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    window.Alpine = Alpine;
    window.AlpineRayPlugin = AlpineRayPlugin;
    window.AlpineRayPlugin.init();
    window.AlpineRayPlugin.start();
    Alpine.start()

  3. Start you webserver and load a page including the alpinejs refference

  4. See error
    alpine.js?id=a3692cc7446e217f9238:2 Uncaught TypeError: e.Alpine.addMagicProperty is not a function at Object.start (alpine.js?id=a3692cc7446e217f9238:2) at alpine.js?id=a3692cc7446e217f9238:2 at alpine.js?id=a3692cc7446e217f9238:2 at alpine.js?id=a3692cc7446e217f9238:2

Expected behavior

When adding alpinejs-ray to an alpine project the plugin should be loaded successfully

Screenshots

None

Environment (please complete the following information):

  • OS: Linux
  • Laravel Version: 8
  • Language: Vanilla Javascript

Error using alpinejs-ray via CDN

Description

Unable to use alpinejs-ray via CDN shows the following error in the web inspector:

TypeError: Cannot read properties of undefined (reading 'randomUUID')

Steps To Reproduce

  1. Using the following file:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/axios@latest/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/alpinejs-ray@2/dist/standalone.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
</head>

<body>
    <div x-data>
        <button @click="$ray('hello from alpine')">Send to Ray</button>
    </div>
</body>

</html>
  1. Open it and click on Send to ray
  2. See the following screenshot of the web inspector
Screenshot 2024-06-30 at 15 06 06

Expected behavior

The web page should not have any errors and should communicate with Ray

Environment

  • OS: macOS 14.5
  • Browser: Chrome 126.0.6478.127 (Official Build) (arm64)

Additional context

Setting up a project with alpinejs-ray installed via npm works correctly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.