Skip to content

Commit

Permalink
Merge pull request #363 from schultek/feat/universal-web
Browse files Browse the repository at this point in the history
migrate to universal_web
  • Loading branch information
schultek authored Jan 12, 2025
2 parents 7ccc0a1 + 0693efd commit 382fa4c
Show file tree
Hide file tree
Showing 32 changed files with 394 additions and 112 deletions.
1 change: 1 addition & 0 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
{"title": "SyncStateMixin", "href": "/utils/sync_state_mixin"},
{"title": "PreloadStateMixin", "href": "/utils/preload_state_mixin"},
{"title": "@encoder/@decoder", "href": "/utils/at_encoder_decoder"},
{"title": "GlobalNodeKey", "href": "/utils/global_node_key"},
{"title": "@Import", "href": "/utils/at_import"},
{"title": "ViewTransitionMixin", "href": "/utils/view_transition_mixin"}
]
Expand Down
4 changes: 4 additions & 0 deletions docs/utils/at_import.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ is only supported on one side, e.g.:
- `dart:html` is only supported on the client,
- `dart:io` is only supported on the server.

<Info>
When using `package:web` you should instead use `package:universal_web` which already works across web **and server** environments. No need to use conditional imports or the `@Import` annotation shown on this page.
</Info>

When you want to use such a library in your project, you would have to resort to *conditional imports*,
which are quite cumbersome to use since you have to create multiple files and stub everything you want to use.

Expand Down
26 changes: 26 additions & 0 deletions docs/utils/global_node_key.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: "GlobalNodeKey"
description: Get access to the unterlying dom node of a component.
---

---

For accessing the underlying DOM node of a component, provide a `GlobalNodeKey` to the component and access the node using `myGlobalNodeKey.currentNode`.

```dart
// 1. Import universal_web
import 'package:universal_web/web.dart';
// 2. Create a GlobalNodeKey with a node type
final GlobalNodeKey<HTMLFormElement> myFormKey = GlobalNodeKey();
// 3. Provide to a html component
yield form(key: myFormKey, [ ... ]);
// 4. Access dom node from key
myFormKey.currentNode?.checkValidity();
```

<Info>
The `universal_web` package is a drop-in replacement for the `web` package that adds compatability for non-web platforms. Therefore its safe to use in components that are both rendered on the server and client.
</Info>
31 changes: 30 additions & 1 deletion packages/jaspr/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
## Unreleased patch
## Unreleased minor


- **BREAKING** Removed `currentState` from `GlobalKey`, use `GlobalStateKey` instead.
- Added `GlobalStateKey<T extends State>` to access the state of a component using `currentState`.

```dart
// Use any State type as the type parameter.
final GlobalStateKey<MyComponentState> myComponentKey = GlobalStateKey();
/* ... */
// Access the state from the key.
MyComponentState? state = myComponentKey.currentState;
```

- Added `GlobalNodeKey<T extends Node>` to access native dom nodes using `currentNode`.

```dart
import 'package:universal_web/web.dart';
// Use any Node type (from package:universal_web) as the type parameter.
final GlobalNodeKey<HTMLFormElement> myFormKey = GlobalNodeKey();
/* ... */
// Access the dom node from the key.
HTMLFormElement? node = myFormKey.currentNode;
```

- Migrated all web imports from `package:web` to `package:universal_web`.
- Added `prefersColorScheme` parameter to `MediaQuery`.

## 0.16.4
Expand Down
2 changes: 1 addition & 1 deletion packages/jaspr/lib/jaspr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export 'src/foundation/basic_types.dart';
export 'src/foundation/binding.dart';
export 'src/foundation/change_notifier.dart';
export 'src/foundation/constants.dart';
export 'src/foundation/events/events.dart';
export 'src/foundation/events.dart';
export 'src/foundation/object.dart';
export 'src/foundation/options.dart';
export 'src/foundation/scheduler.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/jaspr/lib/src/browser/browser_binding.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:async';

import 'package:web/web.dart';
import 'package:universal_web/web.dart';

import '../foundation/basic_types.dart';
import '../foundation/binding.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/jaspr/lib/src/browser/clients.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';
import 'dart:convert';

import 'package:web/web.dart' as web;
import 'package:universal_web/web.dart' as web;

import '../foundation/marker_utils.dart';
import '../framework/framework.dart';
Expand Down
6 changes: 4 additions & 2 deletions packages/jaspr/lib/src/browser/dom_render_object.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:async';
import 'dart:js_interop';

import 'package:web/web.dart' as web;
import 'package:universal_web/web.dart' as web;

import '../foundation/constants.dart';
import '../foundation/events/events.dart';
import '../foundation/events.dart';
import '../framework/framework.dart';
import 'utils.dart';

Expand All @@ -15,7 +15,9 @@ const xmlns = {
};

class DomRenderObject extends RenderObject {
@override
web.Node? node;

List<web.Node> toHydrate = [];

Map<String, EventBinding>? events;
Expand Down
2 changes: 1 addition & 1 deletion packages/jaspr/lib/src/browser/utils.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:web/web.dart' as web;
import 'package:universal_web/web.dart' as web;

extension NodeListIterable on web.NodeList {
Iterable<web.Node> toIterable() sync* {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:js_interop';

import 'package:web/web.dart' as web;
import 'package:universal_web/web.dart' as web;

import '../../../browser.dart';
import '../../browser/utils.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ abstract class Document implements Component {
}) = AttachDocument.body;
}

// only allow a single document
const _documentKey = GlobalKey();
// Only allow a single Document.
const _documentKey = _DocumentKey();

class _DocumentKey extends GlobalKey {
const _DocumentKey() : super.constructor();
}

class BaseDocument extends StatelessComponent implements Document {
const BaseDocument({
Expand Down
2 changes: 1 addition & 1 deletion packages/jaspr/lib/src/components/html/html.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../../foundation/basic_types.dart';
import '../../foundation/events/events.dart';
import '../../foundation/events.dart';
import '../../foundation/styles/styles.dart';
import '../../framework/framework.dart';
import '../raw_text/raw_text.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:js_interop';

import 'package:web/web.dart' as web;
import 'package:universal_web/web.dart' as web;

import '../../../browser.dart';
import '../../browser/utils.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../../components/html/html.dart';
import '../basic_types.dart';
import 'web.dart' if (dart.library.io) 'web_stub.dart' as web;
import 'package:universal_web/js_interop.dart';
import 'package:universal_web/web.dart' as web;

export 'web.dart' if (dart.library.io) 'web_stub.dart' show Event;
import '../components/html/html.dart';
import 'basic_types.dart';

typedef EventCallback = void Function(web.Event event);
typedef EventCallbacks = Map<String, EventCallback>;
Expand Down
3 changes: 0 additions & 3 deletions packages/jaspr/lib/src/foundation/events/web.dart

This file was deleted.

42 changes: 0 additions & 42 deletions packages/jaspr/lib/src/foundation/events/web_stub.dart

This file was deleted.

17 changes: 17 additions & 0 deletions packages/jaspr/lib/src/foundation/object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ String objectRuntimeType(Object? object, String optimizedValue) {
}());
return optimizedValue;
}

/// Returns a 5 character long hexadecimal string generated from
/// [Object.hashCode]'s 20 least-significant bits.
String shortHash(Object? object) {
return object.hashCode.toUnsigned(20).toRadixString(16).padLeft(5, '0');
}

/// Returns a summary of the runtime type and hash code of `object`.
///
/// See also:
///
/// * [Object.hashCode], a value used when placing an object in a [Map] or
/// other similar data structure, and which is also used in debug output to
/// distinguish instances of the same class (hash collisions are
/// possible, but rare enough that its use in debug output is useful).
/// * [Object.runtimeType], the [Type] of an object.
String describeIdentity(Object? object) => '${objectRuntimeType(object, '<optimized out>')}#${shortHash(object)}';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';

import 'package:web/web.dart';
import 'package:universal_web/web.dart';

FutureOr<void> startViewTransition(void Function() callback) {
if (!document.has('startViewTransition')) {
Expand Down
3 changes: 2 additions & 1 deletion packages/jaspr/lib/src/framework/framework.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import 'dart:async';
import 'dart:collection';

import 'package:meta/meta.dart';
import 'package:universal_web/web.dart' as web;

import '../foundation/basic_types.dart';
import '../foundation/binding.dart';
import '../foundation/events/events.dart';
import '../foundation/events.dart';
import '../foundation/object.dart';
import '../foundation/styles/styles.dart';

Expand Down
Loading

0 comments on commit 382fa4c

Please sign in to comment.