Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New JS interop docs #4578

Merged
merged 35 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b6ddf73
outline and first drafts for some pages
MaryaBelanger Feb 6, 2023
c6c417c
add descriptions to fix liquid errors
MaryaBelanger Jan 10, 2024
2d7c606
readd sidenav after rebase, move files under interop
MaryaBelanger Jan 11, 2024
bc2347e
update web/js-interop links to interop/js-interop
MaryaBelanger Jan 11, 2024
7e629dd
remove broken anchors
MaryaBelanger Jan 11, 2024
902703b
typo in link
MaryaBelanger Jan 11, 2024
3ce781f
Add a section for JS types and reference it in the nav
srujzs Jan 13, 2024
0fe4c69
Merge branch 'main' into js-interop
MaryaBelanger Jan 16, 2024
16cfb08
Update JS types to address review comments
srujzs Jan 18, 2024
961b1ec
Merge branch 'main' into js-interop
MaryaBelanger Jan 19, 2024
4c442a1
Incorporate second round of JS types feedback
srujzs Jan 25, 2024
8c2b89f
Add docs on interop usage
srujzs Jan 26, 2024
e04e2d1
Update past JS interop section
srujzs Jan 29, 2024
992fd0f
Review feedback on JS types
srujzs Feb 2, 2024
6733b04
Change "compiler" to "program" in JS types section
srujzs Feb 5, 2024
0e7f70a
Review feedback of past JS interop
srujzs Feb 7, 2024
b811f26
Handle review feedback on usage
srujzs Feb 9, 2024
fe2ad47
Merge branch 'main' into js-interop
parlough Feb 11, 2024
1b3ca1c
Update index, overview, and mocking tutorial
srujzs Feb 12, 2024
ab0fc8d
Add back dom.md
srujzs Feb 12, 2024
95a885a
Fix links in sections
srujzs Feb 12, 2024
ee822fd
Fix links to restrictions
srujzs Feb 12, 2024
51ab02f
Added missing hyphen in link to restrictions in JS types section
srujzs Feb 12, 2024
83a613f
Cleanup wording to be clearer
srujzs Feb 12, 2024
1510117
Link to extension types sections
srujzs Feb 15, 2024
d000a22
Merge branch 'main' into js-interop
parlough Feb 15, 2024
3f52a4b
`package:web` migration page (#5524)
MaryaBelanger Feb 15, 2024
481f0e7
fix some broken links, hide why section in index
MaryaBelanger Feb 15, 2024
4402946
Merge branch 'main' into js-interop
MaryaBelanger Feb 15, 2024
01c64bc
go link, fix index api links, remove old learn directive
MaryaBelanger Feb 15, 2024
efb81a2
more minor link fixes
MaryaBelanger Feb 15, 2024
202b626
Move dart:js_interop and dart:js_interop_unsafe links to dev for late…
srujzs Feb 15, 2024
33f8d05
Small fixes to code snippets and external functions link
srujzs Feb 15, 2024
7e1eb3f
minor fixes
MaryaBelanger Feb 15, 2024
9dadda6
Merge branch 'main' into js-interop
MaryaBelanger Feb 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/_data/side-nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,18 @@
- title: Java & Kotlin interop
permalink: /interop/java-interop
- title: JavaScript interop
permalink: /interop/js-interop
children:
- title: Overview
permalink: /interop/js-interop
- title: JS types
srujzs marked this conversation as resolved.
Show resolved Hide resolved
permalink: /interop/js-interop/js-types
- title: Tutorials
permalink: /interop/js-interop/tutorials
- title: Past JS interop
permalink: /interop/js-interop/past-js-interop
- divider
- title: Web interop
permalink: /interop/js-interop/dom

- title: Tools & techniques
expanded: false
Expand Down
2 changes: 1 addition & 1 deletion src/guides/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ we made the following changes:
[source descriptor]: /tools/pub/cmd/pub-add#source-descriptor
[SDK archive]: /get-dart/archive
[glossary]: /resources/glossary
[JS static interop support]: /interop/js-interop#next-generation-js-interop-preview
[JS static interop support]: /interop/js-interop
[analyzer plugins]: /tools/analysis#plugins

### Articles added to the Dart blog
Expand Down
123 changes: 0 additions & 123 deletions src/interop/js-interop.md

This file was deleted.

38 changes: 38 additions & 0 deletions src/interop/js-interop/dom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: How to interop with DOM APIs
description: How to use package:web to communicate with the browser in Dart.
---

{{site.why.learn}}
* How to...
{{site.why.end}}

// *Introdcution paragraphs*

## Background concepts

The following sections provide some extra background and information
around the technologies and concepts used in the tutorial.
To skip directly to the tutorial content,
go to [Steps](#steps).

### Topic/concept 1

### Topic/concept..n

## Steps

### Prerequisites

Before you begin, you'll need...
*

### Actionable step 1

Step subsections should be actionalble, like "Build...", "Retrieve...", "Configure...", etc.

### Actionable step...n

## What next?

Point to other tutorials, reference, etc.
125 changes: 125 additions & 0 deletions src/interop/js-interop/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
title: JavaScript interoperability
short-title: JS interop
description: Integrate JavaScript code into your Dart web app.
---

The [Dart web platform](/overview#web-platform) supports communication with
JavaScript apps and libraries, and browser DOM APIs, using the `js_interop`
package, also known as `dart:js_interop`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
package, also known as `dart:js_interop`.
library, also known as `dart:js_interop`.

It's not a package! It's a (dart:) library!

Web developers can benefit from using external JS libraries in their Dart code, without
having to rewrite anything in Dart.

## Static interop

The principal model of JS interop in Dart is **static interop**.
Static interop means interop that requires staticly typing external members,
rather than allowing dynamic invocations.
This enables [features](#features) like better performance, type soundness, and more.

Performant static interop model is made possible by **[inline classes][]**.
Inline classes are a special class type that wrap an existing type into a new static type,
without the overhead of a traditional class, enabling zero cost wrapping.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps:

Suggested change
Performant static interop model is made possible by **[inline classes][]**.
Inline classes are a special class type that wrap an existing type into a new static type,
without the overhead of a traditional class, enabling zero cost wrapping.
Performant static interop model is made possible by **[extension types][]**.
Extension types are a special class type that wrap an existing type into a new static type,
without the overhead of a traditional class, enabling zero cost wrapping.


### Why static interop?

Static interop represents the desire to decouple Dart's "core" from the platform
where it's embedded
(*maybe definition of "embedded" or examples here, like the browser, Flutter, etc.?*).

Our [previous iterations of JS interop][] provided the capabillity
to access the embedder, but were not primed to handle that decoupling.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what "embedder" is here.

Too much of the process was entwined into the Dart side,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about ESL folks reading this. It's tricky to parse...

like writing all the bindings to the browser
(*not sure if I understood that note correctly*).
They also had limitations around using the DOM,
typing (which is a cornerstone of Dart),
and expanding to new interop types
(*this is me trying to refer to wasm without actually saying, idk if "interop types" is the right term*).

Static interop addresses all of the shortcomings of Dart's previous JS interop solutions.
Check out the [Features](#features) section for a summary of improvements.

[previous iterations of JS interop]: /interop/js-interop/past-js-interop

## Usage

The following example implements inline class-based, static, JS interop in Dart,
using its key members and syntaxes:

```dart
@JS()
library;
// library names aren't cool anymore...

import 'dart:js_interop';

inline class SomeThing {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inline class SomeThing {
extension type SomeThing {

Not quite what we want, but it's closer!

@JS('JSON.stringify')
external String stringify(Object obj);
}
// idk where `inline` fits into this but basically just show the key components as briefly as possible
```

// *Below the example, go through the steps line by line **and why**:*

1. Append the `@JS` annotation to a `library` directive *so that....*

2. Import `dart:js_interop`, which provides most of the tools needed for static interop,
including annotations, the representation type for inline classes, *... etc.*

3. Create an `inline` class because it *allows you to...*,
which is the core of static interop.

4. Use the `@JS` annotation to call a main-spaced function, or top level external member, from your JS app

// *(or, call the external member from outside an inline class if you don't care about types?*
*idk if that's worth mentioning)*

5. Use the `external` keyword to.... *(allow external and non-external members to communicate? idk)*

6. *show external and non-external members interacting if that's not already shown?*

## Features

Inline class-based static interop enable the following features:

<div class="table-wrapper" markdown="1">
| **Rename external members** | Complex JS member names can be re-written and represented in Dart |
| **Interact external and non-external members** | Process external calls, but "keep it on the type itself" (*idk what that means?*), without writing a separate function |
| **Zero cost wrapping** | Inline classes require virtually no overhead for re-typing external members as static types. (*idk*) |
| **Static error checking** | Since external members must be statically typed, static error checking on types and other parts of the interop is possible (partially sound, more work coming) |
| **Interop with DOM types** | *You can interop with DOM types because...?* |
| **Compatibility with Wasm** | Disallowing generative constructors from the model makes `js_interop` compatible with Wasm |
{:.table .table-striped .nowrap}
</div>


## Up next

For a complete summary of static JS interop concepts, members, and syntaxes:
* The [static interop reference][].

For tutorials and help:
* [How to interop with DOM APIs][]
* [How to interop with JavaScript libraries and apps][]
* [How to test and mock JavaScript interop in Dart][]

For additonal types of documentation on `js_interop`:
* [pub.dev site page][]
* [API reference][]

For information on other iterations of JS interop in Dart:
* [`dart:js_util`][]
* [Past JS interop][]


[inline classes]: /
[static interop reference]: /interop/js-interop/reference
[How to interop with DOM APIs]: /interop/js-interop/dom
[How to interop with JavaScript libraries and apps]: /interop/js-interop/js-app
[How to test and mock JavaScript interop in Dart]: /interop/js-interop/test-and-mock
[pub.dev site page]: /
[API reference]: /
[`dart:js_util`]: /interop/js-interop/js-util
[Past JS interop]: /interop/js-interop/past-js-interop
38 changes: 38 additions & 0 deletions src/interop/js-interop/js-app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: How to interop with JavaScript libraries and apps
description: placeholder description
---

{{site.why.learn}}
* How to...
{{site.why.end}}

// *Introdcution paragraphs*

## Background concepts

The following sections provide some extra background and information
around the technologies and concepts used in the tutorial.
To skip directly to the tutorial content,
go to [Steps](#steps).

### Topic/concept 1

### Topic/concept..n

## Steps

### Prerequisites

Before you begin, you'll need...
*

### Actionable step 1

Step subsections should be actionalble, like "Build...", "Retrieve...", "Configure...", etc.

### Actionable step...n

## What next?

Point to other tutorials, reference, etc.
Loading