
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Flea is a tiny JavaScript UI library based in Snabbdom and ES6 tagged template literals.
npm i flea
app({
model: "Hi.",
view: model => html`<h1>${model}</h1>`
})
app({
model: 0,
update: {
add: model => model + 1,
sub: model => model - 1
},
view: (model, msg) => html`
<div>
<button onclick=${msg.add}>+</button>
<h1>${model}</h1>
<button onclick=${msg.sub} disabled=${model <= 0}>–</button>
</div>`
})
app({
model: "",
update: {
text: (_, value) => value
},
view: (model, msg) => html`
<div>
<h1>Hi${model ? " " + model : ""}.</h1>
<input oninput=${e => msg.text(e.target.value)} />
</div>`
})
CDN
<script src="https://cdn.rawgit.com/fleajs/flea/master/dist/flea.min.js"></script>
<script src="https://cdn.rawgit.com/fleajs/flea/master/dist/html.min.js"></script>
Browserify
browserify index.js > bundle.js
<!doctype html>
<html>
<body>
<script src="bundle.js"></script>
</body>
</html>
Use html
to compose HTML elements.
const hello = html`<h1>Hello World!</h1>`
html
is a tagged template string. If you are familiar with React, this is like JSX, but without breaking JavaScript.
Flea's
html
function translates Hyperx into a Snabbdom/h function call.
Use app
to create your app. The app
function receives an object with any of the following properties.
A value or object that represents the state of your app.
You never modify the model directly, instead, send actions describing how the model should change. See view.
Object composed of functions known as reducers. These are a kind of action you can send.
A reducer describes how the model should change and returns a new model or part of a model.
const update = {
increment: model + 1,
decrement: model - 1
}
If a reducer returns part of a model, that part will be merged with the current model.
You call reducers inside a view, effect or subscription.
Reducers have a signature (model, data)
, where
model
is the current model, anddata
is the data sent along with the action.The view is a function that returns HTML using the html
function.
The view has a signature (model, msg, params)
, where
model
is the current model,msg
is the object you use to send actions (call reducers or cause effects) andparams
are the route parameters if the view belongs to a route.msg.action(data)
Instead of a view as a single function, declare an object with multiple views and use the route path as the key.
app({
view: {
"*": (model, msg) => {}
"/": (model, msg) => {}
"/:slug": (model, msg, params) => {}
}
})
*
default route used when no other route matches, e.g, 404 page, etc.
/
index route
/:a/:b/:c
matches a route with three components using the regular expression [A-Za-z0-9]+
and stores each captured group in the params object, which is passed into the view function.
The route path syntax is based in the same syntax found in Express.
To update the address bar relative location and render a different view, use msg.setLocation(path)
.
As a bonus, we intercept all <a href="/path">...</a>
clicks and call msg.setLocation("/path")
for you. If you want to opt out of this, add the custom attribute data-no-routing
to any anchor element that should be handled differently.
<a data-no-routing>...</a>
Effects cause side effects and are often asynchronous, like writing to a database, or sending requests to servers. They can dispatch other actions too.
Effects have a signature (model, msg, error)
, where
model
is the current model,msg
is an object you use to call reducers / cause effects (see view), anderror
is a function you may call with an error if something goes wrong.const update = {
add: model => model + 1
}
const effects = {
waitThenAdd: (_, msg) => setTimeout(msg.add, 1000)
}
Subscriptions are functions that run once when the DOM is ready. Use a subscription to register global events, like mouse or keyboard listeners.
While reducers and effects are actions you cause, you can't call subscriptions directly.
A subscription has a signature (model, msg, error)
.
const update = {
move: (model, { x, y }) => ({ x, y })
}
const subs = [
(_, msg) => addEventListener("mousemove", e => msg.move({ x: e.clientX, y: e.clientY }))
]
Hooks are functions called for certain events during the lifetime of the app. You can use hooks to implement middleware, loggers, etc.
Called when the model changes. Signature (lastModel, newModel, data)
.
Called when an action (reducer or effect) is dispatched. Signature (name, data)
.
Called when you use the error
function inside a subscription or effect. If you don't use this hook, the default behavior is to throw. Signature (err)
.
The root is the HTML element that will serve as a container for your app. If none is given, a div
element is appended to the document.body.
FAQs
This package is no longer supported and has been deprecated. To avoid malicious use, npm is hanging on to the package name.
The npm package flea receives a total of 4 weekly downloads. As such, flea popularity was classified as not popular.
We found that flea demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.