Sheet Modal - 图1

Sheet Modal

Sheet Modal is a special overlay type which is similar to Picker/Calendar’s overlay. Such modal allows to create custom overlays with custom content

Sheet Layout

Sheet layout is pretty simple:

  1. <body>
  2. ...
  3. <!-- Sheet Modal Container -->
  4. <div class="sheet-modal">
  5. <!-- Sheet Modal Toolbar, optional -->
  6. <div class="toolbar">
  7. <div class="toolbar-inner">
  8. <div class="left"></div>
  9. <div class="right">
  10. <a href="#" class="link sheet-close">Done</a>
  11. </div>
  12. </div>
  13. </div>
  14. <!-- Sheet Modal Inner -->
  15. <div class="sheet-modal-inner">
  16. <!-- Sheet Modal content -->
  17. <div class="block">
  18. <p>Integer mollis nulla id nibh elementum finibus...</p>
  19. </div>
  20. </div>
  21. </div>
  22. </body>

Sheet App Methods

Let’s look at related App methods to work with Sheet:

app.sheet.create(parameters)- create Sheet instance

  • parameters - object. Object with sheet parameters

Method returns created Sheet’s instance

app.sheet.destroy(el)- destroy Sheet instance

  • el - HTMLElement or string (with CSS Selector) or object. Sheet element or Sheet instance to destroy.

app.sheet.get(el)- get Sheet instance by HTML element

  • el - HTMLElement or string (with CSS Selector). Sheet element.

Method returns Sheet’s instance

app.sheet.open(el, animate)- opens Sheet

  • el - HTMLElement or string (with CSS Selector). Sheet element to open.
  • animate - boolean. Open Sheet with animation.

Method returns Sheet’s instance

app.sheet.close(el, animate)- closes Sheet

  • el - HTMLElement or string (with CSS Selector). Sheet element to close.
  • animate - boolean. Close Sheet with animation.

Method returns Sheet’s instance

Sheet Parameters

Now let’s look at list of available parameters we need to create Sheet:

ParameterTypeDefaultDescription
elHTMLElementSheet element. Can be useful if you already have Sheet element in your HTML and want to create new instance using this element
contentstringFull Sheet HTML layout string. Can be useful if you want to create Sheet element dynamically
backdropbooleanEnables Sheet backdrop (dark semi transparent layer behind). By default it is true for MD theme and false for iOS theme
scrollToElHTMLElement
string
HTML element or string (with CSS selector) of element. If specified, then sheet will try to scroll page content to this element on open
closeByBackdropClickbooleantrueWhen enabled, sheet will be closed on backdrop click
closeByOutsideClickbooleanfalseWhen enabled, sheet will be closed on when click outside of it
animatebooleantrueWhether the Sheet should be opened/closed with animation or not. Can be overwritten in .open() and .close() methods
onobjectObject with events handlers. For example:
  1. var sheet = app.sheet.create({
  2. content: ‘<div class=”sheet-modal”>…</div>’,
  3. on: {
  4. opened: function () {
  5. console.log(‘Sheet opened’)
  6. }
  7. }
  8. })

Note that all following parameters can be used in global app parameters under sheet property to set defaults for all sheets. For example:

  1. var app = new Framework7({
  2. sheet: {
  3. closeByBackdropClick: false,
  4. }
  5. });

Sheet Methods & Properties

So to create Sheet we have to call:

  1. var sheet = app.sheet.create({ /* parameters */ })

After that we have its initialized instance (like sheet variable in example above) with useful methods and properties:

Properties
sheet.appLink to global app instance
sheet.elSheet HTML element
sheet.$elDom7 instance with sheet HTML element
sheet.backdropElBackdrop HTML element
sheet.$backdropElDom7 instance with backdrop HTML element
sheet.paramsSheet parameters
sheet.openedBoolean prop indicating whether sheet is opened or not
Methods
sheet.open(animate)Open sheet. Where
  • animate - boolean (by default true) - defines whether it should be opened with animation
sheet.close(animate)Close sheet. Where
  • animate - boolean (by default true) - defines whether it should be closed with animation
sheet.destroy()Destroy sheet
sheet.on(event, handler)Add event handler
sheet.once(event, handler)Add event handler that will be removed after it was fired
sheet.off(event, handler)Remove event handler
sheet.off(event)Remove all handlers for specified event
sheet.emit(event, …args)Fire event on instance

Control Sheet With Links

It is possible to open and close required sheet (if you have them in DOM) using special classes and data attributes on links:

  • To open sheet we need to add “sheet-open“ class to any HTML element (prefered to link)

  • To close sheet we need to add “sheet-close“ class to any HTML element (prefered to link)

  • If you have more than one sheet in DOM, you need to specify appropriate sheet via additional data-sheet=”.my-sheet” attribute on this HTML element

According to above note:

  1. <!-- In data-sheet attribute we specify CSS selector of sheet we need to open -->
  2. <p><a href="#" data-sheet=".my-sheet" class="sheet-open">Open Sheet</a></p>
  3. <!-- And somewhere in DOM -->
  4. <div class="sheet-modal my-sheet">
  5. <div class="sheet-modal-inner">
  6. <!-- Link to close sheet -->
  7. <a class="link sheet-close">Close</a>
  8. </div>
  9. </div>

Sheet Events

Sheet will fire the following DOM events on sheet element and events on app and sheet instance:

DOM Events

EventTargetDescription
sheet:openSheet Element<div class=”sheet”>Event will be triggered when Sheet starts its opening animation
sheet:openedSheet Element<div class=”sheet”>Event will be triggered after Sheet completes its opening animation
sheet:closeSheet Element<div class=”sheet”>Event will be triggered when Sheet starts its closing animation
sheet:closedSheet Element<div class=”sheet”>Event will be triggered after Sheet completes its closing animation

App and Sheet Instance Events

Sheet instance emits events on both self instance and app instance. App instance events has same names prefixed with popup.

EventArgumentsTargetDescription
opensheetsheetEvent will be triggered when Sheet starts its opening animation. As an argument event handler receives sheet instance
sheetOpensheetapp
openedsheetsheetEvent will be triggered after Sheet completes its opening animation. As an argument event handler receives sheet instance
sheetOpenedsheetapp
closesheetsheetEvent will be triggered when Sheet starts its closing animation. As an argument event handler receives sheet instance
sheetClosesheetapp
closedsheetsheetEvent will be triggered after Sheet completes its closing animation. As an argument event handler receives sheet instance
sheetClosedsheetapp
beforeDestroysheetsheetEvent will be triggered right before Sheet instance will be destroyed. As an argument event handler receives sheet instance
sheetBeforeDestroysheetapp

Examples

  1. <body>
  2. ...
  3. <div class="page">
  4. <div class="navbar">
  5. <div class="navbar-inner">
  6. <!-- In data-sheet attribute we specify CSS selector of sheet we need to open-->
  7. <div class="title">Sheet Modal</div>
  8. <div class="right"><a class="link sheet-open" href="#" data-sheet=".my-sheet">Open Sheet</a></div>
  9. </div>
  10. </div>
  11. <div class="page-content">
  12. <div class="block">
  13. <!-- In data-sheet attribute we specify CSS selector of sheet we need to open-->
  14. <p><a class="link sheet-open" href="#" data-sheet=".my-sheet">Open Sheet</a></p>
  15. <!-- Link to close sheet-->
  16. <p><a class="link sheet-close" href="#" data-sheet=".my-sheet">Close Sheet</a></p>
  17. <p><a class="link dynamic-sheet" href="#">Create Dynamic Sheet</a></p>
  18. </div>
  19. </div>
  20. </div>
  21. ...
  22. <div class="sheet-modal my-sheet">
  23. <div class="toolbar">
  24. <div class="toolbar-inner">
  25. <div class="left"></div>
  26. <div class="right"><a class="link sheet-close" href="#">Done</a></div>
  27. </div>
  28. </div>
  29. <div class="sheet-modal-inner">
  30. <div class="block">
  31. <h4>Info</h4>
  32. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque ac diam ac quam euismod porta vel a nunc. Quisque sodales scelerisque est, at porta justo cursus ac.</p>
  33. </div>
  34. </div>
  35. </div>
  36. </body>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // DOM events for my-sheet sheet
  4. $$('.my-sheet').on('sheet:open', function (e, sheet) {
  5. console.log('my-sheet open');
  6. });
  7. $$('.my-sheet').on('sheet:opened', function (e, sheet) {
  8. console.log('my-sheet opened');
  9. });
  10. // Create dynamic Sheet
  11. var dynamicSheet = app.sheet.create({
  12. content: '<div class="sheet-modal">'+
  13. '<div class="toolbar">'+
  14. '<div class="toolbar-inner">'+
  15. '<div class="left"></div>'+
  16. '<div class="right">'+
  17. '<a class="link sheet-close">Done</a>'+
  18. '</div>'+
  19. '</div>'+
  20. '</div>'+
  21. '<div class="sheet-modal-inner">'+
  22. '<div class="block">'+
  23. '<p>Sheet created dynamically.</p>'+
  24. '<p><a href="#" class="link sheet-close">Close me</a></p>'+
  25. '</div>'+
  26. '</div>'+
  27. '</div>',
  28. // Events
  29. on: {
  30. open: function (sheet) {
  31. console.log('Sheet open');
  32. },
  33. opened: function (sheet) {
  34. console.log('Sheet opened');
  35. },
  36. }
  37. });
  38. // Events also can be assigned on instance later
  39. dynamicSheet.on('close', function (sheet) {
  40. console.log('Sheet close');
  41. });
  42. dynamicSheet.on('closed', function (sheet) {
  43. console.log('Sheet closed');
  44. });
  45. // Open dynamic sheet
  46. $$('.dynamic-sheet').on('click', function () {
  47. // Close inline sheet before
  48. app.sheet.close('.my-sheet');
  49. // Open dynamic sheet
  50. dynamicSheet.open();
  51. });