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

[Modal] JS Config Templates, alert, confirm, prompt #1869

Merged
merged 6 commits into from
Feb 25, 2021

Conversation

lubber-de
Copy link
Member

@lubber-de lubber-de commented Feb 7, 2021

Description

This PR adds the possibility of using JS config templates for the modal module.
That means you can declare a reusable dynamic template function which returns predefined config settings (modified depending of your input to the template function) as the modal is already prepared to create dynamic content out of pure js since #1774

For this reason modal now also has an optional autoshow option, which enables you to immediatly show the modal on instantiation time, so no more need for $(.ui.modal).modal({closable:false}).modal('show') but $(.ui.modal).modal({closable:false, autoShow:true})

Modal now also can use the transition methods for show/hide individually (backwards compatible) (just like in toast and for dimmer in #1867)

General usage

A js template is called as a modal behavior

$('body').modal('alert','hello')
$('body').modal('confirm','Are you sure?',function(value){})
$('body').modal('prompt','Enter Code', function(value){})

Three basic templates are included: alert, confirm, prompt as an equivalent to existing vanilla JS variants but with more possibilities to customize the look & feel. (see below)

alert / confirm

Possible parameters are: title, content, handler (in that order to stay nearly identical to vanilla js usage) or a given object {title:'',content:'',handler:function(){}} where as title and content can contain HTML.

If you don't trust the content set the global modal setting for preserveHTML to false.

Example

 // content only
$('body').modal('alert','<span class="ui big text">hello</span>');
// title and content
$('body').modal('confirm','Attention!','Ready?');
 // title, content and handler
$('body').modal('confirm','Attention!','Ready?', function(choice){
	console.log('You '+ (choice ? 'Accepted':'Declined'))
});
// content and handler
$('body').modal('confirm','Ready?', function(choice){
	console.log('You '+ (choice ? 'Accepted':'Declined'))
});
// title and handler
$('body').modal('confirm',{
	title: 'Ready?',
	handler: function(choice){
		console.log('You '+ (choice ? 'Accepted':'Declined'))
	}
});

prompt

The call for prompt is basically identical to alert and confirm. There are 2 more options available when an object is given (placeholder and defaultValue.
If you provide HTML Code for the content and this contains an input, this will be used as the inputfield. Otherwise it creates one dynamically for you.

$('body').modal('prompt',{
	title: 'Entry Code?',
    placeholder: 'do not enter your mothers name!',
	handler: function(choice){
		console.log('You '+ (choice ? 'Accepted':'Declined'))
	}
});

Config template modals will be always autoshown (see above), so no manual show is needed)

Create your own template

Extend the modals templates object. It has to return an object which will be merged into the modals settings prior to creating/showing the modal.

$.fn.modal.settings.templates.welcome = function(username) {
// do something according to modals settings and/or given parameters
  var settings = this.get.settings(); // "this" is the modal instance
	return {
		closable: false,
		title: 'Welcome '+ username
	}
}

Reuse this whenever you need

$('body').modal('welcome','hammy')
$('body').modal('welcome','prudho')
$('body').modal('welcome','colinfrick')
$('body').modal('welcome','ko2in')
$('body').modal('welcome','exoego')
$('body').modal('welcome','lubber')

Testcase

Here is a jsfiddle to show the basic usage as well as a custom template which behaves like the SweetAlert2 Style
https://jsfiddle.net/lubber/7u2s4h6k/56/

Here i started a dedicated jfiddle to use a custom Fomantic modal template as a dropin replacement for sweetalert to see what's possible. This is WIP, but shows that you can reuse your existing code. This code will never be part of fomantic core because it's using promises and simulates the config settings from sweetalert2
Thanks to @prudho for his fui alert plugin (#1716) inspiration (and him mentioning he uses swal ;) )
https://jsfiddle.net/lubber/pujwkxz1/6/

Screenshots

modalconfigtemplates
sweetalert03

Closes

#1407
Semantic-Org/Semantic-UI#3869

@lubber-de lubber-de added type/feat Any feature requests or improvements lang/javascript Anything involving JavaScript state/awaiting-reviews Pull requests which are waiting for reviews labels Feb 7, 2021
@lubber-de lubber-de added this to the 2.8.8 milestone Feb 7, 2021
@lubber-de lubber-de added the state/awaiting-docs Pull requests which need doc changes/additions label Feb 7, 2021
@lubber-de
Copy link
Member Author

Docs (partly) added for show/hide method/duration setting by fomantic/Fomantic-UI-Docs#280

lubber-de added a commit to fomantic/Fomantic-UI-Docs that referenced this pull request Jun 6, 2021
Added settings info for the new unique transition syntax for individual show/hide methods/duration in dimmer, modal, popup and dropdown as of
fomantic/Fomantic-UI#1867
fomantic/Fomantic-UI#1869
fomantic/Fomantic-UI#1966
fomantic/Fomantic-UI#1974
@lubber-de
Copy link
Member Author

Docs added by fomantic/Fomantic-UI-Docs#287

@lubber-de lubber-de added state/has-docs A issue/PR which requires documentation changes and has the corresponding PR open in the docs repo and removed state/awaiting-docs Pull requests which need doc changes/additions labels Jun 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang/javascript Anything involving JavaScript state/has-docs A issue/PR which requires documentation changes and has the corresponding PR open in the docs repo type/feat Any feature requests or improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants