flet-box 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/bin/cli.js +15 -0
- package/bin/commands/checkApi.js +761 -0
- package/bin/commands/create.js +77 -5
- package/bin/utils/dtsProps.js +191 -0
- package/bin/utils/moduleExports.js +165 -0
- package/bin/utils/neutralise.js +64 -0
- package/bin/utils/templates.js +366 -27
- package/docs/CONTRIBUTING.md +156 -0
- package/docs/README.md +68 -0
- package/docs/arquitectura.md +164 -0
- package/docs/cli/README.md +542 -0
- package/docs/guides/README.md +33 -0
- package/docs/guides/app-templates.md +197 -0
- package/docs/guides/compatibility.md +95 -0
- package/docs/guides/embedding.md +284 -0
- package/docs/guides/first-app.md +208 -0
- package/docs/guides/frontend-services.md +171 -0
- package/docs/guides/minification-and-protection.md +260 -0
- package/docs/guides/mobile-and-platforms.md +439 -0
- package/docs/guides/online-updates.md +196 -0
- package/docs/guides/router.md +282 -0
- package/docs/guides/state.md +283 -0
- package/docs/guides/syntax-philosophy.md +150 -0
- package/docs/guides/utilities.md +241 -0
- package/docs/guides/widget-structure.md +163 -0
- package/docs/server/README.md +68 -0
- package/docs/server/START_HERE.md +121 -0
- package/docs/server/api.md +225 -0
- package/docs/server/data-services.md +300 -0
- package/docs/server/deployment.md +113 -0
- package/docs/server/security.md +196 -0
- package/docs/services/README.md +79 -0
- package/docs/services/RamStore.md +84 -0
- package/docs/services/Session.md +84 -0
- package/docs/services/Storage.md +88 -0
- package/docs/services/http.md +86 -0
- package/docs/tools/README.md +138 -0
- package/docs/tools/animation.md +87 -0
- package/docs/tools/async-ids-log.md +80 -0
- package/docs/tools/device.md +132 -0
- package/docs/tools/introspection.md +67 -0
- package/docs/tools/lists.md +96 -0
- package/docs/tools/markdown.md +79 -0
- package/docs/tools/reactivity.md +288 -0
- package/docs/tools/state.md +103 -0
- package/docs/tools/styling.md +92 -0
- package/docs/tools/text-and-time.md +89 -0
- package/docs/tools/theme.md +102 -0
- package/docs/tools/validation.md +75 -0
- package/docs/tools/visual-effects.md +84 -0
- package/docs/widget/Accordion.md +144 -0
- package/docs/widget/AdMob.md +144 -0
- package/docs/widget/AdSense.md +133 -0
- package/docs/widget/AdaptiveScaffold.md +105 -0
- package/docs/widget/AlertDialog.md +103 -0
- package/docs/widget/AnimatedBox.md +130 -0
- package/docs/widget/AnimatedText.md +123 -0
- package/docs/widget/AppBar.md +118 -0
- package/docs/widget/Audio.md +135 -0
- package/docs/widget/Avatar.md +119 -0
- package/docs/widget/Badge.md +128 -0
- package/docs/widget/BottomNavigation.md +116 -0
- package/docs/widget/BottomSheet.md +125 -0
- package/docs/widget/Button.md +137 -0
- package/docs/widget/COMMON_PROPS.md +150 -0
- package/docs/widget/Card.md +125 -0
- package/docs/widget/Carousel.md +151 -0
- package/docs/widget/Chart.md +151 -0
- package/docs/widget/Checkbox.md +118 -0
- package/docs/widget/Chip.md +126 -0
- package/docs/widget/CircularBar.md +168 -0
- package/docs/widget/CircularChart.md +164 -0
- package/docs/widget/CodeViewer.md +130 -0
- package/docs/widget/CollapsibleSideBar.md +112 -0
- package/docs/widget/Column.md +129 -0
- package/docs/widget/Container.md +136 -0
- package/docs/widget/DataTable.md +150 -0
- package/docs/widget/Divider.md +109 -0
- package/docs/widget/DraggBox.md +129 -0
- package/docs/widget/Drawer.md +135 -0
- package/docs/widget/DrawerItem.md +98 -0
- package/docs/widget/Dropdown.md +139 -0
- package/docs/widget/DroppBox.md +152 -0
- package/docs/widget/FloatingActionButton.md +120 -0
- package/docs/widget/GridView.md +140 -0
- package/docs/widget/Icon.md +117 -0
- package/docs/widget/Image.md +125 -0
- package/docs/widget/Input.md +151 -0
- package/docs/widget/Inspector.md +92 -0
- package/docs/widget/InstallButton.md +128 -0
- package/docs/widget/ListTile.md +132 -0
- package/docs/widget/ListView.md +138 -0
- package/docs/widget/Markdown.md +175 -0
- package/docs/widget/MatrixRain.md +122 -0
- package/docs/widget/Modal.md +129 -0
- package/docs/widget/Pagination.md +135 -0
- package/docs/widget/ParallaxBox.md +128 -0
- package/docs/widget/ProgressBar.md +130 -0
- package/docs/widget/QRCode.md +117 -0
- package/docs/widget/README.md +188 -0
- package/docs/widget/Radio.md +123 -0
- package/docs/widget/Rating.md +131 -0
- package/docs/widget/Row.md +131 -0
- package/docs/widget/START_HERE.md +146 -0
- package/docs/widget/Scaffold.md +152 -0
- package/docs/widget/Skeleton.md +130 -0
- package/docs/widget/Slider.md +146 -0
- package/docs/widget/SnackBar.md +102 -0
- package/docs/widget/Stack.md +128 -0
- package/docs/widget/Stepper.md +145 -0
- package/docs/widget/Switch.md +118 -0
- package/docs/widget/Tabs.md +124 -0
- package/docs/widget/Text.md +133 -0
- package/docs/widget/Tooltip.md +145 -0
- package/docs/widget/TreeView.md +176 -0
- package/docs/widget/Video.md +127 -0
- package/docs/widget/_template.md +61 -0
- package/package.json +3 -2
- package/src/animations/animate.js +180 -70
- package/src/animations/index.js +1 -0
- package/src/create-index.sh +1 -1
- package/src/index.d.ts +45 -22
- package/src/index.js +1 -0
- package/src/navigations/AdaptiveScaffold.js +11 -2
- package/src/navigations/BottomNavigation.js +10 -3
- package/src/navigations/Tabs.js +6 -2
- package/src/tools/useState.js +54 -63
- package/src/widget-factory/assignProps.js +23 -1
- package/src/widget-factory/processProps.js +10 -3
- package/src/widget-factory/reactiveValue.js +43 -18
- package/src/widget-factory/translateProps.js +777 -901
- package/src/widgets/AlertDialog.js +7 -5
- package/src/widgets/Checkbox.js +9 -7
- package/src/widgets/CircularBar.js +12 -1
- package/src/widgets/FloatingActionButton.js +4 -7
- package/src/widgets/Image.js +5 -7
- package/src/widgets/Input.js +92 -0
- package/src/widgets/ListView.js +73 -29
- package/src/widgets/Pagination.js +6 -3
- package/src/widgets/ProgressBar.js +16 -13
- package/src/widgets/Radio.js +6 -7
- package/src/widgets/Slider.js +23 -4
- package/src/widgets/Stack.js +36 -3
- package/src/widgets/Stepper.js +4 -3
- package/src/widgets/Switch.js +3 -4
- package/src/widgets/TreeView.js +0 -1
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Tools: FletBox helper functions
|
|
2
|
+
|
|
3
|
+
This directory is the reference index for the helper functions that ship with FletBox — the pre-built code that lets you solve common problems **inside the framework**, without reaching for a third-party library or a search engine.
|
|
4
|
+
|
|
5
|
+
Tools are plain JavaScript functions. You import them from `flet-box` exactly like widgets:
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
import { rgba, uuid, sort, formatDate } from "flet-box";
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
> **Tools vs. Services.** *Tools* are pure helpers — they take input and give you back a value (a color string, a sorted array, a formatted date, a new id). *Services* talk to the outside world — browser storage and the network. Find services in the [services index](../services/README.md).
|
|
12
|
+
|
|
13
|
+
For a task-oriented walkthrough of the most common helpers, read the [Utilities guide](../guides/utilities.md). This index lists everything the framework actually exports today, grouped by category, and **each category has its own reference page** with full signatures, returns, and examples. Every name below is present in `src/index.js`; the implementation lives in `src/tools/` and `src/utils/` (plus `src/animations/` for the animation helpers).
|
|
14
|
+
|
|
15
|
+
## The tourist's map
|
|
16
|
+
|
|
17
|
+
Each category is the next chapter of the reading path:
|
|
18
|
+
|
|
19
|
+
| Page | What you find there |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| [Styling helpers](styling.md) | CSS-in-JS strings: `border`, `margin`, `gradient`, `color`, `transform`, … |
|
|
22
|
+
| [Lists, arrays & data](lists.md) | `mapList`, `repeat`, `sort`, `unique`, `createList`, `dict`, `random` |
|
|
23
|
+
| [Text & time](text-and-time.md) | `capitalize`, `truncate`, `formatDate`, media-time helpers |
|
|
24
|
+
| [State, memo & refs](state.md) | `useState`, `memo`, `ref` |
|
|
25
|
+
| [Reactivity in FletBox](reactivity.md) | Every way a widget updates: state, setters, theme, router, and more |
|
|
26
|
+
| [Async, ids & logging](async-ids-log.md) | `delay`, `retry`, `uuid`, `shortId`, `print` |
|
|
27
|
+
| [Device & environment](device.md) | `device`, `os`, `dimensions`, `clipboard` |
|
|
28
|
+
| [Themes](theme.md) | `colors`, `setTheme`, `ThemeProvider`, `useTheme` |
|
|
29
|
+
| [Markdown, code & HTML](markdown.md) | markdown → HTML/widgets, syntax highlighting, `escapeHtml` |
|
|
30
|
+
| [Visual effects](visual-effects.md) | `applyShimmer`, `applyGlow`, `applyPulse`, `injectKeyframes` |
|
|
31
|
+
| [Widget introspection](introspection.md) | `getWidgetProps`, `getWidgetProp`, `stringifyWidgetProps` |
|
|
32
|
+
| [Input validation](validation.md) | `TextInputValidator` rules |
|
|
33
|
+
| [Animation helpers](animation.md) | `animate`, `fadeIn`, `pulse` + `AnimatedBox` and friends |
|
|
34
|
+
|
|
35
|
+
## Styling helpers (CSS in JS)
|
|
36
|
+
|
|
37
|
+
Small functions that return CSS values, so you can style widgets without writing raw CSS strings.
|
|
38
|
+
|
|
39
|
+
- `animation(value)` — build a CSS `animation` shorthand string.
|
|
40
|
+
- `border(width, style, color)` — build a `border` shorthand string.
|
|
41
|
+
- `gradient(type, colors, angle)` — build linear, radial, and conic gradients.
|
|
42
|
+
- `margin(...)` / `padding(...)` — turn numbers or CSS shorthand into a spacing value.
|
|
43
|
+
- `rgba(r, g, b, a)` — build an `rgba()` string from parts.
|
|
44
|
+
- `shadow(value)` — build a `box-shadow` string.
|
|
45
|
+
- `transform(value)` / `transition(value)` — build CSS `transform`/`transition` strings.
|
|
46
|
+
- `filter(value)` — build a CSS `filter` string.
|
|
47
|
+
- `flex(...)` — build a `flex` shorthand string.
|
|
48
|
+
- `grid(...)` — build grid template and area strings.
|
|
49
|
+
- `color(value)` — create a chainable Color object: `hex()`, `rgb()`, `rgba()`, `lighten(n)`, `darken(n)`, `alpha(a)`, `complement()`, plus `isDark()`/`isLight()` and `contrast()`.
|
|
50
|
+
- `stackPosition(...)` — apply absolute stacking position + centering.
|
|
51
|
+
- `toREM(value)`, `toPX(value)`, `setBaseFontSize(px)`, `getBaseFontSize()` — unit conversion helpers.
|
|
52
|
+
|
|
53
|
+
## Lists, arrays & data
|
|
54
|
+
|
|
55
|
+
Helpers for shaping collections and generating data.
|
|
56
|
+
|
|
57
|
+
- `mapList(list, fn)` — map a list; `repeat(n, fn)` and `range(a, b)` generate lists.
|
|
58
|
+
- `shuffle(list)`, `reverse(list)`, `sort(list)`, `unique(list)`, `chunk(list, size)` — collection transforms.
|
|
59
|
+
- `dict(...)` / `emptyDict()` — a Python-like dictionary with chainable methods.
|
|
60
|
+
- `fromJSON(text)` / `fromEntries(entries)` — object building helpers.
|
|
61
|
+
- `createList(schema, count)` — generate mock/structured data.
|
|
62
|
+
- `random(...)` — random numbers, ids, colors, and fake data.
|
|
63
|
+
|
|
64
|
+
## Text & time
|
|
65
|
+
|
|
66
|
+
- `capitalize(text)`, `capitalizeWords(text)`, `lowerCase(text)`, `upperCase(text)`, `reverseString(text)`, `truncate(text, len)` — text transforms.
|
|
67
|
+
- `formatDate(date)`, `relativeTime(date)`, `now()` — date helpers; `sleep(ms)` — async delay.
|
|
68
|
+
- Media-time helpers: `formatMediaTime(sec)`, `formatMediaTimeLong(sec)`, `getProgressPercent(current, total)`, `percentToSeconds(pct, total)`, `formatMediaProgress(current, total)`.
|
|
69
|
+
|
|
70
|
+
## State, memo & refs
|
|
71
|
+
|
|
72
|
+
- `useState(key, initial, widget?, propName?)` — reactive state that re-renders widgets.
|
|
73
|
+
- `useWatchState(key, callback)` — subscribe to state changes; returns an unsubscribe function.
|
|
74
|
+
- `memo(fn)`, `memoWithKey(fn, keyFn)`, `clearMemo(memoizedFn)` — cache expensive results.
|
|
75
|
+
- `ref(...)` — hold a live reference to a widget and update it.
|
|
76
|
+
- See [Reactivity in FletBox](reactivity.md) for the full catalogue of update mechanisms.
|
|
77
|
+
|
|
78
|
+
## Async, ids & logging
|
|
79
|
+
|
|
80
|
+
- `delay(ms)` — async sleep; `withMinDelay(promise, ms)` — enforce a minimum delay; `retry(call, retries, delayMs)` — retry an async call.
|
|
81
|
+
- `uuid()`, `shortId()`, `numericId()`, `timestampId()` — id generators.
|
|
82
|
+
- `print(...)` — a small logging helper.
|
|
83
|
+
|
|
84
|
+
## Device & environment
|
|
85
|
+
|
|
86
|
+
- `device` — detect device type, touch, and orientation.
|
|
87
|
+
- `os` — detect the operating system and browser.
|
|
88
|
+
- `dimensions` — live viewport dimensions; `width` and `height` — viewport helpers.
|
|
89
|
+
- `clipboard` — copy and read text from the clipboard.
|
|
90
|
+
- `addNavigation(...)` — navigation helper.
|
|
91
|
+
|
|
92
|
+
## Themes
|
|
93
|
+
|
|
94
|
+
- `colors` and `palettes` — theme color tokens.
|
|
95
|
+
- `setTheme(theme)`, `getTheme()`, `toggleTheme()`, `subscribeTheme(callback)`, `applySystemTheme()`, `watchSystemTheme()`, `getColor(name)` — theme control.
|
|
96
|
+
|
|
97
|
+
## Markdown, code & HTML helpers
|
|
98
|
+
|
|
99
|
+
- `parseMarkdown(md)`, `parseInlineMarkdown(md)` — reserved placeholders that pass text through unchanged (real rendering is `markdownToWidgets`/`parseInlineToWidgets`).
|
|
100
|
+
- `markdownToWidgets(md)` / `parseMarkdownToWidgets(md)` — markdown string → widgets (alias names for the same function).
|
|
101
|
+
- `tokenize(code)`, `generateHighlightedHtml(code)`, `highlightColors` — syntax highlighting.
|
|
102
|
+
- `escapeHtml(text)` — escape untrusted text before inserting it as HTML.
|
|
103
|
+
|
|
104
|
+
## Visual effects
|
|
105
|
+
|
|
106
|
+
- `applyStripes(el)`, `removeStripes(el)`, `applyShimmer(el)`, `applyGlow(el)` — static visual effects.
|
|
107
|
+
- `applyIndeterminate(el)`, `applyPulse(el)`, `injectKeyframes(name, keyframes)` — animation-driven effects.
|
|
108
|
+
|
|
109
|
+
## Widget introspection
|
|
110
|
+
|
|
111
|
+
- `getWidgetProps(widget)`, `getWidgetProp(widget, name)`, `stringifyWidgetProps(widget)` — inspect a widget's props.
|
|
112
|
+
|
|
113
|
+
## Input validation
|
|
114
|
+
|
|
115
|
+
- `TextInputValidator` — rules (`isEmail`, `onlyNumbers`, `onlyLetters`, `limitLength`, `sanitize`, `filter`, …) used by the `Input` widget's `validation` prop.
|
|
116
|
+
|
|
117
|
+
## Animation helpers
|
|
118
|
+
|
|
119
|
+
- `animate(el, ...)`, `animateAsync(el, ...)`, `fadeIn(el, ...)`, `fadeOut(el, ...)`, `pulse(el, ...)` and their `*Async` variants.
|
|
120
|
+
- Widgets: `AnimatedBox`, `AnimatedText`, `MatrixRain`, `ParallaxBox`.
|
|
121
|
+
|
|
122
|
+
## Where to go next
|
|
123
|
+
|
|
124
|
+
- [Utilities guide](../guides/utilities.md) — the practical walkthrough for the most-used helpers.
|
|
125
|
+
- [Frontend services guide](../guides/frontend-services.md) — storage and network services.
|
|
126
|
+
- [Guides index](../guides/README.md) — every FletBox guide in reading order.
|
|
127
|
+
- [Services index](../services/README.md) — the storage and HTTP service reference.
|
|
128
|
+
- [Widgets](../widget/README.md) — the widget book.
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Continue reading
|
|
133
|
+
|
|
134
|
+
- **Previous:** [HTTP client](../services/http.md)
|
|
135
|
+
- **Next:** [Styling helpers](styling.md)
|
|
136
|
+
- **Index:** [Services index](../services/README.md) · [The FletBox Book](../README.md)
|
|
137
|
+
|
|
138
|
+
You are reading **Chapter 10 · Tools & Utilities** (chapter opener).
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# Animation helpers
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Two ways to animate in FletBox: `animate`/`fadeIn`/`fadeOut`/`pulse` (and their `Async` variants) run animations on existing elements, while the widgets `AnimatedBox`, `AnimatedText`, `MatrixRain`, and `ParallaxBox` give you animated UI out of the box. Every helper-driven animation is cancelable — through the function it returns or later with `stopAnimation(widget)`.
|
|
5
|
+
|
|
6
|
+
## When to use
|
|
7
|
+
- Animate an element when something happens: fade a panel in, pulse a button, animate a value change.
|
|
8
|
+
- Stop an animation the element no longer needs: `onUnmount(() => stopAnimation(el))`.
|
|
9
|
+
- Add moving backgrounds (`MatrixRain`, `ParallaxBox`).
|
|
10
|
+
|
|
11
|
+
## Import
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { animate, fadeIn, fadeOut, pulse, stopAnimation, AnimatedBox } from "flet-box";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Functions
|
|
18
|
+
|
|
19
|
+
| Function | Signature | Returns | Description |
|
|
20
|
+
| --- | --- | --- | --- |
|
|
21
|
+
| `animate` | `animate(widget, property, from, to, duration?, easing?)` | cancel `() => boolean` | Animate one CSS `property` of a widget from `from` to `to` (both numbers or colors). `duration` is ms, `easing` defaults to `"linear"`. Calling the returned function stops the loop and returns `true` if it was still running. |
|
|
22
|
+
| `animateAsync` | `animateAsync(widget, property, from, to, duration?, easing?)` | `Promise` | Same, resolves with the widget when the animation finishes — or as soon as it is cancelled or replaced, so `await` never hangs. |
|
|
23
|
+
| `stopAnimation` | `stopAnimation(widget, property?)` | `boolean` | Stop what is animating `widget` — one `property`, or every animated property when it is omitted. Use it in `onUnmount` when you did not keep the cancel function. |
|
|
24
|
+
| `fadeIn` | `fadeIn(widget, duration?)` | cancel `() => boolean` | Fade the widget in (default 300 ms). |
|
|
25
|
+
| `fadeOut` | `fadeOut(widget, duration?)` | cancel `() => boolean` | Fade it out. |
|
|
26
|
+
| `pulse` | `pulse(widget, duration?)` | cancel `() => boolean` | A quick scale pulse: up for the first half, back down for the second. |
|
|
27
|
+
| `fadeInAsync` / `fadeOutAsync` / `pulseAsync` | same, `Async` suffix | `Promise` | Promise variants that resolve on completion. |
|
|
28
|
+
|
|
29
|
+
Starting a second animation on the same widget and property **replaces** the first one; two loops writing
|
|
30
|
+
one style would otherwise fight, and the older animation would resume after the newer had finished.
|
|
31
|
+
|
|
32
|
+
## Widgets
|
|
33
|
+
|
|
34
|
+
- `AnimatedBox({ animations, timing, delay, fillMode, child, top, right, bottom, left })` — animates a child widget. `animations` is an array of `{ effect, from, to, duration }` entries; effects include `backgroundColor`, `color`, `borderRadius`, `opacity`, `scale`, `translate`, `rotate`, `width`, `height`, spacing, `boxShadow`, and gradients.
|
|
35
|
+
- `AnimatedText({ child, animations, sameTime, delayBetween, orientation })` — animates a `Text` widget (letters split per animation).
|
|
36
|
+
- `MatrixRain({ chars, fontSize, speed, fadeAmount, resetProbability, useDynamicColor, position, zIndex })` — the falling-characters canvas effect (returns a `canvas`).
|
|
37
|
+
- `ParallaxBox({ type, speed, direction, maxOffset, reverse, child })` — content that moves; `type` is `"scroll"`, `"mouse"`, or `"hover"`, `direction` is `"vertical"`, `"horizontal"`, or `"both"`.
|
|
38
|
+
|
|
39
|
+
## Examples
|
|
40
|
+
|
|
41
|
+
### Everyday example
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
import { Button, Container, Text, fadeOut } from "flet-box";
|
|
45
|
+
|
|
46
|
+
const panel = Container({ padding: 24, child: Text({ text: "Hi" }) });
|
|
47
|
+
|
|
48
|
+
Button({
|
|
49
|
+
text: "Hide",
|
|
50
|
+
onPress: () => fadeOut(panel, 300),
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Full example
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
import { AnimatedBox, Text } from "flet-box";
|
|
58
|
+
|
|
59
|
+
AnimatedBox({
|
|
60
|
+
animations: [
|
|
61
|
+
{ effect: "scale", from: 0.5, to: 1, duration: 600 },
|
|
62
|
+
{ effect: "opacity", from: 0, to: 1, duration: 400 },
|
|
63
|
+
],
|
|
64
|
+
child: Text({ text: "Welcome!" }),
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Notes
|
|
69
|
+
|
|
70
|
+
- Animation timing follows CSS `animation-duration` speeds (seconds/ms values depending on context — pass a number for ms in the widget props).
|
|
71
|
+
- The `Async` variants are handy for sequences: `await fadeInAsync(el); await fadeOutAsync(el);`
|
|
72
|
+
- For CSS-string animations use the `animation()` styling helper from [Styling helpers](styling.md).
|
|
73
|
+
|
|
74
|
+
## Related pages
|
|
75
|
+
- [Input validation](validation.md) — previous chapter.
|
|
76
|
+
- [Utilities guide](../guides/utilities.md) — next stop: the whole toolkit walkthrough.
|
|
77
|
+
- [Styling helpers](styling.md) — the `animation()` CSS builder.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Continue reading
|
|
82
|
+
|
|
83
|
+
- **Previous:** [Input validation](validation.md)
|
|
84
|
+
- **Next:** [Utilities guide](../guides/utilities.md) — Chapter 10 wrap-up
|
|
85
|
+
- **Index:** [Tools index](README.md) · [The FletBox Book](../README.md)
|
|
86
|
+
|
|
87
|
+
You are reading **Chapter 10 · Tools & Utilities** (12 of 12).
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Async, ids & logging
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Handy one-liners for async work, unique identifiers, and pleasant logging.
|
|
5
|
+
|
|
6
|
+
## When to use
|
|
7
|
+
- Pause code with `delay`/`sleep`, enforce a minimum loading time, or retry a flaky network call.
|
|
8
|
+
- Generate unique keys for lists and data (`uuid`, `shortId`, `numericId`, `timestampId`).
|
|
9
|
+
- Replace `console.log` with a colored, labeled `print`.
|
|
10
|
+
|
|
11
|
+
## Import
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { delay, withMinDelay, retry, uuid, shortId, print } from "flet-box";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Functions
|
|
18
|
+
|
|
19
|
+
| Function | Signature | Returns | Description |
|
|
20
|
+
| --- | --- | --- | --- |
|
|
21
|
+
| `delay` | `delay(ms)` | `Promise<void>` | Resolve after `ms` milliseconds. |
|
|
22
|
+
| `sleep` | `sleep(ms)` | `Promise<void>` | Alias-style async sleep. |
|
|
23
|
+
| `withMinDelay` | `withMinDelay(promise, minMs?)` | `Promise<T>` | Resolve only after both the promise and `minMs` complete. |
|
|
24
|
+
| `retry` | `retry(fn, retries?, delayMs?)` | `Promise<T>` | Call `fn` again on rejection, up to `retries` times, waiting `delayMs` between attempts. |
|
|
25
|
+
| `uuid` | `uuid()` | `string` | A version-4 style UUID. |
|
|
26
|
+
| `shortId` | `shortId()` | `string` | A short unique id. |
|
|
27
|
+
| `numericId` | `numericId(length?)` | `string` | A numeric id of `length` digits. |
|
|
28
|
+
| `timestampId` | `timestampId()` | `string` | A time-based unique id. |
|
|
29
|
+
| `print` | `print(message, type?, options?)` | `void` | Log with styling and [types](#the-print-signature). |
|
|
30
|
+
|
|
31
|
+
### The `print` signature
|
|
32
|
+
|
|
33
|
+
`print(message, type = "log", options = {})`
|
|
34
|
+
|
|
35
|
+
- `type`: `"log" | "warn" | "error" | "info" | "success" | "debug"`.
|
|
36
|
+
- `options`: `{ timestamp?: boolean, label?: string, prefix?: boolean }`.
|
|
37
|
+
- Objects are pretty-printed as JSON.
|
|
38
|
+
|
|
39
|
+
## Examples
|
|
40
|
+
|
|
41
|
+
### Everyday example
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
import { delay } from "flet-box";
|
|
45
|
+
|
|
46
|
+
await delay(500); // wait half a second
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Full example
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
import { withMinDelay, retry, uuid, print } from "flet-box";
|
|
53
|
+
|
|
54
|
+
const data = await withMinDelay(fetchData(), 600);
|
|
55
|
+
const saved = await retry(saveData, 3, 300);
|
|
56
|
+
|
|
57
|
+
print("User created", "success", { timestamp: true });
|
|
58
|
+
console.log(uuid()); // e.g. "f47ac10b..."
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Notes
|
|
62
|
+
|
|
63
|
+
- `retry` re-runs the **whole** promise factory on each failure, so it is safe to retry lazy calls like `() => fetch(...)`.
|
|
64
|
+
- `withMinDelay` is perfect for loading spinners: the UI stays visible for at least `minMs`.
|
|
65
|
+
- `print` uses the correct console method per type and is safe to remove later without touching logic.
|
|
66
|
+
|
|
67
|
+
## Related pages
|
|
68
|
+
- [State, memo & refs](state.md) — previous chapter.
|
|
69
|
+
- [Device & environment](device.md) — next chapter.
|
|
70
|
+
- [HTTP client](../services/http.md) — network calls you may want to retry.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Continue reading
|
|
75
|
+
|
|
76
|
+
- **Previous:** [State, memo & refs](state.md)
|
|
77
|
+
- **Next:** [Device & environment](device.md)
|
|
78
|
+
- **Index:** [Tools index](README.md) · [The FletBox Book](../README.md)
|
|
79
|
+
|
|
80
|
+
You are reading **Chapter 10 · Tools & Utilities** (5 of 12).
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Device & environment
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Detect what your app is running on: screen, orientation, operating system, and clipboard access. These helpers read the browser and stay in sync when the window changes.
|
|
5
|
+
|
|
6
|
+
## When to use
|
|
7
|
+
- Layout that changes between phone and desktop (`device`, `dimensions`).
|
|
8
|
+
- Show platform-specific UI (`os`).
|
|
9
|
+
- Copy and paste text with one call (`clipboard`).
|
|
10
|
+
- Walk the widget tree (`addNavigation`).
|
|
11
|
+
|
|
12
|
+
## Import
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
import { device, os, dimensions, width, height, clipboard } from "flet-box";
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Objects
|
|
19
|
+
|
|
20
|
+
### `device`
|
|
21
|
+
|
|
22
|
+
| Method | Returns | Description |
|
|
23
|
+
| --- | --- | --- |
|
|
24
|
+
| `isMobile()` | `boolean` | Is it a phone? |
|
|
25
|
+
| `isTablet()` | `boolean` | Is it a tablet? |
|
|
26
|
+
| `isDesktop()` | `boolean` | Is it a desktop/laptop? |
|
|
27
|
+
| `orientation()` | `'portrait' \| 'landscape'` | Current orientation. |
|
|
28
|
+
| `isTouch()` | `boolean` | Does the device support touch? |
|
|
29
|
+
| `hasGesture()` | `boolean` | Gesture events available? |
|
|
30
|
+
| `onOrientationChange(cb)` | `() => void` | Run `cb(orientation)` on change; returns an unsubscribe function. |
|
|
31
|
+
| `onResize(cb)` | `() => void` | Run `cb({ width, height, orientation })` on resize; returns an unsubscribe function. |
|
|
32
|
+
|
|
33
|
+
### `dimensions` (plus `width` / `height`)
|
|
34
|
+
|
|
35
|
+
| Member | Returns | Description |
|
|
36
|
+
| --- | --- | --- |
|
|
37
|
+
| `width` / `height` | `number` | Live viewport dimensions. |
|
|
38
|
+
| `get()` | `{ width, height }` | Both at once. |
|
|
39
|
+
| `getWidth()` / `getHeight()` | `number` | Each dimension. |
|
|
40
|
+
| `addListener(cb)` | `() => void` | Subscribe to size changes; returns an unsubscribe function. |
|
|
41
|
+
| `removeListener(cb)` | `void` | Remove a subscription. |
|
|
42
|
+
|
|
43
|
+
`width` and `height` are also exported directly as numbers.
|
|
44
|
+
|
|
45
|
+
### `os`
|
|
46
|
+
|
|
47
|
+
| Method | Returns | Description |
|
|
48
|
+
| --- | --- | --- |
|
|
49
|
+
| `name()` | `string` | Operating system: `"Windows"`, `"macOS"`, `"Android"`, `"iOS"`, `"Linux"`, `"ChromeOS"`, or `"Unknown"`. |
|
|
50
|
+
| `version()` | `string` | OS version (or `"Unknown"`). |
|
|
51
|
+
| `isMobile()` / `isDesktop()` | `boolean` | Platform family helpers. |
|
|
52
|
+
|
|
53
|
+
### `clipboard`
|
|
54
|
+
|
|
55
|
+
| Method | Returns | Description |
|
|
56
|
+
| --- | --- | --- |
|
|
57
|
+
| `copy(text)` | `void` | Copy text to the clipboard. |
|
|
58
|
+
| `read()` | `Promise<string>` | Read the current clipboard text. |
|
|
59
|
+
| `copyWithFeedback(text, element)` | `void` | Copy and briefly show feedback near an element. |
|
|
60
|
+
|
|
61
|
+
### `addNavigation(widget)` — walk the widget tree
|
|
62
|
+
|
|
63
|
+
`addNavigation(widget)` attaches tree-walking members to a widget and returns it:
|
|
64
|
+
|
|
65
|
+
| Member | Type | Description |
|
|
66
|
+
| --- | --- | --- |
|
|
67
|
+
| `parent` | `Widget \| null` | The parent widget (when mounted). |
|
|
68
|
+
| `children` | `Widget[]` | Direct children. |
|
|
69
|
+
| `firstChild` / `lastChild` | `Widget \| null` | First/last direct child. |
|
|
70
|
+
| `siblings` | `Widget[]` | Siblings (excluding itself). |
|
|
71
|
+
| `nextSibling` / `prevSibling` | `Widget \| null` | Adjacent siblings. |
|
|
72
|
+
| `index` | `number` | Position among siblings (`-1` when root). |
|
|
73
|
+
| `root` | `Widget` | The root of the tree. |
|
|
74
|
+
| `path` | `object[]` | `{ name, id, index }` for each ancestor up to the root. |
|
|
75
|
+
| `depth` | `number` | Distance from the root. |
|
|
76
|
+
| `findById(id)` | `Widget \| null` | Search the subtree for a widget `id`. |
|
|
77
|
+
| `findAll(widgetName)` | `Widget[]` | Collect all widgets with a `_widgetName`. |
|
|
78
|
+
| `tree` | `string` | An ASCII tree dump for debugging. |
|
|
79
|
+
|
|
80
|
+
## Examples
|
|
81
|
+
|
|
82
|
+
### Everyday example
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
import { Text, device } from "flet-box";
|
|
86
|
+
|
|
87
|
+
Text({
|
|
88
|
+
text: device.isMobile() ? "Mobile layout" : "Desktop layout",
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Full example
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
import { Column, Button, clipboard, dimensions } from "flet-box";
|
|
96
|
+
|
|
97
|
+
Column({
|
|
98
|
+
gap: 8,
|
|
99
|
+
children: [
|
|
100
|
+
Button({
|
|
101
|
+
text: "Copy invite link",
|
|
102
|
+
onPress: () => clipboard.copy("https://myapp.app/invite"),
|
|
103
|
+
}),
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
dimensions.addListener(({ width, height }) => {
|
|
108
|
+
console.log(`viewport ${width}x${height}`);
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Notes
|
|
113
|
+
|
|
114
|
+
- `dimensions` subscribes to window resize internally; `addListener` returns an **unsubscribe function** — call it when the feature is destroyed to avoid leaks. Same for `device.onResize`/`onOrientationChange`.
|
|
115
|
+
- `clipboard.read()` requires a user gesture in most browsers (it uses the same rules as `navigator.clipboard`).
|
|
116
|
+
- `addNavigation` is idempotent — calling it twice on a widget is safe, and widgets already expose these members when they are built by the factory.
|
|
117
|
+
- `os` also detects mobile/desktop families, which `device` derives for touch+screen decisions.
|
|
118
|
+
|
|
119
|
+
## Related pages
|
|
120
|
+
- [Async, ids & logging](async-ids-log.md) — previous chapter.
|
|
121
|
+
- [Themes](theme.md) — next chapter.
|
|
122
|
+
- [AdaptiveScaffold](../widget/AdaptiveScaffold.md) — a shell that adapts to the device.
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Continue reading
|
|
127
|
+
|
|
128
|
+
- **Previous:** [Async, ids & logging](async-ids-log.md)
|
|
129
|
+
- **Next:** [Themes](theme.md)
|
|
130
|
+
- **Index:** [Tools index](README.md) · [The FletBox Book](../README.md)
|
|
131
|
+
|
|
132
|
+
You are reading **Chapter 10 · Tools & Utilities** (6 of 12).
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Widget introspection
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Inspect any widget instance: read its received props, resolve one prop, or get a string dump. This is the engine behind the [Inspector](../widget/Inspector.md) widget.
|
|
5
|
+
|
|
6
|
+
## When to use
|
|
7
|
+
- Debug what props a widget actually received at runtime.
|
|
8
|
+
- Build dev tools that show widget state.
|
|
9
|
+
- Export or serialize widget configs.
|
|
10
|
+
|
|
11
|
+
## Import
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { getWidgetProps, getWidgetProp, stringifyWidgetProps } from "flet-box";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Functions
|
|
18
|
+
|
|
19
|
+
| Function | Signature | Returns | Description |
|
|
20
|
+
| --- | --- | --- | --- |
|
|
21
|
+
| `getWidgetProps` | `getWidgetProps(widget)` | `Record<string, any>` | The props the widget was built with. |
|
|
22
|
+
| `getWidgetProp` | `getWidgetProp(widget, propName)` | `any` | One prop value. |
|
|
23
|
+
| `stringifyWidgetProps` | `stringifyWidgetProps(widget)` | `string` | Props as a readable string. |
|
|
24
|
+
|
|
25
|
+
## Examples
|
|
26
|
+
|
|
27
|
+
### Everyday example
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
import { getWidgetProps } from "flet-box";
|
|
31
|
+
|
|
32
|
+
const button = Button({ text: "Save", onPress: save });
|
|
33
|
+
console.log(getWidgetProps(button));
|
|
34
|
+
// { text: "Save", onPress: save }
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Full example
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
import { Text, getWidgetProp, stringifyWidgetProps } from "flet-box";
|
|
41
|
+
|
|
42
|
+
const label = Text({ text: "Hello", size: 24 });
|
|
43
|
+
|
|
44
|
+
getWidgetProp(label, "size"); // 24
|
|
45
|
+
console.log(stringifyWidgetProps(label));
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Notes
|
|
49
|
+
|
|
50
|
+
- `getWidgetProps` returns what was passed at build time (before defaults are expanded).
|
|
51
|
+
- These helpers are used by `printWidgetCode` and `inspectWidget` to reconstruct source-like descriptions of widgets.
|
|
52
|
+
- Widgets expose an `update(props)` method and prop accessors; introspection is read-only and never mutates the widget.
|
|
53
|
+
|
|
54
|
+
## Related pages
|
|
55
|
+
- [Visual effects](visual-effects.md) — previous chapter.
|
|
56
|
+
- [Input validation](validation.md) — next chapter.
|
|
57
|
+
- [Inspector widget](../widget/Inspector.md).
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Continue reading
|
|
62
|
+
|
|
63
|
+
- **Previous:** [Visual effects](visual-effects.md)
|
|
64
|
+
- **Next:** [Input validation](validation.md)
|
|
65
|
+
- **Index:** [Tools index](README.md) · [The FletBox Book](../README.md)
|
|
66
|
+
|
|
67
|
+
You are reading **Chapter 10 · Tools & Utilities** (10 of 12).
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Lists, arrays & data
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Helpers for shaping collections and generating data: iterate lists into widgets, pick random values, create mock data, and treat dictionaries with a Python-like API.
|
|
5
|
+
|
|
6
|
+
## When to use
|
|
7
|
+
- Turn an array into widget rows (`mapList`, grid/carousel/lists heavily use these).
|
|
8
|
+
- Sort, filter out duplicates, chunk, or shuffle arrays.
|
|
9
|
+
- Build fake data for demos and prototypes (`createList`, `random`).
|
|
10
|
+
|
|
11
|
+
## Import
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { mapList, repeat, range, sort, unique, dict, createList } from "flet-box";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Functions
|
|
18
|
+
|
|
19
|
+
| Function | Signature | Returns | Description |
|
|
20
|
+
| --- | --- | --- | --- |
|
|
21
|
+
| `mapList` | `mapList(list, fn)` | `Widget[]` | Map a list (or a number, or a record) into widgets. `fn` receives `(item, index, key?)`. |
|
|
22
|
+
| `repeat` | `repeat(count, widget \| fn)` | `Widget[]` | Repeat a widget, or build one per index via `fn(index)`. |
|
|
23
|
+
| `range` | `range(start, end?, step?)` | `number[]` | A numeric sequence. |
|
|
24
|
+
| `shuffle` | `shuffle(arr)` | `T[]` | A new array with shuffled order. |
|
|
25
|
+
| `reverse` | `reverse(arr)` | `T[]` | A new array, reversed. |
|
|
26
|
+
| `sort` | `sort(arr, by?, order?)` | `T[]` | Sort by a string key or a comparator; `order` is `'asc'`/`'desc'`. |
|
|
27
|
+
| `unique` | `unique(arr, key?)` | `T[]` | Remove duplicates; optionally by a `key` field. |
|
|
28
|
+
| `chunk` | `chunk(arr, size)` | `T[][]` | Split into arrays of `size`. |
|
|
29
|
+
| `createList` | `createList(schema, count?)` | `T[]` | Generate mock data from a schema. |
|
|
30
|
+
| `createList.fromData` | `(data, mapper)` | `R[]` | Map real data through a schema mapper. |
|
|
31
|
+
| `createList.range` | `(start, end, mapper?)` | `any[]` | Range with optional mapper. |
|
|
32
|
+
| `createList.repeat` | `(value, count)` | `T[]` | Repeat a value. |
|
|
33
|
+
| `createList.paginate` | `(list, page?, pageSize?)` | object | `{ data, page, pageSize, total, totalPages, hasNext, hasPrev, startIndex, endIndex }`. |
|
|
34
|
+
| `createList.search` | `(list, query, fields?)` | `T[]` | Filter by a string (or predicate) across fields. |
|
|
35
|
+
| `createList.sort` | `(list, field, order?)` | `T[]` | Sort by a field. |
|
|
36
|
+
| `createList.groupBy` | `(list, field)` | `Record<string, T[]>` | Group by a field. |
|
|
37
|
+
| `dict` | `dict(obj?)` | any | A Python-like dictionary with chainable methods. |
|
|
38
|
+
| `emptyDict` | `emptyDict()` | any | A new empty dictionary. |
|
|
39
|
+
| `fromJSON` | `fromJSON(jsonStr)` | any | Parse JSON into a dict/value. |
|
|
40
|
+
| `fromEntries` | `fromEntries(entries)` | any | Build from `[key, value]` pairs. |
|
|
41
|
+
| `random` | see below | — | Random numbers, ids, colors, and fake data. |
|
|
42
|
+
|
|
43
|
+
### The `random` object
|
|
44
|
+
|
|
45
|
+
`random.number(min?, max?)`, `random.string(length?)`, `random.firstName()`, `random.lastName()`, `random.fullName()`, `random.email(name?)`, `random.age()`, `random.boolean()`, `random.choice(arr)`, `random.id()`, `random.hexColor()`, `random.rgbColor()`, `random.date(start?, end?)`, `random.dateString(start?, end?)`, `random.time()`, `random.timeAmPm()`, `random.datetime()`, `random.timestamp()`, `random.dayOfWeek()`, `random.month()`.
|
|
46
|
+
|
|
47
|
+
## Examples
|
|
48
|
+
|
|
49
|
+
### Everyday example
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
import { Column, Text, mapList, repeat } from "flet-box";
|
|
53
|
+
|
|
54
|
+
Column({
|
|
55
|
+
children: repeat(3, Text({ text: "hello" })),
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Full example
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
import { Column, Text, createList, sort } from "flet-box";
|
|
63
|
+
|
|
64
|
+
const people = createList(
|
|
65
|
+
{ name: "firstName", age: "age" },
|
|
66
|
+
5,
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
Column({
|
|
70
|
+
children: sort(people, "age", "asc").map((p) =>
|
|
71
|
+
Text({ text: `${p.name} (${p.age})` }),
|
|
72
|
+
),
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Notes
|
|
77
|
+
|
|
78
|
+
- `mapList` accepts a **number** (renders that many items), an **array**, or a **record** of items — so it works with `for`-style and object-style data alike.
|
|
79
|
+
- The collection helpers (`sort`, `shuffle`, `unique`, `chunk`, `reverse`) return **new arrays**: your original array is never mutated.
|
|
80
|
+
- `createList` takes a schema of fields: a string is a generator name (`"firstName"`, `"age"`, `"email"`) and a function is called to produce the value.
|
|
81
|
+
- `dict` is an object with Python-dict methods (chainable); `fromEntries` builds it from pairs.
|
|
82
|
+
|
|
83
|
+
## Related pages
|
|
84
|
+
- [Text & time](text-and-time.md) — next chapter.
|
|
85
|
+
- [Styling helpers](styling.md) — previous chapter.
|
|
86
|
+
- [Widget introspection](introspection.md) — inspect widget props.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Continue reading
|
|
91
|
+
|
|
92
|
+
- **Previous:** [Styling helpers](styling.md)
|
|
93
|
+
- **Next:** [Text & time](text-and-time.md)
|
|
94
|
+
- **Index:** [Tools index](README.md) · [The FletBox Book](../README.md)
|
|
95
|
+
|
|
96
|
+
You are reading **Chapter 10 · Tools & Utilities** (2 of 12).
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Markdown, code & HTML
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Render markdown as HTML or as real FletBox widgets, highlight code, and escape untrusted text. These helpers power the [Markdown](../widget/Markdown.md) and [CodeViewer](../widget/CodeViewer.md) widgets.
|
|
5
|
+
|
|
6
|
+
## When to use
|
|
7
|
+
- Show user-written or file-provided markdown in your app.
|
|
8
|
+
- Render highlighted code snippets.
|
|
9
|
+
- Sanitize text before inserting it into the DOM (`escapeHtml`).
|
|
10
|
+
|
|
11
|
+
## Import
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { parseMarkdown, markdownToWidgets, escapeHtml } from "flet-box";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Functions
|
|
18
|
+
|
|
19
|
+
| Function | Signature | Returns | Description |
|
|
20
|
+
| --- | --- | --- | --- |
|
|
21
|
+
| `markdownToWidgets` | `markdownToWidgets(text, options?)` | `Widget[]` | Markdown → FletBox widgets. This is the real renderer. |
|
|
22
|
+
| `parseMarkdownToWidgets` | `parseMarkdownToWidgets(text, options?)` | `Widget[]` | Alias of `markdownToWidgets`. |
|
|
23
|
+
| `parseInlineToWidgets` | `parseInlineToWidgets(text)` | `Widget[]` | Inline markdown (bold, links, code) → FletBox widgets. |
|
|
24
|
+
| `parseMarkdown` | `parseMarkdown(text)` | `string` | Reserved API: currently returns the input unchanged. Use `markdownToWidgets`. |
|
|
25
|
+
| `parseInlineMarkdown` | `parseInlineMarkdown(text)` | `string` | Reserved API: currently returns the input unchanged. Use `parseInlineToWidgets`. |
|
|
26
|
+
| `tokenize` | `tokenize(code)` | `{ type, value }[]` | Split code into token objects. |
|
|
27
|
+
| `generateHighlightedHtml` | `generateHighlightedHtml(code)` | `string` | Code → highlighted HTML. |
|
|
28
|
+
| `escapeHtml` | `escapeHtml(text)` | `string` | Escape `<`, `>`, `&`, quotes so text is safe as HTML. |
|
|
29
|
+
| `highlightColors` | const | `Record<string, string>` | The token color map used by the highlighter. |
|
|
30
|
+
|
|
31
|
+
`markdownToWidgets` options: `{ codeMaxHeight?: number, codeFontSize?: number }`.
|
|
32
|
+
|
|
33
|
+
## Examples
|
|
34
|
+
|
|
35
|
+
### Everyday example
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
import { Column, markdownToWidgets } from "flet-box";
|
|
39
|
+
|
|
40
|
+
Column({
|
|
41
|
+
padding: 16,
|
|
42
|
+
children: markdownToWidgets("# Hello\n\n**bold** and a [link](https://example.com)"),
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Full example
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
import { Column, parseInlineToWidgets, generateHighlightedHtml, escapeHtml } from "flet-box";
|
|
50
|
+
|
|
51
|
+
const note = Column({
|
|
52
|
+
children: parseInlineToWidgets("Read the **docs** today"),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const code = generateHighlightedHtml("const x = 1;");
|
|
56
|
+
const safe = escapeHtml(userInput); // safe to put into innerHTML
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Notes
|
|
60
|
+
|
|
61
|
+
- `markdownToWidgets` builds real widget elements (headings, paragraphs, code blocks, lists) that you can place inside any container — this is what the `Markdown` widget uses.
|
|
62
|
+
- `parseMarkdown` and `parseInlineMarkdown` are reserved placeholders that currently pass text through unchanged; the widget rendering is `markdownToWidgets`/`parseInlineToWidgets`.
|
|
63
|
+
- `escapeHtml` should be used for **any** text you insert into the DOM that you did not write yourself. Never trust raw input.
|
|
64
|
+
- `tokenize` returns flat token objects and is the base for custom highlighters; `highlightColors` lets you theme them.
|
|
65
|
+
|
|
66
|
+
## Related pages
|
|
67
|
+
- [Themes](theme.md) — previous chapter.
|
|
68
|
+
- [Visual effects](visual-effects.md) — next chapter.
|
|
69
|
+
- [Markdown widget](../widget/Markdown.md) and [CodeViewer widget](../widget/CodeViewer.md).
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Continue reading
|
|
74
|
+
|
|
75
|
+
- **Previous:** [Themes](theme.md)
|
|
76
|
+
- **Next:** [Visual effects](visual-effects.md)
|
|
77
|
+
- **Index:** [Tools index](README.md) · [The FletBox Book](../README.md)
|
|
78
|
+
|
|
79
|
+
You are reading **Chapter 10 · Tools & Utilities** (8 of 12).
|