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 Top Layout

By default sheet modal opens from the bottom of the screen. It is also possible to open it from the top of the screen. In this case we need to add sheet-modal-top class to sheet element. It is also recommended to use bottom toolbar in this case:

  1. <!-- Additional "sheet-modal-top" class to open it from top -->
  2. <div class="sheet-modal sheet-modal-top">
  3. <!-- Bottom toolbar for top sheet -->
  4. <div class="toolbar toolbar-bottom">
  5. <div class="toolbar-inner">
  6. <div class="left"></div>
  7. <div class="right">
  8. <a href="#" class="link sheet-close">Done</a>
  9. </div>
  10. </div>
  11. </div>
  12. <!-- Sheet Modal Inner -->
  13. <div class="sheet-modal-inner">
  14. <!-- Sheet Modal content -->
  15. <div class="block">
  16. <p>Integer mollis nulla id nibh elementum finibus...</p>
  17. </div>
  18. </div>
  19. </div>

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

app.sheet.stepOpen(el)- open/expand Sheet swipe step

  • el - HTMLElement or string (with CSS Selector). Sheet element to open swipe step.

Method returns Sheet’s instance

app.sheet.stepClose(el)- close/collapse Sheet swipe step

  • el - HTMLElement or string (with CSS Selector). Sheet element to close swipe step.

Method returns Sheet’s instance

app.sheet.stepToggle(el)- toggle (open or close) Sheet swipe step

  • el - HTMLElement or string (with CSS Selector). Sheet element to toggle swipe step.

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 and Aurora themes and false for iOS theme
backdropElHTMLElement
string
HTML element or string CSS selector of custom backdrop element
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
closeOnEscapebooleanfalseWhen enabled, sheet will be closed on ESC keyboard key press
animatebooleantrueWhether the Sheet should be opened/closed with animation or not. Can be overwritten in .open() and .close() methods
swipeToClosebooleanfalseWhether the Sheet can be closed with swipe gesture
swipeToStepbooleanfalseWhen enabled it will be possible to split opened sheet into two states: partially opened and fully opened that can be controlled with swipe
swipeHandlerHTMLElement
string
If not passed, then whole Sheet can be swiped to close. You can pass here HTML element or string CSS selector of custom element that will be used as a swipe target. (swipeToClose or swipeToStep must be also enabled)
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.stepOpen()Open/expand sheet swipe step
sheet.stepClose()Close/collapse sheet swipe step
sheet.stepToggle()Toggle (open or close) sheet swipe step
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>

Swipe Step

If you pass swipeToStep parameter, then sheet will be opened partially, and with swipe it can be further expanded. To make it work, we also need to define that first/initial step in sheet HTML, so Framework7 can know on how much Sheet should be opened.

To make it work, we need to wrap initial Sheet content with <div class="sheet-modal-swipe-step"> element, and set height:auto on that Sheet modal:

  1. <div class="sheet-modal" style="height: auto">
  2. <div class="sheet-modal-inner">
  3. <!-- initial sheet modal content -->
  4. <div class="sheet-modal-swipe-step">
  5. ...
  6. </div>
  7. <!-- rest of the content that will be opened with extra swipe -->
  8. ...
  9. </div>
  10. </div>

For top-positioned Sheet modal, this swipe step should be at the bottom:

  1. <div class="sheet-modal sheet-modal-top" style="height: auto">
  2. <div class="sheet-modal-inner">
  3. <!-- rest of the content that will be opened with extra swipe -->
  4. ...
  5. <!-- initial sheet modal content -->
  6. <div class="sheet-modal-swipe-step">
  7. ...
  8. </div>
  9. </div>
  10. </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
sheet:stepopenSheet Element<div class=”sheet”>Event will be triggered on Sheet swipe step open/expand
sheet:stepcloseSheet Element<div class=”sheet”>Event will be triggered on Sheet swipe step close/collapse
sheet:stepprogressSheet Element<div class=”sheet”>Event will be triggered on Sheet swipe step between step opened and closed state. As event.detail it receives step open progress number (from 0 to 1)

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
stepOpensheetsheetEvent will be triggered on Sheet swipe step open/expand
sheetStepOpensheetapp
stepClosesheetsheetEvent will be triggered on Sheet swipe step close/collapse
sheetStepClosesheetapp
stepProgresssheet,progresssheetEvent will be triggered on Sheet swipe step between step opened and closed state. As progress it receives step open progress number (from 0 to 1)
sheetStepProgresssheet,progressapp

CSS Variables

Below is the list of related CSS variables (CSS custom properties).

  1. :root {
  2. --f7-sheet-height: 260px;
  3. }
  4. .ios {
  5. --f7-sheet-bg-color: #cfd5da;
  6. --f7-sheet-border-color: #929499;
  7. }
  8. .ios .theme-dark,
  9. .ios.theme-dark {
  10. --f7-sheet-bg-color: #171717;
  11. --f7-sheet-border-color: var(--f7-bars-border-color);
  12. }
  13. .md {
  14. --f7-sheet-bg-color: #fff;
  15. --f7-sheet-border-color: transparent;
  16. }
  17. .md .theme-dark,
  18. .md.theme-dark {
  19. --f7-sheet-bg-color: #202020;
  20. --f7-sheet-border-color: transparent;
  21. }
  22. .aurora {
  23. --f7-sheet-bg-color: #fff;
  24. --f7-sheet-border-color: transparent;
  25. }
  26. .aurora .theme-dark,
  27. .aurora.theme-dark {
  28. --f7-sheet-bg-color: #202020;
  29. --f7-sheet-border-color: transparent;
  30. }

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="button button-fill sheet-open" href="#" data-sheet=".my-sheet">Open Sheet</a></p>
  15. <!-- Button to close sheet-->
  16. <p><a class="button button-fill sheet-close" href="#" data-sheet=".my-sheet">Close Sheet</a></p>
  17. <p><a class="button button-fill dynamic-sheet" href="#">Create Dynamic Sheet</a></p>
  18. <!-- Button to open another top sheet-->
  19. <p><a class="button button-fill sheet-open" href="#" data-sheet=".my-sheet-top">Open Top Sheet</a></p>
  20. <!-- Button to open swipe-to-close sheet-->
  21. <p><a class="button button-fill sheet-open" href="#" data-sheet=".my-sheet-swipe-to-close">Swipe To Close</a></p>
  22. <!-- Button to open swipe-to-step sheet-->
  23. <p><a class="button button-fill sheet-open" href="#" data-sheet=".my-sheet-swipe-to-step">Swipe To Step</a></p>
  24. </div>
  25. </div>
  26. </div>
  27. ...
  28. <!-- Default bottom sheet -->
  29. <div class="sheet-modal my-sheet">
  30. <div class="toolbar">
  31. <div class="toolbar-inner">
  32. <div class="left"></div>
  33. <div class="right"><a class="link sheet-close" href="#">Done</a></div>
  34. </div>
  35. </div>
  36. <div class="sheet-modal-inner">
  37. <div class="block">
  38. <h4>Info</h4>
  39. <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>
  40. </div>
  41. </div>
  42. </div>
  43. <!-- Top sheet -->
  44. <div class="sheet-modal sheet-modal-top my-sheet-top">
  45. <div class="toolbar toolbar-bottom">
  46. <div class="toolbar-inner">
  47. <div class="left"></div>
  48. <div class="right"><a class="link sheet-close" href="#">Done</a></div>
  49. </div>
  50. </div>
  51. <div class="sheet-modal-inner">
  52. <div class="block">
  53. <h4>Info</h4>
  54. <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>
  55. </div>
  56. </div>
  57. </div>
  58. <!-- Swipe to close sheet -->
  59. <div class="sheet-modal my-sheet-swipe-to-close" style="height:auto; --f7-sheet-bg-color: #fff;">
  60. <div class="sheet-modal-inner">
  61. <div class="page-content">
  62. <div class="block-title block-title-large">Hello!</div>
  63. <div class="block">
  64. <p><b>Swipe me down to close</b></p>
  65. <p>Eaque maiores ducimus, impedit unde culpa qui...</p>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. <!-- Swipe to step sheet -->
  71. <div class="sheet-modal my-sheet-swipe-to-step" style="height:auto; --f7-sheet-bg-color: #fff;">
  72. <div class="sheet-modal-inner">
  73. <div class="sheet-modal-swipe-step">
  74. <div class="display-flex padding justify-content-space-between align-items-center">
  75. <div style="font-size: 18px"><b>Total:</b></div>
  76. <div style="font-size: 22px"><b>$500</b></div>
  77. </div>
  78. <div class="padding-horizontal padding-bottom">
  79. <a class="button button-large button-fill">Make Payment</a>
  80. <div class="margin-top text-align-center">Swipe up for more details</div>
  81. </div>
  82. </div>
  83. <div class="block-title block-title-medium margin-top">Your order:</div>
  84. <div class="list no-hairlines">
  85. <ul>
  86. <li class="item-content">
  87. <div class="item-inner">
  88. <div class="item-title">Item 1</div>
  89. <div class="item-after text-color-black"><b>$200</b></div>
  90. </div>
  91. </li>
  92. <li class="item-content">
  93. <div class="item-inner">
  94. <div class="item-title">Item 2</div>
  95. <div class="item-after text-color-black"><b>$180</b></div>
  96. </div>
  97. </li>
  98. <li class="item-content">
  99. <div class="item-inner">
  100. <div class="item-title">Delivery</div>
  101. <div class="item-after text-color-black"><b>$120</b></div>
  102. </div>
  103. </li>
  104. </ul>
  105. </div>
  106. </div>
  107. </div>
  108. </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. });
  52. // Create swipe-to-close Sheet
  53. app.sheet.create({
  54. el: '.my-sheet-swipe-to-close',
  55. swipeToClose: true,
  56. backdrop: true,
  57. });
  58. // Create swipe-to-step Sheet
  59. app.sheet.create({
  60. el: '.my-sheet-swipe-to-step',
  61. swipeToClose: true,
  62. swipeToStep: true,
  63. backdrop: true,
  64. });