1.2.9 • Published 3 years ago

sensibljs v1.2.9

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

SensibleJS

Super lightweight Javascript sensible utilities

Getting Started

Installing

npm i sensibljs

Start by referencing the script at the end of the tag. It will initialize automatically.

<script src="sensible.min.js" defer>

Then in your html document create the elements that you need. In this example we have a simple form to enter a name, and a submit button.

<form>
    <label for="name">Name</>
    <input id="name" type="text" placeholder="Name">
    <input id="email" type="email" placeholder="Email address">
    <button>Submit</button>
</form>

Now configure the elements that will be sensed. This is done by using the s-bind directive. This is a basic two fields example that declares 2 variables. Sensiblejs will detect this and configure the variable and bind it to the element.

<form>
    <label for="name">Name</>
    <input s-bind="name" id="name" type="text" placeholder="Name">
    <input s-bind="email" id="email" type="email" placeholder="Email address">
</form>

Now anything you type inside either element will automatically update the variable specified.

You can also specify to show or hide an element based on an expression. For this you need to use the s-if directive.

<form>
    <label for="name">Name</>
    <input s-bind="name" id="name" type="text" placeholder="Name">
    <input s-bind="email" id="email" type="email" placeholder="Email address">
    <button s-if="name.length > 0">Create record</button>
</form>

In the example above we added the s-if directive with the expression name.length > 0 meaning the button will be visible if the length of the variable name is larger than zero.

Furthermore, you can dynamically specify css properties to sense variables value changes. Let's say that you want to make the background color of the name field to green once it reached at least five characters.

To accomplish this you need to use the s-css directive.

<form>
    <label for="name">Name</>
    <input s-bind="name" id="name" type="text" placeholder="Name">
    <input s-bind="email" id="email" type="email" placeholder="Email address">
    <button s-if="name.length > 0" s-css="backgroundColor: {name.length >= 5 ? 'green' : 'white'}">Create record</button>
</form>

In the example above, the background color of the button will change to green when the name variables is five or more characters long and white when is below five characters.

There are two options for sensing variable changes, on keyup or on lost focus (blur). To specify which one you must add the s-blur directive, this directive does not require a value.

    <input s-bind="name" s-blur id="name" type="text" placeholder="Name">

In the example above the variable will be updated when the element looses it's focus.

That is all you need to start.

As you have more variables to sense, it is better to declare all by declaring them in the store object.

The store object allows you to specify the persistence of the data, a way to prefix your storage variables, and declare the variables you will be using.

Optionally the store object has to be declared this way:

<script>
    let store = {
        persist: true,
        localPrefix: '__',
        data: {
            results: {
                type: Array, default: [
                    {fullName: 'Fulano de Tal', email: 'fulano@detal.com', phone: '7871234567'},
                    {fullName: 'Mengano de Tal', email: 'mengano@detal.com', phone: '7871234568'},
                ]
            },
            show: {type: Boolean, default: true, callBack: function() {console.log(show)},
        },
    };
</script>

In the example above we are declaring that the store will persist the data in the browser's local storage engine, specified __ as the prefix of every persisted variable, and the data property which will have the variables and properties of these variables. The syntax for declaring a variable inside the data property is:

{variable_name: {
    type: Array|Object|String|Number|etc..., 
    default: 'a default value', 
    callBack: a function that will be executed each time the variable changes.
}

When the tool initializes it will look for the store object, if none is found it will default to:

{
    persist: true,
    localPrefix: '__',
    data: {},
};

Documentation

There are 10 directives available to use:

DirectiveDescription
s-bindAdds "two-way data binding" to an element.
s-dataAdds data to store and defines variables.
s-blurUpdates the variable value when the element looses it's focus rather than onkeyup which is the default.
s-ifShow or hide an element based on expression.
s-cssBinds element css to an expression.
s-forDuplicates elements based on the contents of an array or object.
s-clickExecutes the expression when the element is clicked.
s-unclickExecutes the expression when there is a click away from the element.
s-callbackExecute a function when the elements bind variable changes.
s-srcSets the image source attribute to a variable. Useful in s-for directive.

Directives


s-bind

Example: <div s-bind="name">...</div>

s-bind Makes a two way binding between the element and the variable (name).


s-data

Example: <div s-data="{open: true}">...</div>

s-data Defines a variable and value.


s-blur

Example: <input s-bind="name" s-blur>

s-blur Updates the variable when the element looses it's focus. If you do not specify this directive, the variable will be updated onkeyup.


s-if

Example: <div s-if="name.length > 0">...</div>

s-if Shows or hides the element based on boolean result from expression.


s-css

Example: <div s-css="background-color: backgroundColor, color: myColor">...</div> Test s-css Applies CSS style attribute on the element based on the result from expression. In the example above the css style attribute background-color will be set to the value of the backgroundColor variable, and the color attribute will be set to the value of the myColor variable.


s-for

Example: <ul><li s-for="product of product">{product.name}</li></ul>

Example: <div s-for="product of products">Product name: {product.name}, cost: {product.cost}</div>

s-for Duplicates the element based on the contents of an Object or an Array. You can enter code inside. In the example above the result will be an unordered list of each product's name inside the products array. The code {product.name} will be replaced by the contents of product.name.


s-click

Example: <input type="checkbox" s-click="show = true">

s-click Execute the provided statement when the element is clicked.


s-unclick

Example: <input type="checkbox" s-unclick="show = false">

s-click Execute the provided statement when a click is done away from the element.


s-callback

Example: <input type="checkbox" s-bind="show" s-callback="executeThis()"

s-callback Execute a function when the elements bind variable changes.

Note: If the store data property specifies a callback function, then the store data takes precedence.


s-src

Example: <img s-src="imageUrl">

s-src Sets the image source attribute to the variable value. Useful for s-for directive.


HTML

This tool will allow you to bind local varibles to DOM elements in an easy and light way.

There is an example file (index.html) included in the package with a lot of examples.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Authors

See also the list of contributors who participated in this project.

License

Copyright © 2020 Ricardo Aponte Yunqué and contributors This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

Thanks to Blaize Stewart for Observables logic Thanks to @stimulus for domReady function

  • Inspired by pure necessity
1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.20

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

0.0.1

4 years ago

1.0.0

4 years ago