This page lists the available strawberry directives and exported methods.
- Directives: set on elements
<tag sb-directive="key"></tag>
- Methods: called from the global
sb
object.
Note this section is mentioned only completeness. for a more detailed information on the items in this section check out the documentation on reactivity.
Used to mark an element with a key which will allow the element to be updated when the respective data changes.
<p sb-mark="message">...</p>
<script>
data.message = 'Hello, World';
</script>
Used to render an element when the respective data value is truthy.
<p sb-if="show">Hello, World!</p>
<script>
data.show = true;
</script>
Used to render an element when the respective data value is falsy.
<p sb-ifnot="show">Hello, World!</p>
<script>
data.show = false;
</script>
If strawberry has been loaded correctly then all the methods mentioned in this
section should be available on the global sb
object.
function init(): ReactiveObject;
Info
ReactiveObject
is not a specific type, it is an object that looks like a regular JavaScript object but has reactive properties. For details check the Reactive Values page.
Used to initialize strawberry. Calling it does the following things:
- Returns a reactive object which is used to store all reactive data in a Strawberry app.
- Registers defined components.
<p sb-mark="message"></p>
<script>
const data = sb.init();
data.message = 'Hello, World!';
</script>
When sb.init
is called all templates elements with a name attribute are
registered. Strawberry checks for component templates once again after the
document is done loading, so it's alright to call sb.init
in the <head>
element of a document.
Info
Calling
sb.init
will always return the same reactive object so there is no point in calling it multiple times. If you want to register components after loading you should instead usesb.register
orsb.load
.
function directive(
name: string,
cb: Directive,
isParametric: value = false
): void;
type Directive = (params: {
el: Element; // The element to which the directive has been applied.
value: unknown; // The updated value.
key: string; // Period '.' delimited key that points to the value in the global data object.
isDelete: boolean; // Whether the value was deleted `delete data.prop`.
parent: Prefixed<object>; // The parent object to which the value belongs (the proxied object, unless isDelete).
prop: string; // Property of the parent which points to the value, `parent[prop] ≈ value`
param: string | undefined; // If directive is a parametric directive, `param` is passed
}) => void;
name
: the name of the directive being registeredcb
: the directive callback functionisParametric
: whether the directive is a parametric directive
Used to register a custom directive. For example here is a two way bind directive:
<script>
sb.directive('bind', ({ el, value, parent, prop }) => {
el.value = value;
el.oninput ??= (e) => {
parent[prop] = e.target.value;
};
});
</script>
<input type="text" sb-bind="message" />
function watch(key: string, watcher: Watcher): void;
type Watcher = (newValue: unknown) => any;
key
: dot separated string for a value in the reactive objectwatcher
: function that is called when watched value changes, it receives thenewValue
that is set
Used to set watcher function that is called when a watched value or its child value changes. For example:
data.a = { b: '' };
sb.watch('a.b', (v) => console.log(`b changed to: ${v}`));
sb.watch('a', (v) => console.log(`a changed to: ${v}`));
data.a.b = 'Hello, World';
// b changed to: Hello, World
// a changed to: [object Object]
Note: in the above example even the second watcher is triggered because b
is a
property (child value) of a
.
Warning
Watchers should not alter the reactive object. If a dependent value is required then a
computed
value should be used.
function unwatch(key?: string, watcher?: Watcher): void;
key
: key from which watchers are to be removed.watcher
: specific watcher which is to be removed.
Used to remove watchers. Watchers are removed depending on the args passed:
- Only
key
: all watchers registered under the passedkey
are removed. - Only
watcher
:watcher
is removed from all keys. - Both:
watcher
found registered withkey
is removed. - Neither: all watchers are removed.
Example:
sb.unwatch('a.b');
function register(): void;
function register(parentElement: HTMLElement): void;
function register(template: string): void;
function register(templateString: string[], ...args: unknown): void;
Used to register custom components in Strawberry. These components can be defined dynamically after a document has completed loading.
It can be called in multiple ways:
-
Without args: This will register all the components found in html document.
sb.register();
-
Passing the parent element: This will register all the components found inside the passed
parentElement
.sb.register(parentElement);
-
As a tag function: This allows for dynamically creating templates with interpolated values and expressions.
sb.register` <template name="colored-p"> <p style="font-family: sans-serif; color: ${color};"> <slot /> </p> </template>`;
-
Passing the component string: Functionally, it is the same as using it as a tagged function.
sb.register(`<template name="colored-p"> <p style="font-family: sans-serif; color: ${color};"> <slot /> </p> </template>`);
Warning
Once an element has been registered by a particular name, it cannot be re-registered,
sb-register
will skip over registered elements and only register the ones that have not been registered.
Info
If your components have been defined statically before the document finished loading (i.e. before
document.readyState
becomes"interactive"
) then you don't need to callsb.register
.
function load(files: string | string[]): Promise<void>;
files
: Path to a single file from the calling folder or a list of files.
Used to load components from external files. For example if you have the following folder structure:
.
├── index.html
└── components.html
Where index.html
is your main HTML file that will be served, and
components.html
contains your templates. You can load the components in
index.html
using sb.load
like so:
sb.load('components.html');
function prefix(value: string = 'sb'): void;
value
: value to use for prefix, defaults to'sb'
Used to override the default ('sb'
) prefix for directives, for example
if you want to use data attributes to manage directives you can do this:
<script>
sb.prefix('data-sb');
</script>
<p data-sb-mark="message"></p>