Skip to content
2 changes: 2 additions & 0 deletions packages/jaspr/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased 0.24.0-beta.2

- **Breaking**: Changed `open` on `dialog()` and `details()` to `bool?`. When it is not set, the browser controls the attribute, which fixes a rebuild closing a dialog opened with `showModal()` or `show()` and a `details` element the user opened. Pass `open: false` to keep the element closed.

### Fixes

- Fixed a server built with `dart build cli` on Linux with Dart 3.14 or later not finding its `web` directory and failing with "Could not resolve project directory containing pubspec.yaml" when no `pubspec.yaml` is above it. Since Dart 3.14 such a bundle keeps the snapshot in `lib/libdartaot<name>.so`, which `Platform.script` points at.
Expand Down
34 changes: 34 additions & 0 deletions packages/jaspr/lib/src/client/dom_render_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,47 @@ class DomRenderElement extends DomRenderObject
continue;
}
}
} else if (attrName == 'open' && (attrValue == 'true' || attrValue == 'false')) {
// `dialog()` and `details()` pass 'true' or 'false' when their `open` parameter is set.
final shouldBeOpen = attrValue == 'true';
if (node.isHtmlDialogElement) {
final nodeAsDialogElement = node as web.HTMLDialogElement;
if (nodeAsDialogElement.open != shouldBeOpen) {
if (kVerboseMode) {
print('Set dialog open: $shouldBeOpen');
}
if (shouldBeOpen) {
nodeAsDialogElement.open = true;
} else {
// Removing `open` from a dialog opened with showModal() leaves it modal and the page inert.
nodeAsDialogElement.close();
}
}
continue;
}
if (node.isHtmlDetailsElement) {
final nodeAsDetailsElement = node as web.HTMLDetailsElement;
if (nodeAsDetailsElement.open != shouldBeOpen) {
if (kVerboseMode) {
print('Set details open: $shouldBeOpen');
}
nodeAsDetailsElement.open = shouldBeOpen;
}
continue;
}
}

node.clearOrSetAttribute(attrName, attrValue);
}
}

final attributesToRemove = originalAttributes.difference({'id', 'class', 'style', ...?attributes?.keys});
if (attributesToRemove.contains('open') && (node.isHtmlDialogElement || node.isHtmlDetailsElement)) {
// The browser sets `open` on these elements itself (`showModal()`, `show()`, the user toggling
// the details). Removing it hides a modal dialog without closing it, so the page stays inert.
// Only an explicit `open: false` removes it.
attributesToRemove.remove('open');
}
for (final name in attributesToRemove) {
node.removeAttribute(name);
if (kVerboseMode) {
Expand Down
16 changes: 8 additions & 8 deletions packages/jaspr/lib/src/dom/html/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class details extends StatelessComponent {
/// {@macro jaspr.html.details}
const details(
this.children, {
this.open = false,
this.open,
this.id,
this.classes,
this.styles,
Expand All @@ -22,8 +22,8 @@ final class details extends StatelessComponent {
super.key,
});

/// Indicates whether or not the details — that is, the contents of the &lt;details&gt; element — are currently visible.
final bool open;
/// Indicates whether or not the details — that is, the contents of the &lt;details&gt; element — are currently visible. If not set, the browser keeps control of the state, so the details stay as the user toggled them.
final bool? open;

/// The id of the HTML element. Must be unique within the document.
final String? id;
Expand All @@ -50,7 +50,7 @@ final class details extends StatelessComponent {
id: id,
classes: classes,
styles: styles,
attributes: {...?attributes, if (open) 'open': ''},
attributes: {...?attributes, 'open': ?_explicitBool(open)},
events: events,
children: children,
);
Expand All @@ -64,7 +64,7 @@ final class dialog extends StatelessComponent {
/// {@macro jaspr.html.dialog}
const dialog(
this.children, {
this.open = false,
this.open,
this.id,
this.classes,
this.styles,
Expand All @@ -73,8 +73,8 @@ final class dialog extends StatelessComponent {
super.key,
});

/// Indicates that the dialog is active and can be interacted with. When the open attribute is not set, the dialog shouldn't be shown to the user.
final bool open;
/// Indicates that the dialog is active and can be interacted with. When the open attribute is not set, the dialog shouldn't be shown to the user. If not set, the browser keeps control of the state, which a dialog opened with `showModal()` or `show()` needs.
final bool? open;

/// The id of the HTML element. Must be unique within the document.
final String? id;
Expand All @@ -101,7 +101,7 @@ final class dialog extends StatelessComponent {
id: id,
classes: classes,
styles: styles,
attributes: {...?attributes, if (open) 'open': ''},
attributes: {...?attributes, 'open': ?_explicitBool(open)},
events: events,
children: children,
);
Expand Down
8 changes: 8 additions & 0 deletions packages/jaspr/lib/src/dom/type_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extension DomTypeTests on JSAny? {
bool get isHtmlSelectElement => this == null ? false : instanceof(_cachedHtmlSelectElementConstructor);
bool get isTextAreaElement => this == null ? false : instanceof(_cachedHtmlTextAreaElementConstructor);
bool get isHtmlOptionElement => this == null ? false : instanceof(_cachedHtmlOptionElementConstructor);
bool get isHtmlDialogElement => this == null ? false : instanceof(_cachedHtmlDialogElementConstructor);
bool get isHtmlDetailsElement => this == null ? false : instanceof(_cachedHtmlDetailsElementConstructor);
bool get isText => this == null ? false : instanceof(_cachedTextConstructor);
bool get isComment => this == null ? false : instanceof(_cachedCommentConstructor);
bool get isDocumentFragment => this == null ? false : instanceof(_cachedDocumentFragmentConstructor);
Expand All @@ -26,6 +28,8 @@ final _cachedHtmlAnchorElementConstructor = _htmlAnchorElementConstructor;
final _cachedHtmlSelectElementConstructor = _htmlSelectElementConstructor;
final _cachedHtmlTextAreaElementConstructor = _htmlTextAreaElementConstructor;
final _cachedHtmlOptionElementConstructor = _htmlOptionElementConstructor;
final _cachedHtmlDialogElementConstructor = _htmlDialogElementConstructor;
final _cachedHtmlDetailsElementConstructor = _htmlDetailsElementConstructor;
final _cachedTextConstructor = _textConstructor;
final _cachedCommentConstructor = _commentConstructor;
final _cachedDocumentFragmentConstructor = _documentFragmentConstructor;
Expand All @@ -42,6 +46,10 @@ external JSFunction get _htmlSelectElementConstructor;
external JSFunction get _htmlTextAreaElementConstructor;
@JS('HTMLOptionElement')
external JSFunction get _htmlOptionElementConstructor;
@JS('HTMLDialogElement')
external JSFunction get _htmlDialogElementConstructor;
@JS('HTMLDetailsElement')
external JSFunction get _htmlDetailsElementConstructor;
@JS('Text')
external JSFunction get _textConstructor;
@JS('Comment')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Signature of the details component:

```dart
const details(List<Component> children, {
bool open = false,
bool? open,
String? id,
String? classes,
Styles? styles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Signature of the dialog component:

```dart
const dialog(List<Component> children, {
bool open = false,
bool? open,
String? id,
String? classes,
Styles? styles,
Expand Down
Loading
Loading