ワンクリックで
jaspr-js-interop
// 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.
// 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 you need to convert / rewrite existing HTML (from a file or url) into Jaspr code.
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 **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-js-interop |
| description | 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. |
| metadata | {"jaspr_version":"0.23.1"} |
Dart makes accessing JavaScript simple using modern js_interop. Since Jaspr applications can be compiled to JavaScript or WebAssembly, utilizing Dart's explicit js_interop mechanism is required when you need to interface with raw JS variables, functions, or libraries.
package:universal_web/js_interop.dart. It acts as a safe stub during server-side processing without breaking compilation.if (kIsWeb) { ... } block to avoid throwing exceptions on the server during pre-rendering.dart:js_interop.window.innerHeight, document, DOM nodes), use package:universal_web/web.dart (or package:web/web.dart in client mode) instead of manually creating js_interop wrappers.Top Level Functions
To bind directly to a globally available JS function or object, use @JS():
@JS()
external void alert(JSString message);
void showAlert() {
if (kIsWeb) {
// Make sure to convert Dart String types using .toJS
alert('Hello from Dart!'.toJS);
}
}
Wrapping Third-Party JS Libraries
To interact with specific Javascript Objects from external libraries, map an extension type wrapping JSObject.
// Suppose there is a JS library available via `window.Analytics`
extension type Analytics._(JSObject _) implements JSObject {
external void trackPage(JSString pageName);
external JSNumber getVisitCount();
}
@JS('Analytics')
external Analytics get analytics;
void logCurrentPage() {
if (kIsWeb) {
analytics.trackPage('Home'.toJS);
// Convert Javascript primitives back to Dart types using `.toDart`
int visits = analytics.getVisitCount().toDartInt;
print('Total visits: $visits');
}
}
Anonymous JS Objects
If you need to pass a configuration object (a plain JS object literal {}) into a JS function, or receive one from JavaScript, define an @anonymous extension type.
@JS()
@anonymous
extension type ChartConfig._(JSObject _) implements JSObject {
external factory ChartConfig({
JSString title,
JSBoolean showLegend,
JSArray<JSNumber> data,
});
external JSString get title;
external JSArray<JSNumber> get data;
}
@JS()
external void renderChart(ChartConfig config);
void setupChart() {
if (kIsWeb) {
// Creating an anonymous JS object to pass into a JS function
final config = ChartConfig(
title: 'Monthly Sales'.toJS,
showLegend: true.toJS,
data: [10.toJS, 20.toJS, 30.toJS].toJS,
);
renderChart(config);
// Reading properties from a JS object back to Dart types
String title = config.title.toDart;
List<double> rawData = config.data.toDart.map((e) => e.toDartDouble).toList();
}
}