com um clique
jaspr-fundamentals
// Use when working in a Jaspr project, on Jaspr components, or other Jaspr-related tasks. Contains fundamentals of writing Jaspr components and using HTML components.
// Use when working in a Jaspr project, on Jaspr components, or other Jaspr-related tasks. Contains fundamentals of writing Jaspr components and using HTML components.
Use when you need to convert / rewrite existing HTML (from a file or url) into Jaspr code.
Effectively integrate with Javascript when building Jaspr web applications. Use this skill when wrapping an existing JS library, accessing browser-native APIs, or bridging code safely across environments.
Use when working in a **static** or **server** mode Jaspr project. Covers SSR/SSG, async data fetching, the @client annotation, hydration, and handling dual entrypoints (.server.dart vs .client.dart).
Write type-safe CSS-in-Dart to style Jaspr components. Use this skill when styling components, implementing themes, or using CSS properties.
| name | jaspr-fundamentals |
| description | Use when working in a Jaspr project, on Jaspr components, or other Jaspr-related tasks. Contains fundamentals of writing Jaspr components and using HTML components. |
| metadata | {"jaspr_version":"0.23.1"} |
Jaspr uses a component-based architecture very similar to Flutter's widgets. Concepts like ui composition, architecture and state management are transferable.
Component build(BuildContext context).State class. The state has lifecycle methods like initState() and dispose(). You must override Component build(BuildContext context) in the state class.buildBuilding UIs in Jaspr requires you to return a single Component from build().
Iterable<Component> build(BuildContext context) sync*. This is legacy code..fragment([...]) (Do NOT use Fragment([...]) or fragment([...]))..text('...') (Do NOT use Text('...') or text('...'))..empty() to return an empty space safely.Example Usage:
import 'package:jaspr/jaspr.dart';
import 'package:jaspr/dom.dart';
class MyComponent extends StatelessComponent {
const MyComponent({super.key});
@override
Component build(BuildContext context) {
// 1. Return a single component (e.g. div)
// 2. Use dot-shorthand (.text) instead of Text()
return div(classes: 'my-class', [
.text('Hello World'),
]);
}
}
Jaspr provides typed components for standard HTML elements (e.g., div(), p(), a(), button()). To use these add the package:jaspr/dom.dart import.
All HTML components take standard named Key? key, String? id, String? classes, Styles? style, Map<String, String>? attributes and Map<String, void Function(Event)>? events parameters.
Most HTML components take a positional List<Component> children parameter (except for self-closing tags like img, input, br, etc.).
children list LAST, after all named parameters.href, src, onClick) over using the raw attributes: or events: maps.references/html/<tag>.md contains the full signature and example usage of the component for the given tag. (e.g. references/html/div.md for div(), references/html/button.md for button(), etc.).element(tag: '...', /* other standard params, */ children: [ /* ... */ ]) constructor instead.Example Usage:
import 'package:jaspr/jaspr.dart';
import 'package:jaspr/dom.dart';
class MyHtmlComponent extends StatelessComponent {
const MyHtmlComponent({super.key});
@override
Component build(BuildContext context) {
return div(id: 'my-container', classes: 'my-class', [
p(attributes: {'aria-label': 'Example Paragraph'}, [
.text('Hello World'),
]),
// E.g. signature as found at 'references/html/a.md'
a(href: 'https://example.com', [
.text('Click me'),
]),
]);
}
}
Jaspr has built-in support for styling components using CSS-in-Dart. See the jaspr-styling skill for more information.
package:universal_web/web.dart to access browser APIs and package:universal_web/js_interop.dart to access js interop APIs.package:universal_web in server or static mode, you MUST wrap all API calls in an if (kIsWeb) check to prevent crashing the server render.dart:js_interop and package:web directly.web.EventStreamProviders to listen to events on the window or document as they provide a typed Dart Stream of events.// Example of safe usage in server/static mode:
import 'package:universal_web/web.dart' as web;
void logSize() {
if (kIsWeb) {
print('Window size: ${web.window.innerWidth}x${web.window.innerHeight}');
// Example of global event listener
final sub = web.EventStreamProviders.resizeEvent.forTarget(web.window).listen((event) {
print('Window resized');
});
}
}
All DOM components support an events: parameter, and interactive components feature typed event callbacks (like onClick, onChange).
web.Event when typing raw events.events() helper function for type-safe callback creation when needed.import 'package:jaspr/jaspr.dart';
import 'package:jaspr/dom.dart';
import 'package:universal_web/web.dart' as web;
class MyButton extends StatelessComponent {
@override
Component build(BuildContext context) {
return div(
// Using raw events
events: {'click': (web.Event event) {
print('Div clicked');
}},
[
// Using typed parameters
button(
onClick: () => print('Button clicked'),
[.text('Click me')]
)
]
);
}
}
If you need direct reference to an underlying DOM element rendered by Jaspr, you can assign it a GlobalNodeKey.
import 'package:jaspr/jaspr.dart';
import 'package:jaspr/dom.dart';
import 'package:universal_web/web.dart' as web;
class MyInput extends StatefulComponent {
@override
State<MyInput> createState() => _MyInputState();
}
class _MyInputState extends State<MyInput> {
final GlobalNodeKey<web.HTMLInputElement> inputKey = GlobalNodeKey();
void focusInput() {
inputKey.currentNode?.focus();
}
@override
Component build(BuildContext context) {
return input(key: inputKey, type: .text);
}
}
@client annotation see the related skill: jaspr-pre-rendering-and-hydration.