diff --git a/packages/jaspr/CHANGELOG.md b/packages/jaspr/CHANGELOG.md index 22885e1b8..a3f9692d5 100644 --- a/packages/jaspr/CHANGELOG.md +++ b/packages/jaspr/CHANGELOG.md @@ -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.so`, which `Platform.script` points at. diff --git a/packages/jaspr/lib/src/client/dom_render_object.dart b/packages/jaspr/lib/src/client/dom_render_object.dart index 55e5a6bbf..16721c2d6 100644 --- a/packages/jaspr/lib/src/client/dom_render_object.dart +++ b/packages/jaspr/lib/src/client/dom_render_object.dart @@ -174,6 +174,34 @@ 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); @@ -181,6 +209,12 @@ class DomRenderElement extends DomRenderObject } 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) { diff --git a/packages/jaspr/lib/src/dom/html/other.dart b/packages/jaspr/lib/src/dom/html/other.dart index 82c244b0b..48b0c68c5 100644 --- a/packages/jaspr/lib/src/dom/html/other.dart +++ b/packages/jaspr/lib/src/dom/html/other.dart @@ -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, @@ -22,8 +22,8 @@ final class details extends StatelessComponent { super.key, }); - /// Indicates whether or not the details — that is, the contents of the <details> element — are currently visible. - final bool open; + /// Indicates whether or not the details — that is, the contents of the <details> 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; @@ -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, ); @@ -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, @@ -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; @@ -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, ); diff --git a/packages/jaspr/lib/src/dom/type_checks.dart b/packages/jaspr/lib/src/dom/type_checks.dart index 90ca5a051..645e52ece 100644 --- a/packages/jaspr/lib/src/dom/type_checks.dart +++ b/packages/jaspr/lib/src/dom/type_checks.dart @@ -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); @@ -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; @@ -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') diff --git a/packages/jaspr/skills/jaspr-fundamentals/references/html/details.md b/packages/jaspr/skills/jaspr-fundamentals/references/html/details.md index 7d821d44a..14ffdb389 100644 --- a/packages/jaspr/skills/jaspr-fundamentals/references/html/details.md +++ b/packages/jaspr/skills/jaspr-fundamentals/references/html/details.md @@ -4,7 +4,7 @@ Signature of the details component: ```dart const details(List children, { - bool open = false, + bool? open, String? id, String? classes, Styles? styles, diff --git a/packages/jaspr/skills/jaspr-fundamentals/references/html/dialog.md b/packages/jaspr/skills/jaspr-fundamentals/references/html/dialog.md index 12737007d..377344223 100644 --- a/packages/jaspr/skills/jaspr-fundamentals/references/html/dialog.md +++ b/packages/jaspr/skills/jaspr-fundamentals/references/html/dialog.md @@ -4,7 +4,7 @@ Signature of the dialog component: ```dart const dialog(List children, { - bool open = false, + bool? open, String? id, String? classes, Styles? styles, diff --git a/packages/jaspr/test/client/open_attribute_test.dart b/packages/jaspr/test/client/open_attribute_test.dart new file mode 100644 index 000000000..149d94f39 --- /dev/null +++ b/packages/jaspr/test/client/open_attribute_test.dart @@ -0,0 +1,350 @@ +@TestOn('browser') +library; + +import 'dart:js_interop'; + +import 'package:jaspr/dom.dart'; +import 'package:jaspr/jaspr.dart'; +import 'package:jaspr_test/client_test.dart'; +import 'package:universal_web/web.dart'; + +void main() { + group('open attribute', () { + late void Function(void Function() cb) setState; + + testClient('should keep a dialog opened with showModal() open and modal on rebuild', (tester) async { + final dialogKey = GlobalNodeKey(); + final outsideKey = GlobalNodeKey(); + var label = 'Save'; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return div([ + button(key: outsideKey, [Component.text('Outside')]), + dialog(key: dialogKey, [Component.text(label)]), + ]); + }, + ), + ); + + final node = dialogKey.currentNode!; + node.showModal(); + + setState(() => label = 'Saving'); + await pumpEventQueue(); + + expect(node.textContent, 'Saving'); + expect(node.open, isTrue); + expect(node.matches(':modal'), isTrue); + + node.close(); + + expect(node.open, isFalse); + expect(node.matches(':modal'), isFalse); + + outsideKey.currentNode!.focus(); + expect(document.activeElement, outsideKey.currentNode); + }); + + testClient('should keep a dialog opened with show() open on rebuild', (tester) async { + final dialogKey = GlobalNodeKey(); + var label = 'Save'; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return dialog(key: dialogKey, [Component.text(label)]); + }, + ), + ); + + final node = dialogKey.currentNode!; + node.show(); + + setState(() => label = 'Saving'); + await pumpEventQueue(); + + expect(node.textContent, 'Saving'); + expect(node.open, isTrue); + }); + + testClient('should keep details opened by the user open on rebuild', (tester) async { + final detailsKey = GlobalNodeKey(); + var label = 'Idle'; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return details(key: detailsKey, [ + summary([Component.text('More')]), + p([Component.text(label)]), + ]); + }, + ), + ); + + final node = detailsKey.currentNode!; + node.open = true; + + setState(() => label = 'Busy'); + await pumpEventQueue(); + + expect(node.querySelector('p')!.textContent, 'Busy'); + expect(node.open, isTrue); + }); + + testClient('should close a dialog when open changes to false', (tester) async { + final dialogKey = GlobalNodeKey(); + var open = true; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return dialog(key: dialogKey, open: open, [Component.text('Content')]); + }, + ), + ); + + final node = dialogKey.currentNode!; + expect(node.getAttribute('open'), ''); + + setState(() => open = false); + await pumpEventQueue(); + + expect(node.open, isFalse); + }); + + for (final initialOpen in [null, false]) { + testClient('should fully close a modal dialog when open is false (was $initialOpen)', (tester) async { + final dialogKey = GlobalNodeKey(); + final outsideKey = GlobalNodeKey(); + bool? open = initialOpen; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return div([ + button(key: outsideKey, [Component.text('Outside')]), + dialog(key: dialogKey, open: open, [Component.text('Content')]), + ]); + }, + ), + ); + + final node = dialogKey.currentNode!; + var closeEvents = 0; + node.addEventListener('close', ((Event _) => closeEvents++).toJS); + node.showModal(); + + setState(() => open = false); + await pumpEventQueue(); + + expect(node.open, isFalse); + expect(node.matches(':modal'), isFalse); + expect(closeEvents, 1); + + outsideKey.currentNode!.focus(); + expect(document.activeElement, outsideKey.currentNode); + }); + } + + testClient('should close details when open changes to false', (tester) async { + final detailsKey = GlobalNodeKey(); + var open = true; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return details(key: detailsKey, open: open, [ + summary([Component.text('More')]), + ]); + }, + ), + ); + + final node = detailsKey.currentNode!; + expect(node.open, isTrue); + + setState(() => open = false); + await pumpEventQueue(); + + expect(node.open, isFalse); + }); + + testClient('should close details the user opened when open is false', (tester) async { + final detailsKey = GlobalNodeKey(); + var label = 'Idle'; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return details(key: detailsKey, open: false, [ + summary([Component.text('More')]), + p([Component.text(label)]), + ]); + }, + ), + ); + + final node = detailsKey.currentNode!; + node.open = true; + + setState(() => label = 'Busy'); + await pumpEventQueue(); + + expect(node.open, isFalse); + }); + + testClient('should reopen details the user closed when open is true', (tester) async { + final detailsKey = GlobalNodeKey(); + var label = 'Idle'; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return details(key: detailsKey, open: true, [ + summary([Component.text('More')]), + p([Component.text(label)]), + ]); + }, + ), + ); + + final node = detailsKey.currentNode!; + node.open = false; + + setState(() => label = 'Busy'); + await pumpEventQueue(); + + expect(node.open, isTrue); + }); + + testClient('should leave a dialog open when open is no longer passed', (tester) async { + final dialogKey = GlobalNodeKey(); + bool? open = true; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return dialog(key: dialogKey, open: open, [Component.text('Content')]); + }, + ), + ); + + final node = dialogKey.currentNode!; + expect(node.open, isTrue); + + setState(() => open = null); + await pumpEventQueue(); + + expect(node.open, isTrue); + }); + + testClient('should still remove an open attribute set from code on other elements', (tester) async { + final divKey = GlobalNodeKey(); + var label = 'A'; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return div(key: divKey, attributes: {'data-label': label}, []); + }, + ), + ); + + final node = divKey.currentNode!; + node.setAttribute('open', ''); + node.setAttribute('data-test', ''); + + setState(() => label = 'B'); + await pumpEventQueue(); + + expect(node.getAttribute('data-label'), 'B'); + expect(node.hasAttribute('open'), isFalse); + expect(node.hasAttribute('data-test'), isFalse); + }); + + testClient('should keep a server-rendered open the client does not pass when hydrating', (tester) async { + document.body!.innerHTML = + '
Content
More
'.toJS; + + final dialogNode = document.querySelector('dialog') as HTMLDialogElement; + final detailsNode = document.querySelector('details') as HTMLDetailsElement; + expect(dialogNode.open, isTrue); + expect(detailsNode.open, isTrue); + + tester.pumpComponent( + div([ + dialog([Component.text('Content')]), + details([ + summary([Component.text('More')]), + ]), + ]), + ); + + expect(document.querySelector('dialog'), dialogNode); + expect(document.querySelector('details'), detailsNode); + expect(dialogNode.open, isTrue); + expect(detailsNode.open, isTrue); + }); + + testClient('should remove a server-rendered open when the client passes open: false', (tester) async { + document.body!.innerHTML = + '
Content
More
'.toJS; + + final dialogNode = document.querySelector('dialog') as HTMLDialogElement; + final detailsNode = document.querySelector('details') as HTMLDetailsElement; + + tester.pumpComponent( + div([ + dialog(open: false, [Component.text('Content')]), + details(open: false, [ + summary([Component.text('More')]), + ]), + ]), + ); + + expect(document.querySelector('dialog'), dialogNode); + expect(document.querySelector('details'), detailsNode); + expect(dialogNode.open, isFalse); + expect(detailsNode.open, isFalse); + }); + + testClient('should keep open set after hydration on rebuild', (tester) async { + document.body!.innerHTML = 'Save'.toJS; + + final node = document.querySelector('dialog') as HTMLDialogElement; + var label = 'Save'; + + tester.pumpComponent( + StatefulBuilder( + builder: (context, set) { + setState = set; + return dialog([Component.text(label)]); + }, + ), + ); + + expect(document.querySelector('dialog'), node); + node.showModal(); + + setState(() => label = 'Saving'); + await pumpEventQueue(); + + expect(node.textContent, 'Saving'); + expect(node.open, isTrue); + expect(node.matches(':modal'), isTrue); + }); + }); +} diff --git a/packages/jaspr/test/server/html_test.dart b/packages/jaspr/test/server/html_test.dart index 885073120..db3135a82 100644 --- a/packages/jaspr/test/server/html_test.dart +++ b/packages/jaspr/test/server/html_test.dart @@ -36,5 +36,29 @@ void main() { ), ); }); + + test('renders open attribute of dialog and details correctly', () async { + var result = await renderComponent(div([dialog(open: true, []), details(open: true, [])])); + + expect( + result.body, + decodedMatches( + '\n' + '
\n' + '', + ), + ); + + result = await renderComponent(div([dialog(open: false, []), details(open: false, []), dialog([])])); + + expect( + result.body, + decodedMatches( + '\n' + '
\n' + '', + ), + ); + }); }); } diff --git a/packages/jaspr/tool/data/html.json b/packages/jaspr/tool/data/html.json index cf89e8586..8fbe231ce 100644 --- a/packages/jaspr/tool/data/html.json +++ b/packages/jaspr/tool/data/html.json @@ -732,8 +732,9 @@ "doc": "The <details> HTML element creates a disclosure widget in which information is visible only when the widget is toggled into an \"open\" state. A summary or label must be provided using the <summary> element.", "attributes": { "open": { - "doc": "Indicates whether or not the details — that is, the contents of the <details> element — are currently visible.", - "type": "boolean" + "doc": "Indicates whether or not the details — that is, the contents of the <details> element — are currently visible. If not set, the browser keeps control of the state, so the details stay as the user toggled them.", + "type": "boolean", + "explicit": true } } }, @@ -741,8 +742,9 @@ "doc": "The <dialog> HTML element represents a dialog box or other interactive component, such as a dismissible alert, inspector, or subwindow.", "attributes": { "open": { - "doc": "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.", - "type": "boolean" + "doc": "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.", + "type": "boolean", + "explicit": true } } }, diff --git a/packages/jaspr_cli/lib/src/html_spec.dart b/packages/jaspr_cli/lib/src/html_spec.dart index c0ae974f0..e6218a90b 100644 --- a/packages/jaspr_cli/lib/src/html_spec.dart +++ b/packages/jaspr_cli/lib/src/html_spec.dart @@ -738,8 +738,9 @@ const htmlSpec = { "doc": "The <details> HTML element creates a disclosure widget in which information is visible only when the widget is toggled into an \"open\" state. A summary or label must be provided using the <summary> element.", "attributes": { "open": { - "doc": "Indicates whether or not the details — that is, the contents of the <details> element — are currently visible.", - "type": "boolean" + "doc": "Indicates whether or not the details — that is, the contents of the <details> element — are currently visible. If not set, the browser keeps control of the state, so the details stay as the user toggled them.", + "type": "boolean", + "explicit": true } } }, @@ -747,8 +748,9 @@ const htmlSpec = { "doc": "The <dialog> HTML element represents a dialog box or other interactive component, such as a dismissible alert, inspector, or subwindow.", "attributes": { "open": { - "doc": "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.", - "type": "boolean" + "doc": "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.", + "type": "boolean", + "explicit": true } } },