Skip to content

Commit

Permalink
Dialog: Add option to put the dialog title in a header element
Browse files Browse the repository at this point in the history
Implement a new option: `uiDialogTitleHeadingLevel`, allowing to change
the `span` wrapping the dialog title into a heading element (`h1`-`h6`).
Value `0` represents the `span`, values 1-6 - a heading at the specified
level.

Fixes gh-2271
Closes gh-2275
  • Loading branch information
rpkoller committed Sep 9, 2024
1 parent fd1b8a0 commit d564731
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/unit/dialog/common-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ common.testWidget( "dialog", {
resizable: true,
show: null,
title: null,
uiDialogTitleHeadingLevel: 0,
width: 300,

// Callbacks
Expand Down
1 change: 1 addition & 0 deletions tests/unit/dialog/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ common.testWidget( "dialog", {
resizable: true,
show: null,
title: null,
uiDialogTitleHeadingLevel: 0,
width: 300,

// Callbacks
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/dialog/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,44 @@ QUnit.test( "aria-modal", function( assert ) {
element.remove();
} );

QUnit.test( "ui dialog title heading level", function( assert ) {
assert.expect( 8 );

var element, nodeName;

element = $( "<div>" ).dialog( { modal: true } );
nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );

element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 0 } );
nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );

element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 1 } );
nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
assert.equal( nodeName, "h1", "Element wrapping the dialog title is h1" );

element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 6 } );
nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
assert.equal( nodeName, "h6", "Element wrapping the dialog title is h6" );

element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 9 } );
nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );

element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: -9 } );
nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );

element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 2.3 } );
nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );

element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: "foo" } );
nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );
} );

QUnit.test( "widget method", function( assert ) {
assert.expect( 1 );
var dialog = $( "<div>" ).appendTo( "#qunit-fixture" ).dialog();
Expand Down
9 changes: 8 additions & 1 deletion ui/widgets/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ $.widget( "ui.dialog", {
resizable: true,
show: null,
title: null,
uiDialogTitleHeadingLevel: 0,
width: 300,

// Callbacks
Expand Down Expand Up @@ -437,7 +438,13 @@ $.widget( "ui.dialog", {
}
} );

uiDialogTitle = $( "<span>" ).uniqueId().prependTo( this.uiDialogTitlebar );
var uiDialogHeadingLevel = Number.isInteger( this.options.uiDialogTitleHeadingLevel ) &&
this.options.uiDialogTitleHeadingLevel > 0 &&
this.options.uiDialogTitleHeadingLevel <= 6 ?
"h" + this.options.uiDialogTitleHeadingLevel : "span";

uiDialogTitle = $( "<" + uiDialogHeadingLevel + ">" )
.uniqueId().prependTo( this.uiDialogTitlebar );
this._addClass( uiDialogTitle, "ui-dialog-title" );
this._title( uiDialogTitle );

Expand Down

0 comments on commit d564731

Please sign in to comment.