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

@benbraide/inlinejs

v1.2.1

Published

Run javascript code by embedding them in your HTML using the element as context.

Downloads

29

Readme

InlineJS

npm (scoped) npm bundle size (minified)

Run JavaScript code by embedding them in your HTML using the element as context.

InlineJS is a component-based reactive framework inspired by Alpine.js.

InlineJS works without creating shadow DOMs.

Notes:

  • Directives are of the general form: hx-[DirectiveName] or data-hx-[DirectiveName]. Example: hx-effect or data-hx-effect.
  • The hx- prefix can be configured via the global config object.
  • InlineJS binds to elements with the hx-data directive present.

Install

  • Grab source or distribution versions from GitHub
  • Include script in your HTML file.

CDNs

<script src="https://cdn.jsdelivr.net/npm/@benbraide/[email protected]/dist/inlinejs.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@benbraide/[email protected]/dist/inlinejs.min.js"></script>

NPM Install

npm install @benbraide/inlinejs

Initialization

import { BootstrapAndAttach } from  '@benbraide/inlinejs';

BootstrapAndAttach();

BootstrapAndAttach takes an optional DOM element to search. Defaults to the document element.

Snippets

Dropdown/Modal

<div hx-data="{ open: false }">
    <button hx-on:click="open = true">Open Dropdown</button>
    <div hx-show="open" hx-on:click.outside="open = false">
        Dropdown Body
    </div>
</div>

Tabs

<div hx-data="{ tab: 'foo' }">
    <button hx-class:active="tab === 'foo'" hx-on:click="tab = 'foo'">Foo</button>
    <button hx-class:active="tab === 'bar'" hx-on:click="tab = 'bar'">Bar</button>

    <div hx-show="tab === 'foo'">Tab Foo</div>
    <div hx-show="tab === 'bar'">Tab Bar</div>
</div>

You can even use it for non-trivial things: Pre-fetching a dropdown's HTML content on hover

<div hx-data="{ open: false, html: 'Loading Spinner...' }">
    <button
        hx-on:mouseenter.once="html = $fetch('/dropdown-partial.html')"
        hx-on:click="open = true"
    >Show Dropdown</button>

    <div hx-show="open" hx-html="html" hx-on:click.outside="open = false"></div>
</div>

Text Interpolation

InlineJS supports reactive text interpolation using a pair of {{ and }}. Interpolation is valid for attribute values and text contents.

Example

<form hx-data="{ btnText: 'Save', txtValue: 'Default value' }">
    <input name="content" value="{{ txtValue }}">
    <button type="submit">{{ btnText }} Draft</button>
</form>

Quick Notes

  • When using the compiled scripts in a script tag no initialization is necessary, as InlineJS will automatically initialize and bind to the document.
  • If the result of an evaluated expression is a function, most directives will call that function.
  • When evaluating an expression, this refers to the element that the directive is being executed on.
  • Directives are executed accordingly, from left to right, as they appear on an element. There is no precedence.
  • This is the base API and it can be used for development and extension purposes.

Extending InlineJS

Creating Directives

import { CreateDirectiveHandlerCallback } from  '@benbraide/inlinejs';
import { AddDirectiveHandler } from  '@benbraide/inlinejs';

const greeterDirective = CreateDirectiveHandlerCallback('greeter', (directiveDetails) => { ... });

AddDirectiveHandler(greeterDirective);

Note: The above directive will be referenced as hx-greeter.

  • Call the CreateDirectiveHandlerCallback to create the handler for your directive. The function requires a name for the directive and a callback as the handler.
  • Pass the returned directive details to AddDirectiveHandler to register your new directive.

The specified callback function is provided an object containing the following:

  • contextElement references the element that the directive was used on.
  • componentId holds the id of the context/current component.
  • component may hold the context/current component.
  • expression value specified as the attribute value on the element. E.g. hx-greeter="doSomething()". doSomething() is the expression.
  • argKey if the directive followed by a :, then more text, this refers to the text after the :. E.g. hx-greeter:unique.
  • argOptions holds a list of texts supplied by the user delimited by a .. E.g. hx-greeter.new.transform Note: the user is free to specify both argKey and argOptions in a single usage.

If you would prefer using a class, then you could use:

import { IDirectiveHandler } from  '@benbraide/inlinejs';
import { AddDirectiveHandler } from  '@benbraide/inlinejs';

class GreeterDirective implements IDirectiveHandler{
    public GetName(){ return 'greeter'; }
    public Handle(directiveDetails){ ... }
}

const greeterDirective = new GreeterDirective;

AddDirectiveHandler(greeterDirective);

If you rather not use Node.js and a build step, then you could use:

function greeterDirectiveHandler(directiveDetails){ ... }
  • The function must be defined in the global scope in order to be found.
  • The function name must be suffixed by DirectiveHandler.
  • The function name must be camelCased.

Creating Magic Properties

import { CreateMagicHandlerCallback } from  '@benbraide/inlinejs';
import { AddMagicHandler } from  '@benbraide/inlinejs';

const greeter = CreateMagicHandlerCallback('greeter', (context) => { ... });

AddMagicHandler(greeterDirective);

Note: The above magic property will be referenced as $greeter.

  • Call the CreateMagicHandlerCallback to create the handler for your magic property. The function requires a name for the directive and a callback as the handler.
  • Pass the returned directive details to AddMagicHandler to register your new magic property.

Note: CreateMagicHandlerCallback takes an optional callback function as third parameter. It is called when the magic property is being accessed.

The specified callback function is provided an object containing the following:

  • contextElement references the element that the directive was used on.
  • componentId holds the id of the context/current component.
  • component may hold the context/current component.

If you would prefer using a class, then you could use:

import { IMagicHandler } from  '@benbraide/inlinejs';
import { AddMagicHandler } from  '@benbraide/inlinejs';

class Greeter implements IMagicHandler{
    public GetName(){ return 'greeter'; }
    public Handle(context){ ... }
    public OnAccess(context){ ... }
}

const greeter = new Greeter;

AddMagicHandler(greeter);

Packages and Plugins

Bundles

Resources

Utilities

Security

If you find a security vulnerability, please send an email to [email protected]

InlineJS relies on a custom implementation using the Function object to evaluate its directives. Despite being more secure then eval(), its use is prohibited in some environments, such as Google Chrome App, using restrictive Content Security Policy (CSP).

If you use InlineJS in a website dealing with sensitive data and requiring CSP, you need to include unsafe-eval in your policy. A robust policy correctly configured will help protecting your users when using personal or financial data.

Since a policy applies to all scripts in your page, it's important that other external libraries included in the website are carefully reviewed to ensure that they are trustworthy and they won't introduce any Cross Site Scripting vulnerability either using the eval() function or manipulating the DOM to inject malicious code in your page.

License

Licensed under the MIT license, see LICENSE.md for details.