You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JavaScript expression fenced code blocks are implicitly wrapped with a call to [`display`](#explicit-display). For example, this arithmetic expression displays implicitly:
205
+
JavaScript expression fenced code blocks are implicitly wrapped with a call to [`display`](#display(value)). For example, this arithmetic expression displays implicitly:
206
206
207
207
```js echo
208
208
1+2// implicit display
@@ -238,23 +238,17 @@ Implicit display also implicitly awaits promises.
238
238
239
239
## Responsive display
240
240
241
-
In Markdown, the built-in `width` reactive variable represents the current width of the main element. This variable is implemented by [`Generators.width`](./lib/generators#width(element)) and backed by a [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver). The reactive width can be a handy thing to pass, say, as the **width** option to [Observable Plot](./lib/plot).
241
+
In Markdown, the built-in [`width` reactive variable](#width) represents the current width of the main element. This variable is implemented by [`Generators.width`](./lib/generators#width(element)) and backed by a [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver). The reactive width can be a handy thing to pass, say, as the **width** option to [Observable Plot](./lib/plot).
For more control, or in a [grid](./css/grid) where you want to respond to either width or height changing, use the built-in `resize`helper. This takes a render function which is called whenever the width or height [changes](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver), and the element returned by the render function is inserted into the DOM.
247
+
For non-top-level elements, as when rendering content within an inline expression, use the built-in [`resize`function](#resize(render)) instead. This takes a _render_ function which is called whenever the width or height [changes](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver), and the element returned by the render function is inserted into the DOM.
252
248
253
249
```html echo
254
-
<divclass="grid grid-cols-4">
255
-
<divclass="card">
256
-
${resize((width) => `This card is ${width}px wide.`)}
@@ -272,3 +266,45 @@ If your container defines a height, such as `240px` in the example below, then y
272
266
```
273
267
274
268
<divclass="tip">If you are using <code>resize</code> with both <code>width</code> and <code>height</code> and see nothing rendered, it may be because your parent container does not have its own height specified. When both arguments are used, the rendered element is implicitly <code>position: absolute</code> to avoid affecting the size of its parent and causing a feedback loop.</div>
269
+
270
+
## display(*value*)
271
+
272
+
Displays the specified *value* in the current context, returning *value*. If *value* is a DOM element or node, it is inserted directly into the page. Otherwise, if the current context is a fenced code block, inspects the specified *value*; or, if the current context is an inline expression, coerces the specified *value* to a string and displays it as text.
273
+
274
+
```js echo
275
+
display(1+2);
276
+
```
277
+
278
+
See [Explicit display](#explicit-display) for more.
279
+
280
+
## resize(*render*)
281
+
282
+
Creates and returns a DIV element to contain responsive content; then, using a [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) to observe changes to the returned element’s size, calls the specified *render* function with the new width and height whenever the size changes. The element returned by the *render* function is inserted into the DIV element, replacing any previously-rendered content. This is useful for responsive charts.
283
+
284
+
```js echo
285
+
resize((width) =>`I am ${width} pixels wide.`)
286
+
```
287
+
288
+
If the *render* function returns a promise, the promise is implicitly awaited. If the resulting value is null, the DIV element is cleared; otherwise, if the resulting value is not a DOM element, it is coerced to a string and displayed as text.
289
+
290
+
See [Responsive display](#responsive-display) for more.
291
+
292
+
## width
293
+
294
+
The current width of the main element in pixels as a reactive variable. A fenced code block or inline expression that references `width` will re-run whenever the width of the main element changes, such as when the window is resized; often used for responsive charts.
295
+
296
+
```js echo
297
+
width
298
+
```
299
+
300
+
See [`Generators.width`](./lib/generators#width(element)) for implementation.
301
+
302
+
## now
303
+
304
+
The current time in milliseconds since Unix epoch as a reactive variable. A fenced code block or inline expression that references `now` will run continuously; often used for simple animations.
305
+
306
+
```js echo
307
+
now
308
+
```
309
+
310
+
See [`Generators.now`](./lib/generators#now()) for implementation.
Copy file name to clipboardExpand all lines: docs/lib/generators.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ import {Generators} from "npm:@observablehq/stdlib";
8
8
9
9
## input(*element*)
10
10
11
-
Returns an async generator that yields whenever the given *element* emits an *input* event, with the given *element*’s current value. (It’s a bit fancier than that because we special-case a few element types.) The built-in [`view` function](<../reactivity#inputs>) uses this.
11
+
[Source](https://github.com/observablehq/framework/blob/main/src/client/stdlib/generators/input.js) · Returns an async generator that yields whenever the given *element* emits an *input* event, with the given *element*’s current value. (It’s a bit fancier than that because we special-case a few element types.) The built-in [`view` function](<../reactivity#inputs>) uses this.
@@ -19,9 +19,9 @@ const name = Generators.input(nameInput);
19
19
name
20
20
```
21
21
22
-
## observe(*change*)
22
+
## observe(*initialize*)
23
23
24
-
Returns an async generator that yields whenever the callback function*change* is called, with the value passed.
24
+
[Source](https://github.com/observablehq/framework/blob/main/src/client/stdlib/generators/observe.js) · Returns an async generator that immediately invokes the specified *initialize* function, being passed a *change*callback function, and yields the passed value whenever *change* is called. The *initialize* function may optionally return a *dispose* function that will be called when the generator is terminated.
25
25
26
26
```js echo
27
27
consthash=Generators.observe((change) => {
@@ -37,7 +37,9 @@ hash
37
37
38
38
## queue(*change*)
39
39
40
-
Returns an async generator that yields whenever the callback function *change* is called, with the value passed. This is identical to Generators.observe, except that if *change* is called multiple times before the consumer has a chance to process the yielded result, values will not be dropped; use this if you require that the consumer not miss a yielded value.
40
+
[Source](https://github.com/observablehq/framework/blob/main/src/client/stdlib/generators/queue.js) · Returns an async generator that immediately invokes the specified *initialize* function, being passed a *change* callback function, and yields the passed value whenever *change* is called. The *initialize* function may optionally return a *dispose* function that will be called when the generator is terminated.
41
+
42
+
This is identical to `Generators.observe` except that if *change* is called multiple times before the consumer has a chance to process the yielded result, values will not be dropped; use this if you require that the consumer not miss a yielded value.
41
43
42
44
```js run=false
43
45
consthash=Generators.queue((change) => {
@@ -53,7 +55,7 @@ hash
53
55
54
56
## now()
55
57
56
-
Returns a generator that repeatedly yields `Date.now()`, forever. This generator is available by default as `now` in Markdown.
58
+
[Source](https://github.com/observablehq/framework/blob/main/src/client/stdlib/generators/now.js) · Returns a generator that repeatedly yields `Date.now()`, forever. This generator is available by default as `now` in Markdown.
57
59
58
60
```js run=false
59
61
constnow=Generators.now();
@@ -65,7 +67,7 @@ now
65
67
66
68
## width(*element*)
67
69
68
-
Returns an async generator that yields the width of the given target *element*. Using a [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver), the generator will yield whenever the width of the *element* changes. This generator for the `main` element is available by default as `width` in Markdown.
70
+
[Source](https://github.com/observablehq/framework/blob/main/src/client/stdlib/generators/width.js) · Returns an async generator that yields the width of the given target *element*. Using a [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver), the generator will yield whenever the width of the *element* changes. This generator for the `main` element is available by default as `width` in Markdown.
## dark() <ahref="https://github.com/observablehq/framework/releases/tag/v1.3.0"class="observablehq-version-badge"data-version="^1.3.0"title="Added in 1.3.0"></a>
79
81
80
-
Returns an async generator that yields a boolean indicating whether the page is currently displayed with a dark [color scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme).
82
+
[Source](https://github.com/observablehq/framework/blob/main/src/client/stdlib/generators/dark.js) · Returns an async generator that yields a boolean indicating whether the page is currently displayed with a dark [color scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme).
81
83
82
84
If the page supports both light and dark mode (as with the [default theme](../themes)), the value reflects the user’s [preferred color scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme). The generator will yield a new value if the preferred color changes — as when the user changes their system settings, or if the user’s system adapts automatically to the diurnal cycle — allowing you to update the display as needed without requiring a page reload.
0 commit comments