@benbraide/inlinejs
v1.2.1
Published
Run javascript code by embedding them in your HTML using the element as context.
Downloads
34
Maintainers
Readme
InlineJS
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]
ordata-hx-[DirectiveName]
. Example:hx-effect
ordata-hx-effect
.- The
hx-
prefix can be configured via the globalconfig
object.InlineJS
binds to elements with thehx-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, asInlineJS
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
toright
, 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 theid
of the context/current component.component
may hold the context/current component.expression
value specified as theattribute
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 bothargKey
andargOptions
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 theid
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
- Core
- Extended
- Animation
- Screen
- Alert
- Swal Alert
- Collections
- Router
- Moment
- Database
- Element
- Components
- Canvas
- Sketch
- Theme
- Geolocation
- Quill
- Stripe
- Echo
- Socket
- Socket Server
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.